piral-cli-webpack5 1.0.0-pre.1960 → 1.0.1-beta.5640

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +31 -10
  3. package/extend-config.js +125 -18
  4. package/lib/actions.d.ts +2 -2
  5. package/lib/actions.js +33 -39
  6. package/lib/actions.js.map +1 -1
  7. package/lib/{configs → enhancers/hmr-webpack-config-enhancer}/HotModuleServerPlugin.js +1 -1
  8. package/lib/enhancers/hmr-webpack-config-enhancer/HotModuleServerPlugin.js.map +1 -0
  9. package/lib/enhancers/hmr-webpack-config-enhancer/index.d.ts +5 -0
  10. package/lib/enhancers/hmr-webpack-config-enhancer/index.js +30 -0
  11. package/lib/enhancers/hmr-webpack-config-enhancer/index.js.map +1 -0
  12. package/lib/enhancers/html5-entry-webpack-config-enhancer/helpers.d.ts +2 -2
  13. package/lib/enhancers/html5-entry-webpack-config-enhancer/helpers.js +1 -1
  14. package/lib/enhancers/html5-entry-webpack-config-enhancer/helpers.js.map +1 -1
  15. package/lib/enhancers/html5-entry-webpack-config-enhancer/index.d.ts +2 -1
  16. package/lib/enhancers/html5-entry-webpack-config-enhancer/index.js +17 -6
  17. package/lib/enhancers/html5-entry-webpack-config-enhancer/index.js.map +1 -1
  18. package/lib/enhancers/pilet-webpack-config-enhancer/helpers.d.ts +5 -0
  19. package/lib/enhancers/pilet-webpack-config-enhancer/helpers.js +53 -1
  20. package/lib/enhancers/pilet-webpack-config-enhancer/helpers.js.map +1 -1
  21. package/lib/enhancers/pilet-webpack-config-enhancer/index.d.ts +18 -4
  22. package/lib/enhancers/pilet-webpack-config-enhancer/index.js +117 -49
  23. package/lib/enhancers/pilet-webpack-config-enhancer/index.js.map +1 -1
  24. package/lib/enhancers/piral-instance-webpack-config-enhancer/helpers.js +2 -0
  25. package/lib/enhancers/piral-instance-webpack-config-enhancer/helpers.js.map +1 -1
  26. package/lib/enhancers/piral-instance-webpack-config-enhancer/index.d.ts +2 -3
  27. package/lib/enhancers/piral-instance-webpack-config-enhancer/index.js +6 -7
  28. package/lib/enhancers/piral-instance-webpack-config-enhancer/index.js.map +1 -1
  29. package/lib/helpers.d.ts +2 -1
  30. package/lib/helpers.js +10 -4
  31. package/lib/helpers.js.map +1 -1
  32. package/lib/index.js +1 -1
  33. package/lib/index.js.map +1 -1
  34. package/lib/plugins/SheetLoader.d.ts +1 -0
  35. package/lib/plugins/SheetLoader.js +19 -0
  36. package/lib/plugins/SheetLoader.js.map +1 -0
  37. package/lib/plugins/SheetPlugin.d.ts +7 -0
  38. package/lib/plugins/SheetPlugin.js +31 -0
  39. package/lib/plugins/SheetPlugin.js.map +1 -0
  40. package/lib/plugins/StylesLoader.d.ts +1 -0
  41. package/lib/plugins/StylesLoader.js +14 -0
  42. package/lib/plugins/StylesLoader.js.map +1 -0
  43. package/lib/plugins/StylesPlugin.d.ts +6 -0
  44. package/lib/plugins/StylesPlugin.js +34 -0
  45. package/lib/plugins/StylesPlugin.js.map +1 -0
  46. package/lib/webpack/bundler-run.d.ts +2 -21
  47. package/lib/webpack/bundler-run.js +54 -46
  48. package/lib/webpack/bundler-run.js.map +1 -1
  49. package/lib/webpack/common.d.ts +8 -0
  50. package/lib/webpack/common.js +104 -0
  51. package/lib/webpack/common.js.map +1 -0
  52. package/lib/webpack/pilet.d.ts +19 -0
  53. package/lib/webpack/pilet.js +97 -0
  54. package/lib/webpack/pilet.js.map +1 -0
  55. package/lib/webpack/piral.d.ts +16 -0
  56. package/lib/webpack/piral.js +90 -0
  57. package/lib/webpack/piral.js.map +1 -0
  58. package/package.json +28 -29
  59. package/src/actions.ts +33 -20
  60. package/src/{configs → enhancers/hmr-webpack-config-enhancer}/HotModuleServerPlugin.ts +1 -1
  61. package/src/enhancers/hmr-webpack-config-enhancer/index.ts +34 -0
  62. package/src/enhancers/html5-entry-webpack-config-enhancer/helpers.ts +7 -7
  63. package/src/enhancers/html5-entry-webpack-config-enhancer/index.ts +31 -19
  64. package/src/enhancers/pilet-webpack-config-enhancer/helpers.ts +62 -0
  65. package/src/enhancers/pilet-webpack-config-enhancer/index.ts +187 -59
  66. package/src/enhancers/piral-instance-webpack-config-enhancer/helpers.ts +2 -0
  67. package/src/enhancers/piral-instance-webpack-config-enhancer/index.ts +14 -18
  68. package/src/helpers.ts +15 -5
  69. package/src/index.ts +1 -1
  70. package/src/plugins/SheetLoader.ts +16 -0
  71. package/src/plugins/SheetPlugin.ts +34 -0
  72. package/src/plugins/StylesLoader.ts +11 -0
  73. package/src/plugins/StylesPlugin.ts +35 -0
  74. package/src/webpack/bundler-run.ts +60 -56
  75. package/src/webpack/common.ts +111 -0
  76. package/src/webpack/pilet.ts +121 -0
  77. package/src/webpack/piral.ts +110 -0
  78. package/src/webpack-hot-middleware/LICENSE +20 -0
  79. package/src/webpack-hot-middleware/client-overlay.js +99 -0
  80. package/src/webpack-hot-middleware/client.js +306 -0
  81. package/src/webpack-hot-middleware/helpers.js +9 -0
  82. package/src/webpack-hot-middleware/middleware.js +175 -0
  83. package/src/webpack-hot-middleware/process-update.js +157 -0
  84. package/lib/configs/HotModuleServerPlugin.js.map +0 -1
  85. package/lib/configs/SheetLoader.d.ts +0 -1
  86. package/lib/configs/SheetLoader.js +0 -27
  87. package/lib/configs/SheetLoader.js.map +0 -1
  88. package/lib/configs/common.d.ts +0 -6
  89. package/lib/configs/common.js +0 -115
  90. package/lib/configs/common.js.map +0 -1
  91. package/lib/configs/index.d.ts +0 -2
  92. package/lib/configs/index.js +0 -15
  93. package/lib/configs/index.js.map +0 -1
  94. package/lib/configs/pilet.d.ts +0 -3
  95. package/lib/configs/pilet.js +0 -72
  96. package/lib/configs/pilet.js.map +0 -1
  97. package/lib/configs/piral.d.ts +0 -2
  98. package/lib/configs/piral.js +0 -65
  99. package/lib/configs/piral.js.map +0 -1
  100. package/lib/webpack/bundler-calls.d.ts +0 -3
  101. package/lib/webpack/bundler-calls.js +0 -93
  102. package/lib/webpack/bundler-calls.js.map +0 -1
  103. package/lib/webpack/index.d.ts +0 -1
  104. package/lib/webpack/index.js +0 -14
  105. package/lib/webpack/index.js.map +0 -1
  106. package/lib/webpack/run-build-pilet.d.ts +0 -1
  107. package/lib/webpack/run-build-pilet.js +0 -53
  108. package/lib/webpack/run-build-pilet.js.map +0 -1
  109. package/lib/webpack/run-build-piral.d.ts +0 -1
  110. package/lib/webpack/run-build-piral.js +0 -56
  111. package/lib/webpack/run-build-piral.js.map +0 -1
  112. package/lib/webpack/run-debug-mono-piral.d.ts +0 -1
  113. package/lib/webpack/run-debug-mono-piral.js +0 -61
  114. package/lib/webpack/run-debug-mono-piral.js.map +0 -1
  115. package/lib/webpack/run-debug-pilet.d.ts +0 -1
  116. package/lib/webpack/run-debug-pilet.js +0 -77
  117. package/lib/webpack/run-debug-pilet.js.map +0 -1
  118. package/lib/webpack/run-debug-piral.d.ts +0 -1
  119. package/lib/webpack/run-debug-piral.js +0 -76
  120. package/lib/webpack/run-debug-piral.js.map +0 -1
  121. package/src/configs/SheetLoader.ts +0 -12
  122. package/src/configs/common.ts +0 -118
  123. package/src/configs/index.ts +0 -2
  124. package/src/configs/pilet.ts +0 -82
  125. package/src/configs/piral.ts +0 -73
  126. package/src/webpack/bundler-calls.ts +0 -107
  127. package/src/webpack/index.ts +0 -1
  128. package/src/webpack/run-build-pilet.ts +0 -82
  129. package/src/webpack/run-build-piral.ts +0 -83
  130. package/src/webpack/run-debug-mono-piral.ts +0 -76
  131. package/src/webpack/run-debug-pilet.ts +0 -94
  132. package/src/webpack/run-debug-piral.ts +0 -95
  133. /package/lib/{configs → enhancers/hmr-webpack-config-enhancer}/HotModuleServerPlugin.d.ts +0 -0
