@modern-js/utils 1.1.6 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +23 -0
- package/dist/js/modern/constants.js +2 -1
- package/dist/js/modern/getBrowserslist.js +1 -1
- package/dist/js/modern/prettyInstructions.js +1 -1
- package/dist/js/node/constants.js +2 -1
- package/dist/js/node/getBrowserslist.js +2 -2
- package/dist/js/node/prettyInstructions.js +1 -1
- package/dist/js/treeshaking/constants.js +2 -1
- package/dist/js/treeshaking/getBrowserslist.js +1 -1
- package/dist/js/treeshaking/prettyInstructions.js +1 -1
- package/dist/types/getBrowserslist.d.ts +1 -1
- package/jest.config.js +8 -0
- package/package.json +18 -12
- package/tests/applyOptionsChain.test.ts +1 -1
- package/tests/compatRequire.test.ts +1 -1
- package/tests/debug.test.ts +4 -2
- package/tests/ensureAbsolutePath.test.ts +1 -1
- package/tests/findExists.test.ts +1 -1
- package/tests/getBrowserslist.test.ts +1 -1
- package/tests/getCacheIdentifier.test.ts +1 -1
- package/tests/getEntryOptions.test.ts +1 -1
- package/tests/index.test.ts +1 -1
- package/tests/logger.test.ts +1 -1
- package/tests/removeSlash.test.ts +1 -1
- package/tests/tsconfig.json +1 -3
- package/tsconfig.json +1 -3
- package/src/FileSizeReporter.ts +0 -181
- package/src/alias.ts +0 -90
- package/src/applyOptionsChain.ts +0 -44
- package/src/chalk.ts +0 -3
- package/src/clearConsole.ts +0 -5
- package/src/compatRequire.ts +0 -25
- package/src/constants.ts +0 -262
- package/src/debug.ts +0 -8
- package/src/ensureAbsolutePath.ts +0 -10
- package/src/findExists.ts +0 -15
- package/src/formatWebpackMessages.ts +0 -131
- package/src/generateMetaTags.ts +0 -75
- package/src/getBrowserslist.ts +0 -6
- package/src/getCacheIdentifier.ts +0 -30
- package/src/getEntryOptions.ts +0 -37
- package/src/getPackageManager.ts +0 -30
- package/src/getPort.ts +0 -62
- package/src/import.ts +0 -10
- package/src/index.ts +0 -31
- package/src/is/index.ts +0 -78
- package/src/is/node-env.ts +0 -9
- package/src/is/platform.ts +0 -7
- package/src/is/type.ts +0 -43
- package/src/logger.ts +0 -184
- package/src/monorepo.ts +0 -106
- package/src/nodeEnv.ts +0 -28
- package/src/path.ts +0 -9
- package/src/pkgUp.ts +0 -3
- package/src/prettyInstructions.ts +0 -88
- package/src/printBuildError.ts +0 -47
- package/src/readTsConfig.ts +0 -21
- package/src/removeSlash.ts +0 -6
- package/src/runtimeExports.ts +0 -68
- package/src/types.d.ts +0 -1
- package/src/watch.ts +0 -56
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,28 @@
|
|
1
1
|
# @modern-js/utils
|
2
2
|
|
3
|
+
## 1.2.2
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 83166714: change .npmignore
|
8
|
+
|
9
|
+
## 1.2.1
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- 823809c6: fix: remove plugin-polyfill from app-tools
|
14
|
+
|
15
|
+
## 1.2.0
|
16
|
+
|
17
|
+
### Minor Changes
|
18
|
+
|
19
|
+
- cfe11628: Make Modern.js self bootstraping
|
20
|
+
|
21
|
+
### Patch Changes
|
22
|
+
|
23
|
+
- 2da09c69: Add "typescript" to the dependency list
|
24
|
+
- c3d46ee4: fix: test config invalid
|
25
|
+
|
3
26
|
## 1.1.6
|
4
27
|
|
5
28
|
### Patch Changes
|
@@ -158,6 +158,7 @@ export const INTERNAL_PLUGINS = {
|
|
158
158
|
cli: '@modern-js/plugin-static-hosting/cli'
|
159
159
|
},
|
160
160
|
'@modern-js/plugin-polyfill': {
|
161
|
+
cli: '@modern-js/plugin-polyfill/cli',
|
161
162
|
server: '@modern-js/plugin-polyfill'
|
162
163
|
},
|
163
164
|
'@modern-js/plugin-multiprocess': {
|
@@ -297,7 +298,7 @@ export const PLUGIN_SCHEMAS = {
|
|
297
298
|
}, {
|
298
299
|
target: 'tools.jest',
|
299
300
|
schema: {
|
300
|
-
typeof: ['object']
|
301
|
+
typeof: ['object', 'function']
|
301
302
|
}
|
302
303
|
}],
|
303
304
|
'@modern-js/plugin-micro-frontend': [{
|
@@ -21,7 +21,7 @@ const getAddressUrls = (protocol = 'http', port) => {
|
|
21
21
|
let type = 'Network: ';
|
22
22
|
let url = `${protocol}://${detail.address}:${port}`;
|
23
23
|
|
24
|
-
if (detail.address.includes(`
|
24
|
+
if (detail.address.includes(`localhost`)) {
|
25
25
|
type = 'Local: ';
|
26
26
|
url = `${protocol}://localhost:${port}`;
|
27
27
|
}
|
@@ -179,6 +179,7 @@ const INTERNAL_PLUGINS = {
|
|
179
179
|
cli: '@modern-js/plugin-static-hosting/cli'
|
180
180
|
},
|
181
181
|
'@modern-js/plugin-polyfill': {
|
182
|
+
cli: '@modern-js/plugin-polyfill/cli',
|
182
183
|
server: '@modern-js/plugin-polyfill'
|
183
184
|
},
|
184
185
|
'@modern-js/plugin-multiprocess': {
|
@@ -319,7 +320,7 @@ const PLUGIN_SCHEMAS = {
|
|
319
320
|
}, {
|
320
321
|
target: 'tools.jest',
|
321
322
|
schema: {
|
322
|
-
typeof: ['object']
|
323
|
+
typeof: ['object', 'function']
|
323
324
|
}
|
324
325
|
}],
|
325
326
|
'@modern-js/plugin-micro-frontend': [{
|
@@ -5,12 +5,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.getBrowserslist = exports.defaults = void 0;
|
7
7
|
|
8
|
-
var
|
8
|
+
var _browserslist = require("browserslist");
|
9
9
|
|
10
10
|
const defaults = ['> 0.01%', 'not dead', 'not op_mini all'];
|
11
11
|
exports.defaults = defaults;
|
12
12
|
|
13
|
-
const getBrowserslist = appDirectory => (0,
|
13
|
+
const getBrowserslist = appDirectory => (0, _browserslist.loadConfig)({
|
14
14
|
path: appDirectory
|
15
15
|
}) || defaults;
|
16
16
|
|
@@ -35,7 +35,7 @@ const getAddressUrls = (protocol = 'http', port) => {
|
|
35
35
|
let type = 'Network: ';
|
36
36
|
let url = `${protocol}://${detail.address}:${port}`;
|
37
37
|
|
38
|
-
if (detail.address.includes(`
|
38
|
+
if (detail.address.includes(`localhost`)) {
|
39
39
|
type = 'Local: ';
|
40
40
|
url = `${protocol}://localhost:${port}`;
|
41
41
|
}
|
@@ -160,6 +160,7 @@ export var INTERNAL_PLUGINS = {
|
|
160
160
|
cli: '@modern-js/plugin-static-hosting/cli'
|
161
161
|
},
|
162
162
|
'@modern-js/plugin-polyfill': {
|
163
|
+
cli: '@modern-js/plugin-polyfill/cli',
|
163
164
|
server: '@modern-js/plugin-polyfill'
|
164
165
|
},
|
165
166
|
'@modern-js/plugin-multiprocess': {
|
@@ -297,7 +298,7 @@ export var PLUGIN_SCHEMAS = {
|
|
297
298
|
}, {
|
298
299
|
target: 'tools.jest',
|
299
300
|
schema: {
|
300
|
-
"typeof": ['object']
|
301
|
+
"typeof": ['object', 'function']
|
301
302
|
}
|
302
303
|
}],
|
303
304
|
'@modern-js/plugin-micro-frontend': [{
|
@@ -39,7 +39,7 @@ var getAddressUrls = function getAddressUrls() {
|
|
39
39
|
var type = 'Network: ';
|
40
40
|
var url = "".concat(protocol, "://").concat(detail.address, ":").concat(port);
|
41
41
|
|
42
|
-
if (detail.address.includes("
|
42
|
+
if (detail.address.includes("localhost")) {
|
43
43
|
type = 'Local: ';
|
44
44
|
url = "".concat(protocol, "://localhost:").concat(port);
|
45
45
|
}
|
@@ -1,2 +1,2 @@
|
|
1
1
|
export declare const defaults: string[];
|
2
|
-
export declare const getBrowserslist: (appDirectory: string) =>
|
2
|
+
export declare const getBrowserslist: (appDirectory: string) => string[];
|
package/jest.config.js
ADDED
package/package.json
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "1.
|
14
|
+
"version": "1.2.2",
|
15
15
|
"jsnext:source": "./src/index.ts",
|
16
16
|
"types": "./dist/types/index.d.ts",
|
17
17
|
"main": "./dist/js/node/index.js",
|
@@ -20,13 +20,20 @@
|
|
20
20
|
"exports": {
|
21
21
|
".": {
|
22
22
|
"node": {
|
23
|
+
"jsnext:source": "./src/index.ts",
|
23
24
|
"import": "./dist/js/modern/index.js",
|
24
25
|
"require": "./dist/js/node/index.js"
|
25
26
|
},
|
26
27
|
"default": "./dist/js/treeshaking/index.js"
|
27
28
|
},
|
28
|
-
"./formatWebpackMessages":
|
29
|
-
|
29
|
+
"./formatWebpackMessages": {
|
30
|
+
"jsnext:source": "./src/formatWebpackMessages.ts",
|
31
|
+
"default": "./dist/js/treeshaking/formatWebpackMessages.js"
|
32
|
+
},
|
33
|
+
"./constants": {
|
34
|
+
"jsnext:source": "./src/constants.ts",
|
35
|
+
"default": "./dist/js/treeshaking/constants.js"
|
36
|
+
}
|
30
37
|
},
|
31
38
|
"typesVersions": {
|
32
39
|
"*": {
|
@@ -55,7 +62,8 @@
|
|
55
62
|
"strip-ansi": "6.0.0",
|
56
63
|
"upath": "^2.0.1",
|
57
64
|
"yaml": "^1.10.2",
|
58
|
-
"execa": "5"
|
65
|
+
"execa": "5",
|
66
|
+
"typescript": "^4"
|
59
67
|
},
|
60
68
|
"devDependencies": {
|
61
69
|
"@types/debug": "^4.1.7",
|
@@ -64,23 +72,21 @@
|
|
64
72
|
"@types/jest": "^26",
|
65
73
|
"@types/node": "^14",
|
66
74
|
"@types/recursive-readdir": "^2.2.0",
|
67
|
-
"typescript": "^4",
|
68
75
|
"webpack": "^5.54.0",
|
69
|
-
"@
|
70
|
-
"
|
71
|
-
|
72
|
-
"peerDependencies": {
|
73
|
-
"typescript": "^4.4.3"
|
76
|
+
"@scripts/build": "0.0.0",
|
77
|
+
"jest": "^27",
|
78
|
+
"@scripts/jest-config": "0.0.0"
|
74
79
|
},
|
75
80
|
"sideEffects": false,
|
76
81
|
"publishConfig": {
|
77
82
|
"registry": "https://registry.npmjs.org/",
|
78
|
-
"access": "public"
|
83
|
+
"access": "public",
|
84
|
+
"types": "./dist/types/index.d.ts"
|
79
85
|
},
|
80
86
|
"scripts": {
|
81
87
|
"new": "modern new",
|
82
88
|
"build": "modern build",
|
83
|
-
"test": "
|
89
|
+
"test": "jest --passWithNoTests"
|
84
90
|
},
|
85
91
|
"readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
|
86
92
|
}
|
package/tests/debug.test.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import debug from 'debug';
|
2
|
-
import
|
2
|
+
import stripAnsi from 'strip-ansi';
|
3
|
+
import { createDebugger } from '../src/debug';
|
3
4
|
|
4
5
|
describe('debug utility', () => {
|
5
6
|
test('should return file path', () => {
|
@@ -10,7 +11,8 @@ describe('debug utility', () => {
|
|
10
11
|
debug.enable('modern-js:test2');
|
11
12
|
|
12
13
|
debug.log = (...args) => {
|
13
|
-
|
14
|
+
// XXX: args[0] 的内容有时候是 ' \x1B[38;5;167;1mmodern-js:test2 \x1B[0m22222' 这种格式
|
15
|
+
expect(stripAnsi(args[0])).toContain('modern-js:test2 22222');
|
14
16
|
};
|
15
17
|
|
16
18
|
debug1('11111');
|
package/tests/findExists.test.ts
CHANGED
package/tests/index.test.ts
CHANGED
package/tests/logger.test.ts
CHANGED
package/tests/tsconfig.json
CHANGED
package/tsconfig.json
CHANGED
package/src/FileSizeReporter.ts
DELETED
@@ -1,181 +0,0 @@
|
|
1
|
-
/* eslint-disable eslint-comments/no-unlimited-disable */
|
2
|
-
/* eslint-disable */
|
3
|
-
/**
|
4
|
-
* Copyright (c) 2015-present, Facebook, Inc.
|
5
|
-
*
|
6
|
-
* This source code is licensed under the MIT license found in the
|
7
|
-
* LICENSE file at
|
8
|
-
* https://github.com/facebook/create-react-app/blob/master/LICENSE
|
9
|
-
*/
|
10
|
-
|
11
|
-
// Modified by Chao Xu (xuchaobei)
|
12
|
-
'use strict';
|
13
|
-
|
14
|
-
import fs from 'fs';
|
15
|
-
import path from 'path';
|
16
|
-
import chalk from 'chalk';
|
17
|
-
import filesize from 'filesize';
|
18
|
-
import recursive from 'recursive-readdir';
|
19
|
-
import stripAnsi from 'strip-ansi';
|
20
|
-
import gzipSize from 'gzip-size';
|
21
|
-
import { logger } from './logger';
|
22
|
-
|
23
|
-
|
24
|
-
function canReadAsset(asset:string) {
|
25
|
-
return (
|
26
|
-
/\.(js|css)$/.test(asset) &&
|
27
|
-
!/service-worker\.js/.test(asset) &&
|
28
|
-
!/precache-manifest\.[0-9a-f]+\.js/.test(asset)
|
29
|
-
);
|
30
|
-
}
|
31
|
-
|
32
|
-
// Prints a detailed summary of build files.
|
33
|
-
function printFileSizesAfterBuild(
|
34
|
-
webpackStats:any,
|
35
|
-
previousSizeMap:{root:string, sizes:Record<string, number[]>},
|
36
|
-
buildFolder:string,
|
37
|
-
maxBundleGzipSize:number,
|
38
|
-
maxChunkGzipSize:number
|
39
|
-
) {
|
40
|
-
var root = previousSizeMap.root;
|
41
|
-
var sizes = previousSizeMap.sizes;
|
42
|
-
var assets = (webpackStats.stats || [webpackStats])
|
43
|
-
.map((stats:any) =>
|
44
|
-
stats
|
45
|
-
.toJson({ all: false, assets: true })
|
46
|
-
.assets.filter((asset:any) => canReadAsset(asset.name))
|
47
|
-
.map((asset:any) => {
|
48
|
-
var fileContents = fs.readFileSync(path.join(root, asset.name));
|
49
|
-
var size = fileContents.length;
|
50
|
-
var gzippedSize = gzipSize.sync(fileContents);
|
51
|
-
var [previousSize, previousGzipSize] = sizes[removeFileNameHash(root, asset.name)] || [];
|
52
|
-
var sizeDifference = getDifferenceLabel(size, previousSize);
|
53
|
-
var gzipSizeDifference = getDifferenceLabel(gzippedSize, previousGzipSize);
|
54
|
-
return {
|
55
|
-
folder: path.join(
|
56
|
-
path.basename(buildFolder),
|
57
|
-
path.dirname(asset.name)
|
58
|
-
),
|
59
|
-
name: path.basename(asset.name),
|
60
|
-
gzippedSize: gzippedSize,
|
61
|
-
sizeLabel: filesize(size) + (sizeDifference ? ' (' + sizeDifference + ')' : ''),
|
62
|
-
gzipSizeLabel: filesize(gzippedSize) + (gzipSizeDifference ? ' (' + gzipSizeDifference + ')' : ''),
|
63
|
-
};
|
64
|
-
})
|
65
|
-
)
|
66
|
-
.reduce((single:any, all:any) => all.concat(single), []);
|
67
|
-
assets.sort((a:any, b:any) => b.size - a.size);
|
68
|
-
var longestSizeLabelLength = Math.max.apply(
|
69
|
-
null,
|
70
|
-
assets.map((a:any) => stripAnsi(a.sizeLabel).length)
|
71
|
-
);
|
72
|
-
var longestFileNameLength = Math.max.apply(
|
73
|
-
null,
|
74
|
-
assets.map((a:any) => stripAnsi(a.folder + path.sep + a.name).length)
|
75
|
-
);
|
76
|
-
|
77
|
-
printFileSizesHeader(longestFileNameLength, longestSizeLabelLength);
|
78
|
-
|
79
|
-
var suggestBundleSplitting = false;
|
80
|
-
assets.forEach((asset:any) => {
|
81
|
-
var {folder, name, sizeLabel, gzipSizeLabel, gzippedSize} = asset;
|
82
|
-
var fileNameLength = stripAnsi(folder + path.sep + name).length;
|
83
|
-
var sizeLength = stripAnsi(sizeLabel).length;
|
84
|
-
if (sizeLength < longestSizeLabelLength) {
|
85
|
-
var rightPadding = ' '.repeat(longestSizeLabelLength - sizeLength);
|
86
|
-
sizeLabel += rightPadding;
|
87
|
-
}
|
88
|
-
var fileNameLabel = chalk.dim(asset.folder + path.sep) + chalk.cyan(asset.name);
|
89
|
-
if (fileNameLength < longestFileNameLength) {
|
90
|
-
var rightPadding = ' '.repeat(longestFileNameLength - fileNameLength);
|
91
|
-
fileNameLabel += rightPadding;
|
92
|
-
}
|
93
|
-
var isMainBundle = asset.name.indexOf('main.') === 0;
|
94
|
-
var maxRecommendedSize = isMainBundle
|
95
|
-
? maxBundleGzipSize
|
96
|
-
: maxChunkGzipSize;
|
97
|
-
var isLarge = maxRecommendedSize && gzippedSize > maxRecommendedSize;
|
98
|
-
if (isLarge && path.extname(asset.name) === '.js') {
|
99
|
-
suggestBundleSplitting = true;
|
100
|
-
}
|
101
|
-
logger.log(
|
102
|
-
' ' + fileNameLabel +
|
103
|
-
' ' + sizeLabel +
|
104
|
-
' ' + (isLarge ? chalk.yellow(gzipSizeLabel) : gzipSizeLabel)
|
105
|
-
);
|
106
|
-
});
|
107
|
-
if (suggestBundleSplitting) {
|
108
|
-
logger.log();
|
109
|
-
logger.warn(
|
110
|
-
'The bundle size is significantly larger than recommended.'
|
111
|
-
);
|
112
|
-
}
|
113
|
-
}
|
114
|
-
|
115
|
-
function printFileSizesHeader(longestFileNameLength:number, longestSizeLabelLength:number){
|
116
|
-
const longestLengths = [longestFileNameLength, longestSizeLabelLength]
|
117
|
-
const headerRow = ['File', 'Size', 'Gzipped'].reduce((prev, cur, index) => {
|
118
|
-
const length = longestLengths[index];
|
119
|
-
let curLabel = cur;
|
120
|
-
if(length){
|
121
|
-
curLabel = cur.length < length ? cur + ' '.repeat(length - cur.length) : cur;
|
122
|
-
}
|
123
|
-
return prev + curLabel + ' ';
|
124
|
-
}, ' ')
|
125
|
-
|
126
|
-
logger.log(chalk.bold(chalk.blue(headerRow)))
|
127
|
-
}
|
128
|
-
|
129
|
-
function removeFileNameHash(buildFolder:string, fileName:string) {
|
130
|
-
return fileName
|
131
|
-
.replace(buildFolder, '')
|
132
|
-
.replace(/\\/g, '/')
|
133
|
-
.replace(
|
134
|
-
/\/?(.*)(\.[0-9a-f]+)(\.chunk)?(\.js|\.css)/,
|
135
|
-
(match, p1, p2, p3, p4) => p1 + p4
|
136
|
-
);
|
137
|
-
}
|
138
|
-
|
139
|
-
// Input: 1024, 2048
|
140
|
-
// Output: "(+1 KB)"
|
141
|
-
function getDifferenceLabel(currentSize:number, previousSize:number) {
|
142
|
-
var FIFTY_KILOBYTES = 1024 * 50;
|
143
|
-
var difference = currentSize - previousSize;
|
144
|
-
var fileSize = !Number.isNaN(difference) ? filesize(difference) : 0;
|
145
|
-
if (difference >= FIFTY_KILOBYTES) {
|
146
|
-
return chalk.red('+' + fileSize);
|
147
|
-
} else if (difference < FIFTY_KILOBYTES && difference > 0) {
|
148
|
-
return chalk.yellow('+' + fileSize);
|
149
|
-
} else if (difference < 0) {
|
150
|
-
return chalk.green(fileSize);
|
151
|
-
} else {
|
152
|
-
return '';
|
153
|
-
}
|
154
|
-
}
|
155
|
-
|
156
|
-
function measureFileSizesBeforeBuild(buildFolder:string):Promise<{root:string; sizes: Record<string, number[]>}> {
|
157
|
-
return new Promise(resolve => {
|
158
|
-
recursive(buildFolder, (err, fileNames) => {
|
159
|
-
var sizes;
|
160
|
-
if (!err && fileNames) {
|
161
|
-
sizes = fileNames.filter(canReadAsset).reduce<Record<string, [number,number]>>((memo, fileName) => {
|
162
|
-
var contents = fs.readFileSync(fileName);
|
163
|
-
var key = removeFileNameHash(buildFolder, fileName);
|
164
|
-
// save both the original size and gzip size
|
165
|
-
memo[key] = [contents.length, gzipSize.sync(contents)]
|
166
|
-
return memo;
|
167
|
-
}, {});
|
168
|
-
}
|
169
|
-
resolve({
|
170
|
-
root: buildFolder,
|
171
|
-
sizes: sizes || {},
|
172
|
-
});
|
173
|
-
});
|
174
|
-
});
|
175
|
-
}
|
176
|
-
|
177
|
-
export {
|
178
|
-
measureFileSizesBeforeBuild,
|
179
|
-
printFileSizesAfterBuild,
|
180
|
-
};
|
181
|
-
/* eslint-enable */
|
package/src/alias.ts
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
import fs from 'fs';
|
2
|
-
import path from 'path';
|
3
|
-
import chalk from 'chalk';
|
4
|
-
import { readTsConfigByFile } from './readTsConfig';
|
5
|
-
import { applyOptionsChain } from './applyOptionsChain';
|
6
|
-
|
7
|
-
type AliasOption =
|
8
|
-
| Record<string, string>
|
9
|
-
| ((aliases: Record<string, string>) => Record<string, unknown>)
|
10
|
-
| Record<string, string>
|
11
|
-
| undefined;
|
12
|
-
|
13
|
-
interface NormalizedConfig {
|
14
|
-
source: {
|
15
|
-
alias?: AliasOption | Array<AliasOption>;
|
16
|
-
};
|
17
|
-
}
|
18
|
-
|
19
|
-
interface IAliasConfig {
|
20
|
-
absoluteBaseUrl: string;
|
21
|
-
paths?: Record<string, string | string[]>;
|
22
|
-
isTsPath?: boolean;
|
23
|
-
isTsProject?: boolean;
|
24
|
-
}
|
25
|
-
|
26
|
-
export const validAlias = <T extends NormalizedConfig>(
|
27
|
-
modernConfig: T,
|
28
|
-
{ tsconfigPath }: { tsconfigPath: string },
|
29
|
-
) => {
|
30
|
-
const {
|
31
|
-
source: { alias },
|
32
|
-
} = modernConfig;
|
33
|
-
if (!alias) {
|
34
|
-
return null;
|
35
|
-
}
|
36
|
-
|
37
|
-
const isTsProject = fs.existsSync(tsconfigPath);
|
38
|
-
if (!isTsProject) {
|
39
|
-
return null;
|
40
|
-
}
|
41
|
-
|
42
|
-
const userAlias = getUserAlias(alias as Record<string, string>);
|
43
|
-
if (Object.keys(userAlias).length > 0) {
|
44
|
-
return chalk.red(
|
45
|
-
'Note: Please use `compilerOptions.paths` in "tsconfig.json" file replace `source.alias` config in "modern.config.js/ts" when project is typescript',
|
46
|
-
);
|
47
|
-
}
|
48
|
-
|
49
|
-
return null;
|
50
|
-
};
|
51
|
-
|
52
|
-
export const getAlias = (
|
53
|
-
aliasOption: AliasOption | Array<AliasOption>,
|
54
|
-
option: { appDirectory: string; tsconfigPath: string },
|
55
|
-
) => {
|
56
|
-
const isTsProject = fs.existsSync(option.tsconfigPath);
|
57
|
-
let aliasConfig: IAliasConfig;
|
58
|
-
if (!isTsProject) {
|
59
|
-
aliasConfig = {
|
60
|
-
absoluteBaseUrl: option.appDirectory,
|
61
|
-
paths: applyOptionsChain({ '@': ['./src'] }, aliasOption as any),
|
62
|
-
isTsPath: false,
|
63
|
-
isTsProject,
|
64
|
-
};
|
65
|
-
} else {
|
66
|
-
const tsconfig = readTsConfigByFile(option.tsconfigPath);
|
67
|
-
const baseUrl = tsconfig?.compilerOptions?.baseUrl;
|
68
|
-
aliasConfig = {
|
69
|
-
absoluteBaseUrl: baseUrl
|
70
|
-
? path.join(option.appDirectory, baseUrl)
|
71
|
-
: option.appDirectory,
|
72
|
-
paths: {
|
73
|
-
...(aliasOption || {}),
|
74
|
-
...tsconfig?.compilerOptions?.paths,
|
75
|
-
},
|
76
|
-
isTsPath: true,
|
77
|
-
isTsProject,
|
78
|
-
};
|
79
|
-
}
|
80
|
-
return aliasConfig;
|
81
|
-
};
|
82
|
-
|
83
|
-
// filter invalid ts paths that are not array
|
84
|
-
export const getUserAlias = (alias: Record<string, string | string[]> = {}) =>
|
85
|
-
Object.keys(alias).reduce((o, k) => {
|
86
|
-
if (Array.isArray(alias[k])) {
|
87
|
-
o[k] = alias[k];
|
88
|
-
}
|
89
|
-
return o;
|
90
|
-
}, {} as Record<string, string | string[]>);
|
package/src/applyOptionsChain.ts
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
// eslint-disable-next-line import/no-useless-path-segments
|
2
|
-
import { isFunction, logger, isPlainObject } from './index';
|
3
|
-
|
4
|
-
export const applyOptionsChain = <T, U>(
|
5
|
-
defaults: T,
|
6
|
-
/* eslint-disable @typescript-eslint/no-invalid-void-type */
|
7
|
-
options?:
|
8
|
-
| T
|
9
|
-
| ((config: T, utils?: U) => T | void)
|
10
|
-
| Array<T | ((config: T, utils?: U) => T | void)>,
|
11
|
-
/* eslint-enable @typescript-eslint/no-invalid-void-type */
|
12
|
-
utils?: U,
|
13
|
-
mergeFn = Object.assign,
|
14
|
-
): T => {
|
15
|
-
if (!options) {
|
16
|
-
return defaults;
|
17
|
-
}
|
18
|
-
|
19
|
-
if (isPlainObject(options) as any) {
|
20
|
-
return mergeFn(defaults, options);
|
21
|
-
} else if (isFunction(options)) {
|
22
|
-
const ret = options(defaults, utils);
|
23
|
-
if (ret) {
|
24
|
-
if (!isPlainObject(ret)) {
|
25
|
-
logger.warn(
|
26
|
-
`${options.name}: Function should mutate the config and return nothing, Or return a cloned or merged version of config object.`,
|
27
|
-
);
|
28
|
-
}
|
29
|
-
return ret;
|
30
|
-
}
|
31
|
-
} else if (Array.isArray(options)) {
|
32
|
-
return options.reduce<T>(
|
33
|
-
(memo, cur) => applyOptionsChain(memo, cur, utils, mergeFn),
|
34
|
-
defaults,
|
35
|
-
);
|
36
|
-
} else {
|
37
|
-
throw new Error(
|
38
|
-
`applyOptionsChain error:\ndefault options is: ${JSON.stringify(
|
39
|
-
defaults,
|
40
|
-
)}`,
|
41
|
-
);
|
42
|
-
}
|
43
|
-
return defaults;
|
44
|
-
};
|