diff --git a/packages/taro-vite-runner/src/mini/config.ts b/packages/taro-vite-runner/src/mini/config.ts index 02210acb681b..3c7d4dd6a99e 100644 --- a/packages/taro-vite-runner/src/mini/config.ts +++ b/packages/taro-vite-runner/src/mini/config.ts @@ -233,8 +233,8 @@ export default function (viteCompilerContext: ViteMiniCompilerContext): PluginOp babel(getBabelOption( taroConfig, { - defaultExclude: [/node_modules[/\\](?!@tarojs)/], - defaultInclude: [sourceDir, /taro/] + defaultExclude: [], + defaultInclude: [sourceDir, /(?<=node_modules[\\/]).*taro/] } )) as InputPluginOption, ], diff --git a/packages/taro-vite-runner/src/utils/index.ts b/packages/taro-vite-runner/src/utils/index.ts index b952569c5859..c7668601201f 100644 --- a/packages/taro-vite-runner/src/utils/index.ts +++ b/packages/taro-vite-runner/src/utils/index.ts @@ -211,19 +211,18 @@ export function getBabelOption ( if (isFunction(filter)) { opts.filter = filter } else { - let exclude: (string | RegExp)[] = [] - let include: (string | RegExp)[] = [] - - if (compile.exclude?.length) { - const list = compile.exclude - const isNodeModuleReseted = list.find((reg) => reg.toString().includes('node_modules')) - if (!isNodeModuleReseted) list.push(...defaultExclude) - exclude = list - } else if (compile.include?.length) { - include = [...compile.include, ...defaultInclude] - } else { - exclude = [...defaultExclude] + let exclude: (string | RegExp)[] = [...defaultExclude] + const include: (string | RegExp)[] = [...defaultInclude] + + if (Array.isArray(compile.include)) { + include.unshift(...compile.include) } + + // Note:如果 compile 有 传递exclude,那么就进行覆盖,与 webpack5 逻辑保持一致 + if (Array.isArray(compile.exclude)) { + exclude = [...compile.exclude] + } + const filter = createFilter(include, exclude) opts.filter = filter } diff --git a/packages/taro-webpack5-runner/src/webpack/H5WebpackModule.ts b/packages/taro-webpack5-runner/src/webpack/H5WebpackModule.ts index 80f9035a2202..a01d4c4b9ce9 100644 --- a/packages/taro-webpack5-runner/src/webpack/H5WebpackModule.ts +++ b/packages/taro-webpack5-runner/src/webpack/H5WebpackModule.ts @@ -268,9 +268,10 @@ export class H5WebpackModule { rule.include.unshift(...compile.include) } - rule.exclude = [filename => /@tarojs[\\/]components/.test(filename)] if (Array.isArray(compile.exclude)) { - rule.exclude.unshift(...compile.exclude) + rule.exclude = [...compile.exclude] + } else { + rule.exclude = [filename => /@tarojs[\\/]components/.test(filename)] } return rule diff --git a/packages/taro-webpack5-runner/src/webpack/HarmonyWebpackModule.ts b/packages/taro-webpack5-runner/src/webpack/HarmonyWebpackModule.ts index bae12e2ee7c2..f613ea5ce750 100644 --- a/packages/taro-webpack5-runner/src/webpack/HarmonyWebpackModule.ts +++ b/packages/taro-webpack5-runner/src/webpack/HarmonyWebpackModule.ts @@ -3,7 +3,6 @@ import { recursiveMerge, REG_CSS, REG_LESS, - REG_NODE_MODULES, REG_SASS_SASS, REG_SASS_SCSS, REG_STYLUS, @@ -171,26 +170,17 @@ export class HarmonyWebpackModule { const { compile = {} } = this.combination.config const rule: IRule = WebpackModule.getScriptRule() - if (compile.exclude && compile.exclude.length) { - rule.exclude = [ - ...compile.exclude, - filename => /css-loader/.test(filename) || (REG_NODE_MODULES.test(filename) && !(/taro/.test(filename))) - ] - } else if (compile.include && compile.include.length) { - rule.include = [ - ...compile.include, - sourceDir, - filename => /taro/.test(filename) - ] - } else { - rule.exclude = [filename => /css-loader/.test(filename) || (REG_NODE_MODULES.test(filename) && !(/taro/.test(filename)))] + rule.include = [ + sourceDir, + filename => /(?<=node_modules[\\/]).*taro/.test(filename) + ] + if (Array.isArray(compile.include)) { + rule.include.unshift(...compile.include) } - // rule.use.compilerLoader = WebpackModule.getLoader(path.resolve(__dirname, '../loaders/miniCompilerLoader'), { - // template: this.combination.config.template, - // outputDir: this.combination.outputDir, - // fileType: this.combination.fileType, - // }) + if (Array.isArray(compile.exclude)) { + rule.exclude = [...compile.exclude] + } return rule }