diff --git a/packages/fes-preset-built-in/src/index.js b/packages/fes-preset-built-in/src/index.js index e491f6a2..fd8150af 100644 --- a/packages/fes-preset-built-in/src/index.js +++ b/packages/fes-preset-built-in/src/index.js @@ -45,6 +45,7 @@ export default function () { require.resolve('./plugins/features/mock'), require.resolve('./plugins/features/dynamicImport'), require.resolve('./plugins/features/runtimePublicPath'), + require.resolve('./plugins/features/exportStatic'), // misc require.resolve('./plugins/misc/route'), diff --git a/packages/fes-preset-built-in/src/plugins/commands/buildDevUtils.js b/packages/fes-preset-built-in/src/plugins/commands/buildDevUtils.js index b2b86505..eafc32b3 100644 --- a/packages/fes-preset-built-in/src/plugins/commands/buildDevUtils.js +++ b/packages/fes-preset-built-in/src/plugins/commands/buildDevUtils.js @@ -73,7 +73,7 @@ export async function getBundleAndConfigs({ const bundleConfig = await api.applyPlugins({ type: api.ApplyPluginsType.modify, key: 'modifyBundleConfig', - initialValue: await getConfig(getConfigOpts), + initialValue: await getConfig({ api, ...getConfigOpts }), args: { } }); diff --git a/packages/fes-preset-built-in/src/plugins/commands/webpackConfig/html.js b/packages/fes-preset-built-in/src/plugins/commands/webpackConfig/html.js index 5a5fd5b8..2a81af68 100644 --- a/packages/fes-preset-built-in/src/plugins/commands/webpackConfig/html.js +++ b/packages/fes-preset-built-in/src/plugins/commands/webpackConfig/html.js @@ -6,6 +6,7 @@ import { import resolveDefine from './resolveDefine'; export default async function createHtmlWebpackConfig({ + api, cwd, config, webpackConfig, @@ -38,13 +39,14 @@ export default async function createHtmlWebpackConfig({ const defaultHtmlPath = resolve(__dirname, 'index-default.html'); const publicCopyIgnore = []; - if (!multiPageConfig) { - // default, single page setup. - htmlOptions.template = existsSync(htmlPath) - ? htmlPath - : defaultHtmlPath; + // default, single page setup. + htmlOptions.template = existsSync(htmlPath) + ? htmlPath + : defaultHtmlPath; - publicCopyIgnore.push(winPath(htmlOptions.template)); + publicCopyIgnore.push(winPath(htmlOptions.template)); + + if (!multiPageConfig) { webpackConfig .plugin('html') .use(require.resolve('html-webpack-plugin'), [htmlOptions]); @@ -52,6 +54,34 @@ export default async function createHtmlWebpackConfig({ // TODO 支持多页 } + // 如果需要导出html,则根据路由生成对应的html文件 + if (config.exportStatic) { + const routes = await api.getRoutes(); + const addHtml = (_routes) => { + if (Array.isArray(_routes)) { + _routes.forEach((route) => { + const _fileName = `${route.path.slice(1) || 'index'}.html`; + if (_fileName !== 'index.html') { + const _htmlOptions = { + title: 'fes.js', + filename: _fileName, + ...config.html, + templateParameters: resolveDefine(config, true), + mountElementId: config.mountElementId + }; + webpackConfig + .plugin(_fileName) + .use(require.resolve('html-webpack-plugin'), [_htmlOptions]); + } + if (route.children && route.children.length) { + addHtml(route.children); + } + }); + } + }; + addHtml(routes); + } + if (headScripts) { const headScriptsMap = await headScripts(); webpackConfig diff --git a/packages/fes-preset-built-in/src/plugins/commands/webpackConfig/index.js b/packages/fes-preset-built-in/src/plugins/commands/webpackConfig/index.js index d821b0b4..903fbd00 100644 --- a/packages/fes-preset-built-in/src/plugins/commands/webpackConfig/index.js +++ b/packages/fes-preset-built-in/src/plugins/commands/webpackConfig/index.js @@ -59,6 +59,7 @@ function genTranspileDepRegex(exclude) { export default async function getConfig({ + api, cwd, config, env, @@ -219,6 +220,7 @@ export default async function getConfig({ // --------------- html ----------- const { publicCopyIgnore } = await createHtmlWebpackConfig({ + api, cwd, config, webpackConfig, diff --git a/packages/fes-template/.fes.js b/packages/fes-template/.fes.js index 2d02144c..4374b125 100644 --- a/packages/fes-template/.fes.js +++ b/packages/fes-template/.fes.js @@ -1,10 +1,12 @@ // .fes.js 只负责管理编译时配置,只能使用plain Object export default { + exportStatic: {}, base: "/base/", define: { __DEV__: false, }, + publicPath: "./", html: { title: "海贼王", }, diff --git a/packages/fes-template/src/pages/a/b.vue b/packages/fes-template/src/pages/a/b.vue new file mode 100644 index 00000000..183c640d --- /dev/null +++ b/packages/fes-template/src/pages/a/b.vue @@ -0,0 +1,8 @@ + + diff --git a/packages/fes-template/src/pages/b/index.vue b/packages/fes-template/src/pages/b/index.vue new file mode 100644 index 00000000..26f31da7 --- /dev/null +++ b/packages/fes-template/src/pages/b/index.vue @@ -0,0 +1,8 @@ + +