@flatjs/evolve 2.1.0-next.5 → 2.1.0-next.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. package/dist/constants.js +1 -26
  2. package/dist/create-webpack/create-externals.js +1 -6
  3. package/dist/create-webpack/create-optimization.js +1 -41
  4. package/dist/create-webpack/create-output.js +1 -35
  5. package/dist/create-webpack/create-performance.js +1 -7
  6. package/dist/create-webpack/create-plugins.js +1 -77
  7. package/dist/create-webpack/create-resolve.js +1 -37
  8. package/dist/create-webpack/create-rule-sets.js +1 -19
  9. package/dist/create-webpack/load-webpack-config.js +1 -56
  10. package/dist/create-webpack/resolve-public-path.js +1 -15
  11. package/dist/create-webpack/rule-sets/constants.js +1 -3
  12. package/dist/create-webpack/rule-sets/rule-assets.js +1 -50
  13. package/dist/create-webpack/rule-sets/rule-css.js +1 -101
  14. package/dist/create-webpack/rule-sets/rule-less.js +1 -44
  15. package/dist/create-webpack/rule-sets/rule-scripts.js +1 -34
  16. package/dist/create-webpack/rule-sets/rule-svg-icon.js +1 -25
  17. package/dist/create-webpack/rule-sets/rule-utils.js +1 -10
  18. package/dist/create-webpack/types.js +1 -1
  19. package/dist/default-options.js +1 -84
  20. package/dist/define-config/define-config.js +1 -4
  21. package/dist/define-config/index.js +1 -1
  22. package/dist/dev-server/add-compiler-to-dev-server.js +1 -58
  23. package/dist/dev-server/create-app-page-route.js +1 -13
  24. package/dist/dev-server/create-dev-server-compiler-tasks.js +1 -52
  25. package/dist/dev-server/create-dev-server-entries.js +1 -27
  26. package/dist/dev-server/create-dev-server.js +1 -24
  27. package/dist/dev-server/index.js +1 -6
  28. package/dist/dev-server/middlewares/create-page-middleware.js +1 -33
  29. package/dist/dev-server/middlewares/create-public-assets-middleware.js +1 -25
  30. package/dist/dev-server/middlewares/get-all-sorted-modules.js +1 -24
  31. package/dist/dev-server/middlewares/get-bundle-asset.js +1 -7
  32. package/dist/dev-server/middlewares/get-dev-server-host-uri.js +1 -5
  33. package/dist/dev-server/middlewares/get-hmr-runtime-chunks.js +1 -14
  34. package/dist/dev-server/middlewares/get-normalized-entry-name.js +1 -14
  35. package/dist/dev-server/middlewares/get-page-main-html.js +1 -42
  36. package/dist/dev-server/middlewares/get-page-module-html.js +1 -120
  37. package/dist/dev-server/middlewares/get-project-virtual-path.js +1 -3
  38. package/dist/dev-server/middlewares/get-runtime-manifest.js +1 -25
  39. package/dist/dev-server/middlewares/index.js +1 -2
  40. package/dist/dev-server/middlewares/types.js +1 -1
  41. package/dist/errors/evolve-build-error.js +1 -10
  42. package/dist/helpers/allow-px2rem-for-module.js +1 -6
  43. package/dist/helpers/assert-only-single-entry-item.js +1 -23
  44. package/dist/helpers/chunk-entry-map.js +1 -21
  45. package/dist/helpers/enable-bundle-hashname-for-module.js +1 -6
  46. package/dist/helpers/filter-actived-entries.js +1 -42
  47. package/dist/helpers/get-bundle-file-name.js +1 -23
  48. package/dist/helpers/get-git-root.js +1 -4
  49. package/dist/helpers/get-html-plugin-config.js +1 -47
  50. package/dist/helpers/get-max-process-tasks.js +1 -7
  51. package/dist/helpers/get-pacakge-dir.js +1 -13
  52. package/dist/helpers/index.js +1 -17
  53. package/dist/helpers/json-serializer.js +1 -52
  54. package/dist/helpers/merge-babel-options.js +1 -45
  55. package/dist/helpers/normalize-entry-map.js +1 -38
  56. package/dist/helpers/normalize-page-proxy.js +1 -9
  57. package/dist/helpers/normalize-resolve-alias.js +1 -7
  58. package/dist/helpers/open-page.js +1 -15
  59. package/dist/helpers/print-log.js +1 -49
  60. package/dist/helpers/refresh-evolve-mock-options.js +1 -34
  61. package/dist/helpers/resolve-entry-map-input-files.js +1 -20
  62. package/dist/helpers/script-injects.js +1 -39
  63. package/dist/helpers/should-enable-react-fast-refresh.js +1 -10
  64. package/dist/helpers/split-to-multi-compiler.js +1 -32
  65. package/dist/index.js +1 -5
  66. package/dist/load-config/index.js +1 -1
  67. package/dist/load-config/load-evolve-config.js +1 -41
  68. package/dist/load-config/types.js +1 -1
  69. package/dist/main/create-thread-worker.js +1 -42
  70. package/dist/main/env-verify.js +1 -21
  71. package/dist/main/get-worker-path.js +1 -5
  72. package/dist/main/index.js +1 -4
  73. package/dist/main/prepare-build.js +1 -38
  74. package/dist/main/prepare-serve.js +1 -45
  75. package/dist/main/prepare-static.js +1 -30
  76. package/dist/main/start-build-dynamic.js +1 -157
  77. package/dist/main/start-build-worker.js +1 -46
  78. package/dist/main/start-build.js +1 -53
  79. package/dist/main/start-one-entry-build.js +1 -35
  80. package/dist/main/start-serve.js +1 -34
  81. package/dist/main/start-static.js +1 -19
  82. package/dist/minimizer/create-minimizers.js +1 -25
  83. package/dist/minimizer/default-options.js +1 -14
  84. package/dist/minimizer/image-minimizer.js +1 -65
  85. package/dist/minimizer/index.js +1 -1
  86. package/dist/minimizer/terser-minimizer.js +3 -15
  87. package/dist/minimizer/types.js +1 -1
  88. package/dist/plugins/circular-dependency/circular-dependency-plugin.js +1 -119
  89. package/dist/plugins/circular-dependency/index.js +1 -15
  90. package/dist/plugins/clean-webpack/clean-webpack-plugin.js +1 -173
  91. package/dist/plugins/clean-webpack/index.js +1 -22
  92. package/dist/plugins/define-variable/define-variable-plugin.js +1 -28
  93. package/dist/plugins/define-variable/index.js +1 -1
  94. package/dist/plugins/html-inject-scripts/plugin-html-inject-script.js +1 -27
  95. package/dist/plugins/module-federation/external-template-remotes.js +1 -92
  96. package/dist/plugins/module-federation/index.js +1 -1
  97. package/dist/plugins/module-federation/module-federation.js +1 -98
  98. package/dist/plugins/multi-html/index.js +1 -15
  99. package/dist/plugins/multi-html/multi-html-cdn-plugin.js +1 -84
  100. package/dist/plugins/multi-html/multi-html-plugin.js +1 -70
  101. package/dist/plugins/ts-checker/index.js +1 -1
  102. package/dist/plugins/ts-checker/ts-checker-plugin.js +1 -18
  103. package/dist/types/index.js +1 -8
  104. package/dist/types/types-ci.js +1 -1
  105. package/dist/types/types-dev-server.js +1 -1
  106. package/dist/types/types-entry-map.d.ts +2 -2
  107. package/dist/types/types-entry-map.js +1 -1
  108. package/dist/types/types-federation.js +1 -1
  109. package/dist/types/types-loader-options.js +1 -1
  110. package/dist/types/types-modular-import.js +1 -1
  111. package/dist/types/types-multi-html.js +1 -1
  112. package/dist/types/types-options.js +1 -1
  113. package/dist/types/types-plugin-options.js +1 -1
  114. package/dist/types/types-webpack.js +1 -1
  115. package/package.json +2 -2
