diff --git a/.changeset/great-seas-jump.md b/.changeset/great-seas-jump.md new file mode 100644 index 000000000000..dcf3484c3de8 --- /dev/null +++ b/.changeset/great-seas-jump.md @@ -0,0 +1,6 @@ +--- +'@modern-js/app-tools': patch +--- + +fix(app-tools): init config can't handle moduleScopes as function +fix(app-tools): 初始化 config 不能处理 moduleScopes 是 function 的情况 diff --git a/packages/solutions/app-tools/src/config/initialize/inits.ts b/packages/solutions/app-tools/src/config/initialize/inits.ts index cac87e6be800..be152c62bbdf 100644 --- a/packages/solutions/app-tools/src/config/initialize/inits.ts +++ b/packages/solutions/app-tools/src/config/initialize/inits.ts @@ -58,59 +58,63 @@ export function initSourceConfig( (config as AppNormalizedConfig).source.moduleScopes = createBuilderModuleScope(config as AppNormalizedConfig); } +} - function createBuilderInclude( - config: AppNormalizedConfig<'shared'>, - appContext: IAppContext, - ) { - const { include } = config.source; - const defaultInclude = [appContext.internalDirectory]; - const transformInclude = (include || []) - .map((include: string | RegExp) => { - if (typeof include === 'string') { - if (isAbsolute(include)) { - return include; - } - return new RegExp(include); +function createBuilderInclude( + config: AppNormalizedConfig<'shared'>, + appContext: IAppContext, +) { + const { include } = config.source; + const defaultInclude = [appContext.internalDirectory]; + const transformInclude = (include || []) + .map((include: string | RegExp) => { + if (typeof include === 'string') { + if (isAbsolute(include)) { + return include; } - return include; - }) - .concat(defaultInclude); // concat default Include + return new RegExp(include); + } + return include; + }) + .concat(defaultInclude); // concat default Include + + return transformInclude; +} - return transformInclude; +export function createBuilderModuleScope( + config: AppNormalizedConfig<'webpack'>, +) { + type ModuleScopes = Array; + + const { moduleScopes } = config.source; + if (moduleScopes) { + const DEFAULT_SCOPES: ModuleScopes = ['./src', './shared', /node_modules/]; + + const builderModuleScope = applyScopeOptions(DEFAULT_SCOPES, moduleScopes); + return builderModuleScope; + } else { + return undefined; } - function createBuilderModuleScope(config: AppNormalizedConfig<'webpack'>) { - const { moduleScopes } = config.source; - if (moduleScopes) { - let builderModuleScope: any[] = []; - const DEFAULT_SCOPES: Array = [ - './src', - './shared', - /node_modules/, - ]; - if (Array.isArray(moduleScopes)) { - if (isPrimitiveScope(moduleScopes)) { - builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes); - } else { - builderModuleScope = [DEFAULT_SCOPES, ...moduleScopes]; - } - } else { - builderModuleScope = [DEFAULT_SCOPES, moduleScopes]; - } - return builderModuleScope; - } else { - return undefined; - } + function isPrimitiveScope(items: unknown[]): items is ModuleScopes { + return items.every( + item => + typeof item === 'string' || + Object.prototype.toString.call(item) === '[object RegExp]', + ); + } - function isPrimitiveScope( - items: unknown[], - ): items is Array { - return items.every( - item => - typeof item === 'string' || - Object.prototype.toString.call(item) === '[object RegExp]', - ); + type ScopesOptions = NonNullable< + AppNormalizedConfig<'webpack'>['source']['moduleScopes'] + >; + + function applyScopeOptions(defaults: ModuleScopes, options: ScopesOptions) { + if (Array.isArray(options)) { + if (isPrimitiveScope(options)) { + return defaults.concat(options); + } + return options.reduce(applyScopeOptions, defaults); } + return options(defaults) || defaults; } } diff --git a/packages/solutions/app-tools/tests/config/init.test.ts b/packages/solutions/app-tools/tests/config/init.test.ts new file mode 100644 index 000000000000..0b8972cc4306 --- /dev/null +++ b/packages/solutions/app-tools/tests/config/init.test.ts @@ -0,0 +1,44 @@ +import { createBuilderModuleScope } from '../../src/config/initialize/inits'; +import { AppNormalizedConfig } from '../../src/types'; + +describe('test createBuilderModuleScope', () => { + it('should return undefined when moduleScope = undefined', () => { + const config: AppNormalizedConfig = { + source: {}, + } as any; + const moduleScopes = createBuilderModuleScope(config); + + expect(moduleScopes).toBeUndefined(); + }); + + it('should merge config when moduleScope as a Array', () => { + const config: AppNormalizedConfig = { + source: { + moduleScopes: ['hello', /abc/], + }, + } as any; + + const moduleScopes = createBuilderModuleScope(config); + + expect(moduleScopes).toEqual([ + './src', + './shared', + /node_modules/, + 'hello', + /abc/, + ]); + }); + + it('should though function handle when moduleScopes as a function', () => { + const config: AppNormalizedConfig = { + source: { + moduleScopes(module: Array) { + module.pop(); + module.push('abc'); + }, + }, + } as any; + const moduleScopes = createBuilderModuleScope(config); + expect(moduleScopes).toEqual(['./src', './shared', 'abc']); + }); +});