@@ -1,4 +1,5 @@
1
1
  import * as HtmlWebpackPlugin from 'html-webpack-plugin';
2
+ import { Configuration } from 'webpack';
2
3
  import { load } from 'cheerio';
3
4
  import { readFileSync } from 'fs';
4
5
  import { join, dirname } from 'path';
@@ -6,27 +7,38 @@ import { getTemplates, extractParts, setEntries } from './helpers';
6
7
 
7
8
  export interface Html5EntryWebpackPluginOptions extends Omit<HtmlWebpackPlugin.Options, 'templateContent'> {}
8
9
 
9
- export const html5EntryWebpackConfigEnhancer = (options: Html5EntryWebpackPluginOptions) => (compilerOptions) => {
10
- const entry = compilerOptions.entry;
11
- const [template] = getTemplates(entry);
10
+ export const html5EntryWebpackConfigEnhancer =
11
+ (options: Html5EntryWebpackPluginOptions) => (compilerOptions: Configuration) => {
12
+ const entry = compilerOptions.entry;
13
+ const [template] = getTemplates(entry);
12
14
 
13
- if (template) {
14
- const src = dirname(template);
15
- const templateContent = load(readFileSync(template, 'utf8'));
16
- const entries = extractParts(templateContent).map((entry) => join(src, entry));
17
- const plugins = [
18
- new HtmlWebpackPlugin({
19
- ...options,
20
- templateContent: templateContent.html(),
21
- }),
22
- ];
15
+ if (template) {
16
+ const src = dirname(template);
17
+ const html = readFileSync(template, 'utf8');
18
+ const templateContent = load(
19
+ // try to replace ejs tags, if any
20
+ html.replace(/<%=([\w\W]*?)%>/g, function (match, group) {
21
+ try {
22
+ return eval(group);
23
+ } catch {
24
+ return match;
25
+ }
26
+ }),
27
+ );
28
+ const entries = extractParts(templateContent).map((entry) => join(src, entry));
29
+ const plugins = [
30
+ new HtmlWebpackPlugin({
31
+ ...options,
32
+ templateContent: templateContent.html(),
33
+ }),
34
+ ];
23
35
 
24
- if (!entries.length) throw new Error('Template entries expected to be not empty');
36
+ if (!entries.length) throw new Error('Template entries expected to be not empty');
25
37
 
26
- setEntries(compilerOptions, template, entries as [string, ...string[]]);
38
+ setEntries(compilerOptions, template, entries as [string, ...Array<string>]);
27
39
 
28
- compilerOptions.plugins = [...compilerOptions.plugins, ...plugins];
29
- }
40
+ compilerOptions.plugins = [...compilerOptions.plugins, ...plugins];
41
+ }
30
42
 
31
- return compilerOptions;
32
- };
43
+ return compilerOptions;
44
+ };
@@ -1,3 +1,7 @@
1
+ import { join } from 'path';
2
+ import { SharedDependency } from 'piral-cli';
3
+ import { Configuration } from 'webpack';
4
+
1
5
  export function getVariables(name: string, version: string, env: string): Record<string, string> {
2
6
  return {
3
7
  NODE_ENV: env,
@@ -18,3 +22,61 @@ export function getDefineVariables(variables: Record<string, string>) {
18
22
  return obj;
19
23
  }, {});
20
24
  }
