@modern-js/utils 1.1.7-alpha.0 → 1.3.0

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 (81) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/js/modern/clearConsole.js +1 -1
  3. package/dist/js/modern/compatRequire.js +5 -0
  4. package/dist/js/modern/constants.js +5 -19
  5. package/dist/js/modern/getBrowserslist.js +1 -1
  6. package/dist/js/modern/index.js +2 -1
  7. package/dist/js/modern/prettyInstructions.js +5 -4
  8. package/dist/js/modern/wait.js +5 -0
  9. package/dist/js/node/clearConsole.js +1 -1
  10. package/dist/js/node/compatRequire.js +10 -2
  11. package/dist/js/node/constants.js +7 -24
  12. package/dist/js/node/getBrowserslist.js +2 -2
  13. package/dist/js/node/index.js +14 -0
  14. package/dist/js/node/prettyInstructions.js +5 -4
  15. package/dist/js/node/wait.js +12 -0
  16. package/dist/js/treeshaking/clearConsole.js +1 -1
  17. package/dist/js/treeshaking/compatRequire.js +5 -0
  18. package/dist/js/treeshaking/constants.js +5 -19
  19. package/dist/js/treeshaking/getBrowserslist.js +1 -1
  20. package/dist/js/treeshaking/index.js +2 -1
  21. package/dist/js/treeshaking/prettyInstructions.js +6 -5
  22. package/dist/js/treeshaking/wait.js +8 -0
  23. package/dist/types/compatRequire.d.ts +2 -1
  24. package/dist/types/constants.d.ts +1 -16
  25. package/dist/types/getBrowserslist.d.ts +1 -1
  26. package/dist/types/index.d.ts +2 -1
  27. package/dist/types/wait.d.ts +2 -0
  28. package/jest.config.js +8 -0
  29. package/package.json +20 -11
  30. package/tests/__snapshots__/prettyInstructions.test.ts.snap +19 -0
  31. package/tests/applyOptionsChain.test.ts +1 -1
  32. package/tests/compatRequire.test.ts +11 -1
  33. package/tests/debug.test.ts +4 -2
  34. package/tests/ensureAbsolutePath.test.ts +1 -1
  35. package/tests/findExists.test.ts +1 -1
  36. package/tests/fixtures/compat-require/foo.js +3 -0
  37. package/tests/getBrowserslist.test.ts +1 -1
  38. package/tests/getCacheIdentifier.test.ts +1 -1
  39. package/tests/getEntryOptions.test.ts +1 -1
  40. package/tests/index.test.ts +1 -1
  41. package/tests/logger.test.ts +1 -1
  42. package/tests/prettyInstructions.test.ts +139 -0
  43. package/tests/removeSlash.test.ts +1 -1
  44. package/tests/tsconfig.json +1 -3
  45. package/tests/wait.ts +38 -0
  46. package/tsconfig.json +1 -3
  47. package/src/FileSizeReporter.ts +0 -181
  48. package/src/alias.ts +0 -90
  49. package/src/applyOptionsChain.ts +0 -44
  50. package/src/chalk.ts +0 -3
  51. package/src/clearConsole.ts +0 -5
  52. package/src/compatRequire.ts +0 -25
  53. package/src/constants.ts +0 -262
  54. package/src/debug.ts +0 -8
  55. package/src/ensureAbsolutePath.ts +0 -10
  56. package/src/findExists.ts +0 -15
  57. package/src/formatWebpackMessages.ts +0 -131
  58. package/src/generateMetaTags.ts +0 -75
  59. package/src/getBrowserslist.ts +0 -6
  60. package/src/getCacheIdentifier.ts +0 -30
  61. package/src/getEntryOptions.ts +0 -37
  62. package/src/getPackageManager.ts +0 -30
  63. package/src/getPort.ts +0 -62
  64. package/src/import.ts +0 -10
  65. package/src/index.ts +0 -31
  66. package/src/is/index.ts +0 -78
  67. package/src/is/node-env.ts +0 -9
  68. package/src/is/platform.ts +0 -7
  69. package/src/is/type.ts +0 -43
  70. package/src/logger.ts +0 -184
  71. package/src/monorepo.ts +0 -106
  72. package/src/nodeEnv.ts +0 -28
  73. package/src/path.ts +0 -9
  74. package/src/pkgUp.ts +0 -3
  75. package/src/prettyInstructions.ts +0 -88
  76. package/src/printBuildError.ts +0 -47
  77. package/src/readTsConfig.ts +0 -21
  78. package/src/removeSlash.ts +0 -6
  79. package/src/runtimeExports.ts +0 -68
  80. package/src/types.d.ts +0 -1
  81. package/src/watch.ts +0 -56
@@ -0,0 +1,19 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`prettyInstructions The src directory does not exist 1`] = `
4
+ "App running at:
5
+
6
+ > Network: http://127.0.0.1:8080/api
7
+ > Network: http://11.11.111.11:8080/api
8
+ > Network: http://10.100.100.100:8080/api
9
+ "
10
+ `;
11
+
12
+ exports[`prettyInstructions basic usage 1`] = `
13
+ "App running at:
14
+
15
+ > Network: http://127.0.0.1:8080/
16
+ > Network: http://11.11.111.11:8080/
17
+ > Network: http://10.100.100.100:8080/
18
+ "
19
+ `;
@@ -1,4 +1,4 @@
1
- import { applyOptionsChain } from '@/applyOptionsChain';
1
+ import { applyOptionsChain } from '../src/applyOptionsChain';
2
2
 
