UNPKG

just-scripts

Version:
50 lines 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.webpackDevServerTask = void 0; const utils_1 = require("../utils"); const just_task_1 = require("just-task"); const fs = require("fs"); const path = require("path"); const getTsNodeEnv_1 = require("../typescript/getTsNodeEnv"); const findWebpackConfig_1 = require("../webpack/findWebpackConfig"); const semver = require("semver"); function webpackDevServerTask(options = {}) { const configPath = options && options.config ? (0, just_task_1.resolveCwd)(path.join('.', options.config)) : (0, findWebpackConfig_1.findWebpackConfig)('webpack.serve.config.js', 'webpack.config.js'); // for webpack-cli < 4, use webpack-dev-server directly, for webpack-cli >= 4, use "webpack serve" const webpackCliPath = (0, just_task_1.resolve)('webpack-cli/package.json'); if (!webpackCliPath) { throw new Error('Missing webpack-cli package. Please install webpack-cli as a devDependency.'); } const webpackCliVersion = JSON.parse(fs.readFileSync(webpackCliPath, 'utf-8')).version; const useWebpackServe = semver.gte(webpackCliVersion, '4.0.0'); const devServerCmd = useWebpackServe ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion [(0, just_task_1.resolve)('webpack/bin/webpack.js'), 'serve'] : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion [(0, just_task_1.resolve)('webpack-dev-server/bin/webpack-dev-server.js')]; return function webpackDevServer() { let args = [...(options.nodeArgs || []), ...devServerCmd]; if (configPath && fs.existsSync(configPath)) { args = [...args, '--config', configPath]; options.env = { ...options.env, ...(configPath.endsWith('.ts') && (0, getTsNodeEnv_1.getTsNodeEnv)(options.tsconfig, options.transpileOnly)), }; } if (options.open) { args.push('--open'); } if (options.mode) { args = [...args, '--mode', options.mode]; } if (options.webpackCliArgs) { args = [...args, ...options.webpackCliArgs]; } just_task_1.logger.info(process.execPath, (0, utils_1.encodeArgs)(args).join(' ')); return (0, utils_1.spawn)(process.execPath, args, { stdio: 'inherit', env: options.env }); }; } exports.webpackDevServerTask = webpackDevServerTask; //# sourceMappingURL=webpackDevServerTask.js.map