@modern-js/utils 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +11 -0
- package/dist/analyzeProject.d.ts +1 -0
- package/dist/analyzeProject.js +44 -0
- package/dist/constants.d.ts +1 -1
- package/dist/constants.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/prettyInstructions.js +3 -3
- package/dist/storage.js +1 -1
- package/package.json +4 -2
- package/tests/.eslintrc.js +0 -6
- package/tests/__snapshots__/prettyInstructions.test.ts.snap +0 -19
- package/tests/applyOptionsChain.test.ts +0 -100
- package/tests/compatRequire.test.ts +0 -32
- package/tests/debug.test.ts +0 -25
- package/tests/ensureAbsolutePath.test.ts +0 -16
- package/tests/findExists.test.ts +0 -22
- package/tests/fixtures/browserlist/develop/package.json +0 -11
- package/tests/fixtures/browserlist/dotfile/.browserslistrc +0 -3
- package/tests/fixtures/browserlist/pkg/package.json +0 -7
- package/tests/fixtures/compat-require/cjs.js +0 -1
- package/tests/fixtures/compat-require/empty.js +0 -1
- package/tests/fixtures/compat-require/esm.js +0 -5
- package/tests/fixtures/compat-require/foo.js +0 -3
- package/tests/fixtures/file-exists/a.ejs +0 -0
- package/tests/fixtures/file-exists/a.js/.gitkeep +0 -0
- package/tests/fixtures/file-exists/a.mjs +0 -0
- package/tests/fixtures/file-exists/a.ts +0 -0
- package/tests/format.test.ts +0 -60
- package/tests/generateMetaTags.test.ts +0 -19
- package/tests/getBrowserslist.test.ts +0 -32
- package/tests/getCacheIdentifier.test.ts +0 -37
- package/tests/getEntryOptions.test.ts +0 -53
- package/tests/index.test.ts +0 -18
- package/tests/is.test.ts +0 -27
- package/tests/isPlatform.test.ts +0 -19
- package/tests/isType.test.ts +0 -85
- package/tests/logger.test.ts +0 -53
- package/tests/prettyInstructions.test.ts +0 -139
- package/tests/removeSlash.test.ts +0 -19
- package/tests/tsconfig.json +0 -11
- package/tests/wait.ts +0 -38
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# @modern-js/utils
|
2
2
|
|
3
|
+
## 1.5.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- 3bf4f8b0: feat: support start api server only
|
8
|
+
|
9
|
+
### Patch Changes
|
10
|
+
|
11
|
+
- b8599d09: fix: failed to generate webpack cache
|
12
|
+
- 60f7d8bf: feat: add tests dir to npmignore
|
13
|
+
|
3
14
|
## 1.4.1
|
4
15
|
|
5
16
|
### Patch Changes
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const isApiOnly: (appDirectory: string) => Promise<boolean>;
|
@@ -0,0 +1,44 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5
|
+
}) : (function(o, m, k, k2) {
|
6
|
+
if (k2 === undefined) k2 = k;
|
7
|
+
o[k2] = m[k];
|
8
|
+
}));
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11
|
+
}) : function(o, v) {
|
12
|
+
o["default"] = v;
|
13
|
+
});
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
15
|
+
if (mod && mod.__esModule) return mod;
|
16
|
+
var result = {};
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18
|
+
__setModuleDefault(result, mod);
|
19
|
+
return result;
|
20
|
+
};
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
28
|
+
});
|
29
|
+
};
|
30
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
31
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
32
|
+
};
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
34
|
+
exports.isApiOnly = void 0;
|
35
|
+
const path = __importStar(require("path"));
|
36
|
+
const minimist_1 = __importDefault(require("minimist"));
|
37
|
+
const compiled_1 = require("./compiled");
|
38
|
+
const isApiOnly = (appDirectory) => __awaiter(void 0, void 0, void 0, function* () {
|
39
|
+
const srcDir = path.join(appDirectory, 'src');
|
40
|
+
const existSrc = yield compiled_1.fs.pathExists(srcDir);
|
41
|
+
const options = (0, minimist_1.default)(process.argv.slice(2));
|
42
|
+
return !existSrc || Boolean(options['api-only']);
|
43
|
+
});
|
44
|
+
exports.isApiOnly = isApiOnly;
|
package/dist/constants.d.ts
CHANGED
@@ -39,7 +39,7 @@ export declare const SHARED_DIR = "shared";
|
|
39
39
|
/**
|
40
40
|
* Modern.config.ts cached dir
|
41
41
|
*/
|
42
|
-
export declare const CONFIG_CACHE_DIR = "./node_modules/.node-bundle-require";
|
42
|
+
export declare const CONFIG_CACHE_DIR = "./node_modules/.cache/node-bundle-require";
|
43
43
|
export declare const CONFIG_FILE_EXTENSIONS: string[];
|
44
44
|
/**
|
45
45
|
* Serialized config path
|
package/dist/constants.js
CHANGED
@@ -42,7 +42,7 @@ exports.SHARED_DIR = 'shared';
|
|
42
42
|
/**
|
43
43
|
* Modern.config.ts cached dir
|
44
44
|
*/
|
45
|
-
exports.CONFIG_CACHE_DIR = './node_modules/.node-bundle-require';
|
45
|
+
exports.CONFIG_CACHE_DIR = './node_modules/.cache/node-bundle-require';
|
46
46
|
exports.CONFIG_FILE_EXTENSIONS = ['.js', '.ts', '.ejs', '.mjs'];
|
47
47
|
/**
|
48
48
|
* Serialized config path
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -33,13 +33,13 @@ const getAddressUrls = (protocol = 'http', port) => {
|
|
33
33
|
}, []);
|
34
34
|
};
|
35
35
|
const prettyInstructions = (appContext, config) => {
|
36
|
-
const { entrypoints, serverRoutes, port,
|
36
|
+
const { entrypoints, serverRoutes, port, apiOnly, checkedEntries } = appContext;
|
37
37
|
const urls = getAddressUrls(config.dev.https && (0, is_1.isDev)() ? 'https' : 'http', port);
|
38
|
-
const routes =
|
38
|
+
const routes = !apiOnly
|
39
39
|
? serverRoutes.filter(route => route.entryName)
|
40
40
|
: serverRoutes;
|
41
41
|
let message = 'App running at:\n\n';
|
42
|
-
if ((0, exports.isSingleEntry)(entrypoints) ||
|
42
|
+
if ((0, exports.isSingleEntry)(entrypoints) || apiOnly) {
|
43
43
|
message += urls
|
44
44
|
.map(({ type, url }) => ` ${compiled_1.chalk.bold(`> ${type.padEnd(10)}`)}${compiled_1.chalk.cyanBright(normalizeUrl(`${url}/${routes[0].urlPath}`))}\n`)
|
45
45
|
.join('');
|
package/dist/storage.js
CHANGED
@@ -49,7 +49,7 @@ const createStorage = () => {
|
|
49
49
|
}
|
50
50
|
const context = storage.getStore();
|
51
51
|
if (!context) {
|
52
|
-
throw new Error(`Can't call useContext out of scope,
|
52
|
+
throw new Error(`Can't call useContext out of scope, make sure @modern-js/utils is a single version in node_modules`);
|
53
53
|
}
|
54
54
|
return context;
|
55
55
|
};
|
package/package.json
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "1.
|
14
|
+
"version": "1.5.0",
|
15
15
|
"jsnext:source": "./src/index.ts",
|
16
16
|
"types": "./dist/index.d.ts",
|
17
17
|
"main": "./dist/index.js",
|
@@ -57,10 +57,11 @@
|
|
57
57
|
},
|
58
58
|
"dependencies": {
|
59
59
|
"@babel/runtime": "^7",
|
60
|
+
"minimist": "^1.2.0",
|
60
61
|
"typescript": "^4"
|
61
62
|
},
|
62
63
|
"devDependencies": {
|
63
|
-
"@modern-js/types": "^1.
|
64
|
+
"@modern-js/types": "^1.5.0",
|
64
65
|
"@scripts/build": "0.0.0",
|
65
66
|
"@scripts/jest-config": "0.0.0",
|
66
67
|
"@types/debug": "4.1.7",
|
@@ -69,6 +70,7 @@
|
|
69
70
|
"@types/jest": "^26",
|
70
71
|
"@types/js-yaml": "4.0.5",
|
71
72
|
"@types/lodash": "4.14.181",
|
73
|
+
"@types/minimist": "^1.2.2",
|
72
74
|
"@types/node": "^14",
|
73
75
|
"@types/recursive-readdir": "2.2.0",
|
74
76
|
"@types/signale": "1.4.4",
|
package/tests/.eslintrc.js
DELETED
@@ -1,19 +0,0 @@
|
|
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,100 +0,0 @@
|
|
1
|
-
import { applyOptionsChain } from '../src/applyOptionsChain';
|
2
|
-
|
3
|
-
describe('apply options chain', () => {
|
4
|
-
test(`should return default options`, () => {
|
5
|
-
expect(applyOptionsChain({ default: 'a' })).toEqual({ default: 'a' });
|
6
|
-
});
|
7
|
-
|
8
|
-
test(`should merge default options`, () => {
|
9
|
-
expect(
|
10
|
-
applyOptionsChain(
|
11
|
-
{ name: 'a' },
|
12
|
-
{
|
13
|
-
name: 'b',
|
14
|
-
custom: 'c',
|
15
|
-
},
|
16
|
-
),
|
17
|
-
).toEqual({
|
18
|
-
name: 'b',
|
19
|
-
custom: 'c',
|
20
|
-
});
|
21
|
-
});
|
22
|
-
|
23
|
-
test(`should support custom merge function`, () => {
|
24
|
-
const merge = (target: any, source: any) => {
|
25
|
-
for (const key in source) {
|
26
|
-
if (target.hasOwnProperty(key)) {
|
27
|
-
target[key] += source[key];
|
28
|
-
} else {
|
29
|
-
target[key] = source[key];
|
30
|
-
}
|
31
|
-
}
|
32
|
-
return target;
|
33
|
-
};
|
34
|
-
|
35
|
-
expect(
|
36
|
-
applyOptionsChain(
|
37
|
-
{
|
38
|
-
a: 1,
|
39
|
-
b: 'b',
|
40
|
-
},
|
41
|
-
{
|
42
|
-
a: 2,
|
43
|
-
b: 'b',
|
44
|
-
c: 'c',
|
45
|
-
},
|
46
|
-
{},
|
47
|
-
merge,
|
48
|
-
),
|
49
|
-
).toEqual({
|
50
|
-
a: 3,
|
51
|
-
b: 'bb',
|
52
|
-
c: 'c',
|
53
|
-
});
|
54
|
-
});
|
55
|
-
|
56
|
-
test(`should support funciton or object array`, () => {
|
57
|
-
const defaults = { a: 'a' };
|
58
|
-
|
59
|
-
const options = [
|
60
|
-
{ b: 'b' },
|
61
|
-
(o: any, { add }: { add: any }) => {
|
62
|
-
o.c = add(1, 2);
|
63
|
-
},
|
64
|
-
(o: any) => ({
|
65
|
-
...o,
|
66
|
-
d: 'd',
|
67
|
-
}),
|
68
|
-
{ e: 'e' },
|
69
|
-
];
|
70
|
-
expect(
|
71
|
-
applyOptionsChain(defaults, options as any, {
|
72
|
-
add: (a: any, b: any) => a + b,
|
73
|
-
}),
|
74
|
-
).toEqual({
|
75
|
-
a: 'a',
|
76
|
-
b: 'b',
|
77
|
-
c: 3,
|
78
|
-
d: 'd',
|
79
|
-
e: 'e',
|
80
|
-
});
|
81
|
-
});
|
82
|
-
|
83
|
-
test(`should throw error`, () => {
|
84
|
-
expect(() => {
|
85
|
-
applyOptionsChain(1, 2);
|
86
|
-
}).toThrow(/^applyOptionsChain error:/);
|
87
|
-
});
|
88
|
-
|
89
|
-
test(`should log warning about function result`, () => {
|
90
|
-
let outputs = '';
|
91
|
-
/* eslint-disable no-console */
|
92
|
-
console.log = jest.fn(input => (outputs += input));
|
93
|
-
/* eslint-enable no-console */
|
94
|
-
applyOptionsChain({ name: 'a' } as any, [() => 111]);
|
95
|
-
|
96
|
-
expect(outputs).toContain(
|
97
|
-
'Function should mutate the config and return nothing, Or return a cloned or merged version of config object.',
|
98
|
-
);
|
99
|
-
});
|
100
|
-
});
|
@@ -1,32 +0,0 @@
|
|
1
|
-
import path from 'path';
|
2
|
-
import { compatRequire, cleanRequireCache } from '../src/compatRequire';
|
3
|
-
|
4
|
-
describe('compat require', () => {
|
5
|
-
const fixturePath = path.resolve(__dirname, './fixtures/compat-require');
|
6
|
-
|
7
|
-
test(`should support default property`, () => {
|
8
|
-
expect(compatRequire(path.join(fixturePath, 'esm.js'))).toEqual({
|
9
|
-
name: 'esm',
|
10
|
-
});
|
11
|
-
});
|
12
|
-
|
13
|
-
test(`should support commonjs module`, () => {
|
14
|
-
expect(compatRequire(path.join(fixturePath, 'cjs.js'))).toEqual({
|
15
|
-
name: 'cjs',
|
16
|
-
});
|
17
|
-
});
|
18
|
-
|
19
|
-
test(`should return null`, () => {
|
20
|
-
expect(compatRequire(path.join(fixturePath, 'empty.js'))).toEqual(null);
|
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
|
-
});
|
32
|
-
});
|
package/tests/debug.test.ts
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
import { debug } from '../src/compiled';
|
2
|
-
import { stripAnsi } from '../src';
|
3
|
-
import { createDebugger } from '../src/debug';
|
4
|
-
|
5
|
-
describe('debug utility', () => {
|
6
|
-
test('should return file path', () => {
|
7
|
-
const debug1 = createDebugger('test');
|
8
|
-
|
9
|
-
const debug2 = createDebugger('test2');
|
10
|
-
|
11
|
-
debug.enable('modern-js:test2');
|
12
|
-
|
13
|
-
debug.log = (...args) => {
|
14
|
-
// XXX: args[0] 的内容有时候是 ' \x1B[38;5;167;1mmodern-js:test2 \x1B[0m22222' 这种格式
|
15
|
-
expect(stripAnsi(args[0])).toContain('modern-js:test2 22222');
|
16
|
-
};
|
17
|
-
|
18
|
-
debug1('11111');
|
19
|
-
debug2('22222');
|
20
|
-
|
21
|
-
expect(debug2.enabled).toBe(true);
|
22
|
-
|
23
|
-
expect(debug1.enabled).toBe(false);
|
24
|
-
});
|
25
|
-
});
|
@@ -1,16 +0,0 @@
|
|
1
|
-
import path from 'path';
|
2
|
-
import { ensureAbsolutePath } from '../src/ensureAbsolutePath';
|
3
|
-
|
4
|
-
describe('ensure absolute file path', () => {
|
5
|
-
test('should return filePath directly', () => {
|
6
|
-
expect(
|
7
|
-
path.isAbsolute(ensureAbsolutePath('/a/b', '/a/b/c/d/e.jsx')),
|
8
|
-
).toBeTruthy();
|
9
|
-
});
|
10
|
-
|
11
|
-
test(`should resolve absolute path`, () => {
|
12
|
-
expect(
|
13
|
-
path.isAbsolute(ensureAbsolutePath('/a/b', 'c/d/e.jsx')),
|
14
|
-
).toBeTruthy();
|
15
|
-
});
|
16
|
-
});
|
package/tests/findExists.test.ts
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
import path from 'path';
|
2
|
-
import { findExists } from '../src/findExists';
|
3
|
-
|
4
|
-
describe('find first exists file', () => {
|
5
|
-
const fixturePath = path.resolve(__dirname, './fixtures/file-exists');
|
6
|
-
|
7
|
-
test('should return file path', () => {
|
8
|
-
expect(
|
9
|
-
findExists(
|
10
|
-
['.js', '.ts', '.ejs', '.mjs'].map(ext =>
|
11
|
-
path.join(fixturePath, `a${ext}`),
|
12
|
-
),
|
13
|
-
),
|
14
|
-
).toEqual(path.join(fixturePath, 'a.ts'));
|
15
|
-
});
|
16
|
-
|
17
|
-
test('should return false when no file exists', () => {
|
18
|
-
expect(
|
19
|
-
findExists(['.jsx'].map(ext => path.join(fixturePath, `a${ext}`))),
|
20
|
-
).toBe(false);
|
21
|
-
});
|
22
|
-
});
|
@@ -1 +0,0 @@
|
|
1
|
-
module.exports = { name: 'cjs' };
|
@@ -1 +0,0 @@
|
|
1
|
-
module.exports = null;
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
package/tests/format.test.ts
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
import { formatProxyOptions } from '../src/format';
|
2
|
-
|
3
|
-
describe('test format', () => {
|
4
|
-
it('should format correctly use simply options', async () => {
|
5
|
-
const proxy = {
|
6
|
-
'/simple': `http://localhost`,
|
7
|
-
};
|
8
|
-
|
9
|
-
const options = formatProxyOptions(proxy);
|
10
|
-
expect(options).toEqual([
|
11
|
-
{
|
12
|
-
context: '/simple',
|
13
|
-
changeOrigin: true,
|
14
|
-
logLevel: 'warn',
|
15
|
-
target: 'http://localhost',
|
16
|
-
},
|
17
|
-
]);
|
18
|
-
});
|
19
|
-
|
20
|
-
it('should format correctly use simply options', async () => {
|
21
|
-
const proxy = {
|
22
|
-
'/simple-obj': {
|
23
|
-
target: `http://localhost`,
|
24
|
-
},
|
25
|
-
};
|
26
|
-
const options = formatProxyOptions(proxy);
|
27
|
-
expect(options).toEqual([
|
28
|
-
{
|
29
|
-
context: '/simple-obj',
|
30
|
-
changeOrigin: true,
|
31
|
-
logLevel: 'warn',
|
32
|
-
target: 'http://localhost',
|
33
|
-
},
|
34
|
-
]);
|
35
|
-
});
|
36
|
-
|
37
|
-
it('should format correctly use simply options', async () => {
|
38
|
-
const proxy = {
|
39
|
-
context: '/context',
|
40
|
-
target: `http://localhost`,
|
41
|
-
};
|
42
|
-
const options = formatProxyOptions(proxy);
|
43
|
-
expect(options).toEqual([
|
44
|
-
{ context: '/context', target: 'http://localhost' },
|
45
|
-
]);
|
46
|
-
});
|
47
|
-
|
48
|
-
it('should format correctly use simply options', async () => {
|
49
|
-
const proxy = [
|
50
|
-
{
|
51
|
-
context: '/array',
|
52
|
-
target: `http://localhost`,
|
53
|
-
},
|
54
|
-
];
|
55
|
-
const options = formatProxyOptions(proxy);
|
56
|
-
expect(options).toEqual([
|
57
|
-
{ context: '/array', target: 'http://localhost' },
|
58
|
-
]);
|
59
|
-
});
|
60
|
-
});
|
@@ -1,19 +0,0 @@
|
|
1
|
-
import { generateMetaTags } from '../src/generateMetaTags';
|
2
|
-
|
3
|
-
describe('generateMetaTags', () => {
|
4
|
-
it('should return empty string when params is empty', () => {
|
5
|
-
expect(generateMetaTags()).toEqual('');
|
6
|
-
});
|
7
|
-
|
8
|
-
it('should generate meta tag correctly', () => {
|
9
|
-
const options = {
|
10
|
-
foo: {
|
11
|
-
name: 'viewport',
|
12
|
-
content: 'width=500, initial-scale=1',
|
13
|
-
},
|
14
|
-
};
|
15
|
-
expect(generateMetaTags(options).trim()).toEqual(
|
16
|
-
'<meta name="viewport" content="width=500, initial-scale=1">',
|
17
|
-
);
|
18
|
-
});
|
19
|
-
});
|
@@ -1,32 +0,0 @@
|
|
1
|
-
import path from 'path';
|
2
|
-
import { getBrowserslist, defaults } from '../src/getBrowserslist';
|
3
|
-
|
4
|
-
describe('get browserslist', () => {
|
5
|
-
const fixture = path.resolve(__dirname, './fixtures/browserlist');
|
6
|
-
|
7
|
-
test(`should load browserslist from package.json`, () => {
|
8
|
-
expect(getBrowserslist(path.join(fixture, 'pkg'))).toEqual([
|
9
|
-
'not IE 11',
|
10
|
-
'maintained node versions',
|
11
|
-
]);
|
12
|
-
});
|
13
|
-
|
14
|
-
test(`should load browerslist from .browserslistrc`, () => {
|
15
|
-
expect(getBrowserslist(path.join(fixture, 'dotfile'))).toEqual([
|
16
|
-
'defaults',
|
17
|
-
'not IE 11',
|
18
|
-
'maintained node versions',
|
19
|
-
]);
|
20
|
-
});
|
21
|
-
|
22
|
-
test(`should load browserslist base on environment`, () => {
|
23
|
-
process.env.NODE_ENV = 'development';
|
24
|
-
expect(getBrowserslist(path.join(fixture, 'develop'))).toEqual([
|
25
|
-
'last 1 chrome version',
|
26
|
-
]);
|
27
|
-
});
|
28
|
-
|
29
|
-
test(`should return default browserslist config`, () => {
|
30
|
-
expect(getBrowserslist(path.join(fixture))).toEqual(defaults);
|
31
|
-
});
|
32
|
-
});
|
@@ -1,37 +0,0 @@
|
|
1
|
-
import { getCacheIdentifier } from '../src/getCacheIdentifier';
|
2
|
-
|
3
|
-
describe('get cache identifier', () => {
|
4
|
-
test('should use package name and version', () => {
|
5
|
-
expect(
|
6
|
-
getCacheIdentifier([
|
7
|
-
{
|
8
|
-
name: 'package-a',
|
9
|
-
version: '0.1.0',
|
10
|
-
},
|
11
|
-
{
|
12
|
-
name: 'package-b',
|
13
|
-
version: '1.0.1',
|
14
|
-
},
|
15
|
-
]),
|
16
|
-
).toEqual(`test:package-a@0.1.0:package-b@1.0.1`);
|
17
|
-
});
|
18
|
-
|
19
|
-
test(`should return identifier with node environment`, () => {
|
20
|
-
const packages = [
|
21
|
-
{
|
22
|
-
name: 'a',
|
23
|
-
version: '0.1.0',
|
24
|
-
},
|
25
|
-
];
|
26
|
-
|
27
|
-
process.env.NODE_ENV = 'development';
|
28
|
-
expect(getCacheIdentifier(packages)).toEqual('development:a@0.1.0');
|
29
|
-
|
30
|
-
process.env.NODE_ENV = 'production';
|
31
|
-
expect(getCacheIdentifier(packages)).toEqual('production:a@0.1.0');
|
32
|
-
|
33
|
-
process.env.NODE_ENV = '';
|
34
|
-
|
35
|
-
expect(getCacheIdentifier(packages)).toEqual(`:a@0.1.0`);
|
36
|
-
});
|
37
|
-
});
|
@@ -1,53 +0,0 @@
|
|
1
|
-
import { getEntryOptions } from '../src/getEntryOptions';
|
2
|
-
|
3
|
-
describe('get entry options', () => {
|
4
|
-
test('should return default options', () => {
|
5
|
-
expect(getEntryOptions('page-a', 'default value')).toEqual('default value');
|
6
|
-
|
7
|
-
expect(
|
8
|
-
getEntryOptions('page-a', 'default value', { 'page-b': 'a' }),
|
9
|
-
).toEqual('default value');
|
10
|
-
});
|
11
|
-
|
12
|
-
test(`should return options from optionsByEntries`, () => {
|
13
|
-
expect(
|
14
|
-
getEntryOptions('page-a', 'default value', { 'page-a': 'a' }),
|
15
|
-
).toEqual('a');
|
16
|
-
});
|
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
|
-
|
29
|
-
expect(
|
30
|
-
getEntryOptions(
|
31
|
-
'page-a',
|
32
|
-
{ name: 'a' },
|
33
|
-
{
|
34
|
-
'page-a': {
|
35
|
-
name: 'page-a',
|
36
|
-
about: 'b',
|
37
|
-
},
|
38
|
-
},
|
39
|
-
),
|
40
|
-
).toEqual({
|
41
|
-
name: 'page-a',
|
42
|
-
about: 'b',
|
43
|
-
});
|
44
|
-
|
45
|
-
expect(
|
46
|
-
getEntryOptions<
|
47
|
-
| {
|
48
|
-
[name: string]: string;
|
49
|
-
}
|
50
|
-
| boolean
|
51
|
-
>('page-a', { name: 'a' }, { 'page-a': false }),
|
52
|
-
).toEqual(false);
|
53
|
-
});
|
package/tests/index.test.ts
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
import { canUseNpm, canUsePnpm, canUseYarn } from '../src';
|
2
|
-
|
3
|
-
jest.setTimeout(40000);
|
4
|
-
|
5
|
-
describe('test generator utils', () => {
|
6
|
-
test('test canUseNpm', async () => {
|
7
|
-
const npmAbility = await canUseNpm();
|
8
|
-
expect(typeof npmAbility === 'boolean').toBe(true);
|
9
|
-
});
|
10
|
-
test('test canUsePnpm', async () => {
|
11
|
-
const pnpmAbility = await canUsePnpm();
|
12
|
-
expect(typeof pnpmAbility === 'boolean').toBe(true);
|
13
|
-
});
|
14
|
-
test('test canUseYarn', async () => {
|
15
|
-
const yarnAbility = await canUseYarn();
|
16
|
-
expect(typeof yarnAbility === 'boolean').toBe(true);
|
17
|
-
});
|
18
|
-
});
|
package/tests/is.test.ts
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
import { isEmpty, isFastRefresh } from '../src/is';
|
2
|
-
|
3
|
-
describe('validate', () => {
|
4
|
-
it('should validate empty object correctly', () => {
|
5
|
-
expect(isEmpty({})).toBeTruthy();
|
6
|
-
expect(isEmpty({ foo: 'bar' })).toBeFalsy();
|
7
|
-
});
|
8
|
-
|
9
|
-
it('should validate fast refresh correctly', () => {
|
10
|
-
const { NODE_ENV, FAST_REFRESH } = process.env;
|
11
|
-
|
12
|
-
process.env.NODE_ENV = 'development';
|
13
|
-
process.env.FAST_REFRESH = 'true';
|
14
|
-
expect(isFastRefresh()).toBeTruthy();
|
15
|
-
|
16
|
-
process.env.NODE_ENV = 'production';
|
17
|
-
process.env.FAST_REFRESH = 'true';
|
18
|
-
expect(isFastRefresh()).toBeFalsy();
|
19
|
-
|
20
|
-
process.env.NODE_ENV = 'development';
|
21
|
-
process.env.FAST_REFRESH = 'false';
|
22
|
-
expect(isFastRefresh()).toBeFalsy();
|
23
|
-
|
24
|
-
process.env.NODE_ENV = NODE_ENV;
|
25
|
-
process.env.FAST_REFRESH = FAST_REFRESH;
|
26
|
-
});
|
27
|
-
});
|
package/tests/isPlatform.test.ts
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
import { isNodeJS, isBrowser } from '../src/is/platform';
|
2
|
-
|
3
|
-
describe('validate platform', () => {
|
4
|
-
it('should validate Node.js correctly', () => {
|
5
|
-
expect(isNodeJS()).toBeTruthy();
|
6
|
-
});
|
7
|
-
|
8
|
-
it('should validate browser correctly', () => {
|
9
|
-
const windowSpy = jest.spyOn(window, 'window', 'get');
|
10
|
-
|
11
|
-
windowSpy.mockImplementation((): any => undefined);
|
12
|
-
expect(isBrowser()).toBeFalsy();
|
13
|
-
|
14
|
-
windowSpy.mockImplementation((): any => global);
|
15
|
-
expect(isBrowser()).toBeTruthy();
|
16
|
-
|
17
|
-
windowSpy.mockRestore();
|
18
|
-
});
|
19
|
-
});
|
package/tests/isType.test.ts
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
isArray,
|
3
|
-
isObject,
|
4
|
-
isString,
|
5
|
-
isRegExp,
|
6
|
-
isPromise,
|
7
|
-
isUndefined,
|
8
|
-
isPlainObject,
|
9
|
-
} from '../src/is/type';
|
10
|
-
|
11
|
-
describe('validate type', () => {
|
12
|
-
it('should validate string correctly', () => {
|
13
|
-
expect(isString('')).toBeTruthy();
|
14
|
-
expect(isString('foo')).toBeTruthy();
|
15
|
-
expect(isString(null)).toBeFalsy();
|
16
|
-
expect(isString(123)).toBeFalsy();
|
17
|
-
});
|
18
|
-
|
19
|
-
it('should validate undefined correctly', () => {
|
20
|
-
expect(isUndefined(undefined)).toBeTruthy();
|
21
|
-
expect(isUndefined(null)).toBeFalsy();
|
22
|
-
expect(isUndefined('')).toBeFalsy();
|
23
|
-
expect(isUndefined(123)).toBeFalsy();
|
24
|
-
});
|
25
|
-
|
26
|
-
it('should validate array correctly', () => {
|
27
|
-
expect(isArray(undefined)).toBeFalsy();
|
28
|
-
expect(isArray(null)).toBeFalsy();
|
29
|
-
expect(isArray('')).toBeFalsy();
|
30
|
-
expect(isArray(123)).toBeFalsy();
|
31
|
-
expect(isArray({})).toBeFalsy();
|
32
|
-
expect(isArray([])).toBeTruthy();
|
33
|
-
});
|
34
|
-
|
35
|
-
it('should validate object correctly', () => {
|
36
|
-
expect(isObject(1)).toBeFalsy();
|
37
|
-
expect(isObject('1')).toBeFalsy();
|
38
|
-
expect(isObject(undefined)).toBeFalsy();
|
39
|
-
expect(isObject(null)).toBeFalsy();
|
40
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
41
|
-
expect(isObject(() => {})).toBeFalsy();
|
42
|
-
expect(isObject({})).toBeTruthy();
|
43
|
-
expect(isObject([])).toBeTruthy();
|
44
|
-
expect(isObject(/foo/)).toBeTruthy();
|
45
|
-
});
|
46
|
-
|
47
|
-
it('should validate plain object correctly', () => {
|
48
|
-
expect(isPlainObject(1)).toBeFalsy();
|
49
|
-
expect(isPlainObject('1')).toBeFalsy();
|
50
|
-
expect(isPlainObject(undefined)).toBeFalsy();
|
51
|
-
expect(isPlainObject(null)).toBeFalsy();
|
52
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
53
|
-
expect(isPlainObject(() => {})).toBeFalsy();
|
54
|
-
expect(isPlainObject({})).toBeTruthy();
|
55
|
-
expect(isPlainObject([])).toBeFalsy();
|
56
|
-
expect(isPlainObject(/foo/)).toBeFalsy();
|
57
|
-
});
|
58
|
-
|
59
|
-
it('should validate RegExp correctly', () => {
|
60
|
-
expect(isRegExp(1)).toBeFalsy();
|
61
|
-
expect(isRegExp('1')).toBeFalsy();
|
62
|
-
expect(isRegExp(undefined)).toBeFalsy();
|
63
|
-
expect(isRegExp(null)).toBeFalsy();
|
64
|
-
expect(isRegExp({})).toBeFalsy();
|
65
|
-
expect(isRegExp([])).toBeFalsy();
|
66
|
-
expect(isRegExp(/foo/)).toBeTruthy();
|
67
|
-
});
|
68
|
-
|
69
|
-
it('should validate Promise correctly', () => {
|
70
|
-
expect(isPromise(1)).toBeFalsy();
|
71
|
-
expect(isPromise('1')).toBeFalsy();
|
72
|
-
expect(isPromise(undefined)).toBeFalsy();
|
73
|
-
expect(isPromise(null)).toBeFalsy();
|
74
|
-
expect(isPromise({})).toBeFalsy();
|
75
|
-
expect(isPromise([])).toBeFalsy();
|
76
|
-
expect(isPromise(/foo/)).toBeFalsy();
|
77
|
-
expect(
|
78
|
-
isPromise(
|
79
|
-
new Promise<void>(resolve => {
|
80
|
-
resolve();
|
81
|
-
}),
|
82
|
-
),
|
83
|
-
).toBeTruthy();
|
84
|
-
});
|
85
|
-
});
|
package/tests/logger.test.ts
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
import { logger } from '../src/logger';
|
2
|
-
|
3
|
-
describe('logger', () => {
|
4
|
-
test('should log', () => {
|
5
|
-
// eslint-disable-next-line no-console
|
6
|
-
console.log = jest.fn();
|
7
|
-
|
8
|
-
logger.log('this is a log message');
|
9
|
-
|
10
|
-
// eslint-disable-next-line no-console
|
11
|
-
expect((console.log as jest.Mock).mock.calls[0][0]).toBe(
|
12
|
-
'this is a log message',
|
13
|
-
);
|
14
|
-
|
15
|
-
logger.info('this is an info message');
|
16
|
-
|
17
|
-
logger.warn('this is a warn message');
|
18
|
-
|
19
|
-
logger.error(new Error('this is an error message'));
|
20
|
-
|
21
|
-
// eslint-disable-next-line no-console
|
22
|
-
expect(console.log as jest.Mock).toHaveBeenCalledTimes(4);
|
23
|
-
});
|
24
|
-
|
25
|
-
test('should create new logger', () => {
|
26
|
-
// eslint-disable-next-line no-console
|
27
|
-
console.log = jest.fn();
|
28
|
-
|
29
|
-
const customLogger = new logger.Logger({
|
30
|
-
config: {
|
31
|
-
displayLabel: true,
|
32
|
-
underlineLabel: false,
|
33
|
-
uppercaseLabel: false,
|
34
|
-
},
|
35
|
-
types: {
|
36
|
-
custom: {
|
37
|
-
color: 'blue',
|
38
|
-
label: 'custom',
|
39
|
-
level: 'info',
|
40
|
-
},
|
41
|
-
},
|
42
|
-
});
|
43
|
-
|
44
|
-
customLogger.custom('custom log');
|
45
|
-
|
46
|
-
const reg = new RegExp(`custom.*custom log$`);
|
47
|
-
// eslint-disable-next-line no-console
|
48
|
-
expect((console.log as jest.Mock).mock.calls[0][0]).toMatch(reg);
|
49
|
-
|
50
|
-
// eslint-disable-next-line no-console
|
51
|
-
expect(console.log as jest.Mock).toHaveBeenCalledTimes(1);
|
52
|
-
});
|
53
|
-
});
|
@@ -1,139 +0,0 @@
|
|
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('../compiled/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
|
-
});
|
@@ -1,19 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
removeLeadingSlash,
|
3
|
-
removeSlash,
|
4
|
-
removeTailSlash,
|
5
|
-
} from '../src/removeSlash';
|
6
|
-
|
7
|
-
describe('remove slash', () => {
|
8
|
-
test(`should remove leading slash`, () => {
|
9
|
-
expect(removeLeadingSlash('///yuck/example//')).toEqual('yuck/example//');
|
10
|
-
});
|
11
|
-
|
12
|
-
test(`should remove tail slash`, () => {
|
13
|
-
expect(removeTailSlash('///yuck/example//')).toEqual('///yuck/example');
|
14
|
-
});
|
15
|
-
|
16
|
-
test(`should remove slash`, () => {
|
17
|
-
expect(removeSlash('///yuck/example//')).toEqual('yuck/example');
|
18
|
-
});
|
19
|
-
});
|
package/tests/tsconfig.json
DELETED
package/tests/wait.ts
DELETED
@@ -1,38 +0,0 @@
|
|
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
|
-
});
|