@umijs/mfsu 4.0.0-canary.20220429.2 → 4.0.0-canary.20220506.1

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 (55) hide show
  1. package/dist/babelPlugins/awaitImport/awaitImport.d.ts +27 -0
  2. package/dist/babelPlugins/awaitImport/awaitImport.js +120 -0
  3. package/dist/babelPlugins/awaitImport/checkMatch.d.ts +18 -0
  4. package/dist/babelPlugins/awaitImport/checkMatch.js +127 -0
  5. package/dist/babelPlugins/awaitImport/getAliasedPath.d.ts +4 -0
  6. package/dist/babelPlugins/awaitImport/getAliasedPath.js +30 -0
  7. package/dist/babelPlugins/awaitImport/getRealPath.d.ts +4 -0
  8. package/dist/babelPlugins/awaitImport/getRealPath.js +24 -0
  9. package/dist/babelPlugins/awaitImport/isExternals.d.ts +11 -0
  10. package/dist/babelPlugins/awaitImport/isExternals.js +29 -0
  11. package/dist/babelPlugins/awaitImport/parseSpecifiers.d.ts +1 -0
  12. package/dist/babelPlugins/awaitImport/parseSpecifiers.js +55 -0
  13. package/dist/constants.d.ts +7 -0
  14. package/dist/constants.js +10 -0
  15. package/dist/dep/dep.d.ts +30 -0
  16. package/dist/dep/dep.js +96 -0
  17. package/dist/dep/getCJSExports.d.ts +3 -0
  18. package/dist/dep/getCJSExports.js +58 -0
  19. package/dist/dep/getExposeFromContent.d.ts +6 -0
  20. package/dist/dep/getExposeFromContent.js +69 -0
  21. package/dist/dep/getModuleExports.d.ts +7 -0
  22. package/dist/dep/getModuleExports.js +34 -0
  23. package/dist/depBuilder/depBuilder.d.ts +30 -0
  24. package/dist/depBuilder/depBuilder.js +164 -0
  25. package/dist/depBuilder/getESBuildEntry.d.ts +4 -0
  26. package/dist/depBuilder/getESBuildEntry.js +328 -0
  27. package/dist/depInfo.d.ts +17 -0
  28. package/dist/depInfo.js +50 -0
  29. package/dist/esbuildHandlers/autoCssModules.d.ts +2 -0
  30. package/dist/esbuildHandlers/autoCssModules.js +24 -0
  31. package/dist/esbuildHandlers/awaitImport/index.d.ts +12 -0
  32. package/dist/esbuildHandlers/awaitImport/index.js +44 -0
  33. package/dist/index.d.ts +4 -0
  34. package/dist/index.js +23 -0
  35. package/dist/loader/esbuild.d.ts +5 -0
  36. package/dist/loader/esbuild.js +38 -0
  37. package/dist/mfsu.d.ts +63 -0
  38. package/dist/mfsu.js +294 -0
  39. package/dist/moduleGraph.d.ts +73 -0
  40. package/dist/moduleGraph.js +197 -0
  41. package/dist/types.d.ts +16 -0
  42. package/dist/types.js +8 -0
  43. package/dist/utils/makeArray.d.ts +1 -0
  44. package/dist/utils/makeArray.js +7 -0
  45. package/dist/utils/trimFileContent.d.ts +1 -0
  46. package/dist/utils/trimFileContent.js +7 -0
  47. package/dist/webpackPlugins/buildDepPlugin.d.ts +10 -0
  48. package/dist/webpackPlugins/buildDepPlugin.js +17 -0
  49. package/dist/webpackPlugins/depChunkIdPrefixPlugin.d.ts +5 -0
  50. package/dist/webpackPlugins/depChunkIdPrefixPlugin.js +19 -0
  51. package/dist/webpackPlugins/stripSourceMapUrlPlugin.d.ts +10 -0
  52. package/dist/webpackPlugins/stripSourceMapUrlPlugin.js +28 -0
  53. package/dist/webpackPlugins/writeCachePlugin.d.ts +10 -0
  54. package/dist/webpackPlugins/writeCachePlugin.js +15 -0
  55. package/package.json +4 -4
