@oriflame/config-webpack
Version:
Reusable Webpack configs.
120 lines (96 loc) • 2.9 kB
JavaScript
'use strict';
const args$1 = require('@boost/args');
const execa = require('execa');
const path = require('path');
const helpers = require('./helpers2.js');
const _interopDefault = e => e && e.__esModule ? e : {
default: e
};
const execa__default = /*#__PURE__*/_interopDefault(execa);
const path__default = /*#__PURE__*/_interopDefault(path);
/**
* Try to naively lookup webpack config in parent folder. Only looks for 'webpack.config.js'.
*
* @param { string } rootDir - start directory
* @param { number = 0 } level - current level of the of the lookup. Maximum is 6
* @throws { Error } When level exceeds 6
* @returns { string } Absolute path with webpack config
*/
function findWebpackConfig(rootDir, level = 0) {
if (level > 5) {
console.error('Depth of webpack config exceeded 5 exiting');
throw new Error("Webpack config wasn't found");
}
try {
require.resolve(path__default.default.join(rootDir, './webpack.config.js'));
return rootDir;
} catch (error) {// We don't need to do anything with error
}
return findWebpackConfig(path__default.default.join(rootDir, '..'), level + 1);
} // Remove node binary and script
const argv = process.argv.slice(2); // Parse argv into a consumable object
const _args$1$parse = args$1.parse(argv, {
options: {
help: {
description: 'Show the help menu',
short: 'h',
type: 'boolean'
},
root: {
description: 'Relative path to project in monorepo',
short: 'r',
type: 'string'
},
port: {
description: 'Dev server port',
type: 'number',
short: 'p',
default: helpers.PORT
},
entryPoint: {
description: 'Webpack entry file path (relative to the root)',
type: 'string'
},
env: {
description: 'Node env',
type: 'string',
short: 'e'
}
}
}),
options = _args$1$parse.options,
rest = _args$1$parse.rest;
const NODE_ENV = options.env || process.env.NODE_ENV || 'development';
let LUMOS_WEBPACK_ROOT;
const LUMOS_WEBPACK_ENTRY_POINT = options.entryPoint.toString();
const originalCwd = process.cwd();
if (options.root) {
LUMOS_WEBPACK_ROOT = path__default.default.join(originalCwd, options.root);
} else {
const rootPath = findWebpackConfig(originalCwd);
if (rootPath) {
LUMOS_WEBPACK_ROOT = originalCwd;
process.chdir(rootPath);
}
}
let port = process.env.PORT || helpers.PORT;
const args = [...rest];
if (options.port) {
port = options.port;
} // Unix socket
if (Number.isNaN(Number.parseInt(port.toString(), 10))) {
args.push('--socket', port.toString());
} else {
args.push('--port', port.toString());
}
execa__default.default('webpack', ['serve', ...args], {
cwd: process.cwd(),
env: {
NODE_ENV,
LUMOS_WEBPACK_ROOT,
LUMOS_WEBPACK_ENTRY_POINT
},
preferLocal: true,
stdio: 'inherit'
});
//# sourceMappingURL=server2.js.map