UNPKG

pilet-webpack-plugin

Version:

Webpack plugin for generating a valid pilet bundle.

136 lines 5.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PiletWebpackPlugin = void 0; const webpack_1 = require("webpack"); const helpers_1 = require("./helpers"); const pluginName = 'PiletWebpackPlugin'; class PiletWebpackPlugin { constructor(options) { this.options = options; } piletVxWebpackConfigEnhancer(compiler) { const config = compiler.options; (0, helpers_1.setEnvironment)(this.variables); (0, helpers_1.withExternals)(config, this.externals); const plugins = [new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables))]; compiler.hooks.afterEnvironment.tap(pluginName, () => { }); return plugins; } piletV0WebpackConfigEnhancer(compiler) { const { name } = this.options; const config = compiler.options; const shortName = name.replace(/\W/gi, ''); const prName = `wp4Chunkpr_${shortName}`; const [mainEntry] = Object.keys(config.entry); const banner = `//@pilet v:0`; (0, helpers_1.setEnvironment)(this.variables); (0, helpers_1.withExternals)(config, this.externals); const plugins = [ new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables)), new webpack_1.BannerPlugin({ banner(data) { const { chunk } = data; return chunk.name === mainEntry ? banner : ''; }, entryOnly: true, raw: true, }), ]; compiler.hooks.afterEnvironment.tap(pluginName, () => { config.output.jsonpFunction = `${prName}`; config.output.library = name; config.output.libraryTarget = 'umd'; }); return plugins; } piletV1WebpackConfigEnhancer(compiler) { const { name } = this.options; const config = compiler.options; const shortName = name.replace(/\W/gi, ''); const prName = `wp4Chunkpr_${shortName}`; const [mainEntry] = Object.keys(config.entry); const banner = `//@pilet v:1(${prName})`; (0, helpers_1.setEnvironment)(this.variables); (0, helpers_1.withExternals)(config, this.externals); const plugins = [ new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables)), new webpack_1.BannerPlugin({ banner(data) { const { chunk } = data; return chunk.name === mainEntry ? banner : ''; }, entryOnly: true, raw: true, }), ]; compiler.hooks.afterEnvironment.tap(pluginName, () => { config.output.jsonpFunction = `${prName}`; config.output.library = name; config.output.libraryTarget = 'umd'; config.output.auxiliaryComment = { commonjs2: `\nfunction define(d,k){if(typeof document!=='undefined'){var _r=${prName};delete ${prName};document.currentScript.app=k.apply(null,d.map(_r))}}define.amd=!0;`, }; }); return plugins; } piletV2WebpackConfigEnhancer(compiler) { const { name, importmap } = this.options; const config = compiler.options; const shortName = name.replace(/\W/gi, ''); const prName = `wp4Chunkpr_${shortName}`; const [mainEntry] = Object.keys(config.entry); (0, helpers_1.withExternals)(config, this.externals); (0, helpers_1.setEnvironment)(this.variables); const dependencies = (0, helpers_1.getDependencies)(importmap, config); const banner = `//@pilet v:2(${prName},${JSON.stringify(dependencies)})`; const plugins = [ new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables)), new webpack_1.BannerPlugin({ banner(data) { const { chunk } = data; return chunk.name === mainEntry ? banner : ''; }, entryOnly: true, raw: true, }), ]; compiler.hooks.afterEnvironment.tap(pluginName, () => { config.module.rules.push({ parser: { system: false } }); config.output.jsonpFunction = `${prName}`; config.output.libraryTarget = 'system'; }); return plugins; } piletV3WebpackConfigEnhancer(compiler) { // right now this is just an alias for V2 return this.piletV2WebpackConfigEnhancer(compiler); } setup(compiler) { const { name, version, externals = [], schema } = this.options; const environment = process.env.NODE_ENV || 'development'; this.variables = { ...(0, helpers_1.getVariables)(name, version, environment), ...this.options.variables, }; this.externals = externals; switch (schema) { case 'v0': return this.piletV0WebpackConfigEnhancer(compiler); case 'v1': return this.piletV1WebpackConfigEnhancer(compiler); case 'v2': return this.piletV2WebpackConfigEnhancer(compiler); case 'v3': return this.piletV3WebpackConfigEnhancer(compiler); case 'none': default: return this.piletVxWebpackConfigEnhancer(compiler); } } apply(compiler) { const plugins = this.setup(compiler); plugins.forEach(plugin => plugin.apply(compiler)); } } exports.PiletWebpackPlugin = PiletWebpackPlugin; //# sourceMappingURL=index.js.map