3
3
  describe('apply options chain', () => {
4
4
  test(`should return default options`, () => {
@@ -1,5 +1,5 @@
1
1
  import path from 'path';
2
- import { compatRequire } from '@/compatRequire';
2
+ import { compatRequire, cleanRequireCache } from '../src/compatRequire';
3
3
 
4
4
  describe('compat require', () => {
5
5
  const fixturePath = path.resolve(__dirname, './fixtures/compat-require');
@@ -19,4 +19,14 @@ describe('compat require', () => {
19
19
  test(`should return null`, () => {
20
20
  expect(compatRequire(path.join(fixturePath, 'empty.js'))).toEqual(null);
21
21
  });
22
+
23
+ test('should clearn cache after fn', () => {
24
+ const foo = module.require('./fixtures/compat-require/foo');
25
+ const requirePath = require.resolve('./fixtures/compat-require/foo.js');
26
+ expect(foo.name).toBe('foo');
27
+ expect(require.cache[requirePath]).toBeDefined();
28
+ cleanRequireCache([requirePath]);
29
+ jest.resetModules();
30
+ expect(require.cache[requirePath]).toBeUndefined();
31
+ });
22
32
  });
@@ -1,5 +1,6 @@
1
1
  import debug from 'debug';
2
- import { createDebugger } from '@/debug';
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
- expect(args[0]).toContain('modern-js:test2 22222');
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');
@@ -1,5 +1,5 @@
1
1
  import path from 'path';
2
- import { ensureAbsolutePath } from '@/ensureAbsolutePath';
2
+ import { ensureAbsolutePath } from '../src/ensureAbsolutePath';
3
3
 
4
4
  describe('ensure absolute file path', () => {
5
5
  test('should return filePath directly', () => {
@@ -1,5 +1,5 @@
1
1
  import path from 'path';
2
- import { findExists } from '@/findExists';
2
+ import { findExists } from '../src/findExists';
3
3
 
4
4
  describe('find first exists file', () => {
5
5
  const fixturePath = path.resolve(__dirname, './fixtures/file-exists');
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ name: 'foo',
3
+ };
@@ -1,5 +1,5 @@
1
1
  import path from 'path';
2
- import { getBrowserslist, defaults } from '@/getBrowserslist';
2
+ import { getBrowserslist, defaults } from '../src/getBrowserslist';
3
3
 
4
4
  describe('get browserslist', () => {
5
5
  const fixture = path.resolve(__dirname, './fixtures/browserlist');
@@ -1,4 +1,4 @@
1
- import { getCacheIdentifier } from '@/getCacheIdentifier';
1
+ import { getCacheIdentifier } from '../src/getCacheIdentifier';
2
2
 
3
3
  describe('get cache identifier', () => {
4
4
  test('should use package name and version', () => {
@@ -1,4 +1,4 @@
1
- import { getEntryOptions } from '@/getEntryOptions';
1
+ import { getEntryOptions } from '../src/getEntryOptions';
2
2
 
3
3
  describe('get entry options', () => {
4
4
  test('should return default options', () => {
@@ -1,4 +1,4 @@
1
- import { canUseNpm, canUsePnpm, canUseYarn } from '@/index';
1
+ import { canUseNpm, canUsePnpm, canUseYarn } from '../src';
2
2
 
3
3
  describe('test generator utils', () => {
4
4
  test('test canUseNpm', async () => {
@@ -1,4 +1,4 @@
1
- import { logger } from '@/logger';
1
+ import { logger } from '../src/logger';
2
2
 
3
3
  describe('logger', () => {
4
4
  test('should log', () => {
@@ -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
+ });
@@ -2,7 +2,7 @@ import {
2
2
  removeLeadingSlash,
3
3
  removeSlash,
4
4
  removeTailSlash,
5
- } from '@/removeSlash';
5
+ } from '../src/removeSlash';
6
6
 
7
7
  describe('remove slash', () => {
8
8
  test(`should remove leading slash`, () => {
@@ -5,9 +5,7 @@
5
5
  "jsx": "preserve",
6
6
  "baseUrl": "./",
7
7
  "isolatedModules": true,
8
- "paths": {
9
- "@/*": ["../src/*"]
10
- },
8
+ "paths": {},
11
9
  "types": ["node", "jest"]
12
10
  }
13
11
  }
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/tsconfig.json CHANGED
@@ -6,9 +6,7 @@
6
6
  "jsx": "preserve",
7
7
  "baseUrl": "./",
8
8
  "isolatedModules": true,
9
- "paths": {
10
- "@/*": ["./src/*"]
11
- }
9
+ "paths": {}
12
10
  },
13
11
  "include": ["src"]
14
12
  }
@@ -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[]>);
@@ -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
@@ -1,3 +0,0 @@
1
- import chalk from 'chalk';
2
-
3
- export { chalk };
@@ -1,5 +0,0 @@
1
- export const clearConsole = () => {
2
- if (process.stdout.isTTY) {
3
- process.stdout.write('\x1B[H\x1B[2J');
4
- }
5
- };