Skip to content

Commit

Permalink
fix(app-tools): init config can't handle moduleScopes as function (#4897
Browse files Browse the repository at this point in the history
)
  • Loading branch information
GiveMe-A-Name authored Nov 2, 2023
1 parent 83859a2 commit 957de56
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 47 deletions.
6 changes: 6 additions & 0 deletions .changeset/great-seas-jump.md
Original file line number Diff line number Diff line change
@@ -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 的情况
98 changes: 51 additions & 47 deletions packages/solutions/app-tools/src/config/initialize/inits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | RegExp>;

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<string | RegExp> = [
'./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<string | RegExp> {
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<ModuleScopes>(applyScopeOptions, defaults);
}
return options(defaults) || defaults;
}
}
44 changes: 44 additions & 0 deletions packages/solutions/app-tools/tests/config/init.test.ts
Original file line number Diff line number Diff line change
@@ -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<string>) {
module.pop();
module.push('abc');
},
},
} as any;
const moduleScopes = createBuilderModuleScope(config);
expect(moduleScopes).toEqual(['./src', './shared', 'abc']);
});
});

0 comments on commit 957de56

Please sign in to comment.