Skip to content

Commit

Permalink
fix: absolute path hazard
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoqi15 committed Jul 22, 2021
1 parent 8fe8366 commit 5ee1346
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Example/dist/buz.android.bundle

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Example/dist/buz.ios.bundle

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"babel-jest": "^26.6.3",
"eslint": "^7.14.0",
"jest": "^26.6.3",
"metro-code-split": "^0.1.1",
"metro-code-split": "^0.1.2",
"metro-react-native-babel-preset": "^0.64.0",
"react-test-renderer": "17.0.1",
"typescript": "^3.8.3"
Expand Down
2 changes: 1 addition & 1 deletion Example/public/dll/_dll.android.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"node_modules/@react-native/polyfills/console.js",
"node_modules/@react-native/polyfills/error-guard.js",
"node_modules/@react-native/polyfills/Object.es7.js",
"require-/Users/a58/fe/metro-code-split/Example/node_modules/react-native/Libraries/Core/InitializeCore.js",
"require-node_modules/react-native/Libraries/Core/InitializeCore.js",
"node_modules/react-native/index.js",
"node_modules/react-native/Libraries/Components/AccessibilityInfo/AccessibilityInfo.android.js",
"node_modules/@babel/runtime/helpers/interopRequireDefault.js",
Expand Down
2 changes: 1 addition & 1 deletion Example/public/dll/_dll.ios.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"node_modules/@react-native/polyfills/console.js",
"node_modules/@react-native/polyfills/error-guard.js",
"node_modules/@react-native/polyfills/Object.es7.js",
"require-/Users/a58/fe/metro-code-split/Example/node_modules/react-native/Libraries/Core/InitializeCore.js",
"require-node_modules/react-native/Libraries/Core/InitializeCore.js",
"node_modules/react-native/index.js",
"node_modules/react-native/Libraries/Components/AccessibilityInfo/AccessibilityInfo.ios.js",
"node_modules/@babel/runtime/helpers/interopRequireDefault.js",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metro-code-split",
"version": "0.1.1",
"version": "0.1.2",
"main": "src/index.js",
"bin": {
"mcs-scripts": "./src/bin/index.js"
Expand Down
6 changes: 3 additions & 3 deletions src/config/craeteMustConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { fse, toRelativePath } = require('general-tools')
const { fse } = require('general-tools')
const dynamicImports = require('./dynamicImports')
const { paths } = require('../utils')
const { paths, replacePath } = require('../utils')

/**
* craete must config
Expand All @@ -20,7 +20,7 @@ module.exports = mcs => {
return absolutePath => {
const moduleId = cacheMap.get(absolutePath)
if (moduleId) return moduleId
const relativePath = toRelativePath(absolutePath)
const relativePath = replacePath(absolutePath)
if (mcs.isDllPath(absolutePath)) { // dll module
cacheMap.set(absolutePath, relativePath)
return relativePath
Expand Down
12 changes: 6 additions & 6 deletions src/config/dynamicImports.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path')
const { fse, getContentHash, toRelativePath, babel: { parser, traverse, generate } } = require('general-tools')
const { paths, output } = require('../utils')
const { fse, getContentHash, babel: { parser, traverse, generate } } = require('general-tools')
const { paths, output, replacePath } = require('../utils')

const mainModuleId = 'main' // main moduleId flag

Expand Down Expand Up @@ -41,7 +41,7 @@ module.exports = async (mcs, entryPoint, prepend, graph, bundleOptions) => {
for (const [key, value] of graph.dependencies) {
// common模块一定在主包中 不拆分 但是也必须走流程检测 否则异步引用common模块 不会记录它的chunkModuleIdToHashMap
const asyncTypes = [...value.inverseDependencies].map(absolutePath => {
const relativePath = toRelativePath(absolutePath)
const relativePath = replacePath(absolutePath)
const val = graph.dependencies.get(absolutePath)
for (const [k, v] of val.dependencies) {
if (v.absolutePath === key) {
Expand All @@ -65,7 +65,7 @@ module.exports = async (mcs, entryPoint, prepend, graph, bundleOptions) => {

// [null, asyncFlag, 'src/components/AsyncComA.tsx'] 主包

const relativePath = toRelativePath(key)
const relativePath = replacePath(key)
if (asyncTypes.length === 0 || asyncTypes.some(v => v === null)) { // 没有任何逆依赖 (如入口文件)
map.get(mainModuleId).moduleIds.add(relativePath)
} else if (asyncTypes.every(v => v === asyncFlag)) {
Expand Down Expand Up @@ -125,14 +125,14 @@ module.exports = async (mcs, entryPoint, prepend, graph, bundleOptions) => {
outputChunkFns.push((async () => {
const dir = path.resolve(mcs.outputChunkDir, `${hash}${output.fileSuffix}`)
await fse.writeFile(dir, code)
console.log(`info Writing chunk bundle output to: ${toRelativePath(dir)}`)
console.log(`info Writing chunk bundle output to: ${replacePath(dir)}`)
})())
}
await Promise.all(outputChunkFns)

// inject map info
for (const arr of map.get(mainModuleId).modules) {
if (arr[0] === toRelativePath(paths.chunkModuleIdToHashMapPath)) {
if (arr[0] === replacePath(paths.chunkModuleIdToHashMapPath)) {
const ast = parser.parse(arr[1])
traverse(ast, {
FunctionExpression(nodePath) {
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require('path')
const { mergeConfig } = require('metro-config')
const { fse, ejs, tapable: { SyncHook, SyncBailHook }, log, paths: ps, dataExtend, argv, toRelativePath } = require('general-tools')
const { fse, ejs, tapable: { SyncHook, SyncBailHook }, log, paths: ps, dataExtend, argv } = require('general-tools')
const baseConfig = require('./config/baseConfig')
const InjectVar = require('./plugins/InjectVar')
const { isBaseDllPath, paths, dllJsonName, output } = require('./utils')
const { isBaseDllPath, paths, dllJsonName, output, replacePath } = require('./utils')
const { BuildType } = require('./types')
const pkg = require('../package.json')

Expand Down Expand Up @@ -60,7 +60,7 @@ class MetroCodeSplit {
let timeId = null

return arg => {
const relativePath = toRelativePath(arg.path)
const relativePath = replacePath(arg.path)
const arr = this.options.dll.entry.length !== 0 && isBaseDllPath(relativePath) ? dllArr : busineArr
arr.push(relativePath)

Expand All @@ -70,7 +70,7 @@ class MetroCodeSplit {
const dllOutputPath = path.resolve(paths.outputDir, dllJsonName)
const dllContent = JSON.stringify([...new Set(dllArr)], null, 2)
await fse.writeFile(dllOutputPath, dllContent)
console.log(`info Writing json output to: ${toRelativePath(dllOutputPath)}`)
console.log(`info Writing json output to: ${replacePath(dllOutputPath)}`)
} catch (err) {
console.error(err)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ class MetroCodeSplit {
BuildType.DllJson !== this.bundleOutputInfo.name && log('warning: failed to load the dllRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
}
// inertia method
this.isDllPath = p => commonPaths.includes(toRelativePath(p))
this.isDllPath = p => commonPaths.includes(replacePath(p))
return this.isDllPath(p)
}

Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { argv } = require('general-tools')
const paths = require('./paths')
const isProduction = require('./isProduction')
const output = require('./output')
const replacePath = require('./replacePath')
const { relativeDllEntry } = require('../types')

/**
Expand All @@ -17,6 +18,7 @@ module.exports = {
paths,
isProduction,
output,
replacePath,
dllJsonName: `_dll.${argv.platform}.json`,
isBaseDllPath,
}
12 changes: 12 additions & 0 deletions src/utils/replacePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { paths } = require('general-tools')

/**
* converts absolute paths to relative paths
* @param { string } to
* @param { string } from
* @returns
* @describe Since some paths contain require-xxx, so use replace instead of path.relative
*/
const replacePath = (to, from = paths.cwdDir) => to.replace(from + '/', '')

module.exports = replacePath

0 comments on commit 5ee1346

Please sign in to comment.