UNPKG

piral-cli-webpack5

Version:

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

153 lines • 6.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.piletWebpackConfigEnhancer = void 0; const SystemJSPublicPathWebpackPlugin = require("systemjs-webpack-interop/SystemJSPublicPathWebpackPlugin"); const webpack_1 = require("webpack"); const helpers_1 = require("./helpers"); const StylesPlugin_1 = require("../../plugins/StylesPlugin"); const SheetPlugin_1 = require("../../plugins/SheetPlugin"); const piletCss = 'main.css'; function piletVxWebpackConfigEnhancer(options, compiler) { const { variables, externals } = options; (0, helpers_1.withSetPath)(compiler); (0, helpers_1.setEnvironment)(variables); (0, helpers_1.withExternals)(compiler, externals); compiler.plugins.push(new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(variables))); return compiler; } function piletV0WebpackConfigEnhancer(options, compiler) { const { name, variables, externals, file, entry } = options; const shortName = name.replace(/\W/gi, ''); const jsonpFunction = `pr_${shortName}`; const banner = `//@pilet v:0`; (0, helpers_1.withSetPath)(compiler); (0, helpers_1.setEnvironment)(variables); (0, helpers_1.withExternals)(compiler, externals); compiler.plugins.push(new SheetPlugin_1.default(piletCss, name, entry), new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(variables)), new webpack_1.BannerPlugin({ banner, entryOnly: true, include: file, raw: true, })); compiler.output.uniqueName = `${jsonpFunction}`; compiler.output.library = name; compiler.output.libraryTarget = 'umd'; return compiler; } function piletV1WebpackConfigEnhancer(options, compiler) { const { name, variables, externals, file, entry } = options; const shortName = name.replace(/\W/gi, ''); const jsonpFunction = `pr_${shortName}`; const banner = `//@pilet v:1(${jsonpFunction})`; (0, helpers_1.withSetPath)(compiler); (0, helpers_1.setEnvironment)(variables); (0, helpers_1.withExternals)(compiler, externals); compiler.plugins.push(new SheetPlugin_1.default(piletCss, name, entry), new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(variables)), new webpack_1.BannerPlugin({ banner, entryOnly: true, include: file, raw: true, })); compiler.output.uniqueName = `${jsonpFunction}`; compiler.output.library = name; compiler.output.libraryTarget = 'umd'; compiler.output.auxiliaryComment = { commonjs2: `\nfunction define(d,k){(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window.${jsonpFunction})));}define.amd=!0;`, }; return compiler; } function piletV2WebpackConfigEnhancer(options, compiler) { const { name, variables, externals, file, importmap, entry } = options; const shortName = name.replace(/\W/gi, ''); const jsonpFunction = `pr_${shortName}`; const plugins = []; (0, helpers_1.withExternals)(compiler, externals); (0, helpers_1.setEnvironment)(variables); const dependencies = (0, helpers_1.getDependencies)(importmap, compiler); const banner = `//@pilet v:2(webpackChunk${jsonpFunction},${JSON.stringify(dependencies)})`; plugins.push(new SheetPlugin_1.default(piletCss, name, entry), new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(variables)), new webpack_1.BannerPlugin({ banner, entryOnly: true, include: file, raw: true, }), new SystemJSPublicPathWebpackPlugin()); compiler.plugins = [...compiler.plugins, ...plugins]; compiler.output.uniqueName = `${jsonpFunction}`; compiler.output.library = { type: 'system' }; return compiler; } function piletV3WebpackConfigEnhancer(options, compiler) { const { name, variables, externals, file, importmap, entry } = options; const shortName = name.replace(/\W/gi, ''); const jsonpFunction = `pr_${shortName}`; const plugins = []; (0, helpers_1.withExternals)(compiler, externals); (0, helpers_1.setEnvironment)(variables); const dependencies = (0, helpers_1.getDependencies)(importmap, compiler); const banner = `//@pilet v:3(webpackChunk${jsonpFunction},${JSON.stringify(dependencies)})`; plugins.push(new StylesPlugin_1.default(piletCss, entry), new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(variables)), new webpack_1.BannerPlugin({ banner, entryOnly: true, include: file, raw: true, })); compiler.output.publicPath = ''; compiler.output.chunkFormat = 'module'; compiler.plugins = [...compiler.plugins, ...plugins]; compiler.output.uniqueName = `${jsonpFunction}`; compiler.output.library = { type: 'system' }; compiler.target = 'node'; return compiler; } function piletMfWebpackConfigEnhancer(options, compiler) { const { name, variables, externals, file, importmap, entry } = options; const { ModuleFederationPlugin } = webpack_1.container; (0, helpers_1.setEnvironment)(variables); const plugins = [ new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(variables)), new ModuleFederationPlugin({ filename: file, name: name.replace(/^@/, '').replace('/', '-').replace(/\-/g, '_'), exposes: { './pilet': compiler.entry[entry], }, shared: (0, helpers_1.getShared)(importmap, externals), }), ]; compiler.entry = {}; compiler.output.publicPath = 'auto'; compiler.plugins = [...compiler.plugins, ...plugins]; return compiler; } const piletWebpackConfigEnhancer = (details) => (compiler) => { const { externals = [], schema, importmap } = details; const environment = process.env.NODE_ENV || 'development'; const options = { entry: details.entry, externals, file: details.filename, name: details.name, importmap, variables: { ...(0, helpers_1.getVariables)(details.name, details.version, environment), ...details.variables, }, }; switch (schema) { case 'v0': return piletV0WebpackConfigEnhancer(options, compiler); case 'v1': return piletV1WebpackConfigEnhancer(options, compiler); case 'v2': return piletV2WebpackConfigEnhancer(options, compiler); case 'v3': return piletV3WebpackConfigEnhancer(options, compiler); case 'mf': return piletMfWebpackConfigEnhancer(options, compiler); case 'none': default: return piletVxWebpackConfigEnhancer(options, compiler); } }; exports.piletWebpackConfigEnhancer = piletWebpackConfigEnhancer; //# sourceMappingURL=index.js.map