@umijs/mfsu 4.0.0-beta.9 → 4.0.0-canary-20240513.3

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 (74) hide show
  1. package/compiled/is-absolute-url/LICENSE +9 -0
  2. package/compiled/is-absolute-url/index.d.ts +20 -0
  3. package/compiled/is-absolute-url/index.js +1 -0
  4. package/compiled/is-absolute-url/package.json +1 -0
  5. package/compiled/mrmime/package.json +1 -1
  6. package/compiled/webpack-virtual-modules/index.js +1 -1
  7. package/compiled/webpack-virtual-modules/package.json +1 -1
  8. package/dist/babelPlugins/awaitImport/MFImport.d.ts +24 -0
  9. package/dist/babelPlugins/awaitImport/MFImport.js +66 -0
  10. package/dist/babelPlugins/awaitImport/awaitImport.d.ts +1 -1
  11. package/dist/babelPlugins/awaitImport/awaitImport.js +111 -112
  12. package/dist/babelPlugins/awaitImport/checkMatch.d.ts +3 -1
  13. package/dist/babelPlugins/awaitImport/checkMatch.js +166 -109
  14. package/dist/babelPlugins/awaitImport/getAliasedPath.d.ts +2 -2
  15. package/dist/babelPlugins/awaitImport/getAliasedPath.js +44 -33
  16. package/dist/babelPlugins/awaitImport/getRealPath.js +42 -21
  17. package/dist/babelPlugins/awaitImport/isExternals.js +55 -26
  18. package/dist/babelPlugins/awaitImport/parseSpecifiers.js +62 -47
  19. package/dist/constants.d.ts +1 -0
  20. package/dist/constants.js +50 -10
  21. package/dist/dep/dep.d.ts +7 -3
  22. package/dist/dep/dep.js +117 -95
  23. package/dist/dep/getCJSExports.js +83 -47
  24. package/dist/dep/getExposeFromContent.js +99 -74
  25. package/dist/dep/getModuleExports.js +58 -43
  26. package/dist/depBuilder/depBuilder.d.ts +6 -1
  27. package/dist/depBuilder/depBuilder.js +222 -159
  28. package/dist/depBuilder/getESBuildEntry.d.ts +1 -0
  29. package/dist/depBuilder/getESBuildEntry.js +50 -14
  30. package/dist/depInfo.d.ts +20 -5
  31. package/dist/depInfo.js +90 -39
  32. package/dist/esbuildHandlers/autoCssModules.d.ts +2 -0
  33. package/dist/esbuildHandlers/autoCssModules.js +48 -0
  34. package/dist/esbuildHandlers/awaitImport/index.d.ts +15 -0
  35. package/dist/esbuildHandlers/awaitImport/index.js +86 -0
  36. package/dist/index.d.ts +3 -1
  37. package/dist/index.js +35 -13
  38. package/dist/loader/esbuild.d.ts +5 -0
  39. package/dist/loader/esbuild.js +79 -0
  40. package/dist/mfsu/mfsu.d.ts +69 -0
  41. package/dist/mfsu/mfsu.js +319 -0
  42. package/dist/mfsu/strategyCompileTime.d.ts +20 -0
  43. package/dist/mfsu/strategyCompileTime.js +142 -0
  44. package/dist/mfsu/strategyStaticAnalyze.d.ts +24 -0
  45. package/dist/mfsu/strategyStaticAnalyze.js +196 -0
  46. package/dist/moduleGraph.d.ts +5 -8
  47. package/dist/moduleGraph.js +228 -178
  48. package/dist/staticDepInfo/importParser.d.ts +4 -0
  49. package/dist/staticDepInfo/importParser.js +28 -0
  50. package/dist/staticDepInfo/simulations/babel-plugin-import.d.ts +15 -0
  51. package/dist/staticDepInfo/simulations/babel-plugin-import.js +118 -0
  52. package/dist/staticDepInfo/staticDepInfo.d.ts +61 -0
  53. package/dist/staticDepInfo/staticDepInfo.js +274 -0
  54. package/dist/types.d.ts +12 -0
  55. package/dist/types.js +33 -8
  56. package/dist/utils/makeArray.js +29 -5
  57. package/dist/utils/resolveUtils.d.ts +1 -0
  58. package/dist/utils/resolveUtils.js +104 -0
  59. package/dist/utils/trimFileContent.js +29 -5
  60. package/dist/utils/webpackUtils.d.ts +3 -0
  61. package/dist/utils/webpackUtils.js +80 -0
  62. package/dist/webpackPlugins/buildDepPlugin.d.ts +4 -3
  63. package/dist/webpackPlugins/buildDepPlugin.js +61 -17
  64. package/dist/webpackPlugins/depChunkIdPrefixPlugin.d.ts +1 -1
  65. package/dist/webpackPlugins/depChunkIdPrefixPlugin.js +47 -19
  66. package/dist/webpackPlugins/stripSourceMapUrlPlugin.js +55 -27
  67. package/dist/webpackPlugins/writeCachePlugin.js +45 -15
  68. package/package.json +23 -19
  69. package/vendors/importParser/_importParser.js +683 -0
  70. package/vendors/importParser/importParser.jison +105 -0
  71. package/dist/babelPlugins/autoExport.d.ts +0 -7
  72. package/dist/babelPlugins/autoExport.js +0 -61
  73. package/dist/mfsu.d.ts +0 -54
  74. package/dist/mfsu.js +0 -179
