UNPKG

1.24 kBPlain TextView Raw
1import { Plugin, Compiler, DefinePlugin } from 'webpack';
2import { setEnvironment, getDefineVariables, getVariables } from './helpers';
3
4const pluginName = 'PiralInstanceWebpackPlugin';
5
6export interface PiralInstanceWebpackPluginOptions {
7 name: string;
8 version: string;
9 externals: Array<string>;
10 variables?: Record<string, string>;
11 debug?: boolean | string;
12 emulator?: boolean | string;
13}
14
15export class PiralInstanceWebpackPlugin implements Plugin {
16 constructor(private options: PiralInstanceWebpackPluginOptions) {}
17
18 apply(compiler: Compiler) {
19 const { name, version, debug, emulator, externals } = this.options;
20 const environment = process.env.NODE_ENV || 'development';
21 const variables = {
22 ...getVariables(name, version, externals, environment),
23 ...this.options.variables,
24 };
25
26 if (debug) {
27 variables.DEBUG_PIRAL = debug === true ? '1.0' : debug;
28 }
29
30 if (emulator) {
31 variables.DEBUG_PILET = emulator === true ? '/$pilet-api' : emulator;
32 }
33
34 const plugins = [new DefinePlugin(getDefineVariables(variables))];
35
36 setEnvironment(variables);
37
38 plugins.forEach(plugin => plugin.apply(compiler));
39
40 compiler.hooks.afterEnvironment.tap(pluginName, () => {});
41 }
42}