UNPKG

dn-middleware-webpack5

Version:

--- group: middleware name: webpack5 title: Webpack5 ---

99 lines (78 loc) 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _webpack = _interopRequireDefault(require("webpack")); var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _config = require("./config"); var _formatAndValidateOpts = _interopRequireDefault(require("./dev-utils/formatAndValidateOpts")); var _DevServerUtils = require("./dev-utils/DevServerUtils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // import SpeedMeasurePlugin from "speed-measure-webpack-plugin"; // Migrate from v4 to v5: https://webpack.js.org/migrate/5/ const handler = opts => { return async (next, ctx) => { // register namespace for webpack5 ctx.webpack5 = {}; ctx.webpack = _webpack.default; if (ctx.emit) ctx.emit("webpack.options", opts); const options = (0, _formatAndValidateOpts.default)(opts, ctx); let webpackConfig = await (0, _config.getWebpackConfig)(options, ctx); // merge custom config.js opts.configFile = opts.configFile || "./webpack.config.js"; const customConfigFile = _path.default.resolve(ctx.cwd, opts.configFile); if (_fs.default.existsSync(customConfigFile)) { const customConfigsGenerate = require(customConfigFile); if (typeof customConfigsGenerate === "function") { await customConfigsGenerate(webpackConfig, _webpack.default, void 0); ctx.console.info(`[webpack5] Merged custom webpack config from '${opts.configFile}'.`); } else if (customConfigsGenerate) { webpackConfig = customConfigsGenerate; ctx.console.info(`[webpack5] Use custom webpack config from '${opts.configFile}'.`); } } if (ctx.emit) ctx.emit("webpack.config", webpackConfig, _webpack.default, opts); // console.log("webpackConfig", webpackConfig); // It doesn't work on webpack5 // if (options.analysis) { // const smp = new SpeedMeasurePlugin(); // webpackConfig = smp.wrap(webpackConfig); // } const compiler = (0, _DevServerUtils.createCompiler)({ config: webpackConfig, useTypeScript: ctx.useTypeScript, tscCompileOnError: options.tscCompileOnError, disabledTypeCheck: options.disabledTypeCheck, statsOpts: options.statsOpts }, ctx); // mount compiler to dn ctx ctx.webpackCompiler = compiler; if (ctx.emit) ctx.emit("webpack.compiler", compiler, _webpack.default, webpackConfig); if (options.watch) { compiler.watch(opts.watchOpts, (err, stats) => { // Fatal webpack errors (wrong configuration, etc) if (err) { ctx.console.error(err.stack || err); return; } if (ctx.emit) ctx.emit("webpack.stats", stats); next(); }); } else { // console.log('startrun'); compiler.run(err => { // console.log('done'); if (err) { ctx.console.error("[webpack5] Fatal webpack errors.\n"); ctx.console.error(err.stack || err); // if (err.details) { // ctx.console.error(err.details); // } return; } next(); }); } }; }; var _default = handler; exports.default = _default; module.exports = exports.default;