@modern-js/core 1.6.0 → 1.7.1-beta.peer.1

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 (68) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/js/modern/config/index.js +11 -7
  3. package/dist/js/modern/config/mergeConfig.js +1 -1
  4. package/dist/js/modern/config/schema/index.js +1 -1
  5. package/dist/js/modern/config/types/electron.js +1 -0
  6. package/dist/js/modern/config/types/index.js +1 -0
  7. package/dist/js/modern/config/types/less.js +0 -0
  8. package/dist/js/modern/config/types/sass.js +0 -0
  9. package/dist/js/modern/config/types/ssg.js +0 -0
  10. package/dist/js/modern/config/types/test.js +0 -0
  11. package/dist/js/modern/config/types/unbundle.js +0 -0
  12. package/dist/js/modern/context.js +9 -2
  13. package/dist/js/modern/index.js +27 -9
  14. package/dist/js/modern/initWatcher.js +2 -2
  15. package/dist/js/modern/loadPlugins.js +9 -4
  16. package/dist/js/modern/utils/commander.js +15 -15
  17. package/dist/js/node/config/index.js +38 -11
  18. package/dist/js/node/config/mergeConfig.js +2 -4
  19. package/dist/js/node/config/schema/index.js +3 -5
  20. package/dist/js/node/config/types/electron.js +5 -0
  21. package/dist/js/node/config/types/index.js +5 -0
  22. package/dist/js/node/config/types/less.js +0 -0
  23. package/dist/js/node/config/types/sass.js +0 -0
  24. package/dist/js/node/config/types/ssg.js +0 -0
  25. package/dist/js/node/config/types/test.js +0 -0
  26. package/dist/js/node/config/types/unbundle.js +0 -0
  27. package/dist/js/node/context.js +9 -2
  28. package/dist/js/node/index.js +32 -10
  29. package/dist/js/node/initWatcher.js +2 -3
  30. package/dist/js/node/loadPlugins.js +9 -4
  31. package/dist/js/node/utils/commander.js +16 -19
  32. package/dist/types/config/index.d.ts +4 -140
  33. package/dist/types/config/types/electron.d.ts +13 -0
  34. package/dist/types/config/types/index.d.ts +252 -0
  35. package/dist/types/config/types/less.d.ts +10 -0
  36. package/dist/types/config/types/sass.d.ts +8 -0
  37. package/dist/types/config/types/ssg.d.ts +13 -0
  38. package/dist/types/config/types/test.d.ts +15 -0
  39. package/dist/types/config/types/unbundle.d.ts +28 -0
  40. package/dist/types/context.d.ts +18 -6
  41. package/dist/types/index.d.ts +28 -1
  42. package/dist/types/initWatcher.d.ts +1 -2
  43. package/dist/types/loadPlugins.d.ts +3 -0
  44. package/dist/types/manager.d.ts +7 -6
  45. package/dist/types/utils/commander.d.ts +4 -7
  46. package/jest.config.js +0 -1
  47. package/package.json +18 -17
  48. package/tests/.eslintrc.js +0 -6
  49. package/tests/btsm.test.ts +0 -20
  50. package/tests/config.test.ts +0 -137
  51. package/tests/context.test.ts +0 -70
  52. package/tests/fixtures/index-test/package.json +0 -3
  53. package/tests/fixtures/load-plugin/not-found/package.json +0 -3
  54. package/tests/fixtures/load-plugin/not-found/test-plugin-a.js +0 -1
  55. package/tests/fixtures/load-plugin/user-plugins/package.json +0 -3
  56. package/tests/fixtures/load-plugin/user-plugins/test-plugin-a.js +0 -1
  57. package/tests/fixtures/load-plugin/user-plugins/test-plugin-b.js +0 -3
  58. package/tests/fixtures/load-plugin/user-plugins/test-plugin-c.js +0 -3
  59. package/tests/index.test.ts +0 -69
  60. package/tests/initWatcher.test.ts +0 -63
  61. package/tests/loadEnv.test.ts +0 -173
  62. package/tests/loadPlugin.test.ts +0 -116
  63. package/tests/mergeConfig.test.ts +0 -97
  64. package/tests/pluginAPI.test.ts +0 -19
  65. package/tests/repeatKeyWarning.test.ts +0 -68
  66. package/tests/schema.test.ts +0 -107
  67. package/tests/tsconfig.json +0 -11
  68. package/tests/utils.test.ts +0 -8
