diff --git a/packages/fes-build-vite/types.d.ts b/packages/fes-build-vite/types.d.ts index 96b23fe80..4008e2b35 100644 --- a/packages/fes-build-vite/types.d.ts +++ b/packages/fes-build-vite/types.d.ts @@ -6,9 +6,9 @@ import {createHtmlPlugin} from 'vite-plugin-html' export interface ViteBuildConfig { - viteOption: UserConfig; - viteVuePlugin: Options; - viteVueJsx: Parameters[0]; - viteLegacy: PolyfillOptions; - viteHtml: Parameters[0] + viteOption?: UserConfig; + viteVuePlugin?: Options; + viteVueJsx?: Parameters[0]; + viteLegacy?: PolyfillOptions; + viteHtml?: Parameters[0] } diff --git a/packages/fes-build-webpack/src/index.js b/packages/fes-build-webpack/src/index.js index d1ee8dbc4..695397c49 100644 --- a/packages/fes-build-webpack/src/index.js +++ b/packages/fes-build-webpack/src/index.js @@ -3,15 +3,13 @@ export default function () { plugins: [ // register methods require.resolve('./plugins/registerMethods'), + require.resolve('./plugins/registerType'), // bundle configs - require.resolve('./plugins/features/alias'), require.resolve('./plugins/features/analyze'), - require.resolve('./plugins/features/autoprefixer'), require.resolve('./plugins/features/chainWebpack'), require.resolve('./plugins/features/cssLoader'), require.resolve('./plugins/features/copy'), - require.resolve('./plugins/features/define'), require.resolve('./plugins/features/devServer'), require.resolve('./plugins/features/devtool'), require.resolve('./plugins/features/externals'), @@ -20,12 +18,8 @@ export default function () { require.resolve('./plugins/features/extraBabelPresets'), require.resolve('./plugins/features/extraPostCSSPlugins'), require.resolve('./plugins/features/html'), - require.resolve('./plugins/features/inlineLimit'), require.resolve('./plugins/features/lessLoader'), - require.resolve('./plugins/features/outputPath'), require.resolve('./plugins/features/postcssLoader'), - require.resolve('./plugins/features/publicPath'), - require.resolve('./plugins/features/terserOptions'), require.resolve('./plugins/features/nodeModulesTransform'), require.resolve('./plugins/features/vueLoader'), diff --git a/packages/fes-build-webpack/src/plugins/commands/webpackConfig/index.js b/packages/fes-build-webpack/src/plugins/commands/webpackConfig/index.js index 734493948..d847e6945 100644 --- a/packages/fes-build-webpack/src/plugins/commands/webpackConfig/index.js +++ b/packages/fes-build-webpack/src/plugins/commands/webpackConfig/index.js @@ -38,6 +38,17 @@ function genTranspileDepRegex(exclude) { return deps.length ? new RegExp(deps.join('|')) : null; } +function handleAlias({ api, webpackConfig }) { + const config = api.config; + if (config.alias) { + Object.keys(config.alias).forEach((key) => { + webpackConfig.resolve.alias.set(key, config.alias[key]); + }); + } + webpackConfig.resolve.alias.set('@', api.paths.absSrcPath); + webpackConfig.resolve.alias.set('@@', api.paths.absTmpPath); +} + export default async function getConfig({ api, cwd, config, env, entry = {}, modifyBabelOpts, modifyBabelPresetOpts, chainWebpack, headScripts, publicPath }) { const isDev = env === 'development'; const isProd = env === 'production'; @@ -68,11 +79,7 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod // --------------- resolve ----------- webpackConfig.resolve.extensions.merge(['.mjs', '.js', '.jsx', '.vue', '.ts', '.tsx', '.json', '.wasm']); - if (config.alias) { - Object.keys(config.alias).forEach((key) => { - webpackConfig.resolve.alias.set(key, config.alias[key]); - }); - } + handleAlias({ api, webpackConfig }); // --------------- module ----------- webpackConfig.module diff --git a/packages/fes-build-webpack/src/plugins/features/alias.js b/packages/fes-build-webpack/src/plugins/features/alias.js deleted file mode 100644 index 234f2eb9c..000000000 --- a/packages/fes-build-webpack/src/plugins/features/alias.js +++ /dev/null @@ -1,21 +0,0 @@ -export default (api) => { - const { paths } = api; - - api.describe({ - key: 'alias', - config: { - schema(joi) { - return joi.object(); - }, - default: {}, - }, - }); - - api.chainWebpack(async (memo) => { - // 选择在 chainWebpack 中进行以上 alias 的初始化,是为了支持用户使用 modifyPaths API 对 paths 进行改写 - memo.resolve.alias.set('@', paths.absSrcPath); - memo.resolve.alias.set('@@', paths.absTmpPath); - - return memo; - }); -}; diff --git a/packages/fes-build-webpack/src/plugins/registerType.js b/packages/fes-build-webpack/src/plugins/registerType.js new file mode 100644 index 000000000..ddeb5791f --- /dev/null +++ b/packages/fes-build-webpack/src/plugins/registerType.js @@ -0,0 +1,8 @@ +import { name } from '../package.json'; + +export default function (api) { + api.addConfigType(() => ({ + source: name, + build: ['WebpackBuildConfig'], + })); +} diff --git a/packages/fes-build-webpack/types.d.ts b/packages/fes-build-webpack/types.d.ts index 052b2b91d..bdd3b1c2c 100644 --- a/packages/fes-build-webpack/types.d.ts +++ b/packages/fes-build-webpack/types.d.ts @@ -1,5 +1,57 @@ +import Config from 'webpack-chain' +import webpack from 'webpack'; +import HtmlWebpackPlugin from 'html-webpack-plugin' +interface CopyFileType { + from: string; + to: string; +} export interface WebpackBuildConfig { - + analyze?: { + analyzerMode?: 'server' | 'static' | 'disabled'; + analyzerHost?: string; + analyzerPort?: number | 'auto'; + openAnalyzer?: boolean; + generateStatsFile?: boolean; + statsFilename?: string; + logLevel?: 'info' | 'warn' | 'error' | 'silent'; + defaultSizes?: 'stat' | 'parsed' | 'gzip' + }; + chainWebpack?: (memo: Config, args: {env: string, webpack: typeof webpack}) => void; + copy?: CopyFileType | CopyFileType[]; + cssLoader?: { + url?: boolean | ((url: string, resourcePath: string) => boolean); + import?: boolean | ({ filter: (url: string, media: string, resourcePath: string) => boolean }); + modules?: boolean | string | object; + sourceMap?: boolean; + importLoaders?: number; + onlyLocals?: boolean; + esModule?: boolean; + localsConvention?: 'asIs' | 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly'; + }; + devServer?: { + port?: number; + host?: string; + https?: boolean; + headers?: object; + [key: string]: any; + }; + devtool?: string; + exportStatic?: { + htmlSuffix?: boolean; + dynamicRoot?: boolean; + }; + externals?: object | string | Function; + extraBabelPlugins?: []; + extraBabelPresets?: []; + extraPostCSSPlugins?: []; + html?: HtmlWebpackPlugin.Options; + lessLoader?: Record; + nodeModulesTransform?: { + exclude: string[] + }; + postcssLoader?: Record; + vueLoader?: object; + } \ No newline at end of file diff --git a/packages/fes-plugin-qiankun/src/main/index.js b/packages/fes-plugin-qiankun/src/main/index.js index 9605a1943..f5ccedd2b 100644 --- a/packages/fes-plugin-qiankun/src/main/index.js +++ b/packages/fes-plugin-qiankun/src/main/index.js @@ -57,7 +57,7 @@ export default function (api) { const { main: options } = api.config?.qiankun || {}; const masterHistoryType = api.config?.router?.mode || defaultHistoryType; - const base = api.config.base; + const base = api.config.router?.base; api.writeTmpFile({ path: absMasterOptionsPath, content: ` diff --git a/packages/fes-plugin-qiankun/src/micro/index.js b/packages/fes-plugin-qiankun/src/micro/index.js index 77012c408..e34010cc9 100644 --- a/packages/fes-plugin-qiankun/src/micro/index.js +++ b/packages/fes-plugin-qiankun/src/micro/index.js @@ -36,7 +36,7 @@ export default function (api) { const shouldNotModifyDefaultBase = api.userConfig.qiankun?.slave?.shouldNotModifyDefaultBase ?? initialMicroOptions.shouldNotModifyDefaultBase; if (!shouldNotModifyDefaultBase) { - modifiedDefaultConfig.base = `/${api.pkg.name}`; + modifiedDefaultConfig.router.base = `/${api.pkg.name}`; } return modifiedDefaultConfig; diff --git a/packages/fes-preset-built-in/src/index.js b/packages/fes-preset-built-in/src/index.js index ec54edee8..593082af9 100644 --- a/packages/fes-preset-built-in/src/index.js +++ b/packages/fes-preset-built-in/src/index.js @@ -12,16 +12,22 @@ export default function () { require.resolve('./plugins/generateFiles/genType'), // bundle configs - require.resolve('./plugins/features/base'), + require.resolve('./plugins/features/alias'), + require.resolve('./plugins/features/autoprefixer'), require.resolve('./plugins/features/checkVuePackage'), + require.resolve('./plugins/features/define'), require.resolve('./plugins/features/dynamicImport'), require.resolve('./plugins/features/globalCSS'), + require.resolve('./plugins/features/inlineLimit'), require.resolve('./plugins/features/mountElementId'), require.resolve('./plugins/features/mock'), + require.resolve('./plugins/features/outputPath'), require.resolve('./plugins/features/plugins'), require.resolve('./plugins/features/proxy'), + require.resolve('./plugins/features/publicPath'), require.resolve('./plugins/features/singular'), require.resolve('./plugins/features/targets'), + require.resolve('./plugins/features/terserOptions'), // route require.resolve('./plugins/route'), diff --git a/packages/fes-preset-built-in/src/plugins/features/base.js b/packages/fes-preset-built-in/src/plugins/features/alias.js similarity index 57% rename from packages/fes-preset-built-in/src/plugins/features/base.js rename to packages/fes-preset-built-in/src/plugins/features/alias.js index 28f3a1179..c4fbc0668 100644 --- a/packages/fes-preset-built-in/src/plugins/features/base.js +++ b/packages/fes-preset-built-in/src/plugins/features/alias.js @@ -1,11 +1,11 @@ export default (api) => { api.describe({ - key: 'base', + key: 'alias', config: { - default: '', schema(joi) { - return joi.string().allow(''); + return joi.object(); }, + default: {}, }, }); }; diff --git a/packages/fes-build-webpack/src/plugins/features/autoprefixer.js b/packages/fes-preset-built-in/src/plugins/features/autoprefixer.js similarity index 100% rename from packages/fes-build-webpack/src/plugins/features/autoprefixer.js rename to packages/fes-preset-built-in/src/plugins/features/autoprefixer.js diff --git a/packages/fes-build-webpack/src/plugins/features/define.js b/packages/fes-preset-built-in/src/plugins/features/define.js similarity index 100% rename from packages/fes-build-webpack/src/plugins/features/define.js rename to packages/fes-preset-built-in/src/plugins/features/define.js diff --git a/packages/fes-build-webpack/src/plugins/features/inlineLimit.js b/packages/fes-preset-built-in/src/plugins/features/inlineLimit.js similarity index 100% rename from packages/fes-build-webpack/src/plugins/features/inlineLimit.js rename to packages/fes-preset-built-in/src/plugins/features/inlineLimit.js diff --git a/packages/fes-build-webpack/src/plugins/features/outputPath.js b/packages/fes-preset-built-in/src/plugins/features/outputPath.js similarity index 100% rename from packages/fes-build-webpack/src/plugins/features/outputPath.js rename to packages/fes-preset-built-in/src/plugins/features/outputPath.js diff --git a/packages/fes-build-webpack/src/plugins/features/publicPath.js b/packages/fes-preset-built-in/src/plugins/features/publicPath.js similarity index 100% rename from packages/fes-build-webpack/src/plugins/features/publicPath.js rename to packages/fes-preset-built-in/src/plugins/features/publicPath.js diff --git a/packages/fes-build-webpack/src/plugins/features/terserOptions.js b/packages/fes-preset-built-in/src/plugins/features/terserOptions.js similarity index 100% rename from packages/fes-build-webpack/src/plugins/features/terserOptions.js rename to packages/fes-preset-built-in/src/plugins/features/terserOptions.js diff --git a/packages/fes-preset-built-in/src/plugins/route/index.js b/packages/fes-preset-built-in/src/plugins/route/index.js index 2dc846a11..8113068e8 100644 --- a/packages/fes-preset-built-in/src/plugins/route/index.js +++ b/packages/fes-preset-built-in/src/plugins/route/index.js @@ -263,6 +263,7 @@ export default function (api) { config: { schema(joi) { return joi.object({ + base: joi.string(), routes: joi.array(), mode: joi.string(), }); @@ -328,7 +329,7 @@ export default function (api) { content: Mustache.render(routeExportsTpl, { runtimePath, config: api.config, - routerBase: api.config.base, + routerBase: api.config.router?.base, CREATE_HISTORY: historyType[api.config.router.mode] || 'createWebHashHistory', }), }); diff --git a/packages/fes/types.d.ts b/packages/fes/types.d.ts index 06411f8a1..548e1afcf 100644 --- a/packages/fes/types.d.ts +++ b/packages/fes/types.d.ts @@ -47,20 +47,66 @@ export function defineRuntimeConfig(config: InnerRuntimeConfig & PluginRuntimeCo interface InnerBuildConfig { - base: string; - dynamicImport: boolean; - mock: boolean | { - prefix: string; + alias?: Record, + autoprefixer?: { + /** environment for `Browserslist` */ + env?: string + + /** should Autoprefixer use Visual Cascade, if CSS is uncompressed */ + cascade?: boolean + + /** should Autoprefixer add prefixes. */ + add?: boolean + + /** should Autoprefixer [remove outdated] prefixes */ + remove?: boolean + + /** should Autoprefixer add prefixes for @supports parameters. */ + supports?: boolean + + /** should Autoprefixer add prefixes for flexbox properties */ + flexbox?: boolean | 'no-2009' + + /** should Autoprefixer add IE 10-11 prefixes for Grid Layout properties */ + grid?: boolean | GridValue + + /** custom usage statistics for > 10% in my stats browsers query */ + stats?: Stats + + /** + * list of queries for target browsers. + * Try to not use it. + * The best practice is to use `.browserslistrc` config or `browserslist` key in `package.json` + * to share target browsers with Babel, ESLint and Stylelint + */ + overrideBrowserslist?: string | string[] + + /** do not raise error on unknown browser version in `Browserslist` config. */ + ignoreUnknownVersions?: boolean }; - mountElementId: string; - plugins: string[]; - proxy: { + define?: Record, + router?: { + base?: string; + routes?: RouteRecordRaw[]; + mode?: 'hash' | 'history' | 'memory' + }; + dynamicImport?: boolean; + inlineLimit?: number; + mock?: boolean | { + prefix?: string; + }; + mountElementId?: string; + plugins?: string[]; + proxy?: { [apiPrefix: string]: { target: string; changeOrigin?: boolean; } }; - singular: boolean; + publicPath?: string; + singular?: boolean; + targets?: object; + terserOptions?: object; } export function defineBuildConfig(config: InnerBuildConfig & PluginBuildConfig ): InnerBuildConfig & PluginBuildConfig;