@@ -0,0 +1,105 @@
1
+ /* reference https://262.ecma-international.org/12.0/#prod-ImportsList */
2
+ /* https://tc39.es/ecma262/multipage/ecmascript-language-scripts-and-modules.html#sec-exports*/
3
+ /* lexical grammar */
4
+ %lex
5
+ %%
6
+ \s+ /* skip whitespace */
7
+ "//".* /* IGNORE line comment */
8
+ "/*"((\*+[^/*])|([^*]))*\**"*/" /* IGNORE block comment */
9
+ "*" return 'STAR'
10
+ "as" return 'AS'
11
+ "from" return 'FROM'
12
+ "import" return 'IMPORT'
13
+ "export" return 'EXPORT'
14
+ "{" return 'LBRACE'
15
+ "}" return 'RBRACE'
16
+ "," return 'COMMA'
17
+ ";" return 'SEMICOLON'
18
+ \"[^\"]*\"|\'[^\']*\' yytext = yytext.substr(1,yyleng-2); return 'STRING';
19
+ [a-zA-Z_]\w* return 'ID';
20
+ /lex
21
+
22
+ %start Program
23
+
24
+ %% /* language grammar */
25
+
26
+ Program
27
+ : ImportStatements { return $1}
28
+ ;
29
+
30
+ ImportStatements
31
+ : ImportStatement { $$ = [ $1 ] }
32
+ | ImportStatements ImportStatement { $$ = [ ...$1, $2 ] }
33
+ ;
34
+
35
+ ImportStatement
36
+ : IMPORT ModuleSpecifier { $$ = { from: $2, imports: []} }
37
+ | IMPORT ImportClause FromClause { $$ = { from: $3, imports: $2 } }
38
+ | ExportFromStatement
39
+ ;
40
+
41
+ ImportClause
42
+ : ImportedDefaultBinding { $$ = [ $1 ] }
43
+ | NameSpaceImport { $$ = [ $1 ] }
44
+ | NamedImports
45
+ | ImportedDefaultBinding COMMA NameSpaceImport { $$ = [ $1, $3] }
46
+ | ImportedDefaultBinding COMMA NamedImports { $$ = [ $1, ...$3]}
47
+ ;
48
+
49
+ ImportedDefaultBinding
50
+ : ID { $$ = 'default' }
51
+ ;
52
+
53
+ NamedImports
54
+ : LBRACE ImportsList RBRACE { $$=$2}
55
+ | LBRACE ImportsList COMMA RBRACE { $$=$2 }
56
+ ;
57
+
58
+ NameSpaceImport
59
+ : STAR AS ID { $$ = '*' }
60
+ ;
61
+
62
+ ImportsList
63
+ : ImportSpecifier { $$ = [ $1 ]}
64
+ | ImportsList COMMA ImportSpecifier { $$= [ ...$1, $3 ] }
65
+ ;
66
+
67
+ ImportSpecifier
68
+ : ID
69
+ | ID AS ID { $$ = $1 }
70
+ ;
71
+
72
+ FromClause
73
+ : FROM ModuleSpecifier { $$=$2 }
74
+ ;
75
+
76
+ ModuleSpecifier
77
+ : STRING Semicolon { $$=$1 }
78
+ ;
79
+
80
+ ExportFromStatement
81
+ : EXPORT ExportFromClause FromClause { $$ = {from: $3, imports: $2 } }
82
+ ;
83
+
84
+ ExportFromClause
85
+ : STAR { $$ = [ '*' ] }
86
+ | STAR AS ID { $$ = [ '*' ] }
87
+ | NamedExports
88
+ ;
89
+
90
+ NamedExports
91
+ : LBRACE ExportsList RBRACE { $$ = $2 }
92
+ | LBRACE ExportsList COMMA RBRACE { $$ = $2 }
93
+ ;
94
+
95
+ ExportsList
96
+ : ExportSpecifier { $$ = [ $1 ] }
97
+ | ExportsList COMMA ExportSpecifier { $$ = [ ...$1, $3 ] }
98
+ ;
99
+
100
+ ExportSpecifier
101
+ : ID
102
+ | ID AS ID { $$ = $1 }
103
+ ;
104
+
105
+ Semicolon: | SEMICOLON ;
@@ -1,7 +0,0 @@
1
- import * as Babel from '@umijs/bundler-utils/compiled/babel/core';
2
- import * as t from '@umijs/bundler-utils/compiled/babel/types';
3
- export default function (): {
4
- visitor: {
5
- Program(path: Babel.NodePath<t.Program>): void;
6
- };
7
- };
@@ -1,61 +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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
23
- function default_1() {
24
- return {
25
- visitor: {
26
- Program(path) {
27
- let hasExport = false;
28
- path.node.body.forEach((node) => {
29
- if (
30
- // esm
31
- t.isExportNamedDeclaration(node) ||
32
- t.isExportDefaultDeclaration(node) ||
33
- t.isExportAllDeclaration(node) ||
34
- // cjs
35
- (t.isExpressionStatement(node) &&
36
- t.isAssignmentExpression(node.expression) &&
37
- t.isMemberExpression(node.expression.left) &&
38
- // exports.xxx =
39
- (t.isIdentifier(node.expression.left.object, {
40
- name: 'exports',
41
- }) ||
42
- // module.exports =
43
- (t.isIdentifier(node.expression.left.object, {
44
- name: 'module',
45
- }) &&
46
- t.isIdentifier(node.expression.left.property, {
47
- name: 'exports',
48
- }))))) {
49
- hasExport = true;
50
- }
51
- });
52
- if (!hasExport) {
53
- path.node.body.push(t.exportNamedDeclaration(t.variableDeclaration('const', [
54
- t.variableDeclarator(t.identifier('__mfsu'), t.numericLiteral(1)),
55
- ])));
56
- }
57
- },
58
- },
59
- };
60
- }
61
- exports.default = default_1;
package/dist/mfsu.d.ts DELETED
@@ -1,54 +0,0 @@
1
- import type { NextFunction, Request, Response } from 'express';
2
- import webpack, { Configuration } from 'webpack';
3
- import autoExport from './babelPlugins/autoExport';
4
- import awaitImport from './babelPlugins/awaitImport/awaitImport';
5
- import { DepBuilder } from './depBuilder/depBuilder';
6
- import { DepInfo } from './depInfo';
7
- import { Mode } from './types';
8
- interface IOpts {
9
- cwd?: string;
10
- excludeNodeNatives?: boolean;
11
- exportAllMembers?: Record<string, string[]>;
12
- getCacheDependency?: Function;
13
- mfName?: string;
14
- mode?: Mode;
15
- tmpBase?: string;
16
- unMatchLibs?: string[];
17
- implementor: typeof webpack;
18
- buildDepWithESBuild?: boolean;
19
- }
20
- export declare class MFSU {
21
- opts: IOpts;
22
- alias: Record<string, string>;
23
- externals: (Record<string, string> | Function)[];
24
- depInfo: DepInfo;
25
- depBuilder: DepBuilder;
26
- depConfig: Configuration | null;
27
- constructor(opts: IOpts);
28
- setWebpackConfig(opts: {
29
- config: Configuration;
30
- depConfig: Configuration;
31
- }): void;
32
- buildDeps(): Promise<void>;
33
- getMiddlewares(): ((req: Request, res: Response, next: NextFunction) => void)[];
34
- getBabelPlugins(): (typeof autoExport | (typeof awaitImport | {
35
- onTransformDeps: () => void;
36
- onCollect: ({ file, data, }: {
37
- file: string;
38
- data: {
39
- unMatched: Set<{
40
- sourceValue: string;
41
- }>;
42
- matched: Set<{
43
- sourceValue: string;
44
- }>;
45
- };
46
- }) => void;
47
- exportAllMembers: Record<string, string[]> | undefined;
48
- unMatchLibs: string[] | undefined;
49
- remoteName: string | undefined;
50
- alias: Record<string, string>;
51
- externals: (Function | Record<string, string>)[];
52
- })[])[];
53
- }
54
- export {};
package/dist/mfsu.js DELETED
@@ -1,179 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.MFSU = void 0;
16
- const utils_1 = require("@umijs/utils");
17
- const fs_1 = require("fs");
18
- const path_1 = require("path");
19
- const mrmime_1 = require("../compiled/mrmime");
20
- // @ts-ignore
21
- const webpack_virtual_modules_1 = __importDefault(require("../compiled/webpack-virtual-modules"));
22
- const autoExport_1 = __importDefault(require("./babelPlugins/autoExport"));
23
- const awaitImport_1 = __importDefault(require("./babelPlugins/awaitImport/awaitImport"));
24
- const getRealPath_1 = require("./babelPlugins/awaitImport/getRealPath");
25
- const constants_1 = require("./constants");
26
- const dep_1 = require("./dep/dep");
27
- const depBuilder_1 = require("./depBuilder/depBuilder");
28
- const depInfo_1 = require("./depInfo");
29
- const types_1 = require("./types");
30
- const makeArray_1 = require("./utils/makeArray");
31
- const buildDepPlugin_1 = require("./webpackPlugins/buildDepPlugin");
32
- const writeCachePlugin_1 = require("./webpackPlugins/writeCachePlugin");
33
- class MFSU {
34
- constructor(opts) {
35
- this.alias = {};
36
- this.externals = [];
37
- this.depConfig = null;
38
- this.opts = opts;
39
- this.opts.mfName = this.opts.mfName || constants_1.DEFAULT_MF_NAME;
40
- this.opts.tmpBase =
41
- this.opts.tmpBase || (0, path_1.join)(process.cwd(), constants_1.DEFAULT_TMP_DIR_NAME);
42
- this.opts.mode = this.opts.mode || types_1.Mode.development;
43
- this.opts.getCacheDependency = this.opts.getCacheDependency || (() => ({}));
44
- this.opts.cwd = this.opts.cwd || process.cwd();
45
- this.depInfo = new depInfo_1.DepInfo({ mfsu: this });
46
- this.depBuilder = new depBuilder_1.DepBuilder({ mfsu: this });
47
- this.depInfo.loadCache();
48
- }
49
- setWebpackConfig(opts) {
50
- var _a;
51
- const { mfName } = this.opts;
52
- /**
53
- * config
54
- */
55
- // set alias and externals with reference for babel plugin
56
- Object.assign(this.alias, ((_a = opts.config.resolve) === null || _a === void 0 ? void 0 : _a.alias) || {});
57
- this.externals.push(...(0, makeArray_1.makeArray)(opts.config.externals || []));
58
- // entry
59
- const entry = {};
60
- const virtualModules = {};
61
- Object.keys(opts.config.entry).forEach((key) => {
62
- const virtualPath = `./mfsu-virtual-entry/${key}.js`;
63
- virtualModules[virtualPath] =
64
- // @ts-ignore
65
- opts.config
66
- .entry[key].map((entry) => `await import('${entry}')`)
67
- .join('\n') + `\nexport default 1;`;
68
- entry[key] = virtualPath;
69
- });
70
- opts.config.entry = entry;
71
- // plugins
72
- opts.config.plugins = opts.config.plugins || [];
73
- opts.config.plugins.push(...[
74
- new webpack_virtual_modules_1.default(virtualModules),
75
- new this.opts.implementor.container.ModuleFederationPlugin({
76
- name: '__',
77
- remotes: {
78
- // TODO: support runtime public path
79
- [mfName]: `${mfName}@${opts.config.output.publicPath}${constants_1.REMOTE_FILE_FULL}`,
80
- },
81
- }),
82
- new buildDepPlugin_1.BuildDepPlugin({
83
- onCompileDone: () => {
84
- this.buildDeps().catch((e) => {
85
- utils_1.logger.error(e);
86
- });
87
- },
88
- }),
89
- new writeCachePlugin_1.WriteCachePlugin({
90
- onWriteCache: () => {
91
- this.depInfo.writeCache();
92
- },
93
- }),
94
- ]);
95
- /**
96
- * depConfig
97
- */
98
- this.depConfig = opts.depConfig;
99
- }
100
- buildDeps() {
101
- return __awaiter(this, void 0, void 0, function* () {
102
- if (!this.depInfo.shouldBuild())
103
- return;
104
- this.depInfo.snapshot();
105
- const deps = dep_1.Dep.buildDeps({
106
- deps: this.depInfo.moduleGraph.depSnapshotModules,
107
- cwd: this.opts.cwd,
108
- mfsu: this,
109
- });
110
- yield this.depBuilder.build({
111
- deps,
112
- });
113
- });
114
- }
115
- getMiddlewares() {
116
- return [
117
- (req, res, next) => {
118
- const publicPath = '/';
119
- const isMF = req.path.startsWith(`${publicPath}${constants_1.MF_VA_PREFIX}`) ||
120
- req.path.startsWith(`${publicPath}${constants_1.MF_DEP_PREFIX}`) ||
121
- req.path.startsWith(`${publicPath}${constants_1.MF_STATIC_PREFIX}`);
122
- if (isMF) {
123
- this.depBuilder.onBuildComplete(() => {
124
- if (!req.path.includes(constants_1.REMOTE_FILE)) {
125
- res.setHeader('cache-control', 'max-age=31536000,immutable');
126
- }
127
- res.setHeader('content-type', (0, mrmime_1.lookup)((0, path_1.extname)(req.path)) || 'text/plain');
128
- const relativePath = req.path.replace(new RegExp(`^${publicPath}`), '/');
129
- const content = (0, fs_1.readFileSync)((0, path_1.join)(this.opts.tmpBase, relativePath), 'utf-8');
130
- res.send(content);
131
- });
132
- }
133
- else {
134
- next();
135
- }
136
- },
137
- ];
138
- }
139
- getBabelPlugins() {
140
- return [
141
- autoExport_1.default,
142
- [
143
- awaitImport_1.default,
144
- {
145
- onTransformDeps: () => { },
146
- onCollect: ({ file, data, }) => {
147
- this.depInfo.moduleGraph.onFileChange({
148
- file,
149
- // @ts-ignore
150
- deps: [
151
- ...Array.from(data.matched).map((item) => ({
152
- file: item.sourceValue,
153
- isDependency: true,
154
- version: dep_1.Dep.getDepVersion({
155
- dep: item.sourceValue,
156
- cwd: this.opts.cwd,
157
- }),
158
- })),
159
- ...Array.from(data.unMatched).map((item) => ({
160
- file: (0, getRealPath_1.getRealPath)({
161
- file,
162
- dep: item.sourceValue,
163
- }),
164
- isDependency: false,
165
- })),
166
- ],
167
- });
168
- },
169
- exportAllMembers: this.opts.exportAllMembers,
170
- unMatchLibs: this.opts.unMatchLibs,
171
- remoteName: this.opts.mfName,
172
- alias: this.alias,
173
- externals: this.externals,
174
- },
175
- ],
176
- ];
177
- }
178
- }
179
- exports.MFSU = MFSU;