@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.
Files changed (77) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +1 -4
  3. package/dist/js/modern/FileSizeReporter.js +2 -2
  4. package/dist/js/modern/alias.js +1 -1
  5. package/dist/js/modern/applyOptionsChain.js +2 -1
  6. package/dist/js/modern/constants.js +25 -0
  7. package/dist/js/modern/ensureAbsolutePath.js +1 -1
  8. package/dist/js/modern/getEntryOptions.js +13 -4
  9. package/dist/js/modern/getPackageManager.js +1 -1
  10. package/dist/js/modern/index.js +1 -5
  11. package/dist/js/modern/is/index.js +1 -1
  12. package/dist/js/modern/is/type.js +3 -0
  13. package/dist/js/modern/monorepo.js +1 -1
  14. package/dist/js/modern/path.js +2 -13
  15. package/dist/js/modern/prettyInstructions.js +2 -2
  16. package/dist/js/modern/readTsConfig.js +1 -1
  17. package/dist/js/modern/runtimeExports.js +6 -2
  18. package/dist/js/node/FileSizeReporter.js +13 -17
  19. package/dist/js/node/alias.js +3 -7
  20. package/dist/js/node/applyOptionsChain.js +6 -5
  21. package/dist/js/node/constants.js +28 -2
  22. package/dist/js/node/ensureAbsolutePath.js +3 -5
  23. package/dist/js/node/getEntryOptions.js +14 -4
  24. package/dist/js/node/getPackageManager.js +6 -10
  25. package/dist/js/node/index.js +13 -17
  26. package/dist/js/node/is/index.js +3 -7
  27. package/dist/js/node/is/type.js +5 -0
  28. package/dist/js/node/monorepo.js +13 -17
  29. package/dist/js/node/path.js +5 -37
  30. package/dist/js/node/prettyInstructions.js +5 -3
  31. package/dist/js/node/readTsConfig.js +4 -6
  32. package/dist/js/node/runtimeExports.js +9 -7
  33. package/dist/js/treeshaking/FileSizeReporter.js +2 -2
  34. package/dist/js/treeshaking/alias.js +1 -1
  35. package/dist/js/treeshaking/applyOptionsChain.js +2 -1
  36. package/dist/js/treeshaking/constants.js +25 -0
  37. package/dist/js/treeshaking/ensureAbsolutePath.js +1 -1
  38. package/dist/js/treeshaking/getEntryOptions.js +14 -3
  39. package/dist/js/treeshaking/getPackageManager.js +1 -1
  40. package/dist/js/treeshaking/index.js +1 -5
  41. package/dist/js/treeshaking/is/index.js +1 -1
  42. package/dist/js/treeshaking/is/type.js +3 -0
  43. package/dist/js/treeshaking/monorepo.js +1 -1
  44. package/dist/js/treeshaking/path.js +4 -26
  45. package/dist/js/treeshaking/prettyInstructions.js +2 -2
  46. package/dist/js/treeshaking/readTsConfig.js +1 -1
  47. package/dist/js/treeshaking/runtimeExports.js +8 -2
  48. package/dist/types/constants.d.ts +5 -0
  49. package/dist/types/getEntryOptions.d.ts +1 -1
  50. package/dist/types/index.d.ts +1 -3
  51. package/dist/types/is/type.d.ts +2 -1
  52. package/dist/types/monorepo.d.ts +1 -1
  53. package/dist/types/path.d.ts +2 -11
  54. package/dist/types/prettyInstructions.d.ts +6 -1
  55. package/dist/types/runtimeExports.d.ts +1 -1
  56. package/package.json +3 -3
  57. package/src/FileSizeReporter.ts +2 -2
  58. package/src/alias.ts +1 -1
  59. package/src/applyOptionsChain.ts +2 -1
  60. package/src/constants.ts +20 -1
  61. package/src/ensureAbsolutePath.ts +1 -1
  62. package/src/getEntryOptions.ts +23 -4
  63. package/src/getPackageManager.ts +1 -1
  64. package/src/index.ts +1 -3
  65. package/src/is/index.ts +1 -1
  66. package/src/is/type.ts +4 -0
  67. package/src/monorepo.ts +1 -1
  68. package/src/path.ts +3 -21
  69. package/src/prettyInstructions.ts +2 -2
  70. package/src/readTsConfig.ts +1 -1
  71. package/src/runtimeExports.ts +32 -31
  72. package/tests/.eslintrc.js +6 -0
  73. package/tests/compatRequire.test.ts +1 -1
  74. package/tests/ensureAbsolutePath.test.ts +7 -4
  75. package/tests/findExists.test.ts +1 -1
  76. package/tests/getBrowserslist.test.ts +1 -1
  77. package/tests/getEntryOptions.test.ts +11 -0
@@ -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 { isRelativePath } from './path';
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';
@@ -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
- import * as path from './path';
1
+ import path from 'path';
2
2
  export declare const isLerna: (root: string) => boolean;
