@modern-js/utils 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +7 -0
- package/dist/js/modern/constants.js +5 -0
- package/dist/js/modern/getEntryOptions.js +13 -4
- package/dist/js/modern/is/type.js +3 -0
- package/dist/js/modern/runtimeExports.js +1 -1
- package/dist/js/node/constants.js +8 -2
- package/dist/js/node/getEntryOptions.js +14 -4
- package/dist/js/node/is/type.js +5 -0
- package/dist/js/node/runtimeExports.js +1 -1
- package/dist/js/treeshaking/constants.js +5 -0
- package/dist/js/treeshaking/getEntryOptions.js +14 -3
- package/dist/js/treeshaking/is/type.js +3 -0
- package/dist/js/treeshaking/runtimeExports.js +3 -1
- package/dist/types/constants.d.ts +5 -0
- package/dist/types/getEntryOptions.d.ts +1 -1
- package/dist/types/is/type.d.ts +2 -1
- package/dist/types/runtimeExports.d.ts +1 -1
- package/package.json +1 -1
- package/src/constants.ts +5 -0
- package/src/getEntryOptions.ts +23 -4
- package/src/is/type.ts +4 -0
- package/src/runtimeExports.ts +30 -32
- package/tests/getEntryOptions.test.ts +11 -0
package/CHANGELOG.md
CHANGED
@@ -5,11 +5,20 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
5
5
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
6
6
|
|
7
7
|
import { isPlainObject } from "./is";
|
8
|
-
|
8
|
+
import { MAIN_ENTRY_NAME } from "./constants";
|
9
|
+
export const getEntryOptions = (name, baseOptions, optionsByEntries, packageName) => {
|
9
10
|
if (optionsByEntries) {
|
10
|
-
//
|
11
|
-
|
11
|
+
let optionsByEntry = getOptionsByEntryName(name, optionsByEntries); // compatible with main entry using packageName as the key
|
12
|
+
|
13
|
+
if (optionsByEntry === undefined && name === MAIN_ENTRY_NAME && packageName) {
|
14
|
+
optionsByEntry = getOptionsByEntryName(packageName, optionsByEntries);
|
15
|
+
} // eslint-disable-next-line no-nested-ternary
|
16
|
+
|
17
|
+
|
18
|
+
return optionsByEntry !== undefined ? isPlainObject(optionsByEntry) && isPlainObject(baseOptions) ? _objectSpread(_objectSpread({}, baseOptions), optionsByEntry) : optionsByEntry : baseOptions;
|
12
19
|
} else {
|
13
20
|
return baseOptions;
|
14
21
|
}
|
15
|
-
};
|
22
|
+
};
|
23
|
+
|
24
|
+
const getOptionsByEntryName = (name, optionsByEntries) => optionsByEntries.hasOwnProperty(name) ? optionsByEntries[name] : undefined;
|
@@ -22,4 +22,7 @@ export function isPromise(obj) {
|
|
22
22
|
/* eslint-disable promise/prefer-await-to-then */
|
23
23
|
return Boolean(obj) && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
24
24
|
/* eslint-enable promise/prefer-await-to-then */
|
25
|
+
}
|
26
|
+
export function isRegExp(obj) {
|
27
|
+
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
25
28
|
}
|
@@ -18,7 +18,7 @@ const memo = fn => {
|
|
18
18
|
};
|
19
19
|
};
|
20
20
|
|
21
|
-
export const createRuntimeExportsUtils = memo((pwd, namespace) => {
|
21
|
+
export const createRuntimeExportsUtils = memo((pwd = '', namespace) => {
|
22
22
|
const entryExportFile = path.join(pwd, `.runtime-exports/${namespace ? `${namespace}.js` : 'index.js'}`); // const ensure = () => {
|
23
23
|
// if (!fs.existsSync(entryExportFile)) {
|
24
24
|
// fs.outputFileSync(entryExportFile, '');
|
@@ -3,7 +3,7 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.SERVER_RENDER_FUNCTION_NAME = exports.SERVER_BUNDLE_DIRECTORY = exports.ROUTE_SPEC_FILE = exports.PLUGIN_SCHEMAS = exports.LOADABLE_STATS_FILE = exports.LAUNCH_EDITOR_ENDPOINT = exports.INTERNAL_SRC_ALIAS = exports.INTERNAL_PLUGINS = exports.INTERNAL_DIR_ALAIS = exports.HMR_SOCK_PATH = exports.HIDE_MODERN_JS_DIR = exports.ENTRY_NAME_PATTERN = void 0;
|
6
|
+
exports.SERVER_RENDER_FUNCTION_NAME = exports.SERVER_BUNDLE_DIRECTORY = exports.ROUTE_SPEC_FILE = exports.PLUGIN_SCHEMAS = exports.MAIN_ENTRY_NAME = exports.LOADABLE_STATS_FILE = exports.LAUNCH_EDITOR_ENDPOINT = exports.INTERNAL_SRC_ALIAS = exports.INTERNAL_PLUGINS = exports.INTERNAL_DIR_ALAIS = exports.HMR_SOCK_PATH = exports.HIDE_MODERN_JS_DIR = exports.ENTRY_NAME_PATTERN = void 0;
|
7
7
|
|
8
8
|
/**
|
9
9
|
* alias to src directory
|
@@ -28,10 +28,16 @@ const HMR_SOCK_PATH = '/_modern_js_hmr_ws';
|
|
28
28
|
exports.HMR_SOCK_PATH = HMR_SOCK_PATH;
|
29
29
|
const ROUTE_SPEC_FILE = 'route.json';
|
30
30
|
/**
|
31
|
-
*
|
31
|
+
* main entry name
|
32
32
|
*/
|
33
33
|
|
34
34
|
exports.ROUTE_SPEC_FILE = ROUTE_SPEC_FILE;
|
35
|
+
const MAIN_ENTRY_NAME = 'main';
|
36
|
+
/**
|
37
|
+
* open editor request path
|
38
|
+
*/
|
39
|
+
|
40
|
+
exports.MAIN_ENTRY_NAME = MAIN_ENTRY_NAME;
|
35
41
|
const LAUNCH_EDITOR_ENDPOINT = '/__open-stack-frame-in-editor';
|
36
42
|
/**
|
37
43
|
* server side bundles directory, which relative to dist.
|
@@ -7,19 +7,29 @@ exports.getEntryOptions = void 0;
|
|
7
7
|
|
8
8
|
var _is = require("./is");
|
9
9
|
|
10
|
+
var _constants = require("./constants");
|
11
|
+
|
10
12
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
11
13
|
|
12
14
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
13
15
|
|
14
16
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
15
17
|
|
16
|
-
const getEntryOptions = (name, baseOptions, optionsByEntries) => {
|
18
|
+
const getEntryOptions = (name, baseOptions, optionsByEntries, packageName) => {
|
17
19
|
if (optionsByEntries) {
|
18
|
-
//
|
19
|
-
|
20
|
+
let optionsByEntry = getOptionsByEntryName(name, optionsByEntries); // compatible with main entry using packageName as the key
|
21
|
+
|
22
|
+
if (optionsByEntry === undefined && name === _constants.MAIN_ENTRY_NAME && packageName) {
|
23
|
+
optionsByEntry = getOptionsByEntryName(packageName, optionsByEntries);
|
24
|
+
} // eslint-disable-next-line no-nested-ternary
|
25
|
+
|
26
|
+
|
27
|
+
return optionsByEntry !== undefined ? (0, _is.isPlainObject)(optionsByEntry) && (0, _is.isPlainObject)(baseOptions) ? _objectSpread(_objectSpread({}, baseOptions), optionsByEntry) : optionsByEntry : baseOptions;
|
20
28
|
} else {
|
21
29
|
return baseOptions;
|
22
30
|
}
|
23
31
|
};
|
24
32
|
|
25
|
-
exports.getEntryOptions = getEntryOptions;
|
33
|
+
exports.getEntryOptions = getEntryOptions;
|
34
|
+
|
35
|
+
const getOptionsByEntryName = (name, optionsByEntries) => optionsByEntries.hasOwnProperty(name) ? optionsByEntries[name] : undefined;
|
package/dist/js/node/is/type.js
CHANGED
@@ -8,6 +8,7 @@ exports.isFunction = isFunction;
|
|
8
8
|
exports.isObject = isObject;
|
9
9
|
exports.isPlainObject = isPlainObject;
|
10
10
|
exports.isPromise = isPromise;
|
11
|
+
exports.isRegExp = isRegExp;
|
11
12
|
exports.isString = isString;
|
12
13
|
exports.isUndefined = isUndefined;
|
13
14
|
|
@@ -41,4 +42,8 @@ function isPromise(obj) {
|
|
41
42
|
/* eslint-disable promise/prefer-await-to-then */
|
42
43
|
return Boolean(obj) && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
43
44
|
/* eslint-enable promise/prefer-await-to-then */
|
45
|
+
}
|
46
|
+
|
47
|
+
function isRegExp(obj) {
|
48
|
+
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
44
49
|
}
|
@@ -29,7 +29,7 @@ const memo = fn => {
|
|
29
29
|
};
|
30
30
|
};
|
31
31
|
|
32
|
-
const createRuntimeExportsUtils = memo((pwd, namespace) => {
|
32
|
+
const createRuntimeExportsUtils = memo((pwd = '', namespace) => {
|
33
33
|
const entryExportFile = _path.default.join(pwd, `.runtime-exports/${namespace ? `${namespace}.js` : 'index.js'}`); // const ensure = () => {
|
34
34
|
// if (!fs.existsSync(entryExportFile)) {
|
35
35
|
// fs.outputFileSync(entryExportFile, '');
|
@@ -5,11 +5,22 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
5
5
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
6
6
|
|
7
7
|
import { isPlainObject } from "./is";
|
8
|
-
|
8
|
+
import { MAIN_ENTRY_NAME } from "./constants";
|
9
|
+
export var getEntryOptions = function getEntryOptions(name, baseOptions, optionsByEntries, packageName) {
|
9
10
|
if (optionsByEntries) {
|
10
|
-
//
|
11
|
-
|
11
|
+
var optionsByEntry = getOptionsByEntryName(name, optionsByEntries); // compatible with main entry using packageName as the key
|
12
|
+
|
13
|
+
if (optionsByEntry === undefined && name === MAIN_ENTRY_NAME && packageName) {
|
14
|
+
optionsByEntry = getOptionsByEntryName(packageName, optionsByEntries);
|
15
|
+
} // eslint-disable-next-line no-nested-ternary
|
16
|
+
|
17
|
+
|
18
|
+
return optionsByEntry !== undefined ? isPlainObject(optionsByEntry) && isPlainObject(baseOptions) ? _objectSpread(_objectSpread({}, baseOptions), optionsByEntry) : optionsByEntry : baseOptions;
|
12
19
|
} else {
|
13
20
|
return baseOptions;
|
14
21
|
}
|
22
|
+
};
|
23
|
+
|
24
|
+
var getOptionsByEntryName = function getOptionsByEntryName(name, optionsByEntries) {
|
25
|
+
return optionsByEntries.hasOwnProperty(name) ? optionsByEntries[name] : undefined;
|
15
26
|
};
|
@@ -24,4 +24,7 @@ export function isPromise(obj) {
|
|
24
24
|
/* eslint-disable promise/prefer-await-to-then */
|
25
25
|
return Boolean(obj) && (_typeof(obj) === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
26
26
|
/* eslint-enable promise/prefer-await-to-then */
|
27
|
+
}
|
28
|
+
export function isRegExp(obj) {
|
29
|
+
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
27
30
|
}
|
@@ -22,7 +22,9 @@ var memo = function memo(fn) {
|
|
22
22
|
};
|
23
23
|
};
|
24
24
|
|
25
|
-
export var createRuntimeExportsUtils = memo(function (
|
25
|
+
export var createRuntimeExportsUtils = memo(function () {
|
26
|
+
var pwd = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
27
|
+
var namespace = arguments.length > 1 ? arguments[1] : undefined;
|
26
28
|
var entryExportFile = path.join(pwd, ".runtime-exports/".concat(namespace ? "".concat(namespace, ".js") : 'index.js')); // const ensure = () => {
|
27
29
|
// if (!fs.existsSync(entryExportFile)) {
|
28
30
|
// fs.outputFileSync(entryExportFile, '');
|
@@ -17,6 +17,11 @@ export declare const HMR_SOCK_PATH = "/_modern_js_hmr_ws";
|
|
17
17
|
*/
|
18
18
|
|
19
19
|
export declare const ROUTE_SPEC_FILE = "route.json";
|
20
|
+
/**
|
21
|
+
* main entry name
|
22
|
+
*/
|
23
|
+
|
24
|
+
export declare const MAIN_ENTRY_NAME = "main";
|
20
25
|
/**
|
21
26
|
* open editor request path
|
22
27
|
*/
|
@@ -1 +1 @@
|
|
1
|
-
export declare const getEntryOptions: <T>(name: string, baseOptions?: T | undefined, optionsByEntries?: Record<string, T> | undefined) => T | undefined;
|
1
|
+
export declare const getEntryOptions: <T>(name: string, baseOptions?: T | undefined, optionsByEntries?: Record<string, T> | undefined, packageName?: string | undefined) => T | undefined;
|
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/package.json
CHANGED
package/src/constants.ts
CHANGED
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/is/type.ts
CHANGED
package/src/runtimeExports.ts
CHANGED
@@ -20,38 +20,36 @@ const memo = <T extends (...args: any[]) => any>(fn: T) => {
|
|
20
20
|
};
|
21
21
|
};
|
22
22
|
|
23
|
-
export const createRuntimeExportsUtils = memo(
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
fs.appendFileSync(entryExportFile, `${statement}\n`);
|
44
|
-
}
|
45
|
-
} catch {
|
46
|
-
// 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`);
|
47
43
|
}
|
48
|
-
}
|
44
|
+
} catch {
|
45
|
+
// FIXME:
|
46
|
+
}
|
47
|
+
};
|
49
48
|
|
50
|
-
|
49
|
+
const getPath = () => entryExportFile;
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
);
|
51
|
+
return {
|
52
|
+
addExport,
|
53
|
+
getPath,
|
54
|
+
};
|
55
|
+
});
|
@@ -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',
|