@@ -0,0 +1,27 @@
1
+ import * as Babel from '@umijs/bundler-utils/compiled/babel/core';
2
+ import * as t from '@umijs/bundler-utils/compiled/babel/types';
3
+ export interface IOpts {
4
+ onTransformDeps?: Function;
5
+ onCollect?: Function;
6
+ exportAllMembers?: Record<string, string[]>;
7
+ unMatchLibs?: string[];
8
+ remoteName?: string;
9
+ alias?: Record<string, string>;
10
+ externals?: any;
11
+ }
12
+ export default function (): {
13
+ pre(): void;
14
+ post(state: any): void;
15
+ visitor: {
16
+ Program: {
17
+ exit(path: Babel.NodePath<t.Program>, { opts }: {
18
+ opts: IOpts;
19
+ }): void;
20
+ };
21
+ CallExpression: {
22
+ exit(path: Babel.NodePath<t.CallExpression>, { opts }: {
23
+ opts: IOpts;
24
+ }): void;
25
+ };
26
+ };
27
+ };
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
27
+ const checkMatch_1 = require("./checkMatch");
28
+ function default_1() {
29
+ return {
30
+ pre() {
31
+ // @ts-ignore
32
+ this.cache = new Map();
33
+ },
34
+ post(state) {
35
+ var _a, _b;
36
+ // @ts-ignore
37
+ const { cache } = this;
38
+ if (cache.has(state.opts.filename)) {
39
+ // @ts-ignore
40
+ (_b = (_a = this.opts).onCollect) === null || _b === void 0 ? void 0 : _b.call(_a, {
41
+ file: state.opts.filename,
42
+ data: cache.get(state.opts.filename),
43
+ });
44
+ }
45
+ },
46
+ visitor: {
47
+ Program: {
48
+ exit(path, { opts }) {
49
+ let index = path.node.body.length - 1;
50
+ while (index >= 0) {
51
+ const node = path.node.body[index];
52
+ // import x from 'x';
53
+ // import * as x from 'x';
54
+ // import x, * as xx from 'x';
55
+ // import { x } from 'x';
56
+ if (t.isImportDeclaration(node)) {
57
+ const { isMatch, replaceValue } = (0, checkMatch_1.checkMatch)({
58
+ // @ts-ignore
59
+ cache: this.cache,
60
+ value: node.source.value,
61
+ opts,
62
+ path,
63
+ });
64
+ if (isMatch) {
65
+ node.source.value = replaceValue;
66
+ }
67
+ }
68
+ // export * from 'x';
69
+ else if (t.isExportAllDeclaration(node)) {
70
+ const { isMatch, replaceValue } = (0, checkMatch_1.checkMatch)({
71
+ // @ts-ignore
72
+ cache: this.cache,
73
+ value: node.source.value,
74
+ opts,
75
+ path,
76
+ });
77
+ if (isMatch) {
78
+ node.source.value = replaceValue;
79
+ }
80
+ }
81
+ // export { x } from 'x';
82
+ else if (t.isExportNamedDeclaration(node) && node.source) {
83
+ const { isMatch, replaceValue } = (0, checkMatch_1.checkMatch)({
84
+ // @ts-ignore
85
+ cache: this.cache,
86
+ value: node.source.value,
87
+ opts,
88
+ path,
89
+ });
90
+ if (isMatch) {
91
+ node.source.value = replaceValue;
92
+ }
93
+ }
94
+ index -= 1;
95
+ }
96
+ },
97
+ },
98
+ CallExpression: {
99
+ exit(path, { opts }) {
100
+ const { node } = path;
101
+ if (t.isImport(node.callee) &&
102
+ node.arguments.length === 1 &&
103
+ node.arguments[0].type === 'StringLiteral') {
104
+ const { isMatch, replaceValue } = (0, checkMatch_1.checkMatch)({
105
+ // @ts-ignore
106
+ cache: this.cache,
107
+ value: node.arguments[0].value,
108
+ opts,
109
+ path,
110
+ });
111
+ if (isMatch) {
112
+ node.arguments[0] = t.stringLiteral(replaceValue);
113
+ }
114
+ }
115
+ },
116
+ },
117
+ },
118
+ };
119
+ }
120
+ exports.default = default_1;
@@ -0,0 +1,18 @@
1
+ import * as Babel from '@umijs/bundler-utils/compiled/babel/core';
2
+ import type { IOpts } from './awaitImport';
3
+ export declare function checkMatch({ value, path, opts, isExportAll, depth, cache, filename, }: {
4
+ value: string;
5
+ path?: Babel.NodePath;
6
+ opts?: IOpts;
7
+ isExportAll?: boolean;
8
+ depth?: number;
9
+ cache?: Map<string, any>;
10
+ filename?: string;
11
+ }): {
12
+ isMatch: boolean;
13
+ replaceValue: string;
14
+ };
15
+ export declare function getPath({ value, opts }: {
16
+ value: string;
17
+ opts: IOpts;
18
+ }): string;
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getPath = exports.checkMatch = void 0;
7
+ const utils_1 = require("@umijs/utils");
8
+ const assert_1 = __importDefault(require("assert"));
9
+ const path_1 = require("path");
10
+ const getAliasedPath_1 = require("./getAliasedPath");
11
+ const isExternals_1 = require("./isExternals");
12
+ // const UNMATCH_LIBS = ['umi', 'dumi', '@alipay/bigfish'];
13
+ const RE_NODE_MODULES = /node_modules/;
14
+ const RE_UMI_LOCAL_DEV = /umi(-next)?\/packages\//;
15
+ function isUmiLocalDev(path) {
16
+ return RE_UMI_LOCAL_DEV.test((0, utils_1.winPath)(path));
17
+ }
18
+ function checkMatch({ value, path, opts, isExportAll, depth, cache, filename, }) {
19
+ var _a, _b;
20
+ let isMatch;
21
+ let replaceValue = '';
22
+ depth = depth || 1;
23
+ (0, assert_1.default)(depth <= 10, `endless loop detected in checkMatch, please check your alias config.`);
24
+ opts = opts || {};
25
+ const remoteName = opts.remoteName || 'mf';
26
+ // FIXME: hard code for vite mode
27
+ value = value.replace(/^@fs\//, '/');
28
+ if (
29
+ // unMatch specified libs
30
+ ((_a = opts.unMatchLibs) === null || _a === void 0 ? void 0 : _a.includes(value)) ||
31
+ // do not match bundler-webpack/client/client/client.js
32
+ value.includes('client/client/client.js') ||
33
+ // already handled
34
+ value.startsWith(`${remoteName}/`) ||
35
+ // don't match dynamic path
36
+ // e.g. @umijs/deps/compiled/babel/svgr-webpack.js?-svgo,+titleProp,+ref!./umi.svg
37
+ (0, utils_1.winPath)(value).includes('babel/svgr-webpack') ||
38
+ // don't match webpack loader
39
+ // e.g. !!dumi-raw-code-loader!/path/to/VerticalProgress/index.module.less?dumi-raw-code
40
+ value.startsWith('!!') ||
41
+ // don't match externals
42
+ (0, isExternals_1.isExternals)({ value, externals: opts.externals }) ||
43
+ // relative import
44
+ value.startsWith('.')) {
45
+ isMatch = false;
46
+ }
47
+ else if ((0, path_1.isAbsolute)(value)) {
48
+ isMatch = RE_NODE_MODULES.test(value) || isUmiLocalDev(value);
49
+ }
50
+ else {
51
+ const aliasedPath = (0, getAliasedPath_1.getAliasedPath)({
52
+ value,
53
+ alias: opts.alias || {},
54
+ });
55
+ if (aliasedPath) {
56
+ return checkMatch({
57
+ value: aliasedPath,
58
+ path,
59
+ opts,
60
+ isExportAll,
61
+ depth: depth + 1,
62
+ cache,
63
+ filename,
64
+ });
65
+ }
66
+ else {
67
+ isMatch = true;
68
+ }
69
+ }
70
+ if (isMatch && isExportAll) {
71
+ isMatch = !!(opts.exportAllMembers && value in opts.exportAllMembers);
72
+ }
73
+ if (isMatch) {
74
+ replaceValue = `${remoteName}/${(0, utils_1.winPath)(value)}`;
75
+ }
76
+ // @ts-ignore
77
+ const file = (path === null || path === void 0 ? void 0 : path.hub.file.opts.filename) || filename;
78
+ (_b = opts.onTransformDeps) === null || _b === void 0 ? void 0 : _b.call(opts, {
79
+ sourceValue: value,
80
+ replaceValue,
81
+ isMatch,
82
+ file,
83
+ });
84
+ if (cache) {
85
+ let mod;
86
+ if (cache.has(file)) {
87
+ mod = cache.get(file);
88
+ }
89
+ else {
90
+ mod = {
91
+ matched: new Set(),
92
+ unMatched: new Set(),
93
+ };
94
+ cache.set(file, mod);
95
+ }
96
+ mod[isMatch ? 'matched' : 'unMatched'].add({
97
+ sourceValue: value,
98
+ replaceValue,
99
+ file,
100
+ });
101
+ }
102
+ // console.log(
103
+ // '> check',
104
+ // // @ts-ignore
105
+ // path.hub.file.opts.filename,
106
+ // value,
107
+ // cache,
108
+ // 'isMatch',
109
+ // isMatch,
110
+ // );
111
+ return {
112
+ isMatch,
113
+ replaceValue,
114
+ };
115
+ }
116
+ exports.checkMatch = checkMatch;
117
+ // TODO: REMOVE ME
118
+ function getPath({ value, opts }) {
119
+ const alias = opts.alias || {};
120
+ for (const key of Object.keys(alias)) {
121
+ if (value.startsWith(key)) {
122
+ return value.replace(key, alias[key]);
123
+ }
124
+ }
125
+ return value;
126
+ }
127
+ exports.getPath = getPath;
@@ -0,0 +1,4 @@
1
+ export declare function getAliasedPath({ value, alias, }: {
2
+ value: string;
3
+ alias: Record<string, string>;
4
+ }): string | undefined;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAliasedPath = void 0;
4
+ function getAliasedPath({ value, alias, }) {
5
+ const importValue = value;
6
+ // equal alias
7
+ if (alias[value]) {
8
+ return alias[value];
9
+ }
10
+ for (const key of Object.keys(alias)) {
11
+ const aliasValue = alias[key];
12
+ // exact alias
13
+ // ref: https://webpack.js.org/configuration/resolve/#resolvealias
14
+ // e.g. foo$: path/to/foo
15
+ if (key.endsWith('$')) {
16
+ if (importValue === key.slice(0, -1))
17
+ return aliasValue;
18
+ else
19
+ continue;
20
+ }
21
+ // e.g. foo: path/to/foo.js
22
+ if (importValue.startsWith(addLastSlash(key))) {
23
+ return importValue.replace(key, aliasValue);
24
+ }
25
+ }
26
+ }
27
+ exports.getAliasedPath = getAliasedPath;
28
+ function addLastSlash(path) {
29
+ return path.endsWith('/') ? path : `${path}/`;
30
+ }
@@ -0,0 +1,4 @@
1
+ export declare function getRealPath(opts: {
2
+ file: string;
3
+ dep: string;
4
+ }): string | null;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRealPath = void 0;
4
+ const fs_1 = require("fs");
5
+ const path_1 = require("path");
6
+ const FILE_EXT_NAMES = ['.tsx', '.ts', '.jsx', '.mjs', '.js'];
7
+ function getRealPath(opts) {
8
+ const target = (0, path_1.isAbsolute)(opts.dep)
9
+ ? opts.dep
10
+ : (0, path_1.join)((0, path_1.dirname)(opts.file), opts.dep);
11
+ if (FILE_EXT_NAMES.includes((0, path_1.extname)(target))) {
12
+ return target;
13
+ }
14
+ else {
15
+ for (const fileExtName of FILE_EXT_NAMES) {
16
+ const targetWithExtName = `${target}${fileExtName}`;
17
+ if ((0, fs_1.existsSync)(targetWithExtName)) {
18
+ return targetWithExtName;
19
+ }
20
+ }
21
+ return null;
22
+ }
23
+ }
24
+ exports.getRealPath = getRealPath;
@@ -0,0 +1,11 @@
1
+ declare type IExternal = Record<string, string> | Function;
2
+ declare type IExternals = IExternal[] | IExternal;
3
+ export declare function isExternals({ value, externals, }: {
4
+ value: string;
5
+ externals: IExternals;
6
+ }): boolean;
7
+ export declare function isExternal({ value, external, }: {
8
+ value: string;
9
+ external: IExternal;
10
+ }): boolean;
11
+ export {};
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isExternal = exports.isExternals = void 0;
4
+ function isExternals({ value, externals, }) {
5
+ const externalsArr = Array.isArray(externals) ? externals : [externals];
6
+ for (const external of externalsArr) {
7
+ if (isExternal({ value, external })) {
8
+ return true;
9
+ }
10
+ }
11
+ return false;
12
+ }
13
+ exports.isExternals = isExternals;
14
+ function isExternal({ value, external, }) {
15
+ if (typeof external === 'object') {
16
+ return !!external[value];
17
+ }
18
+ else if (typeof external === 'function') {
19
+ let ret = false;
20
+ external({}, value, (_, b) => {
21
+ ret = !!b;
22
+ });
23
+ return ret;
24
+ }
25
+ else {
26
+ return false;
27
+ }
28
+ }
29
+ exports.isExternal = isExternal;
@@ -0,0 +1 @@
1
+ export declare function parseSpecifiers(specifiers: any[]): any;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.parseSpecifiers = void 0;
27
+ const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
28
+ const DEFAULT = 'default';
29
+ function parseSpecifiers(specifiers) {
30
+ return specifiers.reduce((memo, s) => {
31
+ if (t.isImportDefaultSpecifier(s)) {
32
+ memo.properties.push(t.objectProperty(t.identifier(DEFAULT), s.local));
33
+ }
34
+ else if (t.isExportDefaultSpecifier(s)) {
35
+ memo.properties.push(t.objectProperty(t.identifier(DEFAULT), s.exported));
36
+ }
37
+ else if (t.isExportSpecifier(s)) {
38
+ if (t.isIdentifier(s.exported, { name: DEFAULT })) {
39
+ memo.defaultIdentifier = s.local.name;
40
+ memo.properties.push(t.objectProperty(s.local, s.local));
41
+ }
42
+ else {
43
+ memo.properties.push(t.objectProperty(s.local, s.exported));
44
+ }
45
+ }
46
+ else if (t.isImportNamespaceSpecifier(s)) {
47
+ memo.namespaceIdentifier = s.local;
48
+ }
49
+ else {
50
+ memo.properties.push(t.objectProperty(s.imported, s.local));
51
+ }
52
+ return memo;
53
+ }, { properties: [], namespaceIdentifier: null, defaultIdentifier: null });
54
+ }
55
+ exports.parseSpecifiers = parseSpecifiers;
@@ -0,0 +1,7 @@
1
+ export declare const MF_VA_PREFIX = "mf-va_";
2
+ export declare const MF_DEP_PREFIX = "mf-dep_";
3
+ export declare const MF_STATIC_PREFIX = "mf-static/";
4
+ export declare const DEFAULT_MF_NAME = "mf";
5
+ export declare const DEFAULT_TMP_DIR_NAME = ".mfsu";
6
+ export declare const REMOTE_FILE = "remoteEntry.js";
7
+ export declare const REMOTE_FILE_FULL: string;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.REMOTE_FILE_FULL = exports.REMOTE_FILE = exports.DEFAULT_TMP_DIR_NAME = exports.DEFAULT_MF_NAME = exports.MF_STATIC_PREFIX = exports.MF_DEP_PREFIX = exports.MF_VA_PREFIX = void 0;
4
+ exports.MF_VA_PREFIX = 'mf-va_';
5
+ exports.MF_DEP_PREFIX = 'mf-dep_';
6
+ exports.MF_STATIC_PREFIX = 'mf-static/';
7
+ exports.DEFAULT_MF_NAME = 'mf';
8
+ exports.DEFAULT_TMP_DIR_NAME = '.mfsu';
9
+ exports.REMOTE_FILE = 'remoteEntry.js';
10
+ exports.REMOTE_FILE_FULL = `${exports.MF_VA_PREFIX}${exports.REMOTE_FILE}`;
@@ -0,0 +1,30 @@
1
+ import { MFSU } from '../mfsu';
2
+ export declare class Dep {
3
+ file: string;
4
+ version: string;
5
+ cwd: string;
6
+ shortFile: string;
7
+ normalizedFile: string;
8
+ filePath: string;
9
+ mfsu: MFSU;
10
+ constructor(opts: {
11
+ file: string;
12
+ version: string;
13
+ cwd: string;
14
+ mfsu: MFSU;
15
+ });
16
+ buildExposeContent(): Promise<string>;
17
+ getRealFile(): Promise<string | null>;
18
+ static buildDeps(opts: {
19
+ deps: Record<string, {
20
+ file: string;
21
+ version: string;
22
+ }>;
23
+ cwd: string;
24
+ mfsu: MFSU;
25
+ }): Dep[];
26
+ static getDepVersion(opts: {
27
+ dep: string;
28
+ cwd: string;
29
+ }): string;
30
+ }
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Dep = void 0;
7
+ const utils_1 = require("@umijs/utils");
8
+ const assert_1 = __importDefault(require("assert"));
9
+ const enhanced_resolve_1 = __importDefault(require("enhanced-resolve"));
10
+ const fs_1 = require("fs");
11
+ const path_1 = require("path");
12
+ const constants_1 = require("../constants");
13
+ const trimFileContent_1 = require("../utils/trimFileContent");
14
+ const getExposeFromContent_1 = require("./getExposeFromContent");
15
+ const resolver = enhanced_resolve_1.default.create({
16
+ mainFields: ['module', 'browser', 'main'],
17
+ extensions: ['.js', '.json', '.mjs'],
18
+ exportsFields: ['exports'],
19
+ conditionNames: ['import', 'module', 'require', 'node'],
20
+ });
21
+ async function resolve(context, path) {
22
+ return new Promise((resolve, reject) => {
23
+ resolver(context, path, (err, result) => err ? reject(err) : resolve(result));
24
+ });
25
+ }
26
+ class Dep {
27
+ constructor(opts) {
28
+ this.file = (0, utils_1.winPath)(opts.file);
29
+ this.version = opts.version;
30
+ this.cwd = opts.cwd;
31
+ this.shortFile = this.file;
32
+ this.normalizedFile = this.shortFile.replace(/\//g, '_').replace(/:/g, '_');
33
+ this.filePath = `${constants_1.MF_VA_PREFIX}${this.normalizedFile}.js`;
34
+ this.mfsu = opts.mfsu;
35
+ }
36
+ async buildExposeContent() {
37
+ // node natives
38
+ // @ts-ignore
39
+ const isNodeNatives = !!process.binding('natives')[this.file];
40
+ if (isNodeNatives) {
41
+ return (0, trimFileContent_1.trimFileContent)(this.mfsu.opts.excludeNodeNatives
42
+ ? `
43
+ const _ = require('${this.file}');
44
+ module.exports = _;
45
+ `
46
+ : `
47
+ import _ from '${this.file}';
48
+ export default _;
49
+ export * from '${this.file}';
50
+ `);
51
+ }
52
+ // none node natives
53
+ const realFile = await this.getRealFile();
54
+ (0, assert_1.default)(realFile, `filePath not found of ${this.file}`);
55
+ const content = (0, fs_1.readFileSync)(realFile, 'utf-8');
56
+ return await (0, getExposeFromContent_1.getExposeFromContent)({
57
+ content,
58
+ filePath: realFile,
59
+ dep: this,
60
+ });
61
+ }
62
+ async getRealFile() {
63
+ try {
64
+ // don't need to handle alias here
65
+ // it's already handled by babel plugin
66
+ return await resolve(this.cwd, this.file);
67
+ }
68
+ catch (e) {
69
+ return null;
70
+ }
71
+ }
72
+ static buildDeps(opts) {
73
+ return Object.keys(opts.deps).map((file) => {
74
+ return new Dep({
75
+ ...opts.deps[file],
76
+ cwd: opts.cwd,
77
+ mfsu: opts.mfsu,
78
+ });
79
+ });
80
+ }
81
+ static getDepVersion(opts) {
82
+ // @ts-ignore
83
+ if (!!process.binding('natives')[opts.dep]) {
84
+ return '*';
85
+ }
86
+ const dep = (0, path_1.isAbsolute)(opts.dep)
87
+ ? opts.dep
88
+ : (0, path_1.join)(opts.cwd, 'node_modules', opts.dep);
89
+ const pkg = utils_1.pkgUp.pkgUpSync({
90
+ cwd: dep,
91
+ });
92
+ (0, assert_1.default)(pkg, `package.json not found for ${opts.dep}`);
93
+ return require(pkg).version;
94
+ }
95
+ }
96
+ exports.Dep = Dep;
@@ -0,0 +1,3 @@
1
+ export declare function getCJSExports({ content }: {
2
+ content: string;
3
+ }): string[];
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCJSExports = void 0;
4
+ function getCJSExports({ content }) {
5
+ return matchAll(/Object\.defineProperty\(\s*exports\s*\,\s*[\"|\'](\w+)[\"|\']/g, content)
6
+ .concat(
7
+ // Support export['default']
8
+ // ref: https://unpkg.alibaba-inc.com/browse/echarts-for-react@2.0.16/lib/core.js
9
+ matchAll(/exports(?:\.|\[(?:\'|\"))(\w+)(?:\s*|(?:\'|\")\])\s*\=/g, content))
10
+ .concat(
11
+ // Support __webpack_exports__["default"]
12
+ // ref: https://github.com/margox/braft-editor/blob/master/dist/index.js#L8429
13
+ matchAll(/__webpack_exports__\[(?:\"|\')(\w+)(?:\"|\')\]\s*\=/g, content))
14
+ .concat(
15
+ // Support __webpack_require__.d(__webpack_exports, "EditorState")
16
+ // ref: https://github.com/margox/braft-editor/blob/master/dist/index.js#L8347
17
+ matchAll(/__webpack_require__\.d\(\s*__webpack_exports__\s*,\s*(?:\"|\')(\w+)(?:\"|\')\s*,/g, content))
18
+ .concat(
19
+ // Support __webpack_require__.d(__webpack_exports__, {"default": function() { return /* binding */ clipboard; }});
20
+ // ref: https://unpkg.alibaba-inc.com/browse/clipboard@2.0.8/dist/clipboard.js L26
21
+ ...matchAll(/__webpack_require__\.d\(\s*__webpack_exports__\s*,\s*(\{)/g, content).map((matchResult) => {
22
+ const { index } = matchResult;
23
+ let idx = index;
24
+ let deep = 0;
25
+ let isMeetSymbol = false;
26
+ let symbolBeginIndex = index;
27
+ while (idx < content.length) {
28
+ if (!deep && isMeetSymbol) {
29
+ break;
30
+ }
31
+ if (content[idx] === '{') {
32
+ if (!isMeetSymbol) {
33
+ isMeetSymbol = true;
34
+ symbolBeginIndex = idx;
35
+ }
36
+ deep++;
37
+ }
38
+ if (content[idx] === '}') {
39
+ deep--;
40
+ }
41
+ idx++;
42
+ }
43
+ let result = content.slice(symbolBeginIndex, idx);
44
+ return [
45
+ ...matchAll(/(?:\"|\')(\w+)(?:\"|\')\s*\:\s*(?:function|\()/g, result),
46
+ ];
47
+ }))
48
+ .map((result) => result[1]);
49
+ }
50
+ exports.getCJSExports = getCJSExports;
51
+ function matchAll(regexp, str) {
52
+ const result = [];
53
+ let match;
54
+ while ((match = regexp.exec(str)) !== null) {
55
+ result.push(match);
56
+ }
57
+ return result;
58
+ }