UNPKG

piral-cli-webpack5

Version:

Provides debug and build capabilities for pilets and Piral instances using Webpack v5.

83 lines 3.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.create = void 0; const TerserPlugin = require("terser-webpack-plugin"); const CssMinimizerPlugin = require("css-minimizer-webpack-plugin"); const utils_1 = require("piral-cli/utils"); const path_1 = require("path"); const bundler_run_1 = require("./bundler-run"); const common_1 = require("./common"); const html5_entry_webpack_config_enhancer_1 = require("../enhancers/html5-entry-webpack-config-enhancer"); const piral_instance_webpack_config_enhancer_1 = require("../enhancers/piral-instance-webpack-config-enhancer"); const hmr_webpack_config_enhancer_1 = require("../enhancers/hmr-webpack-config-enhancer"); const constants_1 = require("../constants"); const helpers_1 = require("../helpers"); async function getConfig(template, dist, externals, develop = false, sourceMaps = true, contentHash = true, minimize = true, publicPath = '/', hmr = 0) { const production = !develop; const name = process.env.BUILD_PCKG_NAME; const version = process.env.BUILD_PCKG_VERSION; const enhance = (options) => [ (0, hmr_webpack_config_enhancer_1.hmrWebpackConfigEnhancer)({ port: hmr }), (0, html5_entry_webpack_config_enhancer_1.html5EntryWebpackConfigEnhancer)({}), (0, piral_instance_webpack_config_enhancer_1.piralInstanceWebpackConfigEnhancer)({ name, version, externals, variables: (0, common_1.getVariables)(), }), ].reduceRight((acc, val) => val(acc), options); const styleLoaders = (0, common_1.getStyleLoaders)(production); return [ { devtool: sourceMaps ? (develop ? 'cheap-module-source-map' : 'source-map') : false, mode: develop ? 'development' : 'production', entry: [template], output: { publicPath, path: dist, filename: `index.${contentHash ? '[contenthash:6].' : ''}js`, chunkFilename: contentHash ? '[chunkhash:6].js' : undefined, }, resolve: { extensions: common_1.extensions, }, module: { rules: (0, common_1.getRules)(styleLoaders), }, optimization: { minimize, minimizer: [ new TerserPlugin({ extractComments: false, terserOptions: { ie8: false, }, }), new CssMinimizerPlugin(), ], }, plugins: (0, common_1.getPlugins)([]), }, enhance, ]; } function getRandomPort() { const min = 60000; const max = 65536; const rng = max - min; return ~~(Math.random() * rng) + min; } const handler = { async create(options) { const { 'hmr-port': defaultHmrPort = getRandomPort(), config = constants_1.defaultWebpackConfig } = options.args._; const hmrPort = options.hmr ? await (0, utils_1.getFreePort)(defaultHmrPort) : 0; const otherConfigPath = (0, path_1.resolve)(options.root, config); const baseConfig = await getConfig(options.entryFiles, options.outDir, options.externals, options.emulator, options.sourceMaps, options.contentHash, options.minify, options.publicUrl, hmrPort); const wpConfig = await (0, helpers_1.extendConfig)(baseConfig, otherConfigPath, { watch: options.watch, }); return (0, bundler_run_1.runWebpack)(wpConfig, options.logLevel); }, }; exports.create = handler.create; //# sourceMappingURL=piral.js.map