piral-cli-webpack5 0.13.7 → 0.14.0-alpha.3154

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 (85) hide show
  1. package/README.md +20 -0
  2. package/extend-config.js +51 -14
  3. package/lib/actions.js +6 -40
  4. package/lib/actions.js.map +1 -1
  5. package/lib/enhancers/html5-entry-webpack-config-enhancer/index.js +5 -5
  6. package/lib/enhancers/html5-entry-webpack-config-enhancer/index.js.map +1 -1
  7. package/lib/enhancers/pilet-webpack-config-enhancer/helpers.d.ts +6 -0
  8. package/lib/enhancers/pilet-webpack-config-enhancer/helpers.js +68 -1
  9. package/lib/enhancers/pilet-webpack-config-enhancer/helpers.js.map +1 -1
  10. package/lib/enhancers/pilet-webpack-config-enhancer/index.d.ts +15 -2
  11. package/lib/enhancers/pilet-webpack-config-enhancer/index.js +86 -59
  12. package/lib/enhancers/pilet-webpack-config-enhancer/index.js.map +1 -1
  13. package/lib/enhancers/piral-instance-webpack-config-enhancer/index.js +3 -3
  14. package/lib/enhancers/piral-instance-webpack-config-enhancer/index.js.map +1 -1
  15. package/lib/helpers.d.ts +1 -1
  16. package/lib/helpers.js +1 -1
  17. package/lib/helpers.js.map +1 -1
  18. package/lib/index.js +1 -1
  19. package/lib/index.js.map +1 -1
  20. package/lib/{configs → plugins}/SheetPlugin.d.ts +3 -1
  21. package/lib/plugins/SheetPlugin.js +38 -0
  22. package/lib/plugins/SheetPlugin.js.map +1 -0
  23. package/lib/webpack/bundler-run.d.ts +2 -21
  24. package/lib/webpack/bundler-run.js +43 -45
  25. package/lib/webpack/bundler-run.js.map +1 -1
  26. package/lib/{configs → webpack}/common.d.ts +0 -0
  27. package/lib/{configs → webpack}/common.js +6 -7
  28. package/lib/webpack/common.js.map +1 -0
  29. package/lib/webpack/pilet.d.ts +19 -0
  30. package/lib/{configs → webpack}/pilet.js +30 -9
  31. package/lib/webpack/pilet.js.map +1 -0
  32. package/lib/webpack/piral.d.ts +16 -0
  33. package/lib/{configs → webpack}/piral.js +27 -9
  34. package/lib/webpack/piral.js.map +1 -0
  35. package/package.json +9 -9
  36. package/src/actions.ts +6 -21
  37. package/src/enhancers/pilet-webpack-config-enhancer/helpers.ts +75 -0
  38. package/src/enhancers/pilet-webpack-config-enhancer/index.ts +140 -68
  39. package/src/helpers.ts +1 -1
  40. package/src/index.ts +1 -1
  41. package/src/plugins/SheetPlugin.ts +37 -0
  42. package/src/webpack/bundler-run.ts +49 -57
  43. package/src/{configs → webpack}/common.ts +3 -4
  44. package/src/{configs → webpack}/pilet.ts +42 -4
  45. package/src/{configs → webpack}/piral.ts +32 -1
  46. package/lib/configs/SheetPlugin.js +0 -23
  47. package/lib/configs/SheetPlugin.js.map +0 -1
  48. package/lib/configs/common.js.map +0 -1
  49. package/lib/configs/index.d.ts +0 -2
  50. package/lib/configs/index.js +0 -15
  51. package/lib/configs/index.js.map +0 -1
  52. package/lib/configs/pilet.d.ts +0 -3
  53. package/lib/configs/pilet.js.map +0 -1
  54. package/lib/configs/piral.d.ts +0 -2
  55. package/lib/configs/piral.js.map +0 -1
  56. package/lib/webpack/bundler-calls.d.ts +0 -3
  57. package/lib/webpack/bundler-calls.js +0 -93
  58. package/lib/webpack/bundler-calls.js.map +0 -1
  59. package/lib/webpack/index.d.ts +0 -1
  60. package/lib/webpack/index.js +0 -14
  61. package/lib/webpack/index.js.map +0 -1
  62. package/lib/webpack/run-build-pilet.d.ts +0 -1
  63. package/lib/webpack/run-build-pilet.js +0 -53
  64. package/lib/webpack/run-build-pilet.js.map +0 -1
  65. package/lib/webpack/run-build-piral.d.ts +0 -1
  66. package/lib/webpack/run-build-piral.js +0 -56
  67. package/lib/webpack/run-build-piral.js.map +0 -1
  68. package/lib/webpack/run-debug-mono-piral.d.ts +0 -1
  69. package/lib/webpack/run-debug-mono-piral.js +0 -61
  70. package/lib/webpack/run-debug-mono-piral.js.map +0 -1
  71. package/lib/webpack/run-debug-pilet.d.ts +0 -1
  72. package/lib/webpack/run-debug-pilet.js +0 -77
  73. package/lib/webpack/run-debug-pilet.js.map +0 -1
  74. package/lib/webpack/run-debug-piral.d.ts +0 -1
  75. package/lib/webpack/run-debug-piral.js +0 -76
  76. package/lib/webpack/run-debug-piral.js.map +0 -1
  77. package/src/configs/SheetPlugin.ts +0 -21
  78. package/src/configs/index.ts +0 -2
  79. package/src/webpack/bundler-calls.ts +0 -107
  80. package/src/webpack/index.ts +0 -1
  81. package/src/webpack/run-build-pilet.ts +0 -81
  82. package/src/webpack/run-build-piral.ts +0 -82
  83. package/src/webpack/run-debug-mono-piral.ts +0 -65
  84. package/src/webpack/run-debug-pilet.ts +0 -93
  85. package/src/webpack/run-debug-piral.ts +0 -84
