@modern-js/core 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/.eslintrc.js +0 -2
  2. package/CHANGELOG.md +48 -0
  3. package/dist/js/modern/config/defaults.js +4 -1
  4. package/dist/js/modern/config/index.js +11 -7
  5. package/dist/js/modern/config/mergeConfig.js +1 -1
  6. package/dist/js/modern/config/schema/index.js +1 -1
  7. package/dist/js/modern/config/types/index.js +1 -0
  8. package/dist/js/modern/config/types/less.js +0 -0
  9. package/dist/js/modern/config/types/sass.js +0 -0
  10. package/dist/js/modern/config/types/ssg.js +0 -0
  11. package/dist/js/modern/config/types/test.js +0 -0
  12. package/dist/js/modern/config/types/unbundle.js +0 -0
  13. package/dist/js/modern/context.js +8 -1
  14. package/dist/js/modern/index.js +26 -9
  15. package/dist/js/modern/initWatcher.js +2 -2
  16. package/dist/js/modern/loadPlugins.js +41 -41
  17. package/dist/js/modern/utils/commander.js +15 -15
  18. package/dist/js/node/config/defaults.js +4 -1
  19. package/dist/js/node/config/index.js +38 -11
  20. package/dist/js/node/config/mergeConfig.js +2 -4
  21. package/dist/js/node/config/schema/index.js +3 -5
  22. package/dist/js/node/config/types/index.js +5 -0
  23. package/dist/js/node/config/types/less.js +0 -0
  24. package/dist/js/node/config/types/sass.js +0 -0
  25. package/dist/js/node/config/types/ssg.js +0 -0
  26. package/dist/js/node/config/types/test.js +0 -0
  27. package/dist/js/node/config/types/unbundle.js +0 -0
  28. package/dist/js/node/context.js +8 -1
  29. package/dist/js/node/index.js +31 -10
  30. package/dist/js/node/initWatcher.js +2 -3
  31. package/dist/js/node/loadPlugins.js +40 -41
  32. package/dist/js/node/utils/commander.js +16 -19
  33. package/dist/types/config/defaults.d.ts +3 -0
  34. package/dist/types/config/index.d.ts +4 -129
  35. package/dist/types/config/types/index.d.ts +239 -0
  36. package/dist/types/config/types/less.d.ts +10 -0
  37. package/dist/types/config/types/sass.d.ts +8 -0
  38. package/dist/types/config/types/ssg.d.ts +13 -0
  39. package/dist/types/config/types/test.d.ts +15 -0
  40. package/dist/types/config/types/unbundle.d.ts +28 -0
  41. package/dist/types/context.d.ts +18 -6
  42. package/dist/types/index.d.ts +19 -0
  43. package/dist/types/initWatcher.d.ts +1 -2
  44. package/dist/types/loadPlugins.d.ts +24 -10
  45. package/dist/types/manager.d.ts +1 -1
  46. package/dist/types/utils/commander.d.ts +4 -3
  47. package/jest.config.js +0 -1
  48. package/package.json +17 -17
  49. package/tests/config.test.ts +14 -5
  50. package/tests/context.test.ts +17 -2
  51. package/tests/fixtures/index-test/modern.server-runtime.config.js +0 -0
  52. package/tests/index.test.ts +15 -1
  53. package/tests/initWatcher.test.ts +1 -1
  54. package/tests/loadPlugin.test.ts +31 -6
  55. package/tests/utils.test.ts +1 -2