25
+
26
+ export function getDependencies(importmap: Array<SharedDependency>, compilerOptions: Configuration) {
27
+ const dependencies = {};
28
+ const { entry, externals } = compilerOptions;
29
+
30
+ if (typeof entry === 'object' && entry && Array.isArray(externals) && typeof externals[0] === 'object') {
31
+ for (const dep of importmap) {
32
+ dependencies[dep.id] = dep.ref;
33
+ externals[0][dep.name] = dep.requireId;
34
+
35
+ if (dep.type === 'local') {
36
+ entry[dep.ref.replace(/\.js$/, '')] = dep.entry;
37
+ }
38
+ }
39
+ }
40
+
41
+ return dependencies;
42
+ }
43
+
44
+ export function withSetPath(compilerOptions: Configuration) {
45
+ if (typeof compilerOptions.entry === 'object' && compilerOptions.entry) {
46
+ const setPath = join(__dirname, '..', '..', 'set-path');
47
+
48
+ if (Array.isArray(compilerOptions.entry)) {
49
+ compilerOptions.entry.unshift(setPath);
50
+ } else {
51
+ for (const key of Object.keys(compilerOptions.entry)) {
52
+ const entry = compilerOptions.entry[key];
53
+
54
+ if (Array.isArray(entry)) {
55
+ entry.unshift(setPath);
56
+ }
57
+ }
58
+ }
59
+ }
60
+ }
61
+
62
+ export function withExternals(compilerOptions: Configuration, externals: Array<string>) {
63
+ const current = compilerOptions.externals || [];
64
+ const arrayExternals = Array.isArray(current) ? current : [current];
65
+
66
+ const objectExternal = externals.reduce((external, dep) => {
67
+ external[dep] = dep;
68
+ return external;
69
+ }, {});
70
+
71
+ const newExternals = arrayExternals.filter(external => {
72
+ if (typeof external === 'object' && Object.keys(external).length) {
73
+ for (const dep in external) {
74
+ objectExternal[dep] = external[dep];
75
+ }
76
+ return false;
77
+ }
78
+ return true;
79
+ });
80
+
81
+ compilerOptions.externals = [objectExternal, ...newExternals];
82
+ }
@@ -1,23 +1,44 @@
1
- import { BannerPlugin, DefinePlugin, WebpackPluginInstance } from 'webpack';
2
- import { setEnvironment, getDefineVariables, getVariables } from './helpers';
1
+ import * as SystemJSPublicPathWebpackPlugin from 'systemjs-webpack-interop/SystemJSPublicPathWebpackPlugin';
2
+ import type { SharedDependency } from 'piral-cli';
3
+ import { Configuration, BannerPlugin, DefinePlugin } from 'webpack';
4
+ import {
5
+ setEnvironment,
6
+ getDefineVariables,
7
+ getVariables,
8
+ withSetPath,
9
+ withExternals,
10
+ getDependencies,
11
+ } from './helpers';
12
+ import StylesPlugin from '../../plugins/StylesPlugin';
13
+ import SheetPlugin from '../../plugins/SheetPlugin';
14
+
15
+ const piletCss = 'main.css';
3
16
 
