Skip to content

Commit

Permalink
fix: 修复开发环境中间件对webpack@5.x支持
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongzhi107 committed Oct 16, 2020
1 parent 8a463f0 commit fcf1eff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
17 changes: 8 additions & 9 deletions src/lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ function injectManifestMeta(html, templateEngine, manifest) {

function injectStyles(html, templateEngine, chunkName, assets) {
const publicPath = '/';
const styles = assets.filter(asset => asset.endsWith('.css'));
const styles = assets.filter(({ name }) => name.endsWith('.css'));

if (styles.length > 0) {
let styleHtml;
if (templateEngine === 'pug') {
styleHtml = `block append style\n${
styles
.map(file => ` link(href="${publicPath + file}" rel="stylesheet")`)
.map(({ name }) => ` link(href="${publicPath + name}" rel="stylesheet")`)
.join('\n')
}`;
html = `${html}\n${styleHtml}\n`;
} else {
styleHtml = styles
.map(file => ` <link href="${publicPath + file}" rel="stylesheet">`)
.map(({ name }) => ` <link href="${publicPath + name}" rel="stylesheet">`)
.join('\n');
html = html.replace('</head>', `${styleHtml}\n </head>`);
}
Expand All @@ -84,10 +84,10 @@ function injectStyles(html, templateEngine, chunkName, assets) {
return html;
}

// eslint-disable-next-line
// eslint-disable-next-line max-len
function injectScripts(html, templateEngine, chunkName, assets, commonChunks, scriptInjectPosition) {
const publicPath = '/';
const scripts = assets.filter(asset => asset.endsWith('.js'));
const scripts = assets.filter(({ name }) => name.endsWith('.js'));

if (isObject(commonChunks)) {
Object.keys(commonChunks).forEach((name) => {
Expand All @@ -100,13 +100,13 @@ function injectScripts(html, templateEngine, chunkName, assets, commonChunks, sc
if (templateEngine === 'pug') {
scriptHtml = `block append script\n${
scripts
.map(file => ` script(src="${publicPath + file}")`)
.map(({ name }) => ` script(src="${publicPath + name}")`)
.join('\n')
}`;
html = `${html}\n${scriptHtml}\n`;
} else {
scriptHtml = scripts
.map(file => ` <script src="${publicPath + file}"></script>`)
.map(({ name }) => ` <script src="${publicPath + name}"></script>`)
.join('\n');
html = html.replace(`</${scriptInjectPosition}>`, `${scriptHtml}\n </${scriptInjectPosition}>`);
}
Expand Down Expand Up @@ -142,7 +142,6 @@ export default (app, appConfig) => {

const templatePages = templates.pages || templates;


// 根据 entry 信息在 express 中添加路由
const entryPoints = getEntries(entries);
Object.keys(entryPoints).forEach((chunkName) => {
Expand Down Expand Up @@ -203,7 +202,7 @@ export default (app, appConfig) => {
}
if (inject && assets.length > 0) {
html = injectStyles(html, engine, chunkName, assets);
html = injectScripts(html, engine, chunkName, assets, commonChunks, scriptInjectPosition); // eslint-disable-line
html = injectScripts(html, engine, chunkName, assets, commonChunks, scriptInjectPosition);
}
html = html
// 替换格式为 __var__ 用户自定义变量
Expand Down
38 changes: 19 additions & 19 deletions src/lib/validate-schema.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import validateSchema from 'webpack/lib/validateSchema';
// import validateSchema from 'webpack/lib/validateSchema';
// import WebpackOptionsValidationError from 'webpack/lib/WebpackOptionsValidationError';
import packingOptionsSchema from '../schemas/packing-options';
import { pRequire } from '..';
// import packingOptionsSchema from '../schemas/packing-options';
// import { pRequire } from '..';

class PackingOptionsValidationError extends Error {
constructor(message) {
super();
this.name = 'PackingOptionsValidationError';
this.message = message;
}
}
// class PackingOptionsValidationError extends Error {
// constructor(message) {
// super();
// this.name = 'PackingOptionsValidationError';
// this.message = message;
// }
// }

/**
* 校验 `config/packing.js` 参数
*/
export default () => {
const options = pRequire('config/packing');
const packingOptionsValidationErrors = validateSchema(packingOptionsSchema, options);
// const options = pRequire('config/packing');
// const packingOptionsValidationErrors = validateSchema(packingOptionsSchema, options);

if (packingOptionsValidationErrors.length) {
// 重写 error.message
// 保证输出消息的可读性
// const error = new WebpackOptionsValidationError(packingOptionsValidationErrors);
// const message = error.message.replace(/Webpack/mg, 'Packing');
throw new PackingOptionsValidationError(message);
}
// if (packingOptionsValidationErrors.length !== 0) {
// // 重写 error.message
// // 保证输出消息的可读性
// // const error = new WebpackOptionsValidationError(packingOptionsValidationErrors);
// // const message = error.message.replace(/Webpack/mg, 'Packing');
// throw new PackingOptionsValidationError(packingOptionsValidationErrors);
// }
};

0 comments on commit fcf1eff

Please sign in to comment.