@@ -0,0 +1,10 @@
1
+ /// <reference types="less" />
2
+ import type { LoaderContext } from 'webpack';
3
+ export declare type LessLoaderOptions = {
4
+ lessOptions?: Less.Options;
5
+ additionalData?: string | ((content: string, loaderContext: LoaderContext<LessLoaderOptions>) => string);
6
+ sourceMap?: boolean;
7
+ webpackImporter?: boolean;
8
+ implementation?: boolean;
9
+ };
10
+ export declare type LessConfig = LessLoaderOptions | ((options: LessLoaderOptions, utils?: any) => LessLoaderOptions | void);
@@ -0,0 +1,8 @@
1
+ import type { LegacyFileOptions } from 'sass';
2
+ export interface SassLoaderOptions {
3
+ sassOptions?: LegacyFileOptions<'sync'>;
4
+ sourceMap?: boolean;
5
+ implementation?: string;
6
+ additionalData?: string | ((content: string, filename: string) => string);
7
+ }
8
+ export declare type SassConfig = SassLoaderOptions | ((options: SassLoaderOptions, utils?: any) => SassLoaderOptions | void);
@@ -0,0 +1,13 @@
1
+ export declare type SSGRouteOptions = string | {
2
+ url: string;
3
+ output?: string;
4
+ params?: Record<string, any>[];
5
+ headers?: Record<string, any>;
6
+ };
7
+ export declare type SSGSingleEntryOptions = boolean | {
8
+ preventDefault?: string[];
9
+ headers?: Record<string, any>;
10
+ routes?: SSGRouteOptions[];
11
+ };
12
+ export declare type SSGMultiEntryOptions = Record<string, SSGSingleEntryOptions>;
13
+ export declare type SSGConfig = boolean | SSGSingleEntryOptions | SSGMultiEntryOptions | ((entryName: string) => SSGSingleEntryOptions);
@@ -0,0 +1,15 @@
1
+ import type { Config as JestConfigTypes } from '@jest/types';
2
+ export declare type JestConfig = JestConfigTypes.InitialOptions;
3
+ export interface TestConfig {
4
+ /**
5
+ * Decide which transformer will be used to compile file
6
+ * Default: babel-jest
7
+ */
8
+ transformer?: 'babel-jest' | 'ts-jest';
9
+ /**
10
+ * Original jest config
11
+ * Doc: https://jestjs.io/docs/configuration
12
+ */
13
+
14
+ jest?: JestConfig | ((jestConfig: JestConfig) => JestConfig);
15
+ }
@@ -0,0 +1,28 @@
1
+ export declare type UnbundleConfig = {
2
+ /**
3
+ * Some package A may require another package B that is intended for Node.js
4
+ * use only. In such a case, if package B cannot be converted to ESM, it will
5
+ * cause package A to fail during unbundle development, even though package B
6
+ * is not really required. Package B can thus be safely ignored via this option
7
+ * to ensure transpilation of package A to ESM
8
+ */
9
+ ignore?: string | string[];
10
+ /**
11
+ * ignores cached esm modules and recompiles dependencies not available
12
+ * from PDN host on dev start.
13
+ * default: false
14
+ */
15
+
16
+ ignoreModuleCache?: boolean;
17
+ /**
18
+ * clears cache of downloaded esm modules (from PDN) on dev start.
19
+ * default: false
20
+ */
21
+
22
+ clearPdnCache?: boolean;
23
+ /**
24
+ * modifies host to attempt to download esm modules from
25
+ */
26
+
27
+ pdnHost?: string;
28
+ };
@@ -27,9 +27,21 @@ export declare const useConfigContext: () => UserConfig;
27
27
  */
28
28
 
29
29
  export declare const useResolvedConfigContext: () => NormalizedConfig;
30
- export declare const initAppContext: (appDirectory: string, plugins: Array<LoadedPlugin>, configFile: string | false, options?: {
31
- metaName?: string | undefined;
32
- srcDir?: string | undefined;
33
- distDir?: string | undefined;
34
- sharedDir?: string | undefined;
35
- } | undefined) => IAppContext;
30
+ export declare const initAppContext: ({
31
+ appDirectory,
32
+ plugins,
33
+ configFile,
34
+ options,
35
+ serverConfigFile
36
+ }: {
37
+ appDirectory: string;
38
+ plugins: LoadedPlugin[];
39
+ configFile: string | false;
40
+ options?: {
41
+ metaName?: string | undefined;
42
+ srcDir?: string | undefined;
43
+ distDir?: string | undefined;
44
+ sharedDir?: string | undefined;
45
+ } | undefined;
46
+ serverConfigFile: string;
47
+ }) => IAppContext;
@@ -15,6 +15,7 @@ export type { NormalizedConfig, IAppContext };
15
15
  declare const initAppDir: (cwd?: string | undefined) => Promise<string>;