4
17
  export interface PiletWebpackConfigEnhancerOptions {
5
18
  /**
6
19
  * The name of the pilet.
7
20
  */
8
21
  name: string;
22
+ /**
23
+ * The name of the entry module.
24
+ */
25
+ entry: string;
9
26
  /**
10
27
  * The version of the pilet.
11
28
  */
12
29
  version: string;
13
30
  /**
14
- * The name of the Piral instance / app shell.
31
+ * The name of the Piral instances.
15
32
  */
16
- piral: string;
33
+ piralInstances: Array<string>;
34
+ /**
35
+ * The name of the main output file.
36
+ */
37
+ filename: string;
17
38
  /**
18
39
  * The schema version. By default, v1 is used.
19
40
  */
20
- schema?: 'v0' | 'v1' | 'none';
41
+ schema?: 'v0' | 'v1' | 'v2' | 'v3' | 'none';
21
42
  /**
22
43
  * The shared dependencies. By default, these are read from the
23
44
  * Piral instance.
@@ -27,73 +48,180 @@ export interface PiletWebpackConfigEnhancerOptions {
27
48
  * Additional environment variables to define.
28
49
  */
29
50
  variables?: Record<string, string>;
51
+ /**
52
+ * The shared dependencies to consider.
53
+ */
54
+ importmap: Array<SharedDependency>;
30
55
  }
31
56
 