@@ -1,12 +1,25 @@
1
- import { join } from 'path';
2
- import { Configuration, BannerPlugin, DefinePlugin, WebpackPluginInstance } from 'webpack';
3
- 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
+ getExternals,
12
+ } from './helpers';
4
13
 
5
14
  export interface PiletWebpackConfigEnhancerOptions {
6
15
  /**
7
16
  * The name of the pilet.
8
17
  */
9
18
  name: string;
19
+ /**
20
+ * The name of the entry module.
21
+ */
22
+ entry: string;
10
23
  /**
11
24
  * The version of the pilet.
12
25
  */
@@ -15,10 +28,14 @@ export interface PiletWebpackConfigEnhancerOptions {
15
28
  * The name of the Piral instance / app shell.
16
29
  */
17
30
  piral: string;
31
+ /**
32
+ * The name of the main output file.
33
+ */
34
+ filename: string;
18
35
  /**
19
36
  * The schema version. By default, v1 is used.
20
37
  */
21
- schema?: 'v0' | 'v1' | 'none';
38
+ schema?: 'v0' | 'v1' | 'v2' | 'none';
22
39
  /**
23
40
  * The shared dependencies. By default, these are read from the
24
41
  * Piral instance.
@@ -28,85 +45,140 @@ export interface PiletWebpackConfigEnhancerOptions {
28
45
  * Additional environment variables to define.
29
46
  */
30
47
  variables?: Record<string, string>;
48
+ /**
49
+ * The shared dependencies to consider.
50
+ */
51
+ importmap: Array<SharedDependency>;
31
52
  }
32
53
 
33
- function getExternals(piral: string) {
34
- const shellPkg = require(`${piral}/package.json`);
35
- const piralExternals = shellPkg.pilets?.externals ?? [];
36
- return [
37
- ...piralExternals,
38
- '@dbeining/react-atom',
39
- '@libre/atom',
40
- 'history',
41
- 'react',
42
- 'react-dom',
43
- 'react-router',
44
- 'react-router-dom',
45
- 'tslib',
46
- 'path-to-regexp',
47
- ];
54
+ interface SchemaEnhancerOptions {
55
+ name: string;
56
+ entry: string;
57
+ file: string;
58
+ variables: Record<string, string>;
59
+ externals: Array<string>;
60
+ importmap: Array<SharedDependency>;
48
61
  }
49
62
 
50
- export const piletWebpackConfigEnhancer = (options: PiletWebpackConfigEnhancerOptions) => (
51
- compilerOptions: Configuration,
52
- ) => {
53
- const environment = process.env.NODE_ENV || 'development';
54
- const { name, version, piral, externals = getExternals(piral), schema } = options;
63
+ function piletVxWebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler: Configuration) {
64
+ const { variables, externals } = options;
65
+
66
+ withSetPath(compiler);
67
+ setEnvironment(variables);
68
+ withExternals(compiler, externals);
69
+
70
+ compiler.plugins.push(new DefinePlugin(getDefineVariables(variables)));
71
+
72
+ return compiler;
73
+ }
74
+
75
+ function piletV0WebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler: Configuration) {
76
+ const { name, variables, externals, file } = options;
55
77
  const shortName = name.replace(/\W/gi, '');
56
78
  const jsonpFunction = `pr_${shortName}`;
57
- const variables = {
58
- ...getVariables(name, version, environment),
59
- ...options.variables,
60
- };
61
- const plugins: WebpackPluginInstance[] = [new DefinePlugin(getDefineVariables(variables))];
62
79
 
63
- if (typeof compilerOptions.entry === 'object' && compilerOptions.entry) {
64
- const setPath = join(__dirname, '..', '..', 'set-path');
80
+ withSetPath(compiler);
81
+ setEnvironment(variables);
82
+ withExternals(compiler, externals);
65
83
 
66
- if (Array.isArray(compilerOptions.entry)) {
67
- compilerOptions.entry.unshift(setPath);
68
- } else if (Array.isArray(compilerOptions.entry.main)) {
69
- compilerOptions.entry.main.unshift(setPath);
70
- }
71
- }
84
+ compiler.plugins.push(
85
+ new DefinePlugin(getDefineVariables(variables)),
86
+ new BannerPlugin({
87
+ banner: `//@pilet v:0`,
88
+ entryOnly: true,
89
+ include: file,
90
+ raw: true,
91
+ }),
92
+ );
93
+ compiler.output.uniqueName = `${jsonpFunction}`;
94
+ compiler.output.library = name;
95
+ compiler.output.libraryTarget = 'umd';
72
96
 
73
- if (schema !== 'none') {
74
- const bannerSuffix = schema ? `1(${jsonpFunction})` : `0`;
75
-
76
- plugins.push(
77
- new BannerPlugin({
78
- banner: `//@pilet v:${bannerSuffix}`,
79
- entryOnly: true,
80
- include: /\.js$/,
81
- raw: true,
82
- }),
83
- );
84
- }
97
+ return compiler;
98
+ }
85
99
 
100
+ function piletV1WebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler: Configuration) {
101
+ const { name, variables, externals, file } = options;
102
+ const shortName = name.replace(/\W/gi, '');
103
+ const jsonpFunction = `pr_${shortName}`;
104
+
105
+ withSetPath(compiler);
86
106
  setEnvironment(variables);
107
+ withExternals(compiler, externals);
87
108
 
88
- compilerOptions.plugins = [...compilerOptions.plugins, ...plugins];
109
+ compiler.plugins.push(
110
+ new DefinePlugin(getDefineVariables(variables)),
111
+ new BannerPlugin({
112
+ banner: `//@pilet v:1(${jsonpFunction})`,
113
+ entryOnly: true,
114
+ include: file,
115
+ raw: true,
116
+ }),
117
+ );
118
+ compiler.output.uniqueName = `${jsonpFunction}`;
119
+ compiler.output.library = name;
120
+ compiler.output.libraryTarget = 'umd';
121
+ compiler.output.auxiliaryComment = {
122
+ commonjs2: `\nfunction define(d,k){(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window.${jsonpFunction})));}define.amd=!0;`,
123
+ };
89
124
 
90
- const current = compilerOptions.externals;
91
- compilerOptions.output.uniqueName = `${jsonpFunction}`;
92
- compilerOptions.output.library = name;
125
+ return compiler;
126
+ }
93
127
 
