@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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # @modern-js/utils
2
2
 
3
+ ## 1.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 0fa83663: support more .env files
8
+ - f594fbc8: fix apple icon and favicon support
9
+
3
10
  ## 1.1.1
4
11
 
5
12
  ### Patch Changes
@@ -17,6 +17,11 @@ export const HMR_SOCK_PATH = '/_modern_js_hmr_ws';
17
17
  */
18
18
 
19
19
  export const ROUTE_SPEC_FILE = 'route.json';
20
+ /**
21
+ * main entry name
22
+ */
23
+
24
+ export const MAIN_ENTRY_NAME = 'main';
20
25
  /**
21
26
  * open editor request path
22
27
  */
@@ -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
- export const getEntryOptions = (name, baseOptions, optionsByEntries) => {
8
+ import { MAIN_ENTRY_NAME } from "./constants";
9
+ export const getEntryOptions = (name, baseOptions, optionsByEntries, packageName) => {
9
10
  if (optionsByEntries) {
10
- // eslint-disable-next-line no-nested-ternary
11
- return optionsByEntries.hasOwnProperty(name) ? isPlainObject(optionsByEntries[name]) && isPlainObject(baseOptions) ? _objectSpread(_objectSpread({}, baseOptions), optionsByEntries[name]) : optionsByEntries[name] : baseOptions;
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
- * open editor request path
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
- // eslint-disable-next-line no-nested-ternary
19
- return optionsByEntries.hasOwnProperty(name) ? (0, _is.isPlainObject)(optionsByEntries[name]) && (0, _is.isPlainObject)(baseOptions) ? _objectSpread(_objectSpread({}, baseOptions), optionsByEntries[name]) : optionsByEntries[name] : baseOptions;
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;
@@ -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, '');
@@ -19,6 +19,11 @@ export var HMR_SOCK_PATH = '/_modern_js_hmr_ws';
19
19
  */
20
20
 
21
21
  export var ROUTE_SPEC_FILE = 'route.json';
22
+ /**
23
+ * main entry name
24
+ */
25
+
26
+ export var MAIN_ENTRY_NAME = 'main';
22
27
  /**
23
28
  * open editor request path
24
29
  */
@@ -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
- export var getEntryOptions = function getEntryOptions(name, baseOptions, optionsByEntries) {
8
+ import { MAIN_ENTRY_NAME } from "./constants";
9
+ export var getEntryOptions = function getEntryOptions(name, baseOptions, optionsByEntries, packageName) {
9
10
  if (optionsByEntries) {
10
- // eslint-disable-next-line no-nested-ternary
11
- return optionsByEntries.hasOwnProperty(name) ? isPlainObject(optionsByEntries[name]) && isPlainObject(baseOptions) ? _objectSpread(_objectSpread({}, baseOptions), optionsByEntries[name]) : optionsByEntries[name] : baseOptions;
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 (pwd, namespace) {
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;
@@ -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;
@@ -1,4 +1,4 @@
1
- export declare const createRuntimeExportsUtils: (pwd: string, namespace: string) => {
1
+ export declare const createRuntimeExportsUtils: (pwd: any, namespace: string) => {
2
2
  addExport: (statement: string) => void;
3
3
  getPath: () => string;
4
4
  };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.1.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",
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
  */
@@ -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 optionsByEntries.hasOwnProperty(name)
11
- ? isPlainObject(optionsByEntries[name]) && isPlainObject(baseOptions)
12
- ? { ...baseOptions, ...optionsByEntries[name] }
13
- : optionsByEntries[name]
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
@@ -37,3 +37,7 @@ export function isPromise(obj: any): obj is Promise<any> {
37
37
  );
38
38
  /* eslint-enable promise/prefer-await-to-then */
39
39
  }
40
+
41
+ export function isRegExp(obj: any): obj is RegExp {
42
+ return Object.prototype.toString.call(obj) === '[object RegExp]';
43
+ }
@@ -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
- (pwd: string, namespace: string) => {
25
- const entryExportFile = path.join(
26
- pwd,
27
- `.runtime-exports/${namespace ? `${namespace}.js` : 'index.js'}`,
28
- );
29
-
30
- // const ensure = () => {
31
- // if (!fs.existsSync(entryExportFile)) {
32
- // fs.outputFileSync(entryExportFile, '');
33
- // }
34
- // fs.ensureFileSync(entryExportFile);
35
- // };
36
-
37
- const addExport = (statement: string) => {
38
- // eslint-disable-next-line no-param-reassign
39
- statement = normalizeOutputPath(statement);
40
- try {
41
- fs.ensureFileSync(entryExportFile);
42
- if (!fs.readFileSync(entryExportFile, 'utf8').includes(statement)) {
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
- const getPath = () => entryExportFile;
49
+ const getPath = () => entryExportFile;
51
50
 
52
- return {
53
- addExport,
54
- getPath,
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',