From a5114c4047d09848b56ff282253b13837e69f136 Mon Sep 17 00:00:00 2001 From: luckyadam Date: Tue, 7 Jan 2020 20:01:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(mini-runner):=20=E4=BF=AE=E5=A4=8D=20yarn?= =?UTF-8?q?=20workspace=20=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=BC=95=E5=85=A5=20?= =?UTF-8?q?npm=20=E4=B8=AD=E7=BB=84=E4=BB=B6=E5=87=BA=E9=94=99=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/plugins/MiniPlugin.ts | 50 +++++++------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index 38f469345e4a..b4a60fe2f08c 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -355,7 +355,7 @@ export default class MiniPlugin { componentPath = replaceAliasPath(filePath, componentPath, alias) realComponentPath = resolveScriptPath(path.resolve(filePath, '..', componentPath as string)) } else { - realComponentPath = resolveNpmSync(componentPath, this.context) + realComponentPath = resolveNpmSync(componentPath, this.options.nodeModulesPath) } } else { realComponentPath = resolveScriptPath(path.resolve(filePath, '..', componentPath as string)) @@ -575,15 +575,10 @@ export default class MiniPlugin { } getComponentName (componentPath) { - let componentName - if (NODE_MODULES_REG.test(componentPath)) { - componentName = componentPath.replace(this.context, '').replace(/\\/g, '/').replace(path.extname(componentPath), '') - componentName = componentName.replace(/node_modules/gi, 'npm') - } else { - componentName = componentPath.replace(this.sourceDir, '').replace(/\\/g, '/').replace(path.extname(componentPath), '') - } - - return componentName.replace(/^(\/|\\)/, '') + return this.getRelativePath(componentPath) + .replace(/\\/g, '/') + .replace(path.extname(componentPath), '') + .replace(/^(\/|\\)/, '') } getComponents (compiler: webpack.Compiler, fileList: Set, isRoot: boolean) { @@ -774,12 +769,7 @@ export default class MiniPlugin { const { buildAdapter } = this.options const isQuickApp = buildAdapter === BUILD_TYPES.QUICKAPP Object.keys(taroFileTypeMap).forEach(item => { - let relativePath - if (NODE_MODULES_REG.test(item)) { - relativePath = item.replace(this.context, '').replace(/node_modules/gi, 'npm') - } else { - relativePath = item.replace(this.sourceDir, '') - } + const relativePath = this.getRelativePath(item) const extname = path.extname(item) const templatePath = relativePath.replace(extname, MINI_APP_FILES[buildAdapter].TEMPL).replace(/\\/g, '/') const jsonPath = relativePath.replace(extname, MINI_APP_FILES[buildAdapter].CONFIG).replace(/\\/g, '/') @@ -844,12 +834,7 @@ export default class MiniPlugin { itemInfo.taroSelfComponents.forEach(item => { if (fs.existsSync(item.path)) { const content = fs.readFileSync(item.path).toString() - let relativePath - if (NODE_MODULES_REG.test(item.path)) { - relativePath = item.path.replace(this.context, '').replace(/node_modules/gi, 'npm') - } else { - relativePath = item.path.replace(this.sourceDir, '') - } + const relativePath = this.getRelativePath(item.path).replace(/\\/g, '/') compilation.assets[relativePath] = { size: () => content.length, source: () => content @@ -874,13 +859,7 @@ export default class MiniPlugin { this.quickappStyleFiles.forEach(item => { if (fs.existsSync(item.path)) { const styleContent = fs.readFileSync(item.path).toString() - let relativePath - if (NODE_MODULES_REG.test(item.path)) { - relativePath = item.path.replace(this.context, '').replace(/node_modules/gi, 'npm').replace(/\\\\/g, '/') - } - else { - relativePath = item.path.replace(this.sourceDir, '').replace(/\\\\/g, '/') - } + const relativePath = this.getRelativePath(item.path).replace(/\\/g, '/') compilation.assets[relativePath] = { size: () => styleContent.length, source: () => styleContent @@ -889,6 +868,16 @@ export default class MiniPlugin { }) } + getRelativePath (filePath) { + let relativePath + if (NODE_MODULES_REG.test(filePath)) { + relativePath = filePath.replace(this.options.nodeModulesPath, 'npm') + } else { + relativePath = filePath.replace(this.sourceDir, '') + } + return relativePath + } + addTarBarFilesToDependencies (compilation: webpack.compilation.Compilation) { const { fileDependencies } = compilation this.tabBarIcons.forEach(icon => { @@ -900,11 +889,10 @@ export default class MiniPlugin { transferFileContent (compiler: webpack.Compiler) { Object.keys(taroFileTypeMap).forEach(item => { - const relativePath = item.replace(compiler.context, '') const itemInfo = taroFileTypeMap[item] if (typeof itemInfo.code === 'string') { new VirtualModulePlugin({ - moduleName: relativePath, + path: item, contents: itemInfo.code }).apply(compiler) }