94
- if (schema !== 'none') {
95
- compilerOptions.output.libraryTarget = 'umd';
96
- }
128
+ function piletV2WebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler: Configuration) {
129
+ const { name, variables, externals, file, importmap } = options;
130
+ const shortName = name.replace(/\W/gi, '');
131
+ const jsonpFunction = `pr_${shortName}`;
97
132
 
98
- if (schema === 'v1') {
99
- const reset = `delete window.webpackChunk${jsonpFunction};`;
100
- compilerOptions.output.auxiliaryComment = {
101
- commonjs2: `\nfunction define(d,k){${reset}(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window.${jsonpFunction})));}define.amd=!0;`,
102
- } as any;
103
- }
133
+ withExternals(compiler, externals);
134
+ setEnvironment(variables);
135
+
136
+ const dependencies = getDependencies(importmap, compiler);
137
+ const plugins = [];
138
+
139
+ plugins.push(
140
+ new DefinePlugin(getDefineVariables(variables)),
141
+ new BannerPlugin({
142
+ banner: `//@pilet v:2(webpackChunk${jsonpFunction},${JSON.stringify(dependencies)})`,
143
+ entryOnly: true,
144
+ include: file,
145
+ raw: true,
146
+ }),
147
+ new SystemJSPublicPathWebpackPlugin(),
148
+ );
104
149
 
105
- compilerOptions.externals = Array.isArray(current)
106
- ? [...current, ...externals]
107
- : current
108
- ? [current, ...externals]
109
- : externals;
150
+ compiler.plugins = [...compiler.plugins, ...plugins];
151
+ compiler.module.rules.push({ parser: { system: false } });
152
+ compiler.output.uniqueName = `${jsonpFunction}`;
153
+ compiler.output.library = { type: 'system' };
154
+
155
+ return compiler;
156
+ }
110
157
 
