@webpack-cli/serve
Advanced tools
Comparing version 1.7.0 to 2.0.0
187
lib/index.js
@@ -8,28 +8,6 @@ "use strict"; | ||
const loadDevServerOptions = () => { | ||
// TODO simplify this after drop webpack v4 and webpack-dev-server v3 | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const devServer = require(WEBPACK_DEV_SERVER_PACKAGE); | ||
const isNewDevServerCLIAPI = typeof devServer.schema !== "undefined"; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let options = {}; | ||
if (isNewDevServerCLIAPI) { | ||
if (cli.webpack.cli && typeof cli.webpack.cli.getArguments === "function") { | ||
options = cli.webpack.cli.getArguments(devServer.schema); | ||
} | ||
else { | ||
options = devServer.cli.getArguments(); | ||
} | ||
} | ||
else { | ||
options = require(`${WEBPACK_DEV_SERVER_PACKAGE}/bin/cli-flags`); | ||
} | ||
// Old options format | ||
// { devServer: [{...}, {}...] } | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
if (options.devServer) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
return options.devServer; | ||
} | ||
const options = cli.webpack.cli.getArguments(devServer.schema); | ||
// New options format | ||
@@ -82,6 +60,5 @@ // { flag1: {}, flag2: {} } | ||
const kebabedOption = cli.toKebabCase(optionName); | ||
// `webpack-dev-server` has own logic for the `--hot` option | ||
const isBuiltInOption = kebabedOption !== "hot" && | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
builtInOptions.find((builtInOption) => builtInOption.name === kebabedOption); | ||
const isBuiltInOption = builtInOptions.find( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(builtInOption) => builtInOption.name === kebabedOption); | ||
if (isBuiltInOption) { | ||
@@ -121,11 +98,3 @@ webpackCLIOptions[optionName] = options[optionName]; | ||
Promise.all(servers.map((server) => { | ||
if (typeof server.stop === "function") { | ||
return server.stop(); | ||
} | ||
// TODO remove in the next major release | ||
return new Promise((resolve) => { | ||
server.close(() => { | ||
resolve(); | ||
}); | ||
}); | ||
return server.stop(); | ||
})).then(() => { | ||
@@ -139,7 +108,5 @@ process.exit(0); | ||
const DevServer = require(WEBPACK_DEV_SERVER_PACKAGE); | ||
const isNewDevServerCLIAPI = typeof DevServer.schema !== "undefined"; | ||
let devServerVersion; | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
devServerVersion = require(`${WEBPACK_DEV_SERVER_PACKAGE}/package.json`).version; | ||
require(`${WEBPACK_DEV_SERVER_PACKAGE}/package.json`).version; | ||
} | ||
@@ -153,93 +120,42 @@ catch (err) { | ||
const compilersForDevServer = possibleCompilers.length > 0 ? possibleCompilers : [compilers[0]]; | ||
const isDevServer4 = devServerVersion.startsWith("4"); | ||
const usedPorts = []; | ||
for (const compilerForDevServer of compilersForDevServer) { | ||
let devServerOptions; | ||
if (isNewDevServerCLIAPI) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const args = devServerFlags.reduce((accumulator, flag) => { | ||
accumulator[flag.name] = flag; | ||
return accumulator; | ||
}, {}); | ||
const values = Object.keys(devServerCLIOptions).reduce( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(accumulator, name) => { | ||
const kebabName = cli.toKebabCase(name); | ||
if (args[kebabName]) { | ||
accumulator[kebabName] = options[name]; | ||
} | ||
return accumulator; | ||
}, {}); | ||
const result = Object.assign({}, (compilerForDevServer.options.devServer || {})); | ||
const problems = (cli.webpack.cli && typeof cli.webpack.cli.processArguments === "function" | ||
? cli.webpack.cli | ||
: DevServer.cli).processArguments(args, result, values); | ||
if (problems) { | ||
const groupBy = (xs, key) => { | ||
return xs.reduce((rv, x) => { | ||
(rv[x[key]] = rv[x[key]] || []).push(x); | ||
return rv; | ||
}, {}); | ||
}; | ||
const problemsByPath = groupBy(problems, "path"); | ||
for (const path in problemsByPath) { | ||
const problems = problemsByPath[path]; | ||
problems.forEach((problem) => { | ||
cli.logger.error(`${cli.capitalizeFirstLetter(problem.type.replace(/-/g, " "))}${problem.value ? ` '${problem.value}'` : ""} for the '--${problem.argument}' option${problem.index ? ` by index '${problem.index}'` : ""}`); | ||
if (problem.expected) { | ||
cli.logger.error(`Expected: '${problem.expected}'`); | ||
} | ||
}); | ||
} | ||
process.exit(2); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const args = devServerFlags.reduce((accumulator, flag) => { | ||
accumulator[flag.name] = flag; | ||
return accumulator; | ||
}, {}); | ||
const values = Object.keys(devServerCLIOptions).reduce( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(accumulator, name) => { | ||
const kebabName = cli.toKebabCase(name); | ||
if (args[kebabName]) { | ||
accumulator[kebabName] = options[name]; | ||
} | ||
devServerOptions = result; | ||
} | ||
else { | ||
// TODO remove in the next major release | ||
const mergeOptions = (devServerOptions, devServerCliOptions) => { | ||
// CLI options should take precedence over devServer options, | ||
// and CLI options should have no default values included | ||
const options = Object.assign(Object.assign({}, devServerOptions), devServerCliOptions); | ||
if (devServerOptions.client && | ||
devServerCliOptions.client && | ||
typeof devServerOptions.client === "object" && | ||
typeof devServerCliOptions.client === "object") { | ||
// the user could set some client options in their devServer config, | ||
// then also specify client options on the CLI | ||
options.client = Object.assign(Object.assign({}, devServerOptions.client), devServerCliOptions.client); | ||
} | ||
return options; | ||
return accumulator; | ||
}, {}); | ||
const result = Object.assign({}, (compilerForDevServer.options.devServer || {})); | ||
const problems = (cli.webpack.cli && typeof cli.webpack.cli.processArguments === "function" | ||
? cli.webpack.cli | ||
: DevServer.cli).processArguments(args, result, values); | ||
if (problems) { | ||
const groupBy = (xs, key) => { | ||
return xs.reduce((rv, x) => { | ||
(rv[x[key]] = rv[x[key]] || []).push(x); | ||
return rv; | ||
}, {}); | ||
}; | ||
devServerOptions = mergeOptions(compilerForDevServer.options.devServer || {}, devServerCLIOptions); | ||
const problemsByPath = groupBy(problems, "path"); | ||
for (const path in problemsByPath) { | ||
const problems = problemsByPath[path]; | ||
problems.forEach((problem) => { | ||
cli.logger.error(`${cli.capitalizeFirstLetter(problem.type.replace(/-/g, " "))}${problem.value ? ` '${problem.value}'` : ""} for the '--${problem.argument}' option${problem.index ? ` by index '${problem.index}'` : ""}`); | ||
if (problem.expected) { | ||
cli.logger.error(`Expected: '${problem.expected}'`); | ||
} | ||
}); | ||
} | ||
process.exit(2); | ||
} | ||
// TODO remove in the next major release | ||
if (!isDevServer4) { | ||
const getPublicPathOption = () => { | ||
const normalizePublicPath = (publicPath) => typeof publicPath === "undefined" || publicPath === "auto" ? "/" : publicPath; | ||
if (options.outputPublicPath) { | ||
return normalizePublicPath(compilerForDevServer.options.output.publicPath); | ||
} | ||
if (devServerOptions.publicPath) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
return normalizePublicPath(devServerOptions.publicPath); | ||
} | ||
return normalizePublicPath(compilerForDevServer.options.output.publicPath); | ||
}; | ||
const getStatsOption = () => { | ||
if (options.stats) { | ||
return options.stats; | ||
} | ||
if (devServerOptions.stats) { | ||
return devServerOptions.stats; | ||
} | ||
return compilerForDevServer.options.stats; | ||
}; | ||
devServerOptions.host = devServerOptions.host || "localhost"; | ||
devServerOptions.port = | ||
typeof devServerOptions.port !== "undefined" ? devServerOptions.port : 8080; | ||
devServerOptions.stats = getStatsOption(); | ||
devServerOptions.publicPath = getPublicPathOption(); | ||
} | ||
const devServerOptions = result; | ||
if (devServerOptions.port) { | ||
@@ -253,21 +169,4 @@ const portNumber = Number(devServerOptions.port); | ||
try { | ||
let server; | ||
// TODO: remove after dropping webpack-dev-server@v3 | ||
if (isDevServer4) { | ||
server = new DevServer(devServerOptions, compiler); | ||
} | ||
else { | ||
server = new DevServer(compiler, devServerOptions); | ||
} | ||
if (typeof server.start === "function") { | ||
await server.start(); | ||
} | ||
else { | ||
// TODO remove in the next major release | ||
server.listen(devServerOptions.port, devServerOptions.host, (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
}); | ||
} | ||
const server = new DevServer(devServerOptions, compiler); | ||
await server.start(); | ||
servers.push(server); | ||
@@ -274,0 +173,0 @@ } |
{ | ||
"name": "@webpack-cli/serve", | ||
"version": "1.7.0", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -17,2 +17,5 @@ "main": "lib/index.js", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=14.15.0" | ||
}, | ||
"files": [ | ||
@@ -22,3 +25,4 @@ "lib" | ||
"peerDependencies": { | ||
"webpack-cli": "4.x.x" | ||
"webpack": "5.x.x", | ||
"webpack-cli": "5.x.x" | ||
}, | ||
@@ -30,3 +34,3 @@ "peerDependenciesMeta": { | ||
}, | ||
"gitHead": "20882d463450d010bb76e0824fe555e9785e9561" | ||
"gitHead": "1d6ada1a84c68a00e56c536d2f004f60939bd946" | ||
} |
@@ -5,3 +5,5 @@ # webpack-cli serve | ||
**This package is used by webpack-cli under-the-hood and is not intended for installation as of v0.2.0** | ||
> **Note** | ||
> | ||
> This package is used by webpack-cli under-the-hood and is not intended for installation | ||
@@ -8,0 +10,0 @@ ## Description |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
33
5
11957
2
186