mirror of
https://gitee.com/WeBank/fes.js.git
synced 2024-12-02 19:58:18 +08:00
feat: 支持按路由编译出.html文件
This commit is contained in:
parent
a20b40cbbc
commit
04a33d2c68
@ -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'),
|
||||
|
@ -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: {
|
||||
}
|
||||
});
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -1,10 +1,12 @@
|
||||
// .fes.js 只负责管理编译时配置,只能使用plain Object
|
||||
|
||||
export default {
|
||||
exportStatic: {},
|
||||
base: "/base/",
|
||||
define: {
|
||||
__DEV__: false,
|
||||
},
|
||||
publicPath: "./",
|
||||
html: {
|
||||
title: "海贼王",
|
||||
},
|
||||
|
8
packages/fes-template/src/pages/a/b.vue
Normal file
8
packages/fes-template/src/pages/a/b.vue
Normal file
@ -0,0 +1,8 @@
|
||||
<template>
|
||||
<div>a/b</div>
|
||||
</template>
|
||||
<script>
|
||||
export default {
|
||||
|
||||
};
|
||||
</script>
|
8
packages/fes-template/src/pages/b/index.vue
Normal file
8
packages/fes-template/src/pages/b/index.vue
Normal file
@ -0,0 +1,8 @@
|
||||
<template>
|
||||
<div>b</div>
|
||||
</template>
|
||||
<script>
|
||||
export default {
|
||||
|
||||
};
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user