111
- return compilerOptions;
158
+ export const piletWebpackConfigEnhancer = (details: PiletWebpackConfigEnhancerOptions) => (compiler: Configuration) => {
159
+ const { piral, externals = getExternals(piral), schema, importmap } = details;
160
+ const environment = process.env.NODE_ENV || 'development';
161
+ const options: SchemaEnhancerOptions = {
162
+ entry: details.entry,
163
+ externals,
164
+ file: details.filename,
165
+ name: details.name,
166
+ importmap,
167
+ variables: {
168
+ ...getVariables(details.name, details.version, environment),
169
+ ...details.variables,
170
+ },
171
+ };
172
+
173
+ switch (schema) {
174
+ case 'v0':
175
+ return piletV0WebpackConfigEnhancer(options, compiler);
176
+ case 'v1':
177
+ return piletV1WebpackConfigEnhancer(options, compiler);
178
+ case 'v2':
179
+ return piletV2WebpackConfigEnhancer(options, compiler);
180
+ case 'none':
181
+ default:
182
+ return piletVxWebpackConfigEnhancer(options, compiler);
183
+ }
112
184
  };
package/src/helpers.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { existsSync } from 'fs';
2
2
  import { Configuration } from 'webpack';
3
- import { DefaultConfiguration } from './configs/common';
3
+ import { DefaultConfiguration } from './webpack/common';
4
4
 
