Skip to content

Commit

Permalink
refactor: 修改 compile 的 exclude 和 include 逻辑 (#16469)
Browse files Browse the repository at this point in the history
* feat: 修改 compile 的 exclude 和 include 逻辑

* refactor: 删除不必要的引入

* fix: lint

---------

Co-authored-by: Shijie Yu <[email protected]>
  • Loading branch information
ZEJIA-LIU and tutuxxx authored Nov 27, 2024
1 parent 7029f3e commit 8317e63
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
4 changes: 2 additions & 2 deletions packages/taro-vite-runner/src/mini/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down
23 changes: 11 additions & 12 deletions packages/taro-vite-runner/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions packages/taro-webpack5-runner/src/webpack/H5WebpackModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 9 additions & 19 deletions packages/taro-webpack5-runner/src/webpack/HarmonyWebpackModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
recursiveMerge,
REG_CSS,
REG_LESS,
REG_NODE_MODULES,
REG_SASS_SASS,
REG_SASS_SCSS,
REG_STYLUS,
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 8317e63

Please sign in to comment.