@@ -1,92 +1 @@
1
- import webpackSources from 'webpack-sources';
2
- const PLUGIN_NAME = 'ExternalTemplateRemotesPlugin';
3
- const isExternalModule = (module) => {
4
- return module.constructor.name === 'ExternalModule';
5
- };
6
- /**
7
- * @param {string} urlAndGlobal the script request
8
- * @returns {string[]} script url and its global variable
9
- */
10
- function extractUrlAndGlobal(urlAndGlobal) {
11
- const index = urlAndGlobal.indexOf('@');
12
- if (index <= 0 || index === urlAndGlobal.length - 1) {
13
- throw new Error(`Invalid request "${urlAndGlobal}"`);
14
- }
15
- return [urlAndGlobal.substring(index + 1), urlAndGlobal.substring(0, index)];
16
- }
17
- export class ExternalTemplateRemotesPlugin {
18
- apply(compiler) {
19
- compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => {
20
- const scriptExternalModules = [];
21
- compilation.hooks.buildModule.tap(PLUGIN_NAME, (module) => {
22
- if (isExternalModule(module) && module.externalType === 'script') {
23
- scriptExternalModules.push(module);
24
- }
25
- });
26
- compilation.hooks.afterCodeGeneration.tap(PLUGIN_NAME, () => {
27
- scriptExternalModules.forEach((module) => {
28
- const urlTemplate = extractUrlAndGlobal(module.request)[0];
29
- const urlExpression = toExpression(urlTemplate);
30
- const sourceMap = compilation.codeGenerationResults.get(module, undefined).sources;
31
- const rawSource = sourceMap.get('javascript');
32
- if (rawSource) {
33
- const source = new webpackSources.RawSource(rawSource
34
- .source()
35
- .toString()
36
- .replace(`"${urlTemplate}"`, urlExpression));
37
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
- sourceMap.set('javascript', source);
39
- }
40
- });
41
- });
42
- });
43
- }
44
- }
45
- /**
46
- * app2@localhost/remoteEntry.js --> "\"app2@localhost/remoteEntry.js\""
47
- * app2@[window.app2Url]/remoteEntry.js --> "\"app2@\" + window.app2Url + \"/remoteEntry.js\""
48
- * @param templateUrl
49
- * @returns
50
- */
51
- // eslint-disable-next-line sonarjs/cognitive-complexity
52
- function toExpression(templateUrl) {
53
- const result = [];
54
- const current = [];
55
- let isExpression = false;
56
- let invalid = false;
57
- for (const c of templateUrl) {
58
- if (c === '[') {
59
- if (isExpression) {
60
- invalid = true;
61
- break;
62
- }
63
- isExpression = true;
64
- if (current.length) {
65
- result.push(`"${current.join('')}"`);
66
- current.length = 0;
67
- }
68
- }
69
- else if (c === ']') {
70
- if (!isExpression) {
71
- invalid = true;
72
- break;
73
- }
74
- isExpression = false;
75
- if (current.length) {
76
- result.push(`${current.join('')}`);
77
- current.length = 0;
78
- }
79
- current.length = 0;
80
- }
81
- else {
82
- current.push(c);
83
- }
84
- }
85
- if (isExpression || invalid) {
86
- throw new Error(`Invalid template URL "${templateUrl}"`);
87
- }
88
- if (current.length) {
89
- result.push(`"${current.join('')}"`);
90
- }
91
- return result.join(' + ');
92
- }
1
+ import webpackSources from"webpack-sources";const PLUGIN_NAME="ExternalTemplateRemotesPlugin",isExternalModule=e=>"ExternalModule"===e.constructor.name;function extractUrlAndGlobal(e){const t=e.indexOf("@");if(t<=0||t===e.length-1)throw new Error(`Invalid request "${e}"`);return[e.substring(t+1),e.substring(0,t)]}export class ExternalTemplateRemotesPlugin{apply(e){e.hooks.make.tap(PLUGIN_NAME,(e=>{const t=[];e.hooks.buildModule.tap(PLUGIN_NAME,(e=>{isExternalModule(e)&&"script"===e.externalType&&t.push(e)})),e.hooks.afterCodeGeneration.tap(PLUGIN_NAME,(()=>{t.forEach((t=>{const o=extractUrlAndGlobal(t.request)[0],n=toExpression(o),r=e.codeGenerationResults.get(t,void 0).sources,s=r.get("javascript");if(s){const e=new webpackSources.RawSource(s.source().toString().replace(`"${o}"`,n));r.set("javascript",e)}}))}))}))}}function toExpression(e){const t=[],o=[];let n=!1,r=!1;for(const s of e)if("["===s){if(n){r=!0;break}n=!0,o.length&&(t.push(`"${o.join("")}"`),o.length=0)}else if("]"===s){if(!n){r=!0;break}n=!1,o.length&&(t.push(`${o.join("")}`),o.length=0),o.length=0}else o.push(s);if(n||r)throw new Error(`Invalid template URL "${e}"`);return o.length&&t.push(`"${o.join("")}"`),t.join(" + ")}
@@ -1 +1 @@
1
- export * from './module-federation.js';
1
+ export*from"./module-federation.js";
@@ -1,98 +1 @@
1
- import { join } from 'node:path';
2
- import { ensureSlash } from '@flatjs/common';
3
- import webpack from 'webpack';
4
- import { normalizeEvolveEntryName } from '../../helpers/normalize-entry-map.js';
5
- import { injectFederationScripts } from '../../helpers/script-injects.js';
6
- import { HtmlInjectScriptPlugin } from '../html-inject-scripts/plugin-html-inject-script.js';
7
- import { ExternalTemplateRemotesPlugin } from './external-template-remotes.js';
8
- /**
9
- * `${projectVirtualPath}/mine` --> evolve_demo_mine
10
- * @param entryPath `${projectVirtualPath}/mine`
11
- */
12
- const normalizeWidgetName = (entryPath = '') => {
13
- return entryPath.replace(/[/-]/g, '_').toLowerCase();
14
- };
15
- const remoteFileName = (entryName) => {
16
- return join(entryName, `micro-remote-module.js`);
17
- };
18
- export const createModuleFederationPlugin = (serveMode, entryMapItem, evolveOptions) => {
19
- const projectVirtualPath = evolveOptions.projectVirtualPath;
20
- const [entryName, entryConfig] = entryMapItem;
21
- const multiCdnConfig = evolveOptions.multiHtmlCdn;
22
- const multiHtmlCdnResolver = evolveOptions.multiHtmlCdnEnvResolver;
23
- const moduleFederation = entryConfig.options?.moduleFederation;
24
- const plugins = [];
25
- if (moduleFederation) {
26
- const { remotes, exposes, ...restFederationOptions } = moduleFederation;
27
- // e.g. `flatjs/evolve/mine` => `flatjs/evolve/home/micro-remote-module.js`
28
- const entryRemoteFileName = remoteFileName(entryName);
29
- // e.g. `flatjs/evolve/mine` => `flatjs_evolve_mine`
30
- const containerName = normalizeWidgetName(entryName);
31
- const patchExposes = exposes
32
- ? Array.isArray(exposes)
33
- ? exposes
34
- : [exposes]
35
- : [];
36
- const myExposes = patchExposes.map((s) => {
37
- const exposeItem = {};
38
- for (const [key, config] of Object.entries(s)) {
39
- exposeItem[key] = {
40
- ...config,
41
- name: join(entryName, config.name.replace(/^\//, '')),
42
- };
43
- }
44
- return exposeItem;
45
- });
46
- const myRemotes = (remotes || []).map(({ name, endpoint }) => {
47
- // e.g. `flatjs/evolve/home`
48
- const normalizedEntryName = normalizeEvolveEntryName(name, projectVirtualPath);
49
- // e.g. `flatjs_evolve_home`
50
- const remoteWidgetName = normalizeWidgetName(normalizedEntryName);
51
- // e.g. `flatjs/evolve/home/micro-remote-module.js`
52
- const refRemoteEntryFileName = remoteFileName(normalizedEntryName);
53
- // construct endpoint for remote widget name.
54
- const endpointPath = endpoint
55
- ? ensureSlash(endpoint(name, normalizedEntryName), false)
56
- : `[window.evolveFetchMicroWidgets()]`;
57
- return {
58
- [remoteWidgetName]: `${remoteWidgetName}@${endpointPath}/${refRemoteEntryFileName}`,
59
- };
60
- });
61
- plugins.push(
62
- // https://webpack.js.org/plugins/module-federation-plugin/
63
- new webpack.container.ModuleFederationPlugin({
64
- /**
65
- * Options for library.
66
- * library: { type: 'var', name: containerName },
67
- * other module federation configurations
68
- */
69
- ...restFederationOptions,
70
- /**
71
- * The name of the container
72
- * `${projectName}-${moduleName}` e.g. `flatjs_evolve_home`
73
- */
74
- name: containerName,
75
- /**
76
- * The filename of the container as relative path inside the `output.path` directory.
77
- * `${entryName}/micro-remote-module.js`, e.g. `flatjs/evolve/home/micro-remote-module.js`
78
- */
79
- filename: entryRemoteFileName,
80
- /**
81
- * Container locations and request scopes from which modules should be resolved and loaded at runtime.
82
- * When provided, property name is used as request scope, otherwise request scope is automatically inferred from container location.
83
- */
84
- remotes: myRemotes,
85
- /**
86
- * Modules that should be exposed by this container.
87
- * When provided, property name is used as public name, otherwise public name is automatically inferred from request.
88
- */
89
- exposes: myExposes,
90
- }), new ExternalTemplateRemotesPlugin());
91
- if (!serveMode) {
92
- plugins.unshift(new HtmlInjectScriptPlugin([
93
- injectFederationScripts(multiCdnConfig, multiHtmlCdnResolver),
94
- ]));
95
- }
96
- }
97
- return plugins;
98
- };
1
+ import{join}from"node:path";import{ensureSlash}from"@flatjs/common";import webpack from"webpack";import{normalizeEvolveEntryName}from"../../helpers/normalize-entry-map.js";import{injectFederationScripts}from"../../helpers/script-injects.js";import{HtmlInjectScriptPlugin}from"../html-inject-scripts/plugin-html-inject-script.js";import{ExternalTemplateRemotesPlugin}from"./external-template-remotes.js";const normalizeWidgetName=(e="")=>e.replace(/[/-]/g,"_").toLowerCase(),remoteFileName=e=>join(e,"micro-remote-module.js");export const createModuleFederationPlugin=(e,t,r)=>{const o=r.projectVirtualPath,[n,i]=t,m=r.multiHtmlCdn,a=r.multiHtmlCdnEnvResolver,l=i.options?.moduleFederation,s=[];if(l){const{remotes:t,exposes:r,...i}=l,p=remoteFileName(n),c=normalizeWidgetName(n),u=(r?Array.isArray(r)?r:[r]:[]).map((e=>{const t={};for(const[r,o]of Object.entries(e))t[r]={...o,name:join(n,o.name.replace(/^\//,""))};return t})),d=(t||[]).map((({name:e,endpoint:t})=>{const r=normalizeEvolveEntryName(e,o),n=normalizeWidgetName(r),i=remoteFileName(r);return{[n]:`${n}@${t?ensureSlash(t(e,r),!1):"[window.evolveFetchMicroWidgets()]"}/${i}`}}));s.push(new webpack.container.ModuleFederationPlugin({...i,name:c,filename:p,remotes:d,exposes:u}),new ExternalTemplateRemotesPlugin),e||s.unshift(new HtmlInjectScriptPlugin([injectFederationScripts(m,a)]))}return s};
@@ -1,15 +1 @@
1
- import { FlatEvolveMultiCdnPlugin } from './multi-html-cdn-plugin.js';
2
- import { createMultiHtmlWebpackPlugin } from './multi-html-plugin.js';
3
- export const createHtmlPlugins = (serveMode, entryMapItem, evolveOptions) => {
4
- const plugins = [];
5
- // Only for `production`, or `library` compiler mode do not need to attach `html-webpack-plugin`
6
- if (serveMode || entryMapItem[1]?.options?.output?.library) {
7
- return plugins;
8
- }
9
- // Attach `html-webpack-plugin` first
10
- const allEnv = Object.keys(evolveOptions.multiHtmlCdn);
11
- plugins.push(...createMultiHtmlWebpackPlugin(serveMode, evolveOptions, entryMapItem, allEnv));
12
- // Attach `@flatjs/evolve-plugin-multi-html-cdn`
13
- plugins.push(new FlatEvolveMultiCdnPlugin(evolveOptions));
14
- return plugins;
15
- };
1
+ import{FlatEvolveMultiCdnPlugin}from"./multi-html-cdn-plugin.js";import{createMultiHtmlWebpackPlugin}from"./multi-html-plugin.js";export const createHtmlPlugins=(t,l,u)=>{const i=[];if(t||l[1]?.options?.output?.library)return i;const n=Object.keys(u.multiHtmlCdn);return i.push(...createMultiHtmlWebpackPlugin(t,u,l,n)),i.push(new FlatEvolveMultiCdnPlugin(u)),i};
@@ -1,84 +1 @@
1
- import { basename } from 'node:path';
2
- import HtmlWebpackPlugin from 'html-webpack-plugin';
3
- import webpack from 'webpack';
4
- import { cdnFinder, findEnvCdn, httpUrlJoin, } from '../../helpers/script-injects.js';
5
- export class FlatEvolveMultiCdnPlugin {
6
- constructor(evolveOptions) {
7
- this.pluginName = 'FlatEvolveMultiCdnPlugin';
8
- // https://github.com/webpack/webpack/blob/3d653290fafe385277b48e5a36807124618b9561/lib/MainTemplate.js#L12
9
- // the bundle public path RuntimeGlobals.publicPath: '__webpack_require__.p';
10
- this.requireFn = webpack.RuntimeGlobals.publicPath;
11
- this.config = evolveOptions.multiHtmlCdn;
12
- this.cdnResolver =
13
- evolveOptions.multiHtmlCdnEnvResolver ||
14
- function cdnResolver() {
15
- return undefined;
16
- };
17
- // Make sure we have `prod` configuration for each cdn node at least.
18
- if (!this.config?.prod) {
19
- throw new Error('We must setup `prod` for each CDN config node!');
20
- }
21
- }
22
- /**
23
- * Apply the plugin to check if there are non initial chunks which need to be imported using `require-ensure` or `import`
24
- * https://github.com/webpack/webpack/blob/3d653290fafe385277b48e5a36807124618b9561/lib/MainTemplate.js#L158
25
- * https://www.npmjs.com/package/vscode-webpack-debugger
26
- * https://www.cnblogs.com/Scar007/p/9166068.html
27
- * https://www.cnblogs.com/pluslius/p/10271537.html
28
- */
29
- apply(compiler) {
30
- // Handle chunk assets while `Compilation:before-chunk-assets`
31
- // https://github.com/webpack/webpack/blob/3d653290fafe385277b48e5a36807124618b9561/lib/MainTemplate.js#L58
32
- compiler.hooks.thisCompilation.tap(this.pluginName, (compilation) => {
33
- compilation.mainTemplate.hooks.requireExtensions.tap(this.pluginName, (_source, chunk) => {
34
- const buf = [];
35
- buf.push('// Dynamic assets path override(`@flatjs/evolve`) plugin-multi-html-cdn`)');
36
- const runtimeRequirements = compilation.chunkGraph?.getTreeRuntimeRequirements(chunk);
37
- if (runtimeRequirements &&
38
- runtimeRequirements.has(webpack.RuntimeGlobals.requireScope)) {
39
- buf.push('(function () {');
40
- buf.push(webpack.Template.indent(`var flatjsMultiCdn = {`));
41
- buf.push(webpack.Template.indent(webpack.Template.indent([
42
- `cdnConfig: ${JSON.stringify(this.config || {})},`,
43
- ])));
44
- buf.push(webpack.Template.indent(webpack.Template.indent([
45
- `cdnResolver: ${this.cdnResolver.toString()},`,
46
- ])));
47
- buf.push(webpack.Template.indent(webpack.Template.indent([`cdnFinder: ${cdnFinder.toString()}`])));
48
- buf.push(webpack.Template.indent(`};`));
49
- buf.push(webpack.Template.indent(`${this.requireFn} = flatjsMultiCdn.cdnFinder(flatjsMultiCdn.cdnConfig, flatjsMultiCdn.cdnResolver) || ${this.requireFn};`));
50
- buf.push('})();');
51
- }
52
- return webpack.Template.asString(buf);
53
- });
54
- });
55
- // Using html webpack plugin hooks to replace `scripts` `styles` before inject to html temlate file.
56
- compiler.hooks.compilation.tap(this.pluginName, (compilation) => {
57
- HtmlWebpackPlugin.getHooks(compilation).beforeAssetTagGeneration.tap(this.pluginName, (data) => {
58
- const { assets } = data;
59
- const { userOptions } = data.plugin;
60
- const multiCdn = userOptions.multiCdn;
61
- const publicPath = assets.publicPath;
62
- const scripts = assets.js.map((scriptItem) => {
63
- // Normally for `index-dev.html` we need to use relative path.
64
- if (multiCdn.disabled) {
65
- return basename(scriptItem);
66
- }
67
- const randomCdn = findEnvCdn(this.config, multiCdn.env);
68
- return httpUrlJoin(randomCdn, scriptItem.replace(publicPath, ''));
69
- });
70
- const styles = assets.css.map((styleItem) => {
71
- // Normally for `index-dev.html` we need to use relative path.
72
- if (multiCdn.disabled) {
73
- return basename(styleItem);
74
- }
75
- const randomCdn = findEnvCdn(this.config, multiCdn.env);
76
- return httpUrlJoin(randomCdn, styleItem.replace(publicPath, ''));
77
- });
78
- data.assets.js = scripts;
79
- data.assets.css = styles;
80
- return data;
81
- });
82
- });
83
- }
84
- }
1
+ import{basename}from"node:path";import HtmlWebpackPlugin from"html-webpack-plugin";import webpack from"webpack";import{cdnFinder,findEnvCdn,httpUrlJoin}from"../../helpers/script-injects.js";export class FlatEvolveMultiCdnPlugin{constructor(e){if(this.pluginName="FlatEvolveMultiCdnPlugin",this.requireFn=webpack.RuntimeGlobals.publicPath,this.config=e.multiHtmlCdn,this.cdnResolver=e.multiHtmlCdnEnvResolver||function cdnResolver(){},!this.config?.prod)throw new Error("We must setup `prod` for each CDN config node!")}apply(e){e.hooks.thisCompilation.tap(this.pluginName,(e=>{e.mainTemplate.hooks.requireExtensions.tap(this.pluginName,((n,t)=>{const i=[];i.push("// Dynamic assets path override(`@flatjs/evolve`) plugin-multi-html-cdn`)");const s=e.chunkGraph?.getTreeRuntimeRequirements(t);return s&&s.has(webpack.RuntimeGlobals.requireScope)&&(i.push("(function () {"),i.push(webpack.Template.indent("var flatjsMultiCdn = {")),i.push(webpack.Template.indent(webpack.Template.indent([`cdnConfig: ${JSON.stringify(this.config||{})},`]))),i.push(webpack.Template.indent(webpack.Template.indent([`cdnResolver: ${this.cdnResolver.toString()},`]))),i.push(webpack.Template.indent(webpack.Template.indent([`cdnFinder: ${cdnFinder.toString()}`]))),i.push(webpack.Template.indent("};")),i.push(webpack.Template.indent(`${this.requireFn} = flatjsMultiCdn.cdnFinder(flatjsMultiCdn.cdnConfig, flatjsMultiCdn.cdnResolver) || ${this.requireFn};`)),i.push("})();")),webpack.Template.asString(i)}))})),e.hooks.compilation.tap(this.pluginName,(e=>{HtmlWebpackPlugin.getHooks(e).beforeAssetTagGeneration.tap(this.pluginName,(e=>{const{assets:n}=e,{userOptions:t}=e.plugin,i=t.multiCdn,s=n.publicPath,a=n.js.map((e=>{if(i.disabled)return basename(e);const n=findEnvCdn(this.config,i.env);return httpUrlJoin(n,e.replace(s,""))})),p=n.css.map((e=>{if(i.disabled)return basename(e);const n=findEnvCdn(this.config,i.env);return httpUrlJoin(n,e.replace(s,""))}));return e.assets.js=a,e.assets.css=p,e}))}))}}
@@ -1,70 +1 @@
1
- import HtmlWebpackPlugin from 'html-webpack-plugin';
2
- import { allowPx2remForModule } from '../../helpers/allow-px2rem-for-module.js';
3
- import { getHtmlPluginConfig, } from '../../helpers/get-html-plugin-config.js';
4
- import { findEnvCdn } from '../../helpers/script-injects.js';
5
- const minifyOpts = {
6
- minifyJS: true,
7
- removeComments: true,
8
- collapseWhitespace: true,
9
- collapseBooleanAttributes: false,
10
- };
11
- /**
12
- * Create `html-webpack-plugin` for this build, refer to best practices
13
- * We'd better pass only one entry for each `build` cycle
14
- * @param buildEntryItem the entries for this `build`
15
- * @param allEnv
16
- */
17
- export const createMultiHtmlWebpackPlugin = (serveMode, evolveOptions, entryMapItem, allEnv) => {
18
- const [entryKey, entryConfig] = entryMapItem;
19
- const htmlPlugins = [];
20
- const { options } = entryConfig;
21
- const mode = serveMode ? 'development' : 'production';
22
- for (const env of allEnv) {
23
- const envCdn = findEnvCdn(evolveOptions.multiHtmlCdn, env);
24
- const configData = {
25
- mode,
26
- envCdn,
27
- };
28
- htmlPlugins.push(new HtmlWebpackPlugin({
29
- inject: 'body',
30
- title: getHtmlPluginConfig('title', configData, options?.title),
31
- chunks: [entryKey],
32
- // `minify` is true, `dev` always don't minify.
33
- minify: options?.htmlMinify === false || ['me', 'dev'].includes(env)
34
- ? false
35
- : minifyOpts,
36
- // output file path
37
- filename: `${entryKey}/index${env === 'prod' ? '' : `-${env}`}.html`,
38
- // html template
39
- template: getHtmlPluginConfig('templatePath', configData, options?.templatePath).replace(`{0}`, env),
40
- // template parameters
41
- templateParameters: {
42
- // The page title
43
- title: getHtmlPluginConfig('title', configData, options?.title),
44
- // The page favicon
45
- favicon: getHtmlPluginConfig('favicon', configData, options?.favicon),
46
- // The customized inject head tags.
47
- headBeforeHtmlTags: getHtmlPluginConfig('headBeforeHtmlTags', configData, options?.headBeforeHtmlTags),
48
- // The customized inject inline scripts.
49
- inlineScripts: getHtmlPluginConfig('inlineScripts', configData, options?.inlineScripts),
50
- // The ordered styles will be injected start of html head.
51
- headBeforeStyles: getHtmlPluginConfig('headBeforeStyles', configData, options?.headBeforeStyles),
52
- // The ordered scripts will be injected before html head.
53
- headBeforeScripts: getHtmlPluginConfig('headBeforeScripts', configData, options?.headBeforeScripts),
54
- // The ordered scripts will be injected end of html body.
55
- bodyAfterScripts: getHtmlPluginConfig('bodyAfterScripts', configData, options?.bodyAfterScripts),
56
- // `allowPx2rem` default is true
57
- viewport: allowPx2remForModule(entryMapItem, evolveOptions)
58
- ? getHtmlPluginConfig('viewport', configData, options?.viewport)
59
- : '',
60
- },
61
- // Some options for plugin used the `hook` of `html-webpack-plugin`
62
- multiCdn: {
63
- env,
64
- // use relative path for `me`, `dev`, `ntv`
65
- disabled: (options?.excludeCdnEnvs || ['me', 'dev', 'ntv']).includes(env),
66
- },
67
- }));
68
- }
69
- return htmlPlugins;
70
- };
1
+ import HtmlWebpackPlugin from"html-webpack-plugin";import{allowPx2remForModule}from"../../helpers/allow-px2rem-for-module.js";import{getHtmlPluginConfig}from"../../helpers/get-html-plugin-config.js";import{findEnvCdn}from"../../helpers/script-injects.js";const minifyOpts={minifyJS:!0,removeComments:!0,collapseWhitespace:!0,collapseBooleanAttributes:!1};export const createMultiHtmlWebpackPlugin=(e,t,i,l)=>{const[n,o]=i,r=[],{options:m}=o,g=e?"development":"production";for(const e of l){const l={mode:g,envCdn:findEnvCdn(t.multiHtmlCdn,e)};r.push(new HtmlWebpackPlugin({inject:"body",title:getHtmlPluginConfig("title",l,m?.title),chunks:[n],minify:!1!==m?.htmlMinify&&!["me","dev"].includes(e)&&minifyOpts,filename:`${n}/index${"prod"===e?"":`-${e}`}.html`,template:getHtmlPluginConfig("templatePath",l,m?.templatePath).replace("{0}",e),templateParameters:{title:getHtmlPluginConfig("title",l,m?.title),favicon:getHtmlPluginConfig("favicon",l,m?.favicon),headBeforeHtmlTags:getHtmlPluginConfig("headBeforeHtmlTags",l,m?.headBeforeHtmlTags),inlineScripts:getHtmlPluginConfig("inlineScripts",l,m?.inlineScripts),headBeforeStyles:getHtmlPluginConfig("headBeforeStyles",l,m?.headBeforeStyles),headBeforeScripts:getHtmlPluginConfig("headBeforeScripts",l,m?.headBeforeScripts),bodyAfterScripts:getHtmlPluginConfig("bodyAfterScripts",l,m?.bodyAfterScripts),viewport:allowPx2remForModule(i,t)?getHtmlPluginConfig("viewport",l,m?.viewport):""},multiCdn:{env:e,disabled:(m?.excludeCdnEnvs||["me","dev","ntv"]).includes(e)}}))}return r};
@@ -1 +1 @@
1
- export * from './ts-checker-plugin.js';
1
+ export*from"./ts-checker-plugin.js";
@@ -1,18 +1 @@
1
- import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
2
- export const createTsCheckerPlugins = (serveMode, entryMapItem, evolveOptions) => {
3
- const plugins = [];
4
- const itemEntries = entryMapItem[1].entry || [];
5
- const isVueEntryItem = itemEntries.find((s) => /.vue$/.test(s));
6
- // Runs typescript type checker and linter on separate process.
7
- if (evolveOptions.loaderOptions.runTsChecker && !isVueEntryItem) {
8
- plugins.push(new ForkTsCheckerWebpackPlugin({
9
- async: serveMode,
10
- issue: {},
11
- typescript: {
12
- context: evolveOptions.projectCwd,
13
- memoryLimit: 2048 * 4,
14
- },
15
- }));
16
- }
17
- return plugins;
18
- };
1
+ import ForkTsCheckerWebpackPlugin from"fork-ts-checker-webpack-plugin";export const createTsCheckerPlugins=(e,r,t)=>{const c=[],s=(r[1].entry||[]).find((e=>/.vue$/.test(e)));return t.loaderOptions.runTsChecker&&!s&&c.push(new ForkTsCheckerWebpackPlugin({async:e,issue:{},typescript:{context:t.projectCwd,memoryLimit:8192}})),c};
@@ -1,8 +1 @@
1
- export * from './types-dev-server.js';
2
- export * from './types-entry-map.js';
3
- export * from './types-federation.js';
4
- export * from './types-modular-import.js';
5
- export * from './types-multi-html.js';
6
- export * from './types-options.js';
7
- export * from './types-loader-options.js';
8
- export * from './types-webpack.js';
1
+ export*from"./types-dev-server.js";export*from"./types-entry-map.js";export*from"./types-federation.js";export*from"./types-modular-import.js";export*from"./types-multi-html.js";export*from"./types-options.js";export*from"./types-loader-options.js";export*from"./types-webpack.js";
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -59,8 +59,8 @@ export interface EvolveEntryItemOption extends MultiHtmlCDNEntryItem {
59
59
  */
60
60
  mockFilters?: Array<string | RegExp>;
61
61
  /**
62
- * Extends the default `output` configuration for each entry item.
63
- * normally we use `output.library` to define the global variable name.
62
+ * Extends the default `output` configuration for each entry item, normally we use `output.library` to define the global variable name.
63
+ * Note we don't need to support react fast refresh for library target. because of the library module can not run in the browser individually.
64
64
  * @example
65
65
  * ```ts
66
66
  *'plugin-chart': {
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1 +1 @@
1
- export {};
1
+ export{};
@@ -1 +1 @@
1
- export {};
1
+ export{};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flatjs/evolve",
3
- "version": "2.1.0-next.5",
3
+ "version": "2.1.0-next.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -19,7 +19,7 @@
19
19
  ],
20
20
  "scripts": {
21
21
  "serve": "yarn node --import=@armit/path-alias/register ./tests/dev-server/dev-server.ts",
22
- "build": "rimraf dist && tsc -p ./tsconfig.build.json",
22
+ "build": "rimraf dist && tsc -p ./tsconfig.build.json && npm run minify",
23
23
  "?build-release": "When https://github.com/atlassian/changesets/issues/432 has a solution we can remove this trick",
24
24
  "build-release": "yarn build && rimraf ./_release && yarn pack && mkdir ./_release && tar zxvf ./package.tgz --directory ./_release && rm ./package.tgz",
25
25
  "minify": "node ../../scripts/minify.mjs --dest=dist",