5
5
  export function extendConfig(
6
6
  [webPackConfig, enhancer]: DefaultConfiguration,
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,37 @@
1
+ import InjectPlugin from 'webpack-inject-plugin';
2
+ import { RawSource } from 'webpack-sources';
3
+
4
+ function sheetLoader(cssName: string, name: string) {
5
+ return () => {
6
+ const debug = process.env.NODE_ENV === 'development';
7
+ return [
8
+ `var d=document`,
9
+ `var u=__webpack_public_path__+${JSON.stringify(cssName)}`,
10
+ `var e=d.createElement("link")`,
11
+ `e.setAttribute('data-origin', ${JSON.stringify(name)})`,
12
+ `e.type="text/css"`,
13
+ `e.rel="stylesheet"`,
14
+ `e.href=${debug ? 'u+"?_="+Math.random()' : 'u'}`,
15
+ `d.head.appendChild(e)`,
16
+ ].join(';');
17
+ };
18
+ }
19
+
20
+ export default class SheetPlugin extends InjectPlugin {
21
+ constructor(private cssName: string, name: string) {
22
+ super(sheetLoader(cssName, name));
23
+ }
24
+
25
+ apply(compiler) {
26
+ super.apply(compiler);
27
+
28
+ compiler.hooks.compilation.tap('SheetPlugin', (compilation) => {
29
+ compilation.hooks.afterProcessAssets.tap('SheetPlugin', (module) => {
30
+ if (!compilation.assets[this.cssName] && Object.keys(module).length > 0) {
31
+ const source = new RawSource('');
32
+ compilation.emitAsset(this.cssName, source);
33
+ }
34
+ });
35
+ });
36
+ }
37
+ }
@@ -1,17 +1,15 @@
1
1
  import * as webpack from 'webpack';
2
2
  import { resolve, basename, dirname } from 'path';
3
3
  import { EventEmitter } from 'events';
4
- import type { LogLevels } from 'piral-cli';
5
-
6
- interface BuildResult {
7
- outFile: string;
8
- outDir: string;
9
- }
4
+ import type { LogLevels, BundleHandlerResponse } from 'piral-cli';
10
5
 
11
6
  function getOutput(stats: webpack.Stats) {
12
7
  const { outputPath, entrypoints } = stats.toJson();
13
- const assets = entrypoints.main.assets;
14
- return resolve(outputPath, assets[0].name);
8
+
9
+ for (const name of Object.keys(entrypoints)) {
10
+ const assets = entrypoints[name].assets;
11
+ return resolve(outputPath, assets[0].name);
12
+ }
15
13
  }
16
14
 
17
15
  function getPreset(logLevel: LogLevels) {
@@ -31,77 +29,71 @@ function getPreset(logLevel: LogLevels) {
31
29
  }
32
30
  }
33
31
 
34
- export function runWebpack(wpConfig: webpack.Configuration, logLevel: LogLevels) {
32
+ export function runWebpack(wpConfig: webpack.Configuration, logLevel: LogLevels): BundleHandlerResponse {
35
33
  const eventEmitter = new EventEmitter();
36
34
  const outDir = wpConfig.output.path;
37
- const mainBundle = {
35
+ const bundle = {
36
+ outFile: '',
37
+ outDir,
38
38
  name: '',
39
+ hash: '',
39
40
  requireRef: undefined,
40
- entryAsset: {
41
- hash: '',
42
- },
43
41
  };
44
42
 
45
43
  wpConfig.plugins.push({
46
44
  apply(compiler: webpack.Compiler) {
47
45
  compiler.hooks.beforeCompile.tap('piral-cli', () => {
48
- eventEmitter.emit('buildStart');
46
+ eventEmitter.emit('start');
49
47
  });
50
48
 
51
49
  compiler.hooks.done.tap('piral-cli', (stats) => {
52
- mainBundle.name = getOutput(stats);
53
- mainBundle.requireRef = stats.compilation.outputOptions?.uniqueName;
54
- mainBundle.entryAsset.hash = stats.hash;
55
- eventEmitter.emit('bundled');
50
+ bundle.name = getOutput(stats);
51
+ bundle.requireRef = stats.compilation.outputOptions?.uniqueName;
52
+ bundle.hash = stats.hash;
53
+ eventEmitter.emit('end', bundle);
56
54
  });
57
55
  },
58
56
  });
59
57
 
60
- const bundle = () =>
61
- new Promise<BuildResult>((resolve, reject) => {
62
- const preset = {
63
- current: undefined,
64
- };
65
-
66
- const process = webpack(wpConfig, (err, stats) => {
67
- if (err) {
68
- console.error(err);
69
- reject(err);
70
- } else {
71
- console.log(
72
- stats.toString({
73
- ...preset.current,
74
- colors: true,
75
- }),
76
- );
58
+ return {
59
+ bundle() {
60
+ return new Promise((resolve, reject) => {
61
+ const preset = {
62
+ current: undefined,
63
+ };
77
64
 
78
- if (stats.hasErrors()) {
79
- reject(stats.toJson(preset.current));
65
+ const process = webpack(wpConfig, (err, stats) => {
66
+ if (err) {
67
+ console.error(err);
68
+ reject(err);
80
69
  } else {
81
- const file = getOutput(stats);
82
- resolve({
83
- outFile: `/${basename(file)}`,
84
- outDir: dirname(file),
85
- });
86
- }
87
- }
88
- });
70
+ console.log(
71
+ stats.toString({
72
+ ...preset.current,
73
+ colors: true,
74
+ }),
75
+ );
89
76
 
90
- const compilation = process.createCompilation();
91
- preset.current = compilation.createStatsOptions(getPreset(logLevel));
92
- });
77
+ if (stats.hasErrors()) {
78
+ reject(stats.toJson(preset.current));
79
+ } else {
80
+ const file = getOutput(stats);
81
+ bundle.outFile = `/${basename(file)}`;
82
+ bundle.outDir = dirname(file);
83
+ resolve(bundle);
84
+ }
85
+ }
86
+ });
93
87
 
94
- return {
95
- bundle,
96
- on(ev: string, listener: () => void) {
97
- eventEmitter.on(ev, listener);
88
+ const compilation = process.createCompilation();
89
+ preset.current = compilation.createStatsOptions(getPreset(logLevel));
90
+ });
98
91
  },
99
- off(ev: string, listener: () => void) {
100
- eventEmitter.off(ev, listener);
92
+ onStart(cb) {
93
+ eventEmitter.on('start', cb);
101
94
  },
102
- options: {
103
- outDir,
95
+ onEnd(cb) {
96
+ eventEmitter.on('end', cb);
104
97
  },
105
- mainBundle,
106
98
  };
107
99
  }
@@ -1,8 +1,7 @@
1
1
  import * as MiniCssExtractPlugin from 'mini-css-extract-plugin';
2
2
  import { progress, logReset, log } from 'piral-cli/utils';
3
3
  import { RuleSetRule, ProgressPlugin, WebpackPluginInstance, Configuration } from 'webpack';
4
- import { ImportMapsWebpackPlugin } from 'import-maps-webpack-plugin';
5
- import SheetPlugin from './SheetPlugin';
4
+ import SheetPlugin from '../plugins/SheetPlugin';
6
5
 
7
6
  const piletCss = 'main.css';
8
7
 
@@ -33,7 +32,6 @@ export function getPlugins(plugins: Array<any>, showProgress: boolean, productio
33
32
  filename: pilet ? piletCss : '[name].[fullhash:6].css',
34
33
  chunkFilename: '[id].[chunkhash:6].css',
35
34
  }) as any,
36
- new ImportMapsWebpackPlugin(),
37
35
  ];
38
36
 
39
37
  if (showProgress) {
@@ -52,7 +50,8 @@ export function getPlugins(plugins: Array<any>, showProgress: boolean, productio
52
50
  }
53
51
 
54
52
  if (production && pilet) {
55
- otherPlugins.push(new SheetPlugin(piletCss) as any);
53
+ const name = process.env.BUILD_PCKG_NAME;
54
+ otherPlugins.push(new SheetPlugin(piletCss, name) as any);
56
55
  }
57
56
 
58
57
  return plugins.concat(otherPlugins);
@@ -1,14 +1,20 @@
1
+ import type { PiletBuildHandler } from 'piral-cli';
1
2
  import * as TerserPlugin from 'terser-webpack-plugin';
2
3
  import * as OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin';
3
- import { PiletSchemaVersion } from 'piral-cli';
4
+ import type { PiletSchemaVersion, SharedDependency } from 'piral-cli';
4
5
  import { getRules, getPlugins, extensions, getVariables, DefaultConfiguration } from './common';
5
6
  import { piletWebpackConfigEnhancer } from '../enhancers/pilet-webpack-config-enhancer';
7
+ import { resolve } from 'path';
8
+ import { runWebpack } from './bundler-run';
9
+ import { defaultWebpackConfig } from '../constants';
10
+ import { extendConfig } from '../helpers';
6
11
 
7
- export async function getPiletConfig(
12
+ async function getConfig(
8
13
  template: string,
9
14
  dist: string,
10
15
  filename: string,
11
16
  externals: Array<string>,
17
+ importmap: Array<SharedDependency> = [],
12
18
  piral: string,
13
19
  schema: PiletSchemaVersion,
14
20
  develop = false,
@@ -21,13 +27,17 @@ export async function getPiletConfig(
21
27
  const production = !develop;
22
28
  const name = process.env.BUILD_PCKG_NAME;
23
29
  const version = process.env.BUILD_PCKG_VERSION;
30
+ const entry = filename.replace(/\.js$/i, '');
24
31
 
25
32
  const enhance = piletWebpackConfigEnhancer({
26
33
  name,
27
34
  piral,
28
35
  version,
36
+ entry,
29
37
  externals,
38
+ importmap,
30
39
  schema,
40
+ filename,
31
41
  variables: getVariables(),
32
42
  });
33
43
 
@@ -37,14 +47,16 @@ export async function getPiletConfig(
37
47
 
38
48
  mode: develop ? 'development' : 'production',
39
49
 
50
+ target: 'web',
51
+
40
52
  entry: {
41
- main: [template],
53
+ [entry]: [template],
42
54
  },
43
55
 
44
56
  output: {
45
57
  publicPath,
46
58
  path: dist,
47
- filename,
59
+ filename: '[name].js',
48
60
  chunkFilename: contentHash ? '[chunkhash:8].js' : undefined,
49
61
  },
50
62
 
@@ -80,3 +92,29 @@ export async function getPiletConfig(
80
92
  enhance,
81
93
  ];
82
94
  }
95
+
96
+ const handler: PiletBuildHandler = {
97
+ async create(options) {
98
+ const otherConfigPath = resolve(options.root, defaultWebpackConfig);
99
+ const baseConfig = await getConfig(
100
+ options.entryModule,
101
+ options.outDir,
102
+ options.outFile,
103
+ options.externals,
104
+ options.importmap,
105
+ options.piral,
106
+ options.version,
107
+ options.develop,
108
+ options.sourceMaps,
109
+ options.contentHash,
110
+ options.minify,
111
+ );
112
+ const wpConfig = extendConfig(baseConfig, otherConfigPath, {
113
+ watch: options.watch,
114
+ });
115
+
116
+ return runWebpack(wpConfig, options.logLevel);
117
+ },
118
+ };
119
+
120
+ export const create = handler.create;
@@ -1,12 +1,18 @@
1
+ import type { PiralBuildHandler } from 'piral-cli';
1
2
  import * as webpack from 'webpack';
2
3
  import * as TerserPlugin from 'terser-webpack-plugin';
3
4
  import * as OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin';
5
+ import { getFreePort } from 'piral-cli/utils';
6
+ import { resolve } from 'path';
7
+ import { runWebpack } from './bundler-run';
4
8
  import { getRules, getPlugins, extensions, getVariables, DefaultConfiguration } from './common';
5
9
  import { html5EntryWebpackConfigEnhancer } from '../enhancers/html5-entry-webpack-config-enhancer';
6
10
  import { piralInstanceWebpackConfigEnhancer } from '../enhancers/piral-instance-webpack-config-enhancer';
7
11
  import { hmrWebpackConfigEnhancer } from '../enhancers/hmr-webpack-config-enhancer';
12
+ import { defaultWebpackConfig } from '../constants';
13
+ import { extendConfig } from '../helpers';
8
14
 
9
- export async function getPiralConfig(
15
+ async function getConfig(
10
16
  template: string,
11
17
  dist: string,
12
18
  externals: Array<string>,
@@ -75,3 +81,28 @@ export async function getPiralConfig(
75
81
  enhance,
76
82
  ];
77
83
  }
84
+
85
+ const handler: PiralBuildHandler = {
86
+ async create(options) {
87
+ const otherConfigPath = resolve(options.root, defaultWebpackConfig);
88
+ const hmrPort = options.hmr ? await getFreePort(62123) : 0;
89
+ const baseConfig = await getConfig(
90
+ options.entryFiles,
91
+ options.outDir,
92
+ options.externals,
93
+ options.emulator,
94
+ options.sourceMaps,
95
+ options.contentHash,
96
+ options.minify,
97
+ options.publicUrl,
98
+ hmrPort,
99
+ );
100
+ const wpConfig = extendConfig(baseConfig, otherConfigPath, {
101
+ watch: options.watch,
102
+ });
103
+
104
+ return runWebpack(wpConfig, options.logLevel);
105
+ },
106
+ };
107
+
108
+ export const create = handler.create;
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const webpack_inject_plugin_1 = require("webpack-inject-plugin");
4
- function sheetLoader(cssName) {
5
- return () => {
6
- const lines = [
7
- `var d=document`,
8
- `var e=d.createElement("link")`,
9
- `e.type="text/css"`,
10
- `e.rel="stylesheet"`,
11
- `e.href=__webpack_public_path__ + ${JSON.stringify(cssName)}`,
12
- `d.head.appendChild(e)`,
13
- ];
14
- return lines.join(';');
15
- };
16
- }
17
- class SheetPlugin extends webpack_inject_plugin_1.default {
18
- constructor(cssName) {
19
- super(sheetLoader(cssName));
20
- }
21
- }
22
- exports.default = SheetPlugin;
23
- //# sourceMappingURL=SheetPlugin.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"SheetPlugin.js","sourceRoot":"","sources":["../../src/configs/SheetPlugin.ts"],"names":[],"mappings":";;AAAA,iEAAiD;AAEjD,SAAS,WAAW,CAAC,OAAe;IAClC,OAAO,GAAG,EAAE;QACV,MAAM,KAAK,GAAG;YACZ,gBAAgB;YAChB,+BAA+B;YAC/B,mBAAmB;YACnB,oBAAoB;YACpB,oCAAoC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE;YAC7D,uBAAuB;SACxB,CAAC;QACF,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC,CAAC;AACJ,CAAC;AAED,MAAqB,WAAY,SAAQ,+BAAY;IACnD,YAAY,OAAe;QACzB,KAAK,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC;IAC9B,CAAC;CACF;AAJD,8BAIC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/configs/common.ts"],"names":[],"mappings":";;;AAAA,gEAAgE;AAChE,2CAA0D;AAC1D,qCAA4F;AAC5F,2EAAqE;A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Y;YAClB,GAAG,EAAE,CAAC,WAAW,CAAC;YAClB,OAAO,EAAE,WAAW;SACrB;QACD;YACE,IAAI,EAAE,UAAU;YAChB,GAAG,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;SAC7B;QACD;YACE,IAAI,EAAE,aAAa;YACnB,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,uBAAuB,CAAC,CAAC;SAChD;QACD;YACE,IAAI,EAAE,QAAQ;YACd,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAC;YAC3C,OAAO,EAAE,WAAW;YACpB,OAAO,EAAE,KAAK;SACf;KACF,CAAC;AACJ,CAAC;AAvDD,4BAuDC"}
@@ -1,2 +0,0 @@
1
- export * from './pilet';
2
- export * from './piral';
@@ -1,15 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./pilet"), exports);
14
- __exportStar(require("./piral"), exports);
15
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/configs/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAAwB;AACxB,0CAAwB"}