@@ -7,6 +7,7 @@ export declare type LoadedPlugin = {
7
7
  server?: string;
8
8
  serverPkg?: string;
9
9
  };
10
+ export declare type TransformPlugin = (plugin: PluginConfig, resolvedConfig: UserConfig, pluginOptions?: any) => PluginConfig;
10
11
  /**
11
12
  * @deprecated
12
13
  * Using NewPluginConfig insteand.
@@ -32,10 +33,12 @@ export declare function getAppPlugins(appDirectory: string, oldPluginConfig: Old
32
33
  * @param appDirectory - Application root directory.
33
34
  * @param userConfig - Resolved user config.
34
35
  * @param options.internalPlugins - Internal plugins.
36
+ * @param options.transformPlugin - transform plugin before using it. Used for compatible with legacy jupiter plugins.
35
37
  * @returns Plugin Objects has been required.
36
38
  */
37
39
 
38
40
  export declare const loadPlugins: (appDirectory: string, userConfig: UserConfig, options?: {
39
41
  internalPlugins?: typeof INTERNAL_PLUGINS;
42
+ transformPlugin?: TransformPlugin;
40
43
  }) => LoadedPlugin[];
41
44
  export {};
@@ -2,9 +2,10 @@ import { ToThreads, ToRunners, AsyncSetup, PluginOptions, AsyncWorkflow, AsyncWa
2
2
  import type { Hooks } from '@modern-js/types';
3
3
  import type { Command } from './utils/commander';
4
4
  import type { NormalizedConfig } from './config/mergeConfig';
5
+ import type { UserConfig } from './config';
5
6
  import { pluginAPI } from './pluginAPI';
6
7
  export declare type HooksRunner = ToRunners<{
7
- config: ParallelWorkflow<void>;
8
+ config: ParallelWorkflow<void, UserConfig>;
8
9
  resolvedConfig: AsyncWaterfall<{
9
10
  resolved: NormalizedConfig;
10
11
  }>;
@@ -22,7 +23,7 @@ export declare type HooksRunner = ToRunners<{
22
23
  beforeRestart: AsyncWorkflow<void, void>;
23
24
  }>;
24
25
  declare const baseHooks: {
25
- config: ParallelWorkflow<void, unknown>;
26
+ config: ParallelWorkflow<void, UserConfig>;
26
27
  resolvedConfig: AsyncWaterfall<{
27
28
  resolved: NormalizedConfig;
28
29
  }>;
@@ -48,7 +49,7 @@ export declare type CliHookCallbacks = ToThreads<CliHooks>;
48
49
  export declare const manager: import("@modern-js/plugin").AsyncManager<CliHooks, {
49
50
  setAppContext: (value: import("@modern-js/types").IAppContext) => void;
50
51
  useAppContext: () => import("@modern-js/types").IAppContext;
51
- useConfigContext: () => import("./config").UserConfig;
52
+ useConfigContext: () => UserConfig;
52
53
  useResolvedConfigContext: () => NormalizedConfig;
53
54
  }>;
54
55
  /** Plugin options of a cli plugin. */
@@ -57,17 +58,17 @@ export declare type CliPlugin = PluginOptions<CliHooks, AsyncSetup<CliHooks, typ
57
58
  export declare const createPlugin: (setup?: AsyncSetup<CliHooks, {
58
59
  setAppContext: (value: import("@modern-js/types").IAppContext) => void;
59
60
  useAppContext: () => import("@modern-js/types").IAppContext;
60
- useConfigContext: () => import("./config").UserConfig;
61
+ useConfigContext: () => UserConfig;
61
62
  useResolvedConfigContext: () => NormalizedConfig;
62
63
  }> | undefined, options?: PluginOptions<CliHooks, AsyncSetup<CliHooks, {
63
64
  setAppContext: (value: import("@modern-js/types").IAppContext) => void;
64
65
  useAppContext: () => import("@modern-js/types").IAppContext;
65
- useConfigContext: () => import("./config").UserConfig;
66
+ useConfigContext: () => UserConfig;
66
67
  useResolvedConfigContext: () => NormalizedConfig;
67
68
  }>> | undefined) => import("@modern-js/plugin").AsyncPlugin<CliHooks, {
68
69
  setAppContext: (value: import("@modern-js/types").IAppContext) => void;
69
70
  useAppContext: () => import("@modern-js/types").IAppContext;
70
- useConfigContext: () => import("./config").UserConfig;
71
+ useConfigContext: () => UserConfig;
71
72
  useResolvedConfigContext: () => NormalizedConfig;
72
73
  }>, registerHook: (newHooks: Partial<CliHooks>) => void, mountHook: () => ToRunners<CliHooks>;
73
74
  export declare const usePlugins: (plugins: string[]) => void;
@@ -1,7 +1,4 @@
1
- import { program, Command } from 'commander';
2
- declare module 'commander' {
3
- interface Command {
4
- commandsMap: Map<string, Command>;
5
- }
6
- }
7
- export { program, Command };
1
+ import { program, Command } from '@modern-js/utils';
2
+ export declare function initCommandsMap(): void;
3
+ export type { Command };
4
+ 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.6.0",
14
+ "version": "1.7.1-beta.peer.1",
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,38 @@
42
42
  "dependencies": {
43
43
  "@babel/code-frame": "^7.14.5",
44
44
  "@babel/runtime": "^7",
45
- "@modern-js/load-config": "^1.2.2",
45
+ "@modern-js/load-config": "^1.3.0",
46
46
  "@modern-js/plugin": "^1.3.2",
47
- "@modern-js/utils": "^1.3.6",
47
+ "@modern-js/utils": "^1.4.1",
48
48
  "address": "^1.1.2",
49
49
  "ajv": "^8.6.2",
50
50
  "ajv-keywords": "^5.0.0",
51
- "better-ajv-errors": "^0.7.0",
52
- "chokidar": "^3.5.2",
53
- "commander": "^8.1.0",
51
+ "better-ajv-errors": "1.2.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
+ "electron-builder": "22.7.0",
73
+ "terser-webpack-plugin": "^5.1.4",
74
+ "typescript": "^4",
75
+ "webpack": "^5.71.0",
76
+ "webpack-chain": "^6.5.1"
76
77
  },
77
78
  "sideEffects": false,
78
79
  "modernConfig": {
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- extends: ['@modern-js'],
3
- parserOptions: {
4
- project: require.resolve('./tsconfig.json'),
5
- },
6
- };
@@ -1,20 +0,0 @@
1
- import path from 'path';
2
- import { spawnSync } from 'child_process';
3
-
4
- const kPackageDir = path.resolve(__dirname, '..');
5
-
6
- describe('jsnext:source', () => {
7
- test('process exit status is 0', () => {
8
- const { status, stdout, stderr } = spawnSync(
9
- process.execPath,
10
- ['--conditions=jsnext:source', '-r', 'btsm', 'src/cli.ts'],
11
- {
12
- cwd: kPackageDir,
13
- encoding: 'utf-8',
14
- },
15
- );
16
- expect(stdout).toBe('');
17
- expect(stderr.startsWith('Usage: modern <command> [options]')).toBe(true);
18
- expect(status).toBe(1);
19
- });
20
- });
@@ -1,137 +0,0 @@
1
- import path from 'path';
2
- // import os from 'os';
3
- import { isDev, getPort } from '@modern-js/utils';
4
- import { resolveConfig } from '../src/config';
5
- import {
6
- cli,
7
- loadUserConfig,
8
- initAppContext,
9
- initAppDir,
10
- manager,
11
- createPlugin,
12
- registerHook,
13
- useRunner,
14
- } from '../src';
15
- import { defaults } from '../src/config/defaults';
16
-
17
- jest.mock('@modern-js/utils', () => ({
18
- __esModule: true,
19
- ...jest.requireActual('@modern-js/utils'),
20
- isDev: jest.fn(),
21
- getPort: jest.fn(),
22
- }));
23
-
24
- // const kOSRootDir =
25
- // os.platform() === 'win32' ? process.cwd().split(path.sep)[0] : '/';
26
-
27
- describe('config', () => {
28
- /**
29
- * Typescript Type annotations cannot be used for esbuild-jest
30
- * test files that use jest.mock('@some/module')
31
- * refer to this esbuild-jest issue:
32
- * https://github.com/aelbore/esbuild-jest/issues/57
33
- * TODO: find a better solution to solve this problem while allowing us
34
- * to use esbuild, and have good TypeScript support
35
- */
36
- let loaded = {
37
- config: {},
38
- filePath: '',
39
- dependencies: [],
40
- pkgConfig: {},
41
- jsConfig: {},
42
- };
43
- let schemas: any[] = [];
44
- let restartWithExistingPort = 0;
45
- let argv: string[] = ['dev'];
46
- let configs: any[] = [];
47
-
48
- const getResolvedConfig = async () =>
49
- resolveConfig(loaded, configs, schemas, restartWithExistingPort, argv);
50
-
51
- const resetParams = () => {
52
- loaded = {
53
- config: {},
54
- filePath: '',
55
- dependencies: [],
56
- pkgConfig: {},
57
- jsConfig: {},
58
- };
59
- schemas = [];
60
- restartWithExistingPort = 0;
61
- argv = ['dev'];
62
- configs = [];
63
- };
64
- const resetMock = () => {
65
- jest.resetAllMocks();
66
- (isDev as jest.Mock).mockReturnValue(true);
67
- (getPort as jest.Mock).mockReturnValue(
68
- Promise.resolve(defaults.server.port),
69
- );
70
- };
71
- beforeEach(() => {
72
- resetParams();
73
- resetMock();
74
- });
75
-
76
- it('default', () => {
77
- expect(resolveConfig).toBeDefined();
78
- expect(cli).toBeDefined();
79
- expect(loadUserConfig).toBeDefined();
80
- expect(initAppContext).toBeDefined();
81
- expect(initAppDir).toBeDefined();
82
- expect(manager).toBeDefined();
83
- expect(createPlugin).toBeDefined();
84
- expect(registerHook).toBeDefined();
85
- expect(useRunner).toBeDefined();
86
- });
87
-
88
- it('initAppDir', async () => {
89
- expect(await initAppDir(__dirname)).toBe(path.resolve(__dirname, '..'));
90
- // expect(await initAppDir()).toBe(path.resolve(__dirname, '..'));
91
-
92
- // FIXME: windows 下面会失败,先忽略这个测试
93
- // try {
94
- // await initAppDir(kOSRootDir);
95
- // expect(true).toBe(false); // SHOULD NOT BE HERE
96
- // } catch (err: any) {
97
- // expect(err.message).toMatch(/no package.json found in current work dir/);
98
- // }
99
- });
100
-
101
- test('should use default port if not restarting in dev mode', async () => {
102
- let resolved = await getResolvedConfig();
103
- expect(resolved.server.port).toEqual(defaults.server.port);
104
- expect(getPort).toHaveBeenCalledWith(defaults.server.port);
105
-
106
- // getResolvedConfig should use the value givin by getPort
107
- restartWithExistingPort = -1;
108
- (getPort as jest.Mock).mockClear();
109
- (getPort as jest.Mock).mockReturnValue(1111);
110
- resolved = await getResolvedConfig();
111
- expect(resolved.server.port).toEqual(1111);
112
- expect(getPort).toHaveBeenCalledWith(defaults.server.port);
113
-
114
- argv = ['start'];
115
- (isDev as jest.Mock).mockReturnValue(false);
116
- restartWithExistingPort = 0;
117
- resolved = await getResolvedConfig();
118
- expect(resolved.server.port).toEqual(defaults.server.port);
119
-
120
- restartWithExistingPort = 1234;
121
- resolved = await getResolvedConfig();
122
- expect(resolved.server.port).toEqual(defaults.server.port);
123
-
124
- restartWithExistingPort = -1;
125
- resolved = await getResolvedConfig();
126
- expect(resolved.server.port).toEqual(defaults.server.port);
127
- });
128
-
129
- test('should reuse existing port if restarting in dev mode', async () => {
130
- restartWithExistingPort = 1234;
131
- const resolved = await getResolvedConfig();
132
- expect(resolved.server.port).toEqual(1234);
133
- });
134
- });
135
-
136
- // type TEST = Parameters<typeof resolveConfig>;
137
- // type TypeC = TEST[1];
@@ -1,70 +0,0 @@
1
- import path from 'path';
2
- import { initAppContext } from '../src/context';
3
-
4
- describe('context', () => {
5
- it('initAppContext', () => {
6
- const appDirectory = path.resolve(
7
- __dirname,
8
- './fixtures/load-plugin/user-plugins',
9
- );
10
- const appContext = initAppContext(appDirectory, [], false);
11
- expect(appContext).toEqual({
12
- appDirectory,
13
- configFile: false,
14
- ip: expect.any(String),
15
- port: 0,
16
- packageName: expect.any(String),
17
- srcDirectory: expect.any(String),
18
- distDirectory: expect.any(String),
19
- sharedDirectory: expect.any(String),
20
- nodeModulesDirectory: expect.any(String),
21
- internalDirectory: expect.any(String),
22
- plugins: [],
23
- htmlTemplates: {},
24
- serverRoutes: [],
25
- entrypoints: [],
26
- checkedEntries: [],
27
- existSrc: true,
28
- internalDirAlias: '@_modern_js_internal',
29
- internalSrcAlias: '@_modern_js_src',
30
- metaName: 'modern-js',
31
- });
32
- });
33
-
34
- it('custom AppContext', () => {
35
- const appDirectory = path.resolve(
36
- __dirname,
37
- './fixtures/load-plugin/user-plugins',
38
- );
39
-
40
- const customOptions = {
41
- srcDir: 'source',
42
- distDir: 'dist',
43
- sharedDir: 'myShared',
44
- metaName: 'jupiter',
45
- };
46
-
47
- const appContext = initAppContext(appDirectory, [], false, customOptions);
48
- expect(appContext).toEqual({
49
- appDirectory,
50
- configFile: false,
51
- ip: expect.any(String),
52
- port: 0,
53
- packageName: 'user-plugins',
54
- srcDirectory: path.resolve(appDirectory, './source'),
55
- distDirectory: 'dist',
56
- sharedDirectory: path.resolve(appDirectory, './myShared'),
57
- nodeModulesDirectory: expect.any(String),
58
- internalDirectory: path.resolve(appDirectory, './node_modules/.jupiter'),
59
- plugins: [],
60
- htmlTemplates: {},
61
- serverRoutes: [],
62
- entrypoints: [],
63
- checkedEntries: [],
64
- existSrc: true,
65
- internalDirAlias: '@_jupiter_internal',
66
- internalSrcAlias: '@_jupiter_src',
67
- metaName: 'jupiter',
68
- });
69
- });
70
- });
@@ -1,3 +0,0 @@
1
- {
2
- "name": "index-test"
3
- }
@@ -1,3 +0,0 @@
1
- {
2
- "name": "not-found"
3
- }
@@ -1 +0,0 @@
1
- module.exports = { name: 'a' };
@@ -1,3 +0,0 @@
1
- {
2
- "name": "user-plugins"
3
- }
@@ -1 +0,0 @@
1
- module.exports = { name: 'a' };
@@ -1,3 +0,0 @@
1
- Object.defineProperty(exports, '__esModule', { value: true });
2
-
3
- exports.default = { name: 'b' };
@@ -1,3 +0,0 @@
1
- Object.defineProperty(exports, '__esModule', { value: true });
2
-
3
- exports.default = name => ({ name });
@@ -1,69 +0,0 @@
1
- import path from 'path';
2
- import { cli } from '../src';
3
- import { resolveConfig, loadUserConfig } from '../src/config';
4
- import { loadEnv } from '../src/loadEnv';
5
-
6
- jest.mock('../src/config', () => ({
7
- __esModule: true,
8
- ...jest.requireActual('../src/config'),
9
- loadUserConfig: jest.fn(),
10
- resolveConfig: jest.fn(),
11
- }));
12
-
13
- jest.mock('../src/loadEnv', () => ({
14
- __esModule: true,
15
- ...jest.requireActual('../src/loadEnv'),
16
- loadEnv: jest.fn(),
17
- }));
18
-
19
- describe('@modern-js/core test', () => {
20
- let mockResolveConfig: any = {};
21
- let mockLoadedConfig: any = {};
22
- const cwdSpy = jest.spyOn(process, 'cwd');
23
- const cwd = path.join(__dirname, './fixtures/index-test');
24
-
25
- const resetMock = () => {
26
- jest.resetAllMocks();
27
- cwdSpy.mockReturnValue(cwd);
28
- (resolveConfig as jest.Mock).mockReturnValue(
29
- Promise.resolve(mockResolveConfig),
30
- );
31
- (loadUserConfig as jest.Mock).mockImplementation(() =>
32
- Promise.resolve(mockLoadedConfig),
33
- );
34
- };
35
-
36
- const resetValues = () => {
37
- mockLoadedConfig = {
38
- config: {},
39
- filePath: false,
40
- dependencies: [],
41
- pkgConfig: {},
42
- jsConfig: {},
43
- };
44
- mockResolveConfig = {
45
- server: {
46
- port: 8080,
47
- },
48
- output: {
49
- path: './my/test/path',
50
- },
51
- };
52
- };
53
-
54
- beforeEach(() => {
55
- resetValues();
56
- resetMock();
57
- });
58
-
59
- it('test cli create', () => {
60
- expect(cli).toBeTruthy();
61
- });
62
-
63
- it('test cli init dev', async () => {
64
- cwdSpy.mockReturnValue(path.join(cwd, 'nested-folder'));
65
- await cli.init(['dev']);
66
- expect(loadEnv).toHaveBeenCalledWith(cwd, undefined);
67
- // TODO: add more test cases
68
- });
69
- });
@@ -1,63 +0,0 @@
1
- import * as path from 'path';
2
- import { fs, wait } from '@modern-js/utils';
3
- import { initWatcher } from '../src/initWatcher';
4
-
5
- jest.useRealTimers();
6
-
7
- const mockAppDirectory = path.join(__dirname, './fixtures/index-test');
8
- const mockConfigDir = './config';
9
- const mockSrcDirectory = path.join(mockAppDirectory, './src');
10
-
11
- describe.skip('initWatcher', () => {
12
- afterAll(() => {
13
- const file = path.join(mockSrcDirectory, './index.ts');
14
- if (fs.existsSync(file)) {
15
- fs.unlinkSync(file);
16
- }
17
- });
18
-
19
- test('will trigger add event', async () => {
20
- let triggeredType = '';
21
- let triggeredFile = '';
22
- const loaded = {
23
- filePath: '',
24
- dependencies: [],
25
- };
26
- const hooksRunner = {
27
- watchFiles: async () => [mockSrcDirectory],
28
- fileChange: jest.fn(({ filename, eventType }) => {
29
- triggeredType = eventType;
30
- triggeredFile = filename;
31
- }),
32
- };
33
-
34
- if (await fs.pathExists(mockSrcDirectory)) {
35
- await fs.remove(mockSrcDirectory);
36
- }
37
-
38
- const watcher = await initWatcher(
39
- loaded as any,
40
- mockAppDirectory,
41
- mockConfigDir,
42
- hooksRunner as any,
43
- ['dev'],
44
- );
45
- await wait(100);
46
-
47
- const file = path.join(mockSrcDirectory, './index.ts');
48
- await fs.outputFile(file, '');
49
- await wait(100);
50
- // expect(hooksRunner.fileChange).toBeCalledTimes(1);
51
- // expect(triggeredType).toBe('add');
52
- expect(file.includes(triggeredFile)).toBeTruthy();
53
-
54
- await wait(100);
55
- await fs.remove(file);
56
- await wait(200);
57
- expect(hooksRunner.fileChange).toBeCalledTimes(2);
58
- expect(triggeredType).toBe('unlink');
59
- expect(file.includes(triggeredFile)).toBeTruthy();
60
-
61
- watcher?.close();
62
- });
63
- });