CompilerConfiguration提供更加完善的类型信息

This commit is contained in:
pqcqaq 2024-12-08 01:10:27 +08:00
parent ffe98e1710
commit 6e211b447c
4 changed files with 101 additions and 12 deletions

View File

@ -391,17 +391,30 @@ module.exports = function (webpackEnv) {
net: false, net: false,
tls: false, tls: false,
}; };
let merged = defaultFb;
if ( if (
projectConfiguration && projectConfiguration &&
projectConfiguration.resolve && projectConfiguration.resolve &&
projectConfiguration.resolve.fallback projectConfiguration.resolve.fallback
) { ) {
Object.assign( if(Array.isArray(projectConfiguration.resolve.fallback)) {
defaultFb, merged = [...Object.keys(defaultFb).map(key => {
projectConfiguration.resolve.fallback // transform to :
); // {
// alias: string | false | string[];
// name: string;
// onlyModule?: boolean;
// }[]
return {
alias: defaultFb[key],
name: key,
}
}), ...projectConfiguration.resolve.fallback];
} else {
merged = Object.assign(defaultFb, projectConfiguration.resolve.fallback);
}
} }
return defaultFb; return merged;
})(), })(),
// This allows you to set a fallback for where webpack should look for modules. // This allows you to set a fallback for where webpack should look for modules.
// We placed these paths second because we want `node_modules` to "win" // We placed these paths second because we want `node_modules` to "win"
@ -433,17 +446,34 @@ module.exports = function (webpackEnv) {
'bn.js': require.resolve('bn.js'), 'bn.js': require.resolve('bn.js'),
assert: require.resolve('browser-assert'), assert: require.resolve('browser-assert'),
}; };
let merged = defaultAlias;
if ( if (
projectConfiguration && projectConfiguration &&
projectConfiguration.resolve && projectConfiguration.resolve &&
projectConfiguration.resolve.alias projectConfiguration.resolve.alias
) { ) {
Object.assign( // Object.assign(
defaultAlias, // defaultAlias,
projectConfiguration.resolve.alias // projectConfiguration.resolve.alias
); // );
if (Array.isArray(projectConfiguration.resolve.alias)) {
// transform to :
//{
// alias: string | false | string[];
// name: string;
// onlyModule?: boolean;
// }[]
merged = [...Object.keys(defaultAlias).map(key => {
return {
alias: defaultAlias[key],
name: key,
}
}), ...projectConfiguration.resolve.alias];
} else {
merged = Object.assign(defaultAlias, projectConfiguration.resolve.alias)
}
} }
return defaultAlias; return merged;
})(), })(),
plugins: [ plugins: [
// Prevents users from importing files from outside of src/ (or node_modules/). // Prevents users from importing files from outside of src/ (or node_modules/).

29
lib/createConfig.d.ts vendored
View File

@ -1,4 +1,25 @@
import { CompilerConfiguration } from 'oak-domain/lib/types/Configuration'; import { Configuration as WebpackConfiguration } from 'webpack';
type ResolveType = Required<WebpackConfiguration>['resolve'];
type AliasType = Required<ResolveType>['alias'];
type FallbackType = Required<ResolveType>['fallback'];
type OptimizationType = Required<WebpackConfiguration>['optimization'];
type SplitChunks = Required<OptimizationType>['splitChunks'];
type CacheGroups = Exclude<SplitChunks, false>["cacheGroups"];
/**
*
*/
export type CompilerConfiguration = {
webpack?: {
resolve?: {
alias?: AliasType;
fallback?: FallbackType;
};
extraOakModules?: (string | RegExp)[];
splitChunks: {
cacheGroups: CacheGroups;
};
};
};
/** /**
* oak编译器配置 * oak编译器配置
* @param raw * @param raw
@ -6,3 +27,9 @@ import { CompilerConfiguration } from 'oak-domain/lib/types/Configuration';
* *
*/ */
export declare function CreateComilerConfig(raw: CompilerConfiguration): CompilerConfiguration; export declare function CreateComilerConfig(raw: CompilerConfiguration): CompilerConfiguration;
export {};
/**
* compiler.js中的模块导出使用以下形式
* module.exports = CreateComilerConfig({})
*
*/

View File

@ -11,3 +11,8 @@ function CreateComilerConfig(raw) {
// 在这里可以做配置的预处理 // 在这里可以做配置的预处理
return raw; return raw;
} }
/**
* 将compiler.js中的模块导出使用以下形式
* module.exports = CreateComilerConfig({})
* 可以在编写时获得类型提示
*/

View File

@ -1,4 +1,31 @@
import { CompilerConfiguration } from 'oak-domain/lib/types/Configuration'; // webpack config type
import { Configuration as WebpackConfiguration } from 'webpack';
type ResolveType = Required<WebpackConfiguration>['resolve'];
type AliasType = Required<ResolveType>['alias'];
type FallbackType = Required<ResolveType>['fallback'];
type OptimizationType = Required<WebpackConfiguration>['optimization'];
type SplitChunks = Required<OptimizationType>['splitChunks'];
type CacheGroups = Exclude<SplitChunks, false>["cacheGroups"];
type ExternalsType = Required<WebpackConfiguration>['externals'];
type ExternalsObjectType = Exclude<ExternalsType, RegExp | string | Function | Array<any>>;
/**
*
*/
export type CompilerConfiguration = {
webpack?: {
resolve?: {
alias?: AliasType;
fallback?: FallbackType;
},
extraOakModules?: (string | RegExp)[]; // 被标记的module也会和项目一起被oak编译plugin进行处理注入getRender处理i18n等
splitChunks: {
cacheGroups: CacheGroups;
},
externals?: ExternalsObjectType;
},
};
/** /**
* oak编译器配置 * oak编译器配置