feat: 支持按路由编译出.html文件

This commit is contained in:
harrywan 2021-11-03 14:45:44 +08:00
parent a20b40cbbc
commit 04a33d2c68
7 changed files with 58 additions and 7 deletions

View File

@ -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'),

View File

@ -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: {
}
});

View File

@ -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

View File

@ -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,

View File

@ -1,10 +1,12 @@
// .fes.js 只负责管理编译时配置只能使用plain Object
export default {
exportStatic: {},
base: "/base/",
define: {
__DEV__: false,
},
publicPath: "./",
html: {
title: "海贼王",
},

View File

@ -0,0 +1,8 @@
<template>
<div>a/b</div>
</template>
<script>
export default {
};
</script>

View File

@ -0,0 +1,8 @@
<template>
<div>b</div>
</template>
<script>
export default {
};
</script>