Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 修改 compile 的 exclude 和 include 逻辑 #16469

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading