@umijs/preset-umi 4.0.0-beta.13 → 4.0.0-beta.17
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.
- package/dist/commands/build.js +1 -1
- package/dist/commands/dev/createRouteMiddleware.js +2 -2
- package/dist/commands/dev/dev.js +9 -3
- package/dist/commands/dev/getMarkupArgs.d.ts +2 -0
- package/dist/commands/dev/getMarkupArgs.js +3 -0
- package/dist/commands/generate/prettier.js +35 -3
- package/dist/features/appData/appData.js +27 -0
- package/dist/features/configPlugins/configPlugins.js +10 -3
- package/dist/features/configPlugins/schema.js +16 -4
- package/dist/features/esmi/Service.js +1 -0
- package/dist/features/esmi/esbuildPlugins/requireToImport.d.ts +8 -0
- package/dist/features/esmi/esbuildPlugins/requireToImport.js +61 -0
- package/dist/features/esmi/esbuildPlugins/topLevelExternal.d.ts +13 -0
- package/dist/features/esmi/esbuildPlugins/topLevelExternal.js +37 -0
- package/dist/features/esmi/esmi.js +60 -15
- package/dist/features/favicon/favicon.js +3 -1
- package/dist/features/lowImport/babelPlugin.d.ts +2 -0
- package/dist/features/lowImport/babelPlugin.js +28 -6
- package/dist/features/lowImport/lowImport.js +8 -3
- package/dist/features/mock/getMockData.d.ts +4 -0
- package/dist/features/mock/getMockData.js +14 -5
- package/dist/features/mock/mock.js +9 -2
- package/dist/features/polyfill/polyfill.js +51 -9
- package/dist/features/polyfill/publicPathPolyfill.d.ts +3 -0
- package/dist/features/polyfill/publicPathPolyfill.js +14 -0
- package/dist/features/tmpFiles/routes.js +34 -3
- package/dist/features/tmpFiles/tmpFiles.js +103 -9
- package/dist/features/vite/vite.d.ts +3 -0
- package/dist/features/vite/vite.js +34 -0
- package/dist/index.js +3 -0
- package/dist/libs/scan.d.ts +5 -1
- package/dist/libs/scan.js +58 -4
- package/dist/registerMethods.js +7 -2
- package/dist/types.d.ts +4 -0
- package/dist/utils/transformIEAR.d.ts +22 -0
- package/dist/utils/transformIEAR.js +101 -0
- package/package.json +14 -13
- package/templates/history.tpl +15 -0
- package/templates/umi.tpl +17 -2
- package/dist/libs/moduleGraph.d.ts +0 -2
- package/dist/libs/moduleGraph.js +0 -6
package/dist/commands/build.js
CHANGED
|
@@ -110,7 +110,7 @@ umi build --clean
|
|
|
110
110
|
const { vite } = api.args;
|
|
111
111
|
const markupArgs = yield (0, getMarkupArgs_1.getMarkupArgs)({ api });
|
|
112
112
|
// @ts-ignore
|
|
113
|
-
const markup = yield (0, server_1.getMarkup)(Object.assign(Object.assign({}, markupArgs), { scripts: ['/umi.js'].concat(markupArgs.scripts), esmScript: !!opts.config.esm || vite, path: '/' }));
|
|
113
|
+
const markup = yield (0, server_1.getMarkup)(Object.assign(Object.assign({}, markupArgs), { styles: ['/umi.css'].concat(markupArgs.styles), scripts: ['/umi.js'].concat(markupArgs.scripts), esmScript: !!opts.config.esm || vite, path: '/' }));
|
|
114
114
|
(0, fs_1.writeFileSync)((0, path_1.join)(api.paths.absOutputPath, 'index.html'), markup, 'utf-8');
|
|
115
115
|
utils_1.logger.event('build index.html');
|
|
116
116
|
// print size
|
|
@@ -28,11 +28,11 @@ function createRouteMiddleware(opts) {
|
|
|
28
28
|
// add noshim attr for skip importmap shim logic for this modules
|
|
29
29
|
{ content: viteRefreshScript, noshim: '' },
|
|
30
30
|
{ src: '/@vite/client', noshim: '' },
|
|
31
|
-
'/.umi/umi.ts',
|
|
31
|
+
opts.api.appData.hasSrcDir ? '/src/.umi/umi.ts' : '/.umi/umi.ts',
|
|
32
32
|
];
|
|
33
33
|
const markupArgs = yield (0, getMarkupArgs_1.getMarkupArgs)(opts);
|
|
34
34
|
// @ts-ignore
|
|
35
|
-
const requestHandler = yield (0, server_1.createRequestHandler)(Object.assign(Object.assign({}, markupArgs), { scripts: (vite ? viteScripts : ['/umi.js']).concat(markupArgs.scripts), esmScript: vite }));
|
|
35
|
+
const requestHandler = yield (0, server_1.createRequestHandler)(Object.assign(Object.assign({}, markupArgs), { styles: ['/umi.css'].concat(markupArgs.styles), scripts: (vite ? viteScripts : ['/umi.js']).concat(markupArgs.scripts), esmScript: vite }));
|
|
36
36
|
requestHandler(req, res, next);
|
|
37
37
|
});
|
|
38
38
|
}
|
package/dist/commands/dev/dev.js
CHANGED
|
@@ -56,7 +56,7 @@ umi dev
|
|
|
56
56
|
PORT=8888 umi dev
|
|
57
57
|
`,
|
|
58
58
|
fn() {
|
|
59
|
-
var _a;
|
|
59
|
+
var _a, _b;
|
|
60
60
|
return __awaiter(this, void 0, void 0, function* () {
|
|
61
61
|
const enableVite = api.args.vite;
|
|
62
62
|
// clear tmp except cache
|
|
@@ -102,9 +102,10 @@ PORT=8888 umi dev
|
|
|
102
102
|
key: 'addTmpGenerateWatcherPaths',
|
|
103
103
|
initialValue: [
|
|
104
104
|
absPagesPath,
|
|
105
|
+
!api.config.routes && ((_a = api.config.conventionRoutes) === null || _a === void 0 ? void 0 : _a.base),
|
|
105
106
|
(0, path_1.join)(absSrcPath, 'layouts'),
|
|
106
107
|
...(0, watch_1.expandJSPaths)((0, path_1.join)(absSrcPath, 'app')),
|
|
107
|
-
],
|
|
108
|
+
].filter(Boolean),
|
|
108
109
|
});
|
|
109
110
|
utils_1.lodash.uniq(watcherPaths.map(utils_1.winPath)).forEach((p) => {
|
|
110
111
|
(0, watch_1.watch)({
|
|
@@ -238,7 +239,12 @@ PORT=8888 umi dev
|
|
|
238
239
|
key: 'onDevCompileDone',
|
|
239
240
|
args: opts,
|
|
240
241
|
});
|
|
241
|
-
}, mfsuWithESBuild: (
|
|
242
|
+
}, mfsuWithESBuild: (_b = api.config.mfsu) === null || _b === void 0 ? void 0 : _b.esbuild, cache: {
|
|
243
|
+
buildDependencies: [
|
|
244
|
+
api.pkgPath,
|
|
245
|
+
api.service.configManager.mainConfigFile || '',
|
|
246
|
+
].filter(Boolean),
|
|
247
|
+
} });
|
|
242
248
|
if (enableVite) {
|
|
243
249
|
yield bundlerVite.dev(opts);
|
|
244
250
|
}
|
|
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.getMarkupArgs = void 0;
|
|
16
16
|
const cheerio_1 = __importDefault(require("@umijs/utils/compiled/cheerio"));
|
|
17
17
|
function getMarkupArgs(opts) {
|
|
18
|
+
var _a;
|
|
18
19
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19
20
|
const headScripts = yield opts.api.applyPlugins({
|
|
20
21
|
key: 'addHTMLHeadScripts',
|
|
@@ -41,6 +42,8 @@ function getMarkupArgs(opts) {
|
|
|
41
42
|
initialValue: opts.api.config.favicon,
|
|
42
43
|
});
|
|
43
44
|
return {
|
|
45
|
+
mountElementId: opts.api.config.mountElementId,
|
|
46
|
+
base: ((_a = opts.api.config.history) === null || _a === void 0 ? void 0 : _a.type) === 'browser' ? opts.api.config.base : '/',
|
|
44
47
|
routes: opts.api.appData.routes,
|
|
45
48
|
favicon,
|
|
46
49
|
headScripts,
|
|
@@ -8,8 +8,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
const core_1 = require("@umijs/core");
|
|
16
|
+
const utils_1 = require("@umijs/utils");
|
|
17
|
+
const assert_1 = __importDefault(require("assert"));
|
|
13
18
|
const fs_1 = require("fs");
|
|
14
19
|
const path_1 = require("path");
|
|
15
20
|
exports.default = (api) => {
|
|
@@ -23,9 +28,36 @@ exports.default = (api) => {
|
|
|
23
28
|
// 存在 .prettierrc,不开启
|
|
24
29
|
return !(0, fs_1.existsSync)((0, path_1.join)(api.paths.cwd, '.prettierrc'));
|
|
25
30
|
},
|
|
26
|
-
fn: (
|
|
27
|
-
//
|
|
28
|
-
|
|
31
|
+
fn: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
32
|
+
// 1、修改 package.json,加上 prettier 和插件
|
|
33
|
+
api.pkg.devDependencies = Object.assign(Object.assign({}, api.pkg.devDependencies), { prettier: '^2', 'prettier-plugin-organize-imports': '^2', 'prettier-plugin-packagejson': '^2' });
|
|
34
|
+
(0, fs_1.writeFileSync)(api.pkgPath, JSON.stringify(api.pkg, null, 2));
|
|
35
|
+
utils_1.logger.info('Write package.json');
|
|
36
|
+
// 2、添加 .prettierrc 和 .prettierignore
|
|
37
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(api.cwd, '.prettierrc'), `
|
|
38
|
+
{
|
|
39
|
+
"printWidth": 80,
|
|
40
|
+
"singleQuote": true,
|
|
41
|
+
"trailingComma": "all",
|
|
42
|
+
"proseWrap": "never",
|
|
43
|
+
"overrides": [{ "files": ".prettierrc", "options": { "parser": "json" } }],
|
|
44
|
+
"plugins": ["prettier-plugin-organize-imports", "prettier-plugin-packagejson"]
|
|
45
|
+
}
|
|
46
|
+
`.trimLeft());
|
|
47
|
+
utils_1.logger.info('Write .prettierrc');
|
|
48
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(api.cwd, '.prettierignore'), `
|
|
49
|
+
node_modules
|
|
50
|
+
.umi
|
|
51
|
+
.umi-production
|
|
52
|
+
`.trimLeft());
|
|
53
|
+
utils_1.logger.info('Write .prettierignore');
|
|
54
|
+
// 3、执行 npm install
|
|
55
|
+
const npmClient = api.userConfig.npmClient;
|
|
56
|
+
(0, assert_1.default)(npmClient, `npmClient is required in your config.`);
|
|
57
|
+
(0, utils_1.installWithNpmClient)({
|
|
58
|
+
npmClient,
|
|
59
|
+
});
|
|
60
|
+
utils_1.logger.info(`Install dependencies with ${npmClient}`);
|
|
29
61
|
}),
|
|
30
62
|
});
|
|
31
63
|
};
|
|
@@ -14,6 +14,7 @@ const utils_1 = require("@umijs/utils");
|
|
|
14
14
|
const fs_1 = require("fs");
|
|
15
15
|
const path_1 = require("path");
|
|
16
16
|
const watch_1 = require("../../commands/dev/watch");
|
|
17
|
+
const scan_1 = require("../../libs/scan");
|
|
17
18
|
const routes_1 = require("../tmpFiles/routes");
|
|
18
19
|
exports.default = (api) => {
|
|
19
20
|
api.modifyAppData((memo) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -29,6 +30,7 @@ exports.default = (api) => {
|
|
|
29
30
|
version: require((0, path_1.join)(api.config.alias.react, 'package.json')).version,
|
|
30
31
|
};
|
|
31
32
|
memo.appJS = yield getAppJsInfo();
|
|
33
|
+
memo.vite = api.args.vite ? {} : undefined;
|
|
32
34
|
return memo;
|
|
33
35
|
}));
|
|
34
36
|
// Execute earliest, so that other onGenerateFiles can get it
|
|
@@ -43,6 +45,31 @@ exports.default = (api) => {
|
|
|
43
45
|
},
|
|
44
46
|
stage: Number.NEGATIVE_INFINITY,
|
|
45
47
|
});
|
|
48
|
+
// used in esmi and vite
|
|
49
|
+
api.register({
|
|
50
|
+
key: 'updateAppDataDeps',
|
|
51
|
+
fn() {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const resolver = (0, scan_1.createResolver)({
|
|
54
|
+
alias: api.config.alias,
|
|
55
|
+
});
|
|
56
|
+
api.appData.deps = yield (0, scan_1.scan)({
|
|
57
|
+
entry: (0, path_1.join)(api.paths.absTmpPath, 'umi.ts'),
|
|
58
|
+
externals: api.config.externals,
|
|
59
|
+
resolver,
|
|
60
|
+
});
|
|
61
|
+
// FIXME: force include react & react-dom
|
|
62
|
+
if (api.appData.deps['react']) {
|
|
63
|
+
api.appData.deps['react'].version = api.appData.react.version;
|
|
64
|
+
}
|
|
65
|
+
api.appData.deps['react-dom'] = {
|
|
66
|
+
version: api.appData.react.version,
|
|
67
|
+
matches: ['react-dom'],
|
|
68
|
+
subpaths: [],
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
});
|
|
46
73
|
function getAppJsInfo() {
|
|
47
74
|
return __awaiter(this, void 0, void 0, function* () {
|
|
48
75
|
for (const path of (0, watch_1.expandJSPaths)((0, path_1.join)(api.paths.absSrcPath, 'app'))) {
|
|
@@ -16,8 +16,7 @@ function resolveProjectDep(opts) {
|
|
|
16
16
|
exports.default = (api) => {
|
|
17
17
|
const configDefaults = {
|
|
18
18
|
alias: {
|
|
19
|
-
umi:
|
|
20
|
-
'@umijs/renderer-react': (0, path_1.dirname)(require.resolve('@umijs/renderer-react/package.json')),
|
|
19
|
+
umi: '@@/exports',
|
|
21
20
|
react: resolveProjectDep({
|
|
22
21
|
pkg: api.pkg,
|
|
23
22
|
cwd: api.cwd,
|
|
@@ -34,6 +33,12 @@ exports.default = (api) => {
|
|
|
34
33
|
externals: {},
|
|
35
34
|
autoCSSModules: true,
|
|
36
35
|
publicPath: '/',
|
|
36
|
+
mountElementId: 'root',
|
|
37
|
+
base: '/',
|
|
38
|
+
history: { type: 'browser' },
|
|
39
|
+
targets: {
|
|
40
|
+
chrome: 87,
|
|
41
|
+
},
|
|
37
42
|
};
|
|
38
43
|
const bundleSchemas = (0, schema_1.getSchemas)();
|
|
39
44
|
const extraSchemas = (0, schema_2.getSchemas)();
|
|
@@ -55,7 +60,9 @@ exports.default = (api) => {
|
|
|
55
60
|
}
|
|
56
61
|
// api.paths is ready after register
|
|
57
62
|
api.modifyConfig((memo, args) => {
|
|
58
|
-
memo.alias = Object.assign(Object.assign({}, memo.alias), { '@': args.paths.absSrcPath, '@@': args.paths.absTmpPath
|
|
63
|
+
memo.alias = Object.assign(Object.assign({}, memo.alias), { '@': args.paths.absSrcPath, '@@': args.paths.absTmpPath,
|
|
64
|
+
// like vite, use to pre-bundling dependencies in vite mode
|
|
65
|
+
'@fs': '/' });
|
|
59
66
|
return memo;
|
|
60
67
|
});
|
|
61
68
|
};
|
|
@@ -4,13 +4,25 @@ exports.getSchemas = void 0;
|
|
|
4
4
|
const utils_1 = require("@umijs/utils");
|
|
5
5
|
function getSchemas() {
|
|
6
6
|
return {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
base: (Joi) => Joi.string(),
|
|
8
|
+
conventionRoutes: (Joi) => Joi.object({
|
|
9
|
+
exclude: Joi.array().items(Joi.any()),
|
|
10
|
+
base: Joi.string(),
|
|
11
|
+
}),
|
|
9
12
|
favicon: (Joi) => Joi.string(),
|
|
10
|
-
headScripts: (Joi) => Joi.array()
|
|
11
|
-
|
|
13
|
+
headScripts: (Joi) => Joi.array(),
|
|
14
|
+
history: (Joi) => Joi.object({
|
|
15
|
+
type: Joi.string().valid('browser', 'hash', 'memory'),
|
|
16
|
+
}),
|
|
17
|
+
links: (Joi) => Joi.array(),
|
|
18
|
+
metas: (Joi) => Joi.array(),
|
|
19
|
+
mountElementId: (Joi) => Joi.string(),
|
|
12
20
|
npmClient: (Joi) => Joi.string().valid(utils_1.NpmClientEnum.pnpm, utils_1.NpmClientEnum.tnpm, utils_1.NpmClientEnum.cnpm, utils_1.NpmClientEnum.yarn, utils_1.NpmClientEnum.npm),
|
|
21
|
+
plugins: (Joi) => Joi.array().items(Joi.string()),
|
|
22
|
+
publicPath: (Joi) => Joi.string().regex(/\/$/).error(new Error('publicPath must end with /')),
|
|
13
23
|
routes: (Joi) => Joi.array().items(Joi.object()),
|
|
24
|
+
scripts: (Joi) => Joi.array(),
|
|
25
|
+
styles: (Joi) => Joi.array(),
|
|
14
26
|
};
|
|
15
27
|
}
|
|
16
28
|
exports.getSchemas = getSchemas;
|
|
@@ -101,6 +101,7 @@ class ESMIService {
|
|
|
101
101
|
});
|
|
102
102
|
// get the build ticket id
|
|
103
103
|
const ticketId = yield this.build(data);
|
|
104
|
+
utils_1.logger.info(`ticketId: ${ticketId}`);
|
|
104
105
|
// continue to the next request after 2s
|
|
105
106
|
const next = () => new Promise((resolve) => setTimeout(() => resolve(deferrer()), 2000));
|
|
106
107
|
const deferrer = () => {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Plugin } from '@umijs/bundler-utils/compiled/esbuild';
|
|
2
|
+
import type { DepOptimizationOptions } from 'vite';
|
|
3
|
+
/**
|
|
4
|
+
* transform require call to import
|
|
5
|
+
*/
|
|
6
|
+
export default function requireToImportPlugin({ exclude, }: {
|
|
7
|
+
exclude: NonNullable<DepOptimizationOptions['exclude']>;
|
|
8
|
+
}): Plugin;
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const lodash_1 = require("@umijs/utils/compiled/lodash");
|
|
13
|
+
/**
|
|
14
|
+
* transform require call to import
|
|
15
|
+
*/
|
|
16
|
+
function requireToImportPlugin({ exclude, }) {
|
|
17
|
+
const regSafeExclude = exclude.map((e) => e.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
|
18
|
+
const requireRegExp = new RegExp(`^(${regSafeExclude.join('|')})$`);
|
|
19
|
+
return {
|
|
20
|
+
name: 'preset-umi:esmi-require-to-import',
|
|
21
|
+
setup(build) {
|
|
22
|
+
// handler require calls for external deps
|
|
23
|
+
build.onResolve({
|
|
24
|
+
filter: requireRegExp,
|
|
25
|
+
}, (args) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
if (args.kind === 'require-call') {
|
|
27
|
+
return {
|
|
28
|
+
path: args.path,
|
|
29
|
+
namespace: 'esmi-require-to-import',
|
|
30
|
+
pluginData: {
|
|
31
|
+
resolveDir: args.resolveDir,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}));
|
|
36
|
+
// replace load content
|
|
37
|
+
build.onLoad({
|
|
38
|
+
filter: /.*/,
|
|
39
|
+
namespace: 'esmi-require-to-import',
|
|
40
|
+
}, (args) => {
|
|
41
|
+
const { resolveDir } = args.pluginData || {};
|
|
42
|
+
const packageName = args.path;
|
|
43
|
+
const starSpecifier = `${(0, lodash_1.camelCase)(packageName)}Star`;
|
|
44
|
+
const defaultSpecifier = `${(0, lodash_1.camelCase)(packageName)}Default`;
|
|
45
|
+
return {
|
|
46
|
+
resolveDir,
|
|
47
|
+
contents: [
|
|
48
|
+
`import * as ${starSpecifier} from '${packageName}';`,
|
|
49
|
+
'',
|
|
50
|
+
`const ${defaultSpecifier} = ${starSpecifier}.default ? ${starSpecifier}.default : ${starSpecifier};`,
|
|
51
|
+
'',
|
|
52
|
+
`export default ${defaultSpecifier};`,
|
|
53
|
+
`export * from '${packageName}';`,
|
|
54
|
+
'',
|
|
55
|
+
].join('\n'),
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
exports.default = requireToImportPlugin;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Plugin } from '@umijs/bundler-utils/compiled/esbuild';
|
|
2
|
+
import type { DepOptimizationOptions } from 'vite';
|
|
3
|
+
import type { createResolver } from '../../../libs/scan';
|
|
4
|
+
/**
|
|
5
|
+
* only external top level import, exclude sub-path imports for esmi
|
|
6
|
+
* example:
|
|
7
|
+
* - import from 'antd' will be externalized
|
|
8
|
+
* - import from 'antd/dist/antd.less' will not be externalized
|
|
9
|
+
*/
|
|
10
|
+
export default function topLevelExternal({ exclude, resolver, }: {
|
|
11
|
+
exclude: NonNullable<DepOptimizationOptions['exclude']>;
|
|
12
|
+
resolver: ReturnType<typeof createResolver>;
|
|
13
|
+
}): Plugin;
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
/**
|
|
13
|
+
* only external top level import, exclude sub-path imports for esmi
|
|
14
|
+
* example:
|
|
15
|
+
* - import from 'antd' will be externalized
|
|
16
|
+
* - import from 'antd/dist/antd.less' will not be externalized
|
|
17
|
+
*/
|
|
18
|
+
function topLevelExternal({ exclude, resolver, }) {
|
|
19
|
+
const regSafeExclude = exclude.map((e) => e.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
|
20
|
+
const subImportRegExp = new RegExp(`^(${regSafeExclude.join('|')})/`);
|
|
21
|
+
const extRegExp = /\.((?<!d)\.ts|jsx?|tsx)$/;
|
|
22
|
+
return {
|
|
23
|
+
name: 'preset-umi:esmi-top-level-external',
|
|
24
|
+
setup(build) {
|
|
25
|
+
build.onResolve({
|
|
26
|
+
filter: subImportRegExp,
|
|
27
|
+
}, (args) => __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const resolved = yield resolver.resolve(args.resolveDir, args.path);
|
|
29
|
+
// only process javascript-like files
|
|
30
|
+
if (extRegExp.test(resolved)) {
|
|
31
|
+
return { path: resolved };
|
|
32
|
+
}
|
|
33
|
+
}));
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
exports.default = topLevelExternal;
|
|
@@ -16,8 +16,11 @@ const es_module_lexer_1 = require("@umijs/bundler-utils/compiled/es-module-lexer
|
|
|
16
16
|
const magic_string_1 = __importDefault(require("magic-string"));
|
|
17
17
|
const path_1 = require("path");
|
|
18
18
|
const scan_1 = require("../../libs/scan");
|
|
19
|
+
const requireToImport_1 = __importDefault(require("./esbuildPlugins/requireToImport"));
|
|
20
|
+
const topLevelExternal_1 = __importDefault(require("./esbuildPlugins/topLevelExternal"));
|
|
19
21
|
const Service_1 = __importDefault(require("./Service"));
|
|
20
22
|
let importmap = { imports: {}, scopes: {} };
|
|
23
|
+
let importmatches = {};
|
|
21
24
|
/**
|
|
22
25
|
* esmi vite plugin
|
|
23
26
|
*/
|
|
@@ -25,13 +28,36 @@ function esmi(opts) {
|
|
|
25
28
|
return {
|
|
26
29
|
name: 'preset-umi:esmi',
|
|
27
30
|
configResolved(config) {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
var _c, _d;
|
|
28
33
|
const { include, exclude } = config.optimizeDeps;
|
|
34
|
+
(_a = (_c = config.optimizeDeps).include) !== null && _a !== void 0 ? _a : (_c.include = []);
|
|
29
35
|
// do not pre-compile deps which will be loaded by importmap (for top-level deps)
|
|
30
36
|
if (include === null || include === void 0 ? void 0 : include.length) {
|
|
31
|
-
config.optimizeDeps.include = include.filter((item) => !importmap.imports[item]);
|
|
37
|
+
config.optimizeDeps.include = include.filter((item) => !importmatches[item] && !importmap.imports[item]);
|
|
32
38
|
}
|
|
33
|
-
// exclude pre-compile deps
|
|
34
|
-
config.optimizeDeps.exclude =
|
|
39
|
+
// exclude pre-compile deps
|
|
40
|
+
config.optimizeDeps.exclude = [
|
|
41
|
+
...new Set([
|
|
42
|
+
// deps from user config
|
|
43
|
+
...(exclude || []),
|
|
44
|
+
// deps from local scan
|
|
45
|
+
...Object.keys(importmatches),
|
|
46
|
+
// deps from esmi analyze result
|
|
47
|
+
...Object.keys(importmap.imports),
|
|
48
|
+
]),
|
|
49
|
+
];
|
|
50
|
+
// apply esbuild plugins
|
|
51
|
+
(_b = (_d = config.optimizeDeps).esbuildOptions) !== null && _b !== void 0 ? _b : (_d.esbuildOptions = {});
|
|
52
|
+
config.optimizeDeps.esbuildOptions.plugins = [
|
|
53
|
+
// transform require call to import
|
|
54
|
+
(0, requireToImport_1.default)({ exclude: config.optimizeDeps.exclude }),
|
|
55
|
+
// make sure vite only external top-level npm imports, and resolve sub-path npm imports
|
|
56
|
+
(0, topLevelExternal_1.default)({
|
|
57
|
+
exclude: config.optimizeDeps.exclude,
|
|
58
|
+
resolver: opts.resolver,
|
|
59
|
+
}),
|
|
60
|
+
].concat(config.optimizeDeps.esbuildOptions.plugins || []);
|
|
35
61
|
},
|
|
36
62
|
transform(source) {
|
|
37
63
|
try {
|
|
@@ -42,9 +68,17 @@ function esmi(opts) {
|
|
|
42
68
|
imports.forEach((item) => {
|
|
43
69
|
const { n: specifier, s: start, e: end } = item;
|
|
44
70
|
// replace npm package to CDN url for matched imports
|
|
45
|
-
if (specifier
|
|
46
|
-
|
|
47
|
-
|
|
71
|
+
if (specifier) {
|
|
72
|
+
const replacement =
|
|
73
|
+
// search from local scan matches first (for alias)
|
|
74
|
+
(importmatches[specifier] &&
|
|
75
|
+
importmap.imports[importmatches[specifier]]) ||
|
|
76
|
+
// search from esmi analyze result
|
|
77
|
+
importmap.imports[specifier];
|
|
78
|
+
if (replacement) {
|
|
79
|
+
s !== null && s !== void 0 ? s : (s = new magic_string_1.default(source));
|
|
80
|
+
s.overwrite(start, end, replacement);
|
|
81
|
+
}
|
|
48
82
|
}
|
|
49
83
|
});
|
|
50
84
|
return (s === null || s === void 0 ? void 0 : s.toString()) || source;
|
|
@@ -78,7 +112,11 @@ function generatePkgData(api) {
|
|
|
78
112
|
name: 'default',
|
|
79
113
|
path: 'es/index.js',
|
|
80
114
|
from: '',
|
|
81
|
-
deps: Object.entries(api.appData.deps)
|
|
115
|
+
deps: Object.entries(api.appData.deps)
|
|
116
|
+
// only compile entry imports
|
|
117
|
+
.filter(([_, { matches }]) => matches.length)
|
|
118
|
+
// convert to esmi config
|
|
119
|
+
.map(([name, { version }]) => ({
|
|
82
120
|
name,
|
|
83
121
|
version,
|
|
84
122
|
usedMap: {
|
|
@@ -102,23 +140,29 @@ exports.default = (api) => {
|
|
|
102
140
|
return __awaiter(this, void 0, void 0, function* () {
|
|
103
141
|
// scan and module graph
|
|
104
142
|
// TODO: module graph
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
resolver,
|
|
143
|
+
yield api.applyPlugins({
|
|
144
|
+
key: 'updateAppDataDeps',
|
|
145
|
+
type: api.ApplyPluginsType.event,
|
|
109
146
|
});
|
|
110
147
|
// skip umi by default
|
|
111
148
|
delete api.appData.deps['umi'];
|
|
112
|
-
// FIXME: force include react & react-dom
|
|
113
|
-
api.appData.deps['react'] = api.appData.react.version;
|
|
114
|
-
api.appData.deps['react-dom'] = api.appData.react.version;
|
|
115
149
|
const data = generatePkgData(api);
|
|
116
150
|
const deps = data.pkgInfo.exports.reduce((r, exp) => r.concat(exp.deps.map((dep) => dep.name)), []);
|
|
117
151
|
const hasNewDep = deps.some((i) => !importmap.imports[i]);
|
|
118
152
|
// update importmap from esm if there has new import
|
|
119
153
|
if (hasNewDep) {
|
|
120
|
-
// TODO: add local cache and restore
|
|
121
154
|
importmap = (_a = (yield service.getImportmap(data))) === null || _a === void 0 ? void 0 : _a.importMap;
|
|
155
|
+
// update matches map to dep name
|
|
156
|
+
importmatches = Object.keys(api.appData.deps).reduce((r, dep) => {
|
|
157
|
+
// filter subpath imports
|
|
158
|
+
if (!api.appData.deps[dep].subpaths.length) {
|
|
159
|
+
// map all matches to dep name
|
|
160
|
+
api.appData.deps[dep].matches.forEach((m) => {
|
|
161
|
+
r[m] = dep;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
return r;
|
|
165
|
+
}, {});
|
|
122
166
|
// because we will replaced package name to CDN url in vite plugin
|
|
123
167
|
// so we must append scope rules for the CDN url like the import specifier
|
|
124
168
|
// example:
|
|
@@ -194,6 +238,7 @@ exports.default = (api) => {
|
|
|
194
238
|
yield refreshImportMap();
|
|
195
239
|
// TODO: refresh page when importmap changed
|
|
196
240
|
}),
|
|
241
|
+
resolver,
|
|
197
242
|
}));
|
|
198
243
|
return memo;
|
|
199
244
|
});
|
|
@@ -53,6 +53,8 @@ exports.default = (api) => {
|
|
|
53
53
|
}
|
|
54
54
|
});
|
|
55
55
|
api.modifyHTMLFavicon((memo) => {
|
|
56
|
-
return
|
|
56
|
+
return api.appData.faviconFile
|
|
57
|
+
? `${api.config.publicPath}${api.appData.faviconFile}`
|
|
58
|
+
: memo;
|
|
57
59
|
});
|
|
58
60
|
};
|
|
@@ -3,11 +3,13 @@ import * as t from '@umijs/bundler-utils/compiled/babel/types';
|
|
|
3
3
|
import { IOpts } from './lowImport';
|
|
4
4
|
interface IPluginOpts {
|
|
5
5
|
opts: IOpts;
|
|
6
|
+
css: string;
|
|
6
7
|
}
|
|
7
8
|
export default function (): {
|
|
8
9
|
visitor: {
|
|
9
10
|
Identifier(path: Babel.NodePath<t.Identifier>, state: {
|
|
10
11
|
opts: IPluginOpts;
|
|
12
|
+
file: any;
|
|
11
13
|
}): void;
|
|
12
14
|
MemberExpression(path: Babel.NodePath<t.MemberExpression>, state: {
|
|
13
15
|
opts: IPluginOpts;
|
|
@@ -21,11 +21,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
22
|
const helper_module_imports_1 = require("@umijs/bundler-utils/compiled/babel/helper-module-imports");
|
|
23
23
|
const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
|
|
24
|
+
const path_1 = require("path");
|
|
24
25
|
function default_1() {
|
|
25
26
|
return {
|
|
26
27
|
visitor: {
|
|
27
28
|
Identifier(path, state) {
|
|
28
|
-
var _a, _b;
|
|
29
|
+
var _a, _b, _c;
|
|
29
30
|
const { name } = path.node;
|
|
30
31
|
if (path.scope.hasBinding(path.node.name)) {
|
|
31
32
|
return;
|
|
@@ -36,17 +37,38 @@ function default_1() {
|
|
|
36
37
|
t.isImportNamespaceSpecifier(parentNode)) {
|
|
37
38
|
return;
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
+
// don't support member expression
|
|
41
|
+
// e.g. foo.styles
|
|
42
|
+
if (t.isMemberExpression(parentNode) &&
|
|
43
|
+
path.node === parentNode.property) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// don't support object property
|
|
47
|
+
// e.g. { styles: 1 }
|
|
48
|
+
if (t.isObjectProperty(parentNode) && path.node === parentNode.key) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// TODO: 替换方式需要缓存,避免过多临时变量
|
|
52
|
+
if ((_a = state.opts.opts.identifierToLib) === null || _a === void 0 ? void 0 : _a.hasOwnProperty(name)) {
|
|
40
53
|
path.replaceWith((0, helper_module_imports_1.addNamed)(path, name, state.opts.opts.identifierToLib[name]));
|
|
41
54
|
}
|
|
42
|
-
else if ((_b = state.opts.opts.defaultToLib) === null || _b === void 0 ? void 0 : _b
|
|
55
|
+
else if ((_b = state.opts.opts.defaultToLib) === null || _b === void 0 ? void 0 : _b.hasOwnProperty(name)) {
|
|
43
56
|
path.replaceWith((0, helper_module_imports_1.addDefault)(path, state.opts.opts.defaultToLib[name], {
|
|
44
57
|
nameHint: name,
|
|
45
58
|
}));
|
|
46
59
|
}
|
|
47
|
-
|
|
60
|
+
else if ((_c = state.opts.opts.namespaceToLib) === null || _c === void 0 ? void 0 : _c.hasOwnProperty(name)) {
|
|
61
|
+
path.replaceWith((0, helper_module_imports_1.addNamespace)(path, state.opts.opts.namespaceToLib[name]));
|
|
62
|
+
}
|
|
63
|
+
// import css
|
|
64
|
+
if (name === 'styles' && state.opts.css) {
|
|
65
|
+
const { filename } = state.file.opts;
|
|
66
|
+
const cssFilename = (0, path_1.basename)(filename, (0, path_1.extname)(filename)) + '.' + state.opts.css;
|
|
67
|
+
path.replaceWith((0, helper_module_imports_1.addDefault)(path, './' + cssFilename, { nameHint: name }));
|
|
68
|
+
}
|
|
48
69
|
},
|
|
49
70
|
MemberExpression(path, state) {
|
|
71
|
+
var _a, _b;
|
|
50
72
|
const { object, property } = path.node;
|
|
51
73
|
if (path.scope.hasBinding(object.name)) {
|
|
52
74
|
return;
|
|
@@ -57,8 +79,8 @@ function default_1() {
|
|
|
57
79
|
t.isImportNamespaceSpecifier(parentNode)) {
|
|
58
80
|
return;
|
|
59
81
|
}
|
|
60
|
-
if (state.opts.opts.withObjs[object.name] &&
|
|
61
|
-
(state.opts.opts.withObjs[object.name].members || []).includes(property.name)) {
|
|
82
|
+
if (((_a = state.opts.opts.withObjs) === null || _a === void 0 ? void 0 : _a[object.name]) &&
|
|
83
|
+
(((_b = state.opts.opts.withObjs) === null || _b === void 0 ? void 0 : _b[object.name].members) || []).includes(property.name)) {
|
|
62
84
|
path.replaceWith((0, helper_module_imports_1.addNamed)(path, property.name, state.opts.opts.withObjs[object.name].importFrom));
|
|
63
85
|
}
|
|
64
86
|
},
|