16
16
  export interface CoreOptions {
17
17
  configFile?: string;
18
+ serverConfigFile?: string;
18
19
  packageJsonConfig?: string;
19
20
  plugins?: typeof INTERNAL_PLUGINS;
20
21
  onSchemaError?: (error: ErrorObject) => void;
@@ -25,6 +26,24 @@ export interface CoreOptions {
25
26
  sharedDir?: string;
26
27
  };
27
28
  }
29
+ export declare const mergeOptions: (options?: CoreOptions | undefined) => {
30
+ configFile?: string | undefined;
31
+ serverConfigFile: string;
32
+ packageJsonConfig?: string | undefined;
33
+ plugins?: {
34
+ [name: string]: {
35
+ cli?: string | undefined;
36
+ server?: string | undefined;
37
+ };
38
+ } | undefined;
39
+ onSchemaError?: ((error: ErrorObject) => void) | undefined;
40
+ options?: {
41
+ metaName?: string | undefined;
42
+ srcDir?: string | undefined;
43
+ distDir?: string | undefined;
44
+ sharedDir?: string | undefined;
45
+ } | undefined;
46
+ };
28
47
  export declare const cli: {
29
48
  init: (argv?: string[], options?: CoreOptions | undefined) => Promise<{
30
49
  loadedConfig: import("./config").LoadedConfig;
@@ -1,4 +1,3 @@
1
- import chokidar from 'chokidar';
2
1
  import { LoadedConfig } from './config';
3
2
  import { HooksRunner } from './manager';
4
- export declare const initWatcher: (loaded: LoadedConfig, appDirectory: string, configDir: string | undefined, hooksRunner: HooksRunner, argv: string[]) => Promise<chokidar.FSWatcher | undefined>;
3
+ export declare const initWatcher: (loaded: LoadedConfig, appDirectory: string, configDir: string | undefined, hooksRunner: HooksRunner, argv: string[]) => Promise<import("@modern-js/utils").FSWatcher | undefined>;
@@ -1,18 +1,32 @@
1
1
  import { INTERNAL_PLUGINS } from '@modern-js/utils';
2
2
  import type { UserConfig } from './config';
3
- declare type Plugin = string | [string, any];
3
+ import { CliPlugin } from './manager';
4
+ declare type PluginItem = string | [string, any];
4
5
  export declare type LoadedPlugin = {
5
- cli?: any;
6
- cliPkg?: string;
7
- server?: any;
6
+ cli?: CliPlugin;
7
+ server?: string;
8
8
  serverPkg?: string;
9
9
  };
10
- export declare type PluginConfigItem = {
11
- cli?: Plugin;
12
- server?: Plugin;
13
- } | Plugin;
14
- export declare type PluginConfig = Array<PluginConfigItem>;
15
- export declare function getAppPlugins(appDirectory: string, pluginConfig: PluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): PluginConfigItem[];
10
+ /**
11
+ * @deprecated
12
+ * Using NewPluginConfig insteand.
13
+ */
14
+
15
+ declare type OldPluginConfig = Array<PluginItem | {
16
+ cli?: PluginItem;
17
+ server?: PluginItem;
18
+ }>;
19
+ declare type NewPluginConfig = CliPlugin[] | {
20
+ cli?: CliPlugin[];
21
+ /** Custom server plugin is not supported yet. */
22
+
23
+ server?: never;
24
+ };
25
+ export declare type PluginConfig = OldPluginConfig | NewPluginConfig;
26
+ export declare function getAppPlugins(appDirectory: string, oldPluginConfig: OldPluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): (PluginItem | {
27
+ cli?: PluginItem | undefined;
28
+ server?: PluginItem | undefined;
29
+ })[];
16
30
  /**
17
31
  * Load internal plugins which in @modern-js scope and user's custom plugins.
18
32
  * @param appDirectory - Application root directory.
@@ -1,6 +1,6 @@
1
1
  import { ToThreads, ToRunners, AsyncSetup, PluginOptions, AsyncWorkflow, AsyncWaterfall, ParallelWorkflow } from '@modern-js/plugin';
2
+ import { Command } from '@modern-js/utils';
2
3
  import type { Hooks } from '@modern-js/types';
3
- import type { Command } from './utils/commander';
4
4
  import type { NormalizedConfig } from './config/mergeConfig';
5
5
  import { pluginAPI } from './pluginAPI';
6
6
  export declare type HooksRunner = ToRunners<{
@@ -1,7 +1,8 @@
1
- import { program, Command } from 'commander';
2
- declare module 'commander' {
1
+ import { program } from '@modern-js/utils';
2
+ declare module '@modern-js/utils/compiled/commander' {
3
3
  interface Command {
4
4
  commandsMap: Map<string, Command>;
5
5
  }
6
6
  }
7
- export { program, Command };
7
+ export declare function initCommandsMap(): void;
8
+ export { program };
package/jest.config.js CHANGED
@@ -2,7 +2,6 @@ const sharedConfig = require('@scripts/jest-config');
2
2
 
3
3
  /** @type {import('@jest/types').Config.InitialOptions} */
4
4
  module.exports = {
5
- // eslint-disable-next-line node/no-unsupported-features/es-syntax
6
5
  ...sharedConfig,
7
6
  rootDir: __dirname,
8
7
  };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.5.0",
14
+ "version": "1.7.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -42,37 +42,37 @@
42
42
  "dependencies": {
43
43
  "@babel/code-frame": "^7.14.5",
44
44
  "@babel/runtime": "^7",
45
- "@modern-js/load-config": "^1.2.2",
46
- "@modern-js/plugin": "^1.3.0",
47
- "@modern-js/utils": "^1.3.5",
45
+ "@modern-js/load-config": "^1.3.0",
46
+ "@modern-js/plugin": "^1.3.2",
47
+ "@modern-js/utils": "^1.4.0",
48
48
  "address": "^1.1.2",
49
49
  "ajv": "^8.6.2",
50
50
  "ajv-keywords": "^5.0.0",
51
51
  "better-ajv-errors": "^0.7.0",
52
- "chokidar": "^3.5.2",
53
- "commander": "^8.1.0",
54
52
  "dotenv": "^10.0.0",
55
53
  "dotenv-expand": "^5.1.0",
56
- "lodash.clonedeep": "^4.5.0",
57
- "lodash.mergewith": "^4.6.2",
58
- "signale": "^1.4.0",
59
54
  "v8-compile-cache": "^2.3.0"
60
55
  },
61
56
  "devDependencies": {
62
- "btsm": "2.2.2",
57
+ "@jest/types": "^27.0.6",
58
+ "@modern-js/types": "^1.4.0",
59
+ "@scripts/build": "0.0.0",
60
+ "@scripts/jest-config": "0.0.0",
63
61
  "@types/babel__code-frame": "^7.0.3",
64
- "@modern-js/types": "^1.3.5",
62
+ "@types/babel__core": "^7.1.16",
65
63
  "@types/jest": "^26",
66
- "@types/lodash.clonedeep": "^4.5.6",
67
- "@types/lodash.mergewith": "^4.6.6",
64
+ "@types/less": "^3.0.3",
68
65
  "@types/node": "^14",
69
66
  "@types/react": "^17",
70
67
  "@types/react-dom": "^17",
71
- "@types/signale": "^1.4.2",
72
- "typescript": "^4",
68
+ "autoprefixer": "^10.3.1",
69
+ "btsm": "2.2.2",
73
70
  "jest": "^27",
74
- "@scripts/build": "0.0.0",
75
- "@scripts/jest-config": "0.0.0"
71
+ "sass": "^1.45.0",
72
+ "terser-webpack-plugin": "^5.1.4",
73
+ "typescript": "^4",
74
+ "webpack": "^5.71.0",
75
+ "webpack-chain": "^6.5.1"
76
76
  },
77
77
  "sideEffects": false,
78
78
  "modernConfig": {
@@ -1,7 +1,7 @@
1
1
  import path from 'path';
2
2
  // import os from 'os';
3
- import { isDev, getPort } from '@modern-js/utils';
4
- import { resolveConfig } from '../src/config';
3
+ import { isDev, getPort, DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
4
+ import { resolveConfig, addServerConfigToDeps } from '../src/config';
5
5
  import {
6
6
  cli,
7
7
  loadUserConfig,
@@ -26,7 +26,7 @@ jest.mock('@modern-js/utils', () => ({
26
26
 
27
27
  describe('config', () => {
28
28
  /**
29
- * Typescript Type annotations cannot be used for esbuild-jest
29
+ * TypeScript Type annotations cannot be used for esbuild-jest
30
30
  * test files that use jest.mock('@some/module')
31
31
  * refer to this esbuild-jest issue:
32
32
  * https://github.com/aelbore/esbuild-jest/issues/57
@@ -133,5 +133,14 @@ describe('config', () => {
133
133
  });
134
134
  });
135
135
 
136
- // type TEST = Parameters<typeof resolveConfig>;
137
- // type TypeC = TEST[1];
136
+ describe('addServerConfigToDeps', () => {
137
+ it('should add server config to deps', async () => {
138
+ const appDirectory = path.join(__dirname, './fixtures/index-test');
139
+ const deps: string[] = [];
140
+ await addServerConfigToDeps(deps, appDirectory, DEFAULT_SERVER_CONFIG);
141
+ expect(deps.length).toBe(1);
142
+ expect(deps[0]).toBe(
143
+ path.join(appDirectory, `${DEFAULT_SERVER_CONFIG}.js`),
144
+ );
145
+ });
146
+ });
@@ -1,4 +1,5 @@
1
1
  import path from 'path';
2
+ import { DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
2
3
  import { initAppContext } from '../src/context';
3
4
 
4
5
  describe('context', () => {
@@ -7,10 +8,17 @@ describe('context', () => {
7
8
  __dirname,
8
9
  './fixtures/load-plugin/user-plugins',
9
10
  );
10
- const appContext = initAppContext(appDirectory, [], false);
11
+ const appContext = initAppContext({
12
+ appDirectory,
13
+ plugins: [],
14
+ configFile: false,
15
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
16
+ });
17
+
11
18
  expect(appContext).toEqual({
12
19
  appDirectory,
13
20
  configFile: false,
21
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
14
22
  ip: expect.any(String),
15
23
  port: 0,
16
24
  packageName: expect.any(String),
@@ -44,10 +52,17 @@ describe('context', () => {
44
52
  metaName: 'jupiter',
45
53
  };
46
54
 
47
- const appContext = initAppContext(appDirectory, [], false, customOptions);
55
+ const appContext = initAppContext({
56
+ appDirectory,
57
+ plugins: [],
58
+ configFile: false,
59
+ options: customOptions,
60
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
61
+ });
48
62
  expect(appContext).toEqual({
49
63
  appDirectory,
50
64
  configFile: false,
65
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
51
66
  ip: expect.any(String),
52
67
  port: 0,
53
68
  packageName: 'user-plugins',
@@ -1,5 +1,5 @@
1
1
  import path from 'path';
2
- import { cli } from '../src';
2
+ import { cli, mergeOptions } from '../src';
3
3
  import { resolveConfig, loadUserConfig } from '../src/config';
4
4
  import { loadEnv } from '../src/loadEnv';
5
5
 
@@ -67,3 +67,17 @@ describe('@modern-js/core test', () => {
67
67
  // TODO: add more test cases
68
68
  });
69
69
  });
70
+
71
+ describe('test mergeOptions', () => {
72
+ it('serverConfigFile must exist', () => {
73
+ const options = mergeOptions({});
74
+ expect(options).toHaveProperty('serverConfigFile');
75
+ });
76
+
77
+ it('serverConfigFile can be overwritten', () => {
78
+ const options = mergeOptions({
79
+ serverConfigFile: 'test',
80
+ });
81
+ expect(options.serverConfigFile).toBe('test');
82
+ });
83
+ });
@@ -16,7 +16,7 @@ describe('initWatcher', () => {
16
16
  }
17
17
  });
18
18
 
19
- test('will trigger add event', async () => {
19
+ xtest('will trigger add event', async () => {
20
20
  let triggeredType = '';
21
21
  let triggeredFile = '';
22
22
  const loaded = {
@@ -1,4 +1,5 @@
1
1
  import path from 'path';
2
+ import { CliPlugin } from '../src';
2
3
  import { loadPlugins, getAppPlugins } from '../src/loadPlugins';
3
4
 
4
5
  describe('load plugins', () => {
@@ -33,9 +34,7 @@ describe('load plugins', () => {
33
34
  {
34
35
  cli: {
35
36
  name: 'a',
36
- pluginPath: path.join(fixture, './test-plugin-a.js'),
37
37
  },
38
- cliPkg: path.join(fixture, './test-plugin-a.js'),
39
38
  },
40
39
  {
41
40
  server: './test-plugin-b',
@@ -54,8 +53,8 @@ describe('load plugins', () => {
54
53
  plugins: [{ cli: ['./test-plugin-c', 'c'] }, ['./test-plugin-c', 'c2']],
55
54
  });
56
55
 
57
- expect(plugins[0].cli.name).toEqual('c');
58
- expect(plugins[1].cli.name).toEqual('c2');
56
+ expect(plugins[0].cli!.name).toEqual('c');
57
+ expect(plugins[1].cli!.name).toEqual('c2');
59
58
  });
60
59
 
61
60
  test('should load user string plugin successfully', () => {
@@ -72,9 +71,7 @@ describe('load plugins', () => {
72
71
  {
73
72
  cli: {
74
73
  name: 'a',
75
- pluginPath: path.join(fixture, './test-plugin-a.js'),
76
74
  },
77
- cliPkg: path.join(fixture, './test-plugin-a.js'),
78
75
  },
79
76
  ]);
80
77
  });
@@ -88,4 +85,32 @@ describe('load plugins', () => {
88
85
  });
89
86
  }).toThrowError(/^Can not find plugin /);
90
87
  });
88
+
89
+ test(`should load new plugin array correctly`, () => {
90
+ const appDirectory = path.resolve(
91
+ __dirname,
92
+ './fixtures/load-plugin/user-plugins',
93
+ );
94
+ const plugin = (): CliPlugin => ({
95
+ name: 'foo',
96
+ });
97
+ const userConfig = { plugins: [plugin()] };
98
+ const loadedPlugins = loadPlugins(appDirectory, userConfig);
99
+
100
+ expect(loadedPlugins[0].cli?.name).toEqual('foo');
101
+ });
102
+
103
+ test(`should load new plugin object correctly`, () => {
104
+ const appDirectory = path.resolve(
105
+ __dirname,
106
+ './fixtures/load-plugin/user-plugins',
107
+ );
108
+ const plugin = (): CliPlugin => ({
109
+ name: 'foo',
110
+ });
111
+ const userConfig = { plugins: { cli: [plugin()] } };
112
+ const loadedPlugins = loadPlugins(appDirectory, userConfig);
113
+
114
+ expect(loadedPlugins[0].cli?.name).toEqual('foo');
115
+ });
91
116
  });
@@ -1,8 +1,7 @@
1
- import { program, Command } from '../src/utils/commander';
1
+ import { program } from '../src/utils/commander';
2
2
 
3
3
  describe('utils', () => {
4
4
  it('default', () => {
5
5
  expect(program).toBeDefined();
6
- expect(Command).toBeDefined();
7
6
  });
8
7
  });