3
3
  export declare const isYarnWorkspaces: (root: string) => boolean;
4
4
  export declare const isPnpmWorkspaces: (root: string) => boolean;
@@ -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 join: (...paths: string[]) => string;
5
- export declare const resolve: (...paths: string[]) => string;
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
- export declare const prettyInstructions: (appContext: any, config: any) => string;
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 {};
@@ -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.0.0",
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.0.0",
69
- "@modern-js/module-tools": "^1.0.0"
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"
@@ -12,13 +12,13 @@
12
12
  'use strict';
13
13
 
14
14
  import fs from 'fs';
15
- import * as path from './path';
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
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
+ import path from 'path';
2
3
  import chalk from 'chalk';
3
- import * as path from './path';
4
4
  import { readTsConfigByFile } from './readTsConfig';
5
5
  import { applyOptionsChain } from './applyOptionsChain';
6
6
 
@@ -1,4 +1,5 @@
1
- import { isFunction, logger, isPlainObject } from '.';
1
+ // eslint-disable-next-line import/no-useless-path-segments
2
+ import { isFunction, logger, isPlainObject } from './index';
2
3
 
3
4
  export const applyOptionsChain = <T, U>(
4
5
  defaults: T,
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: { oneOf: [{ type: 'boolean' }, { instanceof: 'Function' }] },
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': [
@@ -1,4 +1,4 @@
1
- import * as path from './path';
1
+ import path from 'path';
2
2
 
3
3
  /**
4
4
  * ensure absolute file path.
@@ -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;
@@ -1,6 +1,6 @@
1
1
  import os from 'os';
2
+ import path from 'path';
2
3
  import fs from 'fs-extra';
3
- import * as path from './path';
4
4
 
5
5
  export function getPackageManager(cwd: string = process.cwd()) {
6
6
  let appDirectory = cwd;
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 { isRelativePath } from './path';
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
@@ -1,5 +1,5 @@
1
1
  import fs from 'fs';
2
- import * as path from '../path';
2
+ import path from 'path';
3
3
  import { isDev } from './node-env';
4
4
 
5
5
  /**
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
+ }
package/src/monorepo.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from 'fs';
2
+ import path from 'path';
2
3
  import glob from 'glob';
3
4
  import yaml from 'yaml';
4
- import * as path from './path';
5
5
 
6
6
  const PACKAGE_MAX_DEPTH = 5;
7
7
 
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 join = (...paths: string[]) =>
8
- upath.normalizeSafe(path.join(...paths));
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 isSPA = (entrypoints: EntryPoint[]) =>
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 (isSPA(entrypoints)) {
65
+ if (isSingleEntry(entrypoints)) {
66
66
  message += urls
67
67
  .map(
68
68
  ({ type, url }) =>
@@ -1,4 +1,4 @@
1
- import * as path from './path';
1
+ import path from 'path';
2
2
 
3
3
  export const readTsConfig = (root: string) => {
4
4
  // import typescript from 'typescript' cause eslint fromat error.
@@ -1,5 +1,6 @@
1
+ import path from 'path';
1
2
  import fs from 'fs-extra';
2
- import * as path from './path';
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
- (pwd: string, 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
- try {
38
- fs.ensureFileSync(entryExportFile);
39
- if (!fs.readFileSync(entryExportFile, 'utf8').includes(statement)) {
40
- fs.appendFileSync(entryExportFile, `${statement}\n`);
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
- const getPath = () => entryExportFile;
49
+ const getPath = () => entryExportFile;
48
50
 
49
- return {
50
- addExport,
51
- getPath,
52
- };
53
- },
54
- );
51
+ return {
52
+ addExport,
53
+ getPath,
54
+ };
55
+ });
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ extends: ['@modern-js'],
3
+ parserOptions: {
4
+ project: require.resolve('./tsconfig.json'),
5
+ },
6
+ };
@@ -1,4 +1,4 @@
1
- import { path } from '@/index';
1
+ import path from 'path';
2
2
  import { compatRequire } from '@/compatRequire';
3
3
 
4
4
  describe('compat require', () => {
@@ -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(ensureAbsolutePath('/a/b', '/a/b/c/d/e.jsx')).toEqual(
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(ensureAbsolutePath('/a/b', 'c/d/e.jsx')).toEqual('/a/b/c/d/e.jsx');
12
+ expect(
13
+ path.isAbsolute(ensureAbsolutePath('/a/b', 'c/d/e.jsx')),
14
+ ).toBeTruthy();
12
15
  });
13
16
  });
@@ -1,4 +1,4 @@
1
- import { path } from '@/index';
1
+ import path from 'path';
2
2
  import { findExists } from '@/findExists';
3
3
 
4
4
  describe('find first exists file', () => {
@@ -1,4 +1,4 @@
1
- import { path } from '@/index';
1
+ import path from 'path';
2
2
  import { getBrowserslist, defaults } from '@/getBrowserslist';
3
3
 
4
4
  describe('get browserslist', () => {
@@ -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',