@modern-js/utils 1.0.0 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +19 -0
- package/README.md +1 -4
- package/dist/js/modern/FileSizeReporter.js +2 -2
- package/dist/js/modern/alias.js +1 -1
- package/dist/js/modern/applyOptionsChain.js +2 -1
- package/dist/js/modern/constants.js +25 -0
- package/dist/js/modern/ensureAbsolutePath.js +1 -1
- package/dist/js/modern/getEntryOptions.js +13 -4
- package/dist/js/modern/getPackageManager.js +1 -1
- package/dist/js/modern/index.js +1 -5
- package/dist/js/modern/is/index.js +1 -1
- package/dist/js/modern/is/type.js +3 -0
- package/dist/js/modern/monorepo.js +1 -1
- package/dist/js/modern/path.js +2 -13
- package/dist/js/modern/prettyInstructions.js +2 -2
- package/dist/js/modern/readTsConfig.js +1 -1
- package/dist/js/modern/runtimeExports.js +6 -2
- package/dist/js/node/FileSizeReporter.js +13 -17
- package/dist/js/node/alias.js +3 -7
- package/dist/js/node/applyOptionsChain.js +6 -5
- package/dist/js/node/constants.js +28 -2
- package/dist/js/node/ensureAbsolutePath.js +3 -5
- package/dist/js/node/getEntryOptions.js +14 -4
- package/dist/js/node/getPackageManager.js +6 -10
- package/dist/js/node/index.js +13 -17
- package/dist/js/node/is/index.js +3 -7
- package/dist/js/node/is/type.js +5 -0
- package/dist/js/node/monorepo.js +13 -17
- package/dist/js/node/path.js +5 -37
- package/dist/js/node/prettyInstructions.js +5 -3
- package/dist/js/node/readTsConfig.js +4 -6
- package/dist/js/node/runtimeExports.js +9 -7
- package/dist/js/treeshaking/FileSizeReporter.js +2 -2
- package/dist/js/treeshaking/alias.js +1 -1
- package/dist/js/treeshaking/applyOptionsChain.js +2 -1
- package/dist/js/treeshaking/constants.js +25 -0
- package/dist/js/treeshaking/ensureAbsolutePath.js +1 -1
- package/dist/js/treeshaking/getEntryOptions.js +14 -3
- package/dist/js/treeshaking/getPackageManager.js +1 -1
- package/dist/js/treeshaking/index.js +1 -5
- package/dist/js/treeshaking/is/index.js +1 -1
- package/dist/js/treeshaking/is/type.js +3 -0
- package/dist/js/treeshaking/monorepo.js +1 -1
- package/dist/js/treeshaking/path.js +4 -26
- package/dist/js/treeshaking/prettyInstructions.js +2 -2
- package/dist/js/treeshaking/readTsConfig.js +1 -1
- package/dist/js/treeshaking/runtimeExports.js +8 -2
- package/dist/types/constants.d.ts +5 -0
- package/dist/types/getEntryOptions.d.ts +1 -1
- package/dist/types/index.d.ts +1 -3
- package/dist/types/is/type.d.ts +2 -1
- package/dist/types/monorepo.d.ts +1 -1
- package/dist/types/path.d.ts +2 -11
- package/dist/types/prettyInstructions.d.ts +6 -1
- package/dist/types/runtimeExports.d.ts +1 -1
- package/package.json +3 -3
- package/src/FileSizeReporter.ts +2 -2
- package/src/alias.ts +1 -1
- package/src/applyOptionsChain.ts +2 -1
- package/src/constants.ts +20 -1
- package/src/ensureAbsolutePath.ts +1 -1
- package/src/getEntryOptions.ts +23 -4
- package/src/getPackageManager.ts +1 -1
- package/src/index.ts +1 -3
- package/src/is/index.ts +1 -1
- package/src/is/type.ts +4 -0
- package/src/monorepo.ts +1 -1
- package/src/path.ts +3 -21
- package/src/prettyInstructions.ts +2 -2
- package/src/readTsConfig.ts +1 -1
- package/src/runtimeExports.ts +32 -31
- package/tests/.eslintrc.js +6 -0
- package/tests/compatRequire.test.ts +1 -1
- package/tests/ensureAbsolutePath.test.ts +7 -4
- package/tests/findExists.test.ts +1 -1
- package/tests/getBrowserslist.test.ts +1 -1
- package/tests/getEntryOptions.test.ts +11 -0
package/dist/types/index.d.ts
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
export * as fs from 'fs-extra';
|
2
|
-
export * as upath from 'upath';
|
3
2
|
export * from './chalk';
|
4
3
|
export * from './formatWebpackMessages';
|
5
4
|
export * from './FileSizeReporter';
|
@@ -23,8 +22,7 @@ export * from './monorepo';
|
|
23
22
|
export * from './getPackageManager';
|
24
23
|
export * from './runtimeExports';
|
25
24
|
export * from './readTsConfig';
|
26
|
-
export
|
27
|
-
export * as path from './path';
|
25
|
+
export * from './path';
|
28
26
|
export * from './generateMetaTags';
|
29
27
|
export * from './prettyInstructions';
|
30
28
|
export * from './alias';
|
package/dist/types/is/type.d.ts
CHANGED
@@ -4,4 +4,5 @@ export declare function isArray(obj: any): obj is any[];
|
|
4
4
|
export declare function isFunction(func: any): func is Function;
|
5
5
|
export declare function isObject(obj: any): obj is object;
|
6
6
|
export declare function isPlainObject(obj: any): obj is Record<string, any>;
|
7
|
-
export declare function isPromise(obj: any): obj is Promise<any>;
|
7
|
+
export declare function isPromise(obj: any): obj is Promise<any>;
|
8
|
+
export declare function isRegExp(obj: any): obj is RegExp;
|
package/dist/types/monorepo.d.ts
CHANGED
package/dist/types/path.d.ts
CHANGED
@@ -1,12 +1,3 @@
|
|
1
|
-
/// <reference types="node" />
|
2
|
-
import path from 'path';
|
3
1
|
export declare const isRelativePath: (test: string) => boolean;
|
4
|
-
export declare const
|
5
|
-
export declare const
|
6
|
-
export declare const relative: (from: string, to: string) => string;
|
7
|
-
export declare const basename: (p: string, ext?: string | undefined) => string;
|
8
|
-
export declare const dirname: (p: string) => string;
|
9
|
-
export declare const extname: (p: string) => string;
|
10
|
-
export declare const isAbsolute: (p: string) => boolean;
|
11
|
-
export declare const normalize: (p: string) => string;
|
12
|
-
export declare const sep: string, win32: path.PlatformPath, posix: path.PlatformPath;
|
2
|
+
export declare const normalizeOutputPath: (s: string) => string;
|
3
|
+
export declare const normalizeToPosixPath: (p: string) => string;
|
@@ -1 +1,6 @@
|
|
1
|
-
|
1
|
+
interface EntryPoint {
|
2
|
+
entryName: string;
|
3
|
+
}
|
4
|
+
export declare const isSingleEntry: (entrypoints: EntryPoint[]) => boolean;
|
5
|
+
export declare const prettyInstructions: (appContext: any, config: any) => string;
|
6
|
+
export {};
|
package/package.json
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "1.
|
14
|
+
"version": "1.1.2",
|
15
15
|
"jsnext:source": "./src/index.ts",
|
16
16
|
"types": "./dist/types/index.d.ts",
|
17
17
|
"main": "./dist/js/node/index.js",
|
@@ -65,8 +65,8 @@
|
|
65
65
|
"@types/recursive-readdir": "^2.2.0",
|
66
66
|
"typescript": "^4",
|
67
67
|
"webpack": "^5.54.0",
|
68
|
-
"@modern-js/plugin-testing": "^1.
|
69
|
-
"@modern-js/module-tools": "^1.
|
68
|
+
"@modern-js/plugin-testing": "^1.1.0",
|
69
|
+
"@modern-js/module-tools": "^1.1.0"
|
70
70
|
},
|
71
71
|
"peerDependencies": {
|
72
72
|
"typescript": "^4.4.3"
|
package/src/FileSizeReporter.ts
CHANGED
@@ -12,13 +12,13 @@
|
|
12
12
|
'use strict';
|
13
13
|
|
14
14
|
import fs from 'fs';
|
15
|
-
import
|
15
|
+
import path from 'path';
|
16
16
|
import chalk from 'chalk';
|
17
17
|
import filesize from 'filesize';
|
18
18
|
import recursive from 'recursive-readdir';
|
19
19
|
import stripAnsi from 'strip-ansi';
|
20
20
|
import gzipSize from 'gzip-size';
|
21
|
-
import { logger } from '
|
21
|
+
import { logger } from './logger';
|
22
22
|
|
23
23
|
|
24
24
|
function canReadAsset(asset:string) {
|
package/src/alias.ts
CHANGED
package/src/applyOptionsChain.ts
CHANGED
package/src/constants.ts
CHANGED
@@ -18,6 +18,11 @@ export const HMR_SOCK_PATH = '/_modern_js_hmr_ws';
|
|
18
18
|
*/
|
19
19
|
export const ROUTE_SPEC_FILE = 'route.json';
|
20
20
|
|
21
|
+
/**
|
22
|
+
* main entry name
|
23
|
+
*/
|
24
|
+
export const MAIN_ENTRY_NAME = 'main';
|
25
|
+
|
21
26
|
/**
|
22
27
|
* open editor request path
|
23
28
|
*/
|
@@ -91,6 +96,14 @@ export const INTERNAL_PLUGINS: {
|
|
91
96
|
},
|
92
97
|
'@modern-js/plugin-jarvis': { cli: '@modern-js/plugin-jarvis/cli' },
|
93
98
|
'@modern-js/plugin-tailwindcss': { cli: '@modern-js/plugin-tailwindcss/cli' },
|
99
|
+
'@modern-js/plugin-lambda-fc': { cli: '@modern-js/plugin-lambda-fc/cli' },
|
100
|
+
'@modern-js/plugin-lambda-scf': { cli: '@modern-js/plugin-lambda-scf/cli' },
|
101
|
+
'@modern-js/plugin-cdn-oss': { cli: '@modern-js/plugin-cdn-oss/cli' },
|
102
|
+
'@modern-js/plugin-cdn-cos': { cli: '@modern-js/plugin-cdn-cos/cli' },
|
103
|
+
'@modern-js/plugin-static-hosting': {
|
104
|
+
cli: '@modern-js/plugin-static-hosting/cli',
|
105
|
+
},
|
106
|
+
'@modern-js/plugin-polyfill': { server: '@modern-js/plugin-polyfill' },
|
94
107
|
};
|
95
108
|
|
96
109
|
/**
|
@@ -175,7 +188,13 @@ export const PLUGIN_SCHEMAS = {
|
|
175
188
|
'@modern-js/plugin-ssg': [
|
176
189
|
{
|
177
190
|
target: 'output.ssg',
|
178
|
-
schema: {
|
191
|
+
schema: {
|
192
|
+
oneOf: [
|
193
|
+
{ type: 'boolean' },
|
194
|
+
{ type: 'object' },
|
195
|
+
{ instanceof: 'Function' },
|
196
|
+
],
|
197
|
+
},
|
179
198
|
},
|
180
199
|
],
|
181
200
|
'@modern-js/plugin-ssr': [
|
package/src/getEntryOptions.ts
CHANGED
@@ -1,18 +1,37 @@
|
|
1
1
|
import { isPlainObject } from './is';
|
2
|
+
import { MAIN_ENTRY_NAME } from './constants';
|
2
3
|
|
3
4
|
export const getEntryOptions = <T>(
|
4
5
|
name: string,
|
5
6
|
baseOptions?: T,
|
6
7
|
optionsByEntries?: Record<string, T>,
|
8
|
+
packageName?: string,
|
7
9
|
) => {
|
8
10
|
if (optionsByEntries) {
|
11
|
+
let optionsByEntry = getOptionsByEntryName(name, optionsByEntries);
|
12
|
+
|
13
|
+
// compatible with main entry using packageName as the key
|
14
|
+
if (
|
15
|
+
optionsByEntry === undefined &&
|
16
|
+
name === MAIN_ENTRY_NAME &&
|
17
|
+
packageName
|
18
|
+
) {
|
19
|
+
optionsByEntry = getOptionsByEntryName(packageName, optionsByEntries);
|
20
|
+
}
|
21
|
+
|
9
22
|
// eslint-disable-next-line no-nested-ternary
|
10
|
-
return
|
11
|
-
? isPlainObject(
|
12
|
-
? { ...baseOptions, ...
|
13
|
-
:
|
23
|
+
return optionsByEntry !== undefined
|
24
|
+
? isPlainObject(optionsByEntry) && isPlainObject(baseOptions)
|
25
|
+
? { ...baseOptions, ...optionsByEntry }
|
26
|
+
: optionsByEntry
|
14
27
|
: baseOptions;
|
15
28
|
} else {
|
16
29
|
return baseOptions;
|
17
30
|
}
|
18
31
|
};
|
32
|
+
|
33
|
+
const getOptionsByEntryName = <T>(
|
34
|
+
name: string,
|
35
|
+
optionsByEntries: Record<string, T>,
|
36
|
+
) =>
|
37
|
+
optionsByEntries.hasOwnProperty(name) ? optionsByEntries[name] : undefined;
|
package/src/getPackageManager.ts
CHANGED
package/src/index.ts
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
export * as fs from 'fs-extra';
|
2
|
-
export * as upath from 'upath';
|
3
2
|
export * from './chalk';
|
4
3
|
export * from './formatWebpackMessages';
|
5
4
|
export * from './FileSizeReporter';
|
@@ -23,8 +22,7 @@ export * from './monorepo';
|
|
23
22
|
export * from './getPackageManager';
|
24
23
|
export * from './runtimeExports';
|
25
24
|
export * from './readTsConfig';
|
26
|
-
export
|
27
|
-
export * as path from './path';
|
25
|
+
export * from './path';
|
28
26
|
export * from './generateMetaTags';
|
29
27
|
export * from './prettyInstructions';
|
30
28
|
export * from './alias';
|
package/src/is/index.ts
CHANGED
package/src/is/type.ts
CHANGED
package/src/monorepo.ts
CHANGED
package/src/path.ts
CHANGED
@@ -4,24 +4,6 @@ import upath from 'upath';
|
|
4
4
|
export const isRelativePath = (test: string): boolean =>
|
5
5
|
/^\.\.?($|[\\/])/.test(test);
|
6
6
|
|
7
|
-
export const
|
8
|
-
|
9
|
-
|
10
|
-
export const resolve = (...paths: string[]) =>
|
11
|
-
upath.normalizeSafe(path.resolve(...paths));
|
12
|
-
|
13
|
-
export const relative = (from: string, to: string) =>
|
14
|
-
upath.normalizeSafe(path.relative(from, to));
|
15
|
-
|
16
|
-
export const basename = (p: string, ext?: string) =>
|
17
|
-
upath.normalizeSafe(path.basename(p, ext));
|
18
|
-
|
19
|
-
export const dirname = (p: string) => upath.normalizeSafe(path.dirname(p));
|
20
|
-
|
21
|
-
export const extname = (p: string) => path.extname(p);
|
22
|
-
|
23
|
-
export const isAbsolute = (p: string) => path.isAbsolute(p);
|
24
|
-
|
25
|
-
export const normalize = (p: string) => upath.normalizeSafe(path.normalize(p));
|
26
|
-
|
27
|
-
export const { sep, win32, posix } = path;
|
7
|
+
export const normalizeOutputPath = (s: string) => s.replace(/\\/g, '\\\\');
|
8
|
+
export const normalizeToPosixPath = (p: string) =>
|
9
|
+
upath.normalizeSafe(path.normalize(p));
|
@@ -14,7 +14,7 @@ interface ServerRoute {
|
|
14
14
|
}
|
15
15
|
|
16
16
|
// TODO: remove hard code 'main'
|
17
|
-
const
|
17
|
+
export const isSingleEntry = (entrypoints: EntryPoint[]) =>
|
18
18
|
entrypoints.length === 1 && entrypoints[0].entryName === 'main';
|
19
19
|
|
20
20
|
const normalizeUrl = (url: string) => url.replace(/([^:]\/)\/+/g, '$1');
|
@@ -62,7 +62,7 @@ export const prettyInstructions = (appContext: any, config: any) => {
|
|
62
62
|
|
63
63
|
let message = 'App running at:\n\n';
|
64
64
|
|
65
|
-
if (
|
65
|
+
if (isSingleEntry(entrypoints)) {
|
66
66
|
message += urls
|
67
67
|
.map(
|
68
68
|
({ type, url }) =>
|
package/src/readTsConfig.ts
CHANGED
package/src/runtimeExports.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
+
import path from 'path';
|
1
2
|
import fs from 'fs-extra';
|
2
|
-
import
|
3
|
+
import { normalizeOutputPath } from './path';
|
3
4
|
|
4
5
|
const memo = <T extends (...args: any[]) => any>(fn: T) => {
|
5
6
|
const cache = new Map();
|
@@ -19,36 +20,36 @@ const memo = <T extends (...args: any[]) => any>(fn: T) => {
|
|
19
20
|
};
|
20
21
|
};
|
21
22
|
|
22
|
-
export const createRuntimeExportsUtils = memo(
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
}
|
42
|
-
} catch {
|
43
|
-
// FIXME:
|
23
|
+
export const createRuntimeExportsUtils = memo((pwd = '', namespace: string) => {
|
24
|
+
const entryExportFile = path.join(
|
25
|
+
pwd,
|
26
|
+
`.runtime-exports/${namespace ? `${namespace}.js` : 'index.js'}`,
|
27
|
+
);
|
28
|
+
|
29
|
+
// const ensure = () => {
|
30
|
+
// if (!fs.existsSync(entryExportFile)) {
|
31
|
+
// fs.outputFileSync(entryExportFile, '');
|
32
|
+
// }
|
33
|
+
// fs.ensureFileSync(entryExportFile);
|
34
|
+
// };
|
35
|
+
|
36
|
+
const addExport = (statement: string) => {
|
37
|
+
// eslint-disable-next-line no-param-reassign
|
38
|
+
statement = normalizeOutputPath(statement);
|
39
|
+
try {
|
40
|
+
fs.ensureFileSync(entryExportFile);
|
41
|
+
if (!fs.readFileSync(entryExportFile, 'utf8').includes(statement)) {
|
42
|
+
fs.appendFileSync(entryExportFile, `${statement}\n`);
|
44
43
|
}
|
45
|
-
}
|
44
|
+
} catch {
|
45
|
+
// FIXME:
|
46
|
+
}
|
47
|
+
};
|
46
48
|
|
47
|
-
|
49
|
+
const getPath = () => entryExportFile;
|
48
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
);
|
51
|
+
return {
|
52
|
+
addExport,
|
53
|
+
getPath,
|
54
|
+
};
|
55
|
+
});
|
@@ -1,13 +1,16 @@
|
|
1
|
+
import path from 'path';
|
1
2
|
import { ensureAbsolutePath } from '@/ensureAbsolutePath';
|
2
3
|
|
3
4
|
describe('ensure absolute file path', () => {
|
4
5
|
test('should return filePath directly', () => {
|
5
|
-
expect(
|
6
|
-
'/a/b/c/d/e.jsx',
|
7
|
-
);
|
6
|
+
expect(
|
7
|
+
path.isAbsolute(ensureAbsolutePath('/a/b', '/a/b/c/d/e.jsx')),
|
8
|
+
).toBeTruthy();
|
8
9
|
});
|
9
10
|
|
10
11
|
test(`should resolve absolute path`, () => {
|
11
|
-
expect(
|
12
|
+
expect(
|
13
|
+
path.isAbsolute(ensureAbsolutePath('/a/b', 'c/d/e.jsx')),
|
14
|
+
).toBeTruthy();
|
12
15
|
});
|
13
16
|
});
|
package/tests/findExists.test.ts
CHANGED
@@ -15,6 +15,17 @@ describe('get entry options', () => {
|
|
15
15
|
).toEqual('a');
|
16
16
|
});
|
17
17
|
|
18
|
+
test(`should compatible with main entry using packageName as key`, () => {
|
19
|
+
expect(
|
20
|
+
getEntryOptions(
|
21
|
+
'main',
|
22
|
+
'default value',
|
23
|
+
{ 'package-name': 'a' },
|
24
|
+
'package-name',
|
25
|
+
),
|
26
|
+
).toEqual('a');
|
27
|
+
});
|
28
|
+
|
18
29
|
expect(
|
19
30
|
getEntryOptions(
|
20
31
|
'page-a',
|