32
- function getExternals(piral: string) {
33
- const shellPkg = require(`${piral}/package.json`);
34
- const piralExternals = shellPkg.pilets?.externals ?? [];
35
- return [
36
- ...piralExternals,
37
- '@dbeining/react-atom',
38
- '@libre/atom',
39
- 'history',
40
- 'react',
41
- 'react-dom',
42
- 'react-router',
43
- 'react-router-dom',
44
- 'tslib',
45
- 'path-to-regexp',
46
- ];
57
+ interface SchemaEnhancerOptions {
58
+ name: string;
59
+ entry: string;
60
+ file: string;
61
+ variables: Record<string, string>;
62
+ externals: Array<string>;
63
+ importmap: Array<SharedDependency>;
47
64
  }
48
65
 
49
- export const piletWebpackConfigEnhancer = (options: PiletWebpackConfigEnhancerOptions) => (compilerOptions) => {
50
- const environment = process.env.NODE_ENV || 'development';
51
- const { name, version, piral, externals = getExternals(piral), schema } = options;
66
+ function piletVxWebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler: Configuration) {
67
+ const { variables, externals } = options;
68
+
69
+ withSetPath(compiler);
70
+ setEnvironment(variables);
71
+ withExternals(compiler, externals);
72
+
73
+ compiler.plugins.push(new DefinePlugin(getDefineVariables(variables)));
74
+
75
+ return compiler;
76
+ }
77
+
78
+ function piletV0WebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler: Configuration) {
79
+ const { name, variables, externals, file, entry } = options;
80
+ const shortName = name.replace(/\W/gi, '');
81
+ const jsonpFunction = `pr_${shortName}`;
82
+ const banner = `//@pilet v:0`;
83
+
84
+ withSetPath(compiler);
85
+ setEnvironment(variables);
86
+ withExternals(compiler, externals);
87
+
88
+ compiler.plugins.push(
89
+ new SheetPlugin(piletCss, name, entry),
90
+ new DefinePlugin(getDefineVariables(variables)),
91
+ new BannerPlugin({
92
+ banner,
93
+ entryOnly: true,
94
+ include: file,
95
+ raw: true,
96
+ }),
97
+ );
98
+ compiler.output.uniqueName = `${jsonpFunction}`;
99
+ compiler.output.library = name;
100
+ compiler.output.libraryTarget = 'umd';
101
+
102
+ return compiler;
103
+ }
104
+
105
+ function piletV1WebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler: Configuration) {
106
+ const { name, variables, externals, file, entry } = options;
52
107
  const shortName = name.replace(/\W/gi, '');
53
108
  const jsonpFunction = `pr_${shortName}`;
54
- const variables = {
55
- ...getVariables(name, version, environment),
56
- ...options.variables,
109
+ const banner = `//@pilet v:1(${jsonpFunction})`;
110
+
111
+ withSetPath(compiler);
112
+ setEnvironment(variables);
113
+ withExternals(compiler, externals);
114
+
115
+ compiler.plugins.push(
116
+ new SheetPlugin(piletCss, name, entry),
117
+ new DefinePlugin(getDefineVariables(variables)),
118
+ new BannerPlugin({
119
+ banner,
120
+ entryOnly: true,
121
+ include: file,
122
+ raw: true,
123
+ }),
124
+ );
125
+ compiler.output.uniqueName = `${jsonpFunction}`;
126
+ compiler.output.library = name;
127
+ compiler.output.libraryTarget = 'umd';
128
+ compiler.output.auxiliaryComment = {
129
+ commonjs2: `\nfunction define(d,k){(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window.${jsonpFunction})));}define.amd=!0;`,
57
130
  };
58
- const plugins: WebpackPluginInstance[] = [new DefinePlugin(getDefineVariables(variables))];
59
-
60
- if (schema !== 'none') {
61
- const bannerSuffix = schema ? `1(${jsonpFunction})` : `0`;
62
-
63
- plugins.push(
64
- new BannerPlugin({
65
- banner: `//@pilet v:${bannerSuffix}`,
66
- entryOnly: true,
67
- include: /\.js$/,
68
- raw: true,
69
- }),
70
- );
71
- }
72
131
 
132
+ return compiler;
133
+ }
134
+
135
+ function piletV2WebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler: Configuration) {
136
+ const { name, variables, externals, file, importmap, entry } = options;
137
+ const shortName = name.replace(/\W/gi, '');
138
+ const jsonpFunction = `pr_${shortName}`;
139
+ const plugins = [];
140
+
141
+ withExternals(compiler, externals);
73
142
  setEnvironment(variables);
74
143
 
75
- compilerOptions.plugins = [...compilerOptions.plugins, ...plugins];
144
+ const dependencies = getDependencies(importmap, compiler);
145
+ const banner = `//@pilet v:2(webpackChunk${jsonpFunction},${JSON.stringify(dependencies)})`;
76
146
 
77
- const current = compilerOptions.externals;
78
- compilerOptions.output.uniqueName = `${jsonpFunction}`;
79
- compilerOptions.output.library = name;
147
+ plugins.push(
148
+ new SheetPlugin(piletCss, name, entry),
149
+ new DefinePlugin(getDefineVariables(variables)),
150
+ new BannerPlugin({
151
+ banner,
152
+ entryOnly: true,
153
+ include: file,
154
+ raw: true,
155
+ }),
156
+ new SystemJSPublicPathWebpackPlugin(),
157
+ );
80
158
 
81
- if (schema !== 'none') {
82
- compilerOptions.output.libraryTarget = 'umd';
83
- }
159
+ compiler.plugins = [...compiler.plugins, ...plugins];
160
+ compiler.output.uniqueName = `${jsonpFunction}`;
161
+ compiler.output.library = { type: 'system' };
84
162
 
85
- if (schema === 'v1') {
86
- const reset = environment !== 'production' ? `delete ${jsonpFunction}_chunks;` : '';
87
- compilerOptions.output.auxiliaryComment = {
88
- commonjs2: `\nfunction define(d,k){${reset}(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window.${jsonpFunction})));}define.amd=!0;`,
89
- } as any;
90
- }
163
+ return compiler;
164
+ }
165
+
166
+ function piletV3WebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler: Configuration) {
167
+ const { name, variables, externals, file, importmap, entry } = options;
168
+ const shortName = name.replace(/\W/gi, '');
169
+ const jsonpFunction = `pr_${shortName}`;
170
+ const plugins = [];
171
+
172
+ withExternals(compiler, externals);
173
+ setEnvironment(variables);
174
+
175
+ const dependencies = getDependencies(importmap, compiler);
176
+ const banner = `//@pilet v:3(webpackChunk${jsonpFunction},${JSON.stringify(dependencies)})`;
177
+
178
+ plugins.push(
179
+ new StylesPlugin(piletCss, entry),
180
+ new DefinePlugin(getDefineVariables(variables)),
181
+ new BannerPlugin({
182
+ banner,
183
+ entryOnly: true,
184
+ include: file,
185
+ raw: true,
186
+ }),
187
+ );
91
188
 
92
- compilerOptions.externals = Array.isArray(current)
93
- ? [...current, ...externals]
94
- : current
95
- ? [current, ...externals]
96
- : externals;
189
+ compiler.output.publicPath = '';
190
+ compiler.output.chunkFormat = 'module';
191
+ compiler.plugins = [...compiler.plugins, ...plugins];
192
+ compiler.output.uniqueName = `${jsonpFunction}`;
193
+ compiler.output.library = { type: 'system' };
194
+ compiler.target = 'node';
97
195
 
98
- return compilerOptions;
196
+ return compiler;
197
+ }
198
+
199
+ export const piletWebpackConfigEnhancer = (details: PiletWebpackConfigEnhancerOptions) => (compiler: Configuration) => {
200
+ const { externals = [], schema, importmap } = details;
201
+ const environment = process.env.NODE_ENV || 'development';
202
+ const options: SchemaEnhancerOptions = {
203
+ entry: details.entry,
204
+ externals,
205
+ file: details.filename,
206
+ name: details.name,
207
+ importmap,
208
+ variables: {
209
+ ...getVariables(details.name, details.version, environment),
210
+ ...details.variables,
211
+ },
212
+ };
213
+
214
+ switch (schema) {
215
+ case 'v0':
216
+ return piletV0WebpackConfigEnhancer(options, compiler);
217
+ case 'v1':
218
+ return piletV1WebpackConfigEnhancer(options, compiler);
219
+ case 'v2':
220
+ return piletV2WebpackConfigEnhancer(options, compiler);
221
+ case 'v3':
222
+ return piletV3WebpackConfigEnhancer(options, compiler);
223
+ case 'none':
224
+ default:
225
+ return piletVxWebpackConfigEnhancer(options, compiler);
226
+ }
99
227
  };
