fix: html 模版参数配置问题

This commit is contained in:
bac-joker 2021-03-03 15:11:11 +08:00
parent 39860d31b0
commit 8514d1b4e6
5 changed files with 46 additions and 43 deletions

View File

@ -1,40 +1,5 @@
import webpack from 'webpack'; import webpack from 'webpack';
import resolveDefine from './resolveDefine';
const prefixRE = /^FES_APP_/;
const ENV_SHOULD_PASS = ['NODE_ENV', 'HMR', 'SOCKET_SERVER', 'ERROR_OVERLAY'];
function resolveDefine(opts = {}) {
const env = {};
Object.keys(process.env).forEach((key) => {
if (prefixRE.test(key) || ENV_SHOULD_PASS.includes(key)) {
env[key] = process.env[key];
}
});
for (const key in env) {
if (Object.prototype.hasOwnProperty.call(env, key)) {
env[key] = JSON.stringify(env[key]);
}
}
const define = {
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: false,
...opts.define
};
for (const key in define) {
if (Object.prototype.hasOwnProperty.call(define, key)) {
define[key] = JSON.stringify(define[key]);
}
}
return {
'process.env': env,
...define
};
}
export default function createDefineWebpackConfig({ export default function createDefineWebpackConfig({
config, config,

View File

@ -1,5 +1,6 @@
import { join, resolve } from 'path'; import { join, resolve } from 'path';
import { existsSync } from 'fs'; import { existsSync } from 'fs';
import resolveDefine from './resolveDefine';
export default async function createHtmlWebpackConfig({ export default async function createHtmlWebpackConfig({
cwd, cwd,
@ -10,8 +11,10 @@ export default async function createHtmlWebpackConfig({
}) { }) {
const htmlOptions = { const htmlOptions = {
filename: '[name].html', filename: '[name].html',
...config.html.options ...config.html,
templateParameters: resolveDefine(null, true)
}; };
htmlOptions.title = htmlOptions.title || 'fes.js';
if (isProd) { if (isProd) {

View File

@ -4,7 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Fes App</title> <title><%= htmlWebpackPlugin.options.title %></title>
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>

View File

@ -0,0 +1,39 @@
const prefixRE = /^FES_APP_/;
const ENV_SHOULD_PASS = ['NODE_ENV', 'HMR', 'SOCKET_SERVER', 'ERROR_OVERLAY'];
export default function resolveDefine(opts = {}, raw) {
const env = {};
Object.keys(process.env).forEach((key) => {
if (prefixRE.test(key) || ENV_SHOULD_PASS.includes(key)) {
env[key] = process.env[key];
}
});
if (raw) {
return env;
}
for (const key in env) {
if (Object.prototype.hasOwnProperty.call(env, key)) {
env[key] = JSON.stringify(env[key]);
}
}
const define = {
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: false,
...opts.define
};
for (const key in define) {
if (Object.prototype.hasOwnProperty.call(define, key)) {
define[key] = JSON.stringify(define[key]);
}
}
return {
'process.env': env,
...define
};
}

View File

@ -4,16 +4,12 @@ export default (api) => {
config: { config: {
schema(joi) { schema(joi) {
return joi return joi
.object({ .object()
options: joi.object(),
pages: joi.object()
})
.description( .description(
'more html-webpack-plugin options see https://github.com/jantimon/html-webpack-plugin#configuration' 'more html-webpack-plugin options see https://github.com/jantimon/html-webpack-plugin#configuration'
); );
}, },
default: { default: {
options: {}
} }
} }
}); });