UNPKG

dn-middleware-webpack5

Version:

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

260 lines (198 loc) 13.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var fs = _interopRequireWildcard(require("fs")); var path = _interopRequireWildcard(require("path")); var assert = _interopRequireWildcard(require("assert")); var _lodash = require("lodash"); var _utils = require("./utils"); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } // 生成排除配置 function makeExternal(commonjs, root, amd) { amd = amd || commonjs; const commonjs2 = commonjs; return { commonjs, commonjs2, root, amd }; } // 库默认排除设定 const LIB_DEFAULT_EXTERNALS = { jquery: makeExternal("jquery", "jQuery"), zepto: makeExternal("zepto", "Zepto"), react: makeExternal("react", "React"), "react-dom": makeExternal("react-dom", "ReactDOM") }; // 普通项目默认排除设定 const PRO_DEFAULT_EXTERNALS = { jquery: "jQuery", zepto: "Zepto", react: "React", "react-dom": "ReactDOM" }; // Validate and format input opts const formatAndValidateOpts = (opts, ctx) => { var _opts$entry, _Object$keys, _opts$template, _Object$keys2, _options$watch, _options$watchOpts, _options$publicPath, _options$devtool, _options$output, _options$output2, _options$folders$js, _options$folders, _options$folders$css, _options$folders2, _options$folders$img, _options$folders3, _options$folders$html, _options$folders4, _options$hot, _options$performance, _options$compress, _options$target, _options$analysis, _options$profiling, _options$cache, _options$cssLoader, _options$tscCompileOn, _options$disabledType, _options$entry, _options$entry$some, _options$config$name, _options$config, _options$config$name2, _options$config2, _options$config3; const options = Object.assign({}, opts); // cwd options.cwd = options.cwd || ctx.cwd; // env const isLegalEnv = e => ["development", "production"].includes(e); if (!isLegalEnv(options.env)) { var _process$env, _process$env2; let envMessage = "[webpack5] `env` should be one of development/production"; if (isLegalEnv((_process$env = process.env) === null || _process$env === void 0 ? void 0 : _process$env.DN_ENV)) { options.env = process.env.DN_ENV; envMessage += `, set to "${options.env}" automatically by DN_ENV`; } else if (isLegalEnv((_process$env2 = process.env) === null || _process$env2 === void 0 ? void 0 : _process$env2.NODE_ENV)) { options.env = process.env.NODE_ENV; envMessage += `, set to "${options.env}" automatically by NODE_ENV`; } else { // ctx.command == current pipe full-name: init/dev/build/publish/.. options.env = ctx.command.includes("dev") || ctx.command.includes("daily") ? "development" : "production"; envMessage += `, set to "${options.env}" automatically by DN_CMD`; } ctx.console.warn(envMessage); } // entry if (!opts.entry || Array.isArray(opts.entry) && !((_opts$entry = opts.entry) !== null && _opts$entry !== void 0 && _opts$entry.length) || typeof opts.entry === "object" && !((_Object$keys = Object.keys(opts.entry)) !== null && _Object$keys !== void 0 && _Object$keys.length)) { opts.entry = (0, _utils.getExistFile)({ cwd: options.cwd, files: ["src/index.tsx", "src/index.ts", "src/index.jsx", "src/index.js"], returnRelative: true }); } assert.ok(opts.entry, "[webpack5] No `entry` found, checkout guide for usage details."); options.entry = (0, _utils.formatReglikeObject)(opts.entry); // template if (opts.template === true || opts.template === undefined) { // auto find templates opts.template = (0, _utils.getExistFile)({ cwd: options.cwd, // `src/assets/index.html` is not recommanded and will be removed soon files: ["public/index.html", "src/assets/index.html"], returnRelative: true }); options.template = (0, _utils.formatReglikeObject)(opts.template); } else if (!opts.template || Array.isArray(opts.template) && !((_opts$template = opts.template) !== null && _opts$template !== void 0 && _opts$template.length) || typeof opts.template === "object" && !((_Object$keys2 = Object.keys(opts.template)) !== null && _Object$keys2 !== void 0 && _Object$keys2.length)) { options.template = []; } else { options.template = (0, _utils.formatReglikeObject)(opts.template); } // watch // default true when in development options.watch = (_options$watch = options.watch) !== null && _options$watch !== void 0 ? _options$watch : false /*options.env === "development"*/ ; // watchOpts options.watchOpts = (_options$watchOpts = options.watchOpts) !== null && _options$watchOpts !== void 0 ? _options$watchOpts : { // aggregateTimeout: 200, poll: true, ignored: /node_modules/ }; // injectCss // default: only inject when dev, not inject when build ctx.injectCSS = options.injectCSS === undefined ? options.env === "development" : !!options.injectCSS; // publicPath ctx.publicPath = (_options$publicPath = options.publicPath) !== null && _options$publicPath !== void 0 ? _options$publicPath : ""; // devtool/sourceMap options.devtool = (_options$devtool = options.devtool) !== null && _options$devtool !== void 0 ? _options$devtool : options.sourceMap; // inject & append options.inject = (0, _utils.formatNullStringToList)(options.inject); options.append = (0, _utils.formatNullStringToList)(options.append); // output const defaultOutputPath = "./build"; if (typeof options.output === "string") { options.output = { path: options.output }; } else if (!options.output || !((_options$output = options.output) !== null && _options$output !== void 0 && _options$output.path)) { options.output = { ...(options === null || options === void 0 ? void 0 : options.output), path: defaultOutputPath }; } if (((_options$output2 = options.output) === null || _options$output2 === void 0 ? void 0 : _options$output2.libraryTarget) === "umd") { options.output.library = options.output.library || (0, _lodash.camelCase)(path.basename(ctx.project.name)); } // folders options.folders = { script: (_options$folders$js = options === null || options === void 0 ? void 0 : (_options$folders = options.folders) === null || _options$folders === void 0 ? void 0 : _options$folders.js) !== null && _options$folders$js !== void 0 ? _options$folders$js : ".", style: (_options$folders$css = options === null || options === void 0 ? void 0 : (_options$folders2 = options.folders) === null || _options$folders2 === void 0 ? void 0 : _options$folders2.css) !== null && _options$folders$css !== void 0 ? _options$folders$css : ".", media: (_options$folders$img = options === null || options === void 0 ? void 0 : (_options$folders3 = options.folders) === null || _options$folders3 === void 0 ? void 0 : _options$folders3.img) !== null && _options$folders$img !== void 0 ? _options$folders$img : "assets", html: (_options$folders$html = options === null || options === void 0 ? void 0 : (_options$folders4 = options.folders) === null || _options$folders4 === void 0 ? void 0 : _options$folders4.html) !== null && _options$folders$html !== void 0 ? _options$folders$html : "", ...options.folders }; // chunkFilename for v3 if (options.chunkFilename) { options.output.chunkFilename = options.chunkFilename; ctx.console.warn("[webpack5] `chunkFilename` is not recommanded in dn-middleware-webpack5, please use `output.chunkFilename` instead"); } options.hot = (_options$hot = options.hot) !== null && _options$hot !== void 0 ? _options$hot : options.env === "development" && options.watch; assert.ok(!(options.hot && options.env === "production"), "[webpack5] react-refresh must be disabled in production mode"); // externals if (typeof options.external !== "boolean") { // auto disable external when dev mode options.external = options.env === "development" ? false : true; } if (options.external === false) { options.externals = {}; } else { var _options$output3; if (options.hot && options.externals) { options.externals = {}; ctx.console.warn("[webpack5] Auto set `externals` to {} by using react-refresh in development mode. You can set `hot` to false to disabled it in development mode"); } options.externals = options.externals || ((_options$output3 = options.output) !== null && _options$output3 !== void 0 && _options$output3.library ? LIB_DEFAULT_EXTERNALS : PRO_DEFAULT_EXTERNALS); } // performance // default is false // true means warning options.performance = options.performance === true ? "warning" : (_options$performance = options.performance) !== null && _options$performance !== void 0 ? _options$performance : false; options.performanceConfig = { hints: options.performance }; // compress // default is true when in production // Make sure to turn off this when you need dn-middleware-compress options.compress = (_options$compress = options.compress) !== null && _options$compress !== void 0 ? _options$compress : options.env === "production"; // target // browser means web // default is web options.target = options.target === "browser" ? "web" : (_options$target = options.target) !== null && _options$target !== void 0 ? _options$target : "web"; // alias if (ctx.useTypeScript && options.alias) { ctx.console.warn("[webpack5] `alias` is not recommanded in ts project, please use paths in tsconfig.json"); } // common chunk options.common = { disabled: true, // break change: default disabled common name: "common", ...options.common }; // analysis options.analysis = (_options$analysis = options.analysis) !== null && _options$analysis !== void 0 ? _options$analysis : false; if (options.analysis === true) { // set default analysisConfig options.analysis = { analyzerMode: "server", openAnalyzer: false }; } // profiling // TODO: change default to true? options.profiling = (_options$profiling = options.profiling) !== null && _options$profiling !== void 0 ? _options$profiling : false; // cache default filesystem options.cache = (_options$cache = options.cache) !== null && _options$cache !== void 0 ? _options$cache : { type: "memory" }; // cssLoader // style.module.(css|less|scss|sass)$ options.cssLoader = (_options$cssLoader = options.cssLoader) !== null && _options$cssLoader !== void 0 ? _options$cssLoader : {}; if (options.cssModules) { options.cssLoader = { modules: opts.cssModules // CamelCase is not supported anymore. It's recommended for local selectors. // camelCase: opts.cssModules, // 只要启用就采用「小驼峰」 }; } // tscCompileOnError // default is true // If `true`, errors in TypeScript type checking will not prevent start script from running app, // and will not cause build script to exit unsuccessfully. // Also downgrades all TypeScript type checking error messages to warning messages. options.tscCompileOnError = (_options$tscCompileOn = options.tscCompileOnError) !== null && _options$tscCompileOn !== void 0 ? _options$tscCompileOn : true; options.disabledTypeCheck = (_options$disabledType = options.disabledTypeCheck) !== null && _options$disabledType !== void 0 ? _options$disabledType : false; // other judges // useTypeScript judge by entry file ext ctx.useTypeScript = (_options$entry = options.entry) === null || _options$entry === void 0 ? void 0 : (_options$entry$some = _options$entry.some) === null || _options$entry$some === void 0 ? void 0 : _options$entry$some.call(_options$entry, ({ file }) => file.endsWith(".ts") || file.endsWith(".tsx")); assert.ok( // if entry is dot ts(x), but not found tsconfig.json, exist !(ctx.useTypeScript && !fs.existsSync(path.join(ctx.cwd, "tsconfig.json"))), "[webpack5] Your entry is typescript but missing tsconfig.json file."); // $config options.config = { name: (_options$config$name = (_options$config = options.config) === null || _options$config === void 0 ? void 0 : _options$config.name) !== null && _options$config$name !== void 0 ? _options$config$name : "$config", path: (_options$config$name2 = (_options$config2 = options.config) === null || _options$config2 === void 0 ? void 0 : _options$config2.name) !== null && _options$config$name2 !== void 0 ? _options$config$name2 : "./src/config", env: ((_options$config3 = options.config) === null || _options$config3 === void 0 ? void 0 : _options$config3.env) || ctx.env || ctx.command }; return options; }; var _default = formatAndValidateOpts; exports.default = _default; module.exports = exports.default;