@@ -11,6 +11,8 @@ export function getVariables(
11
11
  BUILD_PCKG_VERSION: version,
12
12
  BUILD_PCKG_NAME: name,
13
13
  SHARED_DEPENDENCIES: externals.join(','),
14
+ DEBUG_PIRAL: '',
15
+ DEBUG_PILET: '',
14
16
  };
15
17
  }
16
18
 
@@ -1,4 +1,4 @@
1
- import { DefinePlugin } from 'webpack';
1
+ import { Configuration, DefinePlugin } from 'webpack';
2
2
  import { setEnvironment, getDefineVariables, getVariables } from './helpers';
3
3
 
4
4
  export interface PiralInstanceWebpackPluginOptions {
@@ -6,26 +6,22 @@ export interface PiralInstanceWebpackPluginOptions {
6
6
  version: string;
7
7
  externals: Array<string>;
8
8
  variables?: Record<string, boolean | string>;
9
- debug?: boolean | string;
10
- emulator?: boolean | string;
11
9
  }
12
10
 
13
- export const piralInstanceWebpackConfigEnhancer = (options: PiralInstanceWebpackPluginOptions) => (compilerOptions) => {
14
- const { name, version, debug, emulator, externals } = options;
15
- const environment = process.env.NODE_ENV || 'development';
16
- const variables = {
17
- ...getVariables(name, version, externals, environment),
18
- ...options.variables,
19
- };
20
-
21
- variables.DEBUG_PIRAL = debug === true ? '1.0' : debug;
22
- variables.DEBUG_PILET = emulator === true ? '/$pilet-api' : emulator;
11
+ export const piralInstanceWebpackConfigEnhancer =
12
+ (options: PiralInstanceWebpackPluginOptions) => (compilerOptions: Configuration) => {
13
+ const { name, version, externals } = options;
14
+ const environment = process.env.NODE_ENV || 'development';
15
+ const variables = {
16
+ ...getVariables(name, version, externals, environment),
17
+ ...options.variables,
18
+ };
23
19
 
24
- const plugins = [new DefinePlugin(getDefineVariables(variables))];
20
+ const plugins = [new DefinePlugin(getDefineVariables(variables))];
25
21
 
26
- setEnvironment(variables);
22
+ setEnvironment(variables);
27
23
 
28
- compilerOptions.plugins = [...(compilerOptions.plugins || []), ...plugins];
24
+ compilerOptions.plugins = [...(compilerOptions.plugins || []), ...plugins];
29
25
 
30
- return compilerOptions;
31
- };
26
+ return compilerOptions;
27
+ };
package/src/helpers.ts CHANGED
@@ -1,29 +1,39 @@
1
1
  import { existsSync } from 'fs';
2
2
  import { Configuration } from 'webpack';
3
+ import { DefaultConfiguration } from './webpack/common';
3
4
 
4
5
  export function extendConfig(
5
- webPackConfig: Configuration,
6
+ [webPackConfig, enhancer]: DefaultConfiguration,
6
7
  otherConfigPath: string,
7
8
  overrides: Configuration = {},
8
9
  ): Configuration {
10
+ const original = webPackConfig;
11
+
9
12
  if (existsSync(otherConfigPath)) {
10
13
  const otherConfig = require(otherConfigPath);
11
14
 
12
15
  if (typeof otherConfig === 'function') {
13
16
  webPackConfig = otherConfig(webPackConfig);
14
17
  } else if (typeof otherConfig === 'object') {
15
- return {
18
+ webPackConfig = {
16
19
  ...webPackConfig,
17
20
  ...otherConfig,
18
- ...overrides,
19
21
  };
20
22
  } else {
21
23
  console.warn(`Did not recognize the export from "${otherConfigPath}". Skipping.`);
22
24
  }
23
25
  }
24
26
 
25
- return {
27
+ ['entry', 'output', 'optimization'].forEach((s) => {
28
+ if (original[s] !== webPackConfig[s]) {
29
+ console.warn(
30
+ `You've overwritten the "${s}" section of the Webpack config. Make sure you know what you are doing.`,
31
+ );
32
+ }
33
+ });
34
+
35
+ return enhancer({
26
36
  ...webPackConfig,
27
37
  ...overrides,
28
- };
38
+ });
29
39
  }
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ import * as actions from './actions';
2
2
  import { CliPlugin } from 'piral-cli';
3
3
 
4
4
  const plugin: CliPlugin = (cli) => {
5
- cli.withBundler('webpack', actions);
5
+ cli.withBundler('webpack5', actions);
6
6
  };
7
7
 
8
8
  module.exports = plugin;
@@ -0,0 +1,16 @@
1
+ import { getOptions } from 'loader-utils';
2
+
3
+ export default function sheetLoader() {
4
+ const { cssName, piletName } = getOptions(this);
5
+ const debug = process.env.NODE_ENV === 'development';
6
+ return [
7
+ `var d=document`,
8
+ `var u=__webpack_public_path__+${JSON.stringify(cssName)}`,
9
+ `var e=d.createElement("link")`,
10
+ `e.setAttribute('data-origin', ${JSON.stringify(piletName)})`,
11
+ `e.type="text/css"`,
12
+ `e.rel="stylesheet"`,
13
+ `e.href=${debug ? 'u+"?_="+Math.random()' : 'u'}`,
14
+ `d.head.appendChild(e)`,
15
+ ].join(';');
16
+ }
@@ -0,0 +1,34 @@
1
+ import { resolve } from 'path';
2
+ import { Compilation } from 'webpack';
3
+ import { RawSource } from 'webpack-sources';
4
+
5
+ export default class SheetPlugin {
6
+ private loaderPath: string;
7
+
8
+ constructor(private cssName: string, piletName: string, private entryName: string) {
9
+ this.loaderPath = resolve(__dirname, `SheetLoader?cssName=${cssName}&piletName=${piletName}!`);
10
+ }
11
+
12
+ apply(compiler) {
13
+ const { entry } = compiler.options;
14
+
15
+ entry[this.entryName].import = [this.loaderPath, ...entry[this.entryName].import];
16
+
17
+ compiler.hooks.compilation.tap('SheetPlugin', (compilation: Compilation) => {
18
+ if (!compilation.compiler.parentCompilation) {
19
+ compilation.hooks.processAssets.tap(
20
+ {
21
+ name: 'SheetPlugin',
22
+ stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
23
+ },
24
+ (assets) => {
25
+ if (!assets[this.cssName]) {
26
+ const source = new RawSource('');
27
+ compilation.emitAsset(this.cssName, source);
28
+ }
29
+ },
30
+ );
31
+ }
32
+ });
33
+ }
34
+ }
@@ -0,0 +1,11 @@
1
+ import { getOptions } from 'loader-utils';
2
+
3
+ export default function stylesLoader() {
4
+ const { cssName, entries } = getOptions(this);
5
+ const debug = process.env.NODE_ENV === 'development';
6
+ return [
7
+ `const u = ${JSON.stringify(cssName)}`,
8
+ `export const styles = [${debug ? 'u+"?_="+Math.random()' : 'u'}]`,
9
+ ...entries.split(',').map((entry) => `export * from ${JSON.stringify(entry)}`),
10
+ ].join(';');
11
+ }
@@ -0,0 +1,35 @@
1
+ import { resolve } from 'path';
2
+ import { Compilation } from 'webpack';
3
+ import { RawSource } from 'webpack-sources';
4
+
5
+ export default class StylesPlugin {
6
+ constructor(private cssName: string, private entryName: string) {}
7
+
8
+ apply(compiler) {
9
+ const { entry } = compiler.options;
10
+
11
+ const entries = entry[this.entryName].import;
12
+ const query = `cssName=${this.cssName}&entries=${entries.join(',')}!`;
13
+ const setPath = resolve(__dirname, '..', 'set-path');
14
+ const loaderPath = resolve(__dirname, `StylesLoader?${query}`);
15
+
16
+ entry[this.entryName].import = [setPath, loaderPath];
17
+
18
+ compiler.hooks.compilation.tap('StylesPlugin', (compilation: Compilation) => {
19
+ if (!compilation.compiler.parentCompilation) {
20
+ compilation.hooks.processAssets.tap(
21
+ {
22
+ name: 'StylesPlugin',
23
+ stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
24
+ },
25
+ (assets) => {
26
+ if (!assets[this.cssName]) {
27
+ const source = new RawSource('');
28
+ compilation.emitAsset(this.cssName, source);
29
+ }
30
+ },
31
+ );
32
+ }
33
+ });
34
+ }
35
+ }