@modern-js/utils 1.2.0 → 1.3.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.
- package/CHANGELOG.md +36 -0
- package/dist/js/modern/clearConsole.js +1 -1
- package/dist/js/modern/compatRequire.js +5 -0
- package/dist/js/modern/constants.js +4 -18
- package/dist/js/modern/index.js +2 -1
- package/dist/js/modern/prettyInstructions.js +15 -5
- package/dist/js/modern/wait.js +5 -0
- package/dist/js/node/clearConsole.js +1 -1
- package/dist/js/node/compatRequire.js +10 -2
- package/dist/js/node/constants.js +6 -23
- package/dist/js/node/index.js +14 -0
- package/dist/js/node/prettyInstructions.js +15 -5
- package/dist/js/node/wait.js +12 -0
- package/dist/js/treeshaking/clearConsole.js +1 -1
- package/dist/js/treeshaking/compatRequire.js +5 -0
- package/dist/js/treeshaking/constants.js +4 -18
- package/dist/js/treeshaking/index.js +2 -1
- package/dist/js/treeshaking/prettyInstructions.js +17 -6
- package/dist/js/treeshaking/wait.js +8 -0
- package/dist/types/compatRequire.d.ts +2 -1
- package/dist/types/constants.d.ts +1 -16
- package/dist/types/index.d.ts +2 -1
- package/dist/types/wait.d.ts +2 -0
- package/package.json +2 -3
- package/tests/__snapshots__/prettyInstructions.test.ts.snap +19 -0
- package/tests/compatRequire.test.ts +11 -1
- package/tests/fixtures/compat-require/foo.js +3 -0
- package/tests/prettyInstructions.test.ts +139 -0
- package/tests/wait.ts +38 -0
- 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
@@ -0,0 +1,139 @@
|
|
1
|
+
import { prettyInstructions } from '../src/prettyInstructions';
|
2
|
+
|
3
|
+
const mockNetworkInterfaces = {
|
4
|
+
lo0: [
|
5
|
+
{
|
6
|
+
address: '127.0.0.1',
|
7
|
+
netmask: '255.0.0.0',
|
8
|
+
family: 'IPv4',
|
9
|
+
mac: '00:00:00:00:00:00',
|
10
|
+
internal: true,
|
11
|
+
cidr: '127.0.0.1/8',
|
12
|
+
},
|
13
|
+
],
|
14
|
+
en5: [
|
15
|
+
{
|
16
|
+
address: 'fe80::aede:48ff:fe00:1122',
|
17
|
+
netmask: 'ffff:ffff:ffff:ffff::',
|
18
|
+
family: 'IPv6',
|
19
|
+
mac: 'ac:de:48:00:11:22',
|
20
|
+
internal: false,
|
21
|
+
cidr: 'fe80::aede:48ff:fe00:1122/64',
|
22
|
+
scopeid: 4,
|
23
|
+
},
|
24
|
+
],
|
25
|
+
en0: [
|
26
|
+
{
|
27
|
+
address: '11.11.111.11',
|
28
|
+
netmask: '255.255.252.0',
|
29
|
+
family: 'IPv4',
|
30
|
+
mac: '90:9c:4a:cf:11:d2',
|
31
|
+
internal: false,
|
32
|
+
cidr: '10.85.117.60/22',
|
33
|
+
},
|
34
|
+
],
|
35
|
+
utun2: [
|
36
|
+
{
|
37
|
+
address: '10.100.100.100',
|
38
|
+
netmask: '255.255.224.0',
|
39
|
+
family: 'IPv4',
|
40
|
+
mac: '00:00:00:00:00:00',
|
41
|
+
internal: false,
|
42
|
+
cidr: '10.255.182.172/19',
|
43
|
+
},
|
44
|
+
],
|
45
|
+
};
|
46
|
+
|
47
|
+
jest.mock('os', () => {
|
48
|
+
const originalModule = jest.requireActual('os');
|
49
|
+
return {
|
50
|
+
__esModule: true,
|
51
|
+
...originalModule,
|
52
|
+
default: {
|
53
|
+
networkInterfaces() {
|
54
|
+
return mockNetworkInterfaces;
|
55
|
+
},
|
56
|
+
},
|
57
|
+
};
|
58
|
+
});
|
59
|
+
|
60
|
+
jest.mock('chalk', () => ({
|
61
|
+
__esModule: true,
|
62
|
+
default: {
|
63
|
+
bold: jest.fn(str => str),
|
64
|
+
green: jest.fn(str => str),
|
65
|
+
red: jest.fn(str => str),
|
66
|
+
yellow: jest.fn(str => str),
|
67
|
+
cyanBright: jest.fn(str => str),
|
68
|
+
},
|
69
|
+
}));
|
70
|
+
|
71
|
+
describe('prettyInstructions', () => {
|
72
|
+
test('basic usage', () => {
|
73
|
+
const mockAppContext = {
|
74
|
+
entrypoints: [
|
75
|
+
{
|
76
|
+
entryName: 'main',
|
77
|
+
entry: '/example/node_modules/.modern-js/main/index.js',
|
78
|
+
isAutoMount: true,
|
79
|
+
customBootstrap: false,
|
80
|
+
},
|
81
|
+
],
|
82
|
+
serverRoutes: [
|
83
|
+
{
|
84
|
+
urlPath: '/',
|
85
|
+
entryName: 'main',
|
86
|
+
entryPath: 'html/main/index.html',
|
87
|
+
isSPA: true,
|
88
|
+
isSSR: false,
|
89
|
+
enableModernMode: false,
|
90
|
+
},
|
91
|
+
{
|
92
|
+
urlPath: '/api',
|
93
|
+
isApi: true,
|
94
|
+
entryPath: '',
|
95
|
+
isSPA: false,
|
96
|
+
isSSR: false,
|
97
|
+
},
|
98
|
+
],
|
99
|
+
port: 8080,
|
100
|
+
existSrc: true,
|
101
|
+
};
|
102
|
+
const mockConfig = {
|
103
|
+
dev: {
|
104
|
+
https: true,
|
105
|
+
},
|
106
|
+
};
|
107
|
+
|
108
|
+
const message = prettyInstructions(mockAppContext, mockConfig);
|
109
|
+
|
110
|
+
expect(message).toMatchSnapshot();
|
111
|
+
});
|
112
|
+
|
113
|
+
test('The src directory does not exist', () => {
|
114
|
+
const mockAppContext = {
|
115
|
+
entrypoints: [],
|
116
|
+
serverRoutes: [
|
117
|
+
{
|
118
|
+
urlPath: '/api',
|
119
|
+
isApi: true,
|
120
|
+
entryPath: '',
|
121
|
+
isSPA: false,
|
122
|
+
isSSR: false,
|
123
|
+
},
|
124
|
+
],
|
125
|
+
port: 8080,
|
126
|
+
existSrc: false,
|
127
|
+
};
|
128
|
+
|
129
|
+
const mockConfig = {
|
130
|
+
dev: {
|
131
|
+
https: true,
|
132
|
+
},
|
133
|
+
};
|
134
|
+
|
135
|
+
const message = prettyInstructions(mockAppContext, mockConfig);
|
136
|
+
|
137
|
+
expect(message).toMatchSnapshot();
|
138
|
+
});
|
139
|
+
});
|
package/tests/wait.ts
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
import { wait } from '../src/wait';
|
2
|
+
|
3
|
+
jest.useRealTimers();
|
4
|
+
|
5
|
+
describe('wait', () => {
|
6
|
+
test('basic usage', async () => {
|
7
|
+
const fn1 = jest.fn();
|
8
|
+
const fn2 = jest.fn();
|
9
|
+
const fn3 = async () => {
|
10
|
+
fn1();
|
11
|
+
await wait();
|
12
|
+
fn2();
|
13
|
+
};
|
14
|
+
|
15
|
+
fn3();
|
16
|
+
expect(fn1).toBeCalled();
|
17
|
+
expect(fn2).not.toBeCalled();
|
18
|
+
await wait();
|
19
|
+
expect(fn2).toBeCalled();
|
20
|
+
});
|
21
|
+
|
22
|
+
test('delay', async () => {
|
23
|
+
const fn1 = jest.fn();
|
24
|
+
const fn2 = jest.fn();
|
25
|
+
const time = 100;
|
26
|
+
const fn3 = async () => {
|
27
|
+
fn1();
|
28
|
+
await wait(time);
|
29
|
+
fn2();
|
30
|
+
};
|
31
|
+
|
32
|
+
fn3();
|
33
|
+
expect(fn1).toBeCalled();
|
34
|
+
expect(fn2).not.toBeCalled();
|
35
|
+
await wait(time);
|
36
|
+
expect(fn2).toBeCalled();
|
37
|
+
});
|
38
|
+
});
|
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
|
-
};
|
package/src/chalk.ts
DELETED
package/src/clearConsole.ts
DELETED
package/src/compatRequire.ts
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
import { findExists } from './findExists';
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Require function compatible with esm and cjs module.
|
5
|
-
* @param filePath - File to required.
|
6
|
-
* @returns module export object.
|
7
|
-
*/
|
8
|
-
export const compatRequire = (filePath: string) => {
|
9
|
-
const mod = require(filePath);
|
10
|
-
|
11
|
-
return mod?.__esModule ? mod.default : mod;
|
12
|
-
};
|
13
|
-
|
14
|
-
export const requireExistModule = (
|
15
|
-
filename: string,
|
16
|
-
extensions = ['.ts', '.js'],
|
17
|
-
) => {
|
18
|
-
const exist = findExists(extensions.map(ext => `${filename}${ext}`));
|
19
|
-
|
20
|
-
if (!exist) {
|
21
|
-
return null;
|
22
|
-
}
|
23
|
-
|
24
|
-
return compatRequire(exist);
|
25
|
-
};
|