@modern-js/core 2.1.0 → 2.2.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @modern-js/core
2
2
 
3
+ ## 2.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 12ef50f: feat: `modern-js/core` can watch files change
8
+ feat: `modern-js/core` 可以监听文件变化
9
+
10
+ ### Patch Changes
11
+
12
+ - cb12ee7: chore: remove some unused deps, bump postcss version
13
+
14
+ chore: 移除未使用的依赖, 升级 postcss 版本
15
+
16
+ - 360a259: fix: `@modern-js/core` add `afterPrepare` hook
17
+ fix: `@modern-js/core` 新增 `afterPrepare` hook
18
+ - Updated dependencies [49eff0c]
19
+ - @modern-js/utils@2.2.0
20
+ - @modern-js/node-bundle-require@2.2.0
21
+ - @modern-js/plugin@2.2.0
22
+
3
23
  ## 2.1.0
4
24
 
5
25
  ### Patch Changes
package/dist/index.js CHANGED
@@ -20,7 +20,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.initAppContext = exports.initAppDir = exports.cli = exports.mergeOptions = exports.useResolvedConfigContext = exports.useConfigContext = exports.useAppContext = exports.ResolvedConfigContext = exports.ConfigContext = exports.AppContext = exports.registerHook = exports.createPlugin = exports.mountHook = exports.manager = void 0;
21
21
  const path_1 = __importDefault(require("path"));
22
22
  const utils_1 = require("@modern-js/utils");
23
- const commander_1 = require("./utils/commander");
23
+ const utils_2 = require("./utils");
24
24
  const loadPlugins_1 = require("./loadPlugins");
25
25
  const context_1 = require("./context");
26
26
  Object.defineProperty(exports, "initAppContext", { enumerable: true, get: function () { return context_1.initAppContext; } });
@@ -76,7 +76,7 @@ const createCli = () => {
76
76
  const mergedOptions = (0, exports.mergeOptions)(options);
77
77
  initOptions = mergedOptions;
78
78
  const appDirectory = await initAppDir(options === null || options === void 0 ? void 0 : options.cwd);
79
- (0, commander_1.initCommandsMap)();
79
+ (0, utils_2.initCommandsMap)();
80
80
  setProgramVersion(options === null || options === void 0 ? void 0 : options.version);
81
81
  const metaName = (_b = (_a = mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.options) === null || _a === void 0 ? void 0 : _a.metaName) !== null && _b !== void 0 ? _b : 'MODERN';
82
82
  (0, loadEnv_1.loadEnv)(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
@@ -123,14 +123,16 @@ const createCli = () => {
123
123
  context_1.ResolvedConfigContext.set(resolved);
124
124
  await hooksRunner.addRuntimeExports();
125
125
  await hooksRunner.prepare();
126
+ await hooksRunner.afterPrepare();
126
127
  return {
127
128
  resolved,
128
129
  appContext: (0, context_1.useAppContext)(),
129
130
  };
130
131
  };
131
132
  async function run(options) {
132
- await init(options);
133
+ const { appContext } = await init(options);
133
134
  await hooksRunner.commands({ program: utils_1.program });
135
+ await (0, utils_2.createFileWatcher)(appContext, hooksRunner);
134
136
  utils_1.program.parse(process.argv);
135
137
  }
136
138
  async function test(argv, options) {
package/dist/manager.d.ts CHANGED
@@ -6,6 +6,12 @@ export declare const manager: import("@modern-js/plugin").AsyncManager<{
6
6
  }>;
7
7
  validateSchema: import("@modern-js/plugin").ParallelWorkflow<void, any>;
8
8
  prepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
9
+ afterPrepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
10
+ watchFiles: import("@modern-js/plugin").ParallelWorkflow<void, any>;
11
+ fileChange: import("@modern-js/plugin").AsyncWorkflow<{
12
+ filename: string;
13
+ eventType: "add" | "unlink" | "change";
14
+ }, void>;
9
15
  commands: import("@modern-js/plugin").AsyncWorkflow<{
10
16
  program: import("@modern-js/utils").Command;
11
17
  }, void>;
@@ -19,6 +25,12 @@ export declare const createPlugin: (setup?: import("@modern-js/plugin").AsyncSet
19
25
  }>;
20
26
  validateSchema: import("@modern-js/plugin").ParallelWorkflow<void, any>;
21
27
  prepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
28
+ afterPrepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
29
+ watchFiles: import("@modern-js/plugin").ParallelWorkflow<void, any>;
30
+ fileChange: import("@modern-js/plugin").AsyncWorkflow<{
31
+ filename: string;
32
+ eventType: "add" | "unlink" | "change";
33
+ }, void>;
22
34
  commands: import("@modern-js/plugin").AsyncWorkflow<{
23
35
  program: import("@modern-js/utils").Command;
24
36
  }, void>;
@@ -31,6 +43,12 @@ export declare const createPlugin: (setup?: import("@modern-js/plugin").AsyncSet
31
43
  }>;
32
44
  validateSchema: import("@modern-js/plugin").ParallelWorkflow<void, any>;
33
45
  prepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
46
+ afterPrepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
47
+ watchFiles: import("@modern-js/plugin").ParallelWorkflow<void, any>;
48
+ fileChange: import("@modern-js/plugin").AsyncWorkflow<{
49
+ filename: string;
50
+ eventType: "add" | "unlink" | "change";
51
+ }, void>;
34
52
  commands: import("@modern-js/plugin").AsyncWorkflow<{
35
53
  program: import("@modern-js/utils").Command;
36
54
  }, void>;
@@ -43,6 +61,12 @@ export declare const createPlugin: (setup?: import("@modern-js/plugin").AsyncSet
43
61
  }>;
44
62
  validateSchema: import("@modern-js/plugin").ParallelWorkflow<void, any>;
45
63
  prepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
64
+ afterPrepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
65
+ watchFiles: import("@modern-js/plugin").ParallelWorkflow<void, any>;
66
+ fileChange: import("@modern-js/plugin").AsyncWorkflow<{
67
+ filename: string;
68
+ eventType: "add" | "unlink" | "change";
69
+ }, void>;
46
70
  commands: import("@modern-js/plugin").AsyncWorkflow<{
47
71
  program: import("@modern-js/utils").Command;
48
72
  }, void>;
@@ -55,6 +79,12 @@ export declare const createPlugin: (setup?: import("@modern-js/plugin").AsyncSet
55
79
  }>;
56
80
  validateSchema: import("@modern-js/plugin").ParallelWorkflow<void, any>;
57
81
  prepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
82
+ afterPrepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
83
+ watchFiles: import("@modern-js/plugin").ParallelWorkflow<void, any>;
84
+ fileChange: import("@modern-js/plugin").AsyncWorkflow<{
85
+ filename: string;
86
+ eventType: "add" | "unlink" | "change";
87
+ }, void>;
58
88
  commands: import("@modern-js/plugin").AsyncWorkflow<{
59
89
  program: import("@modern-js/utils").Command;
60
90
  }, void>;
@@ -67,6 +97,12 @@ export declare const createPlugin: (setup?: import("@modern-js/plugin").AsyncSet
67
97
  }>;
68
98
  validateSchema: import("@modern-js/plugin").ParallelWorkflow<void, any>;
69
99
  prepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
100
+ afterPrepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
101
+ watchFiles: import("@modern-js/plugin").ParallelWorkflow<void, any>;
102
+ fileChange: import("@modern-js/plugin").AsyncWorkflow<{
103
+ filename: string;
104
+ eventType: "add" | "unlink" | "change";
105
+ }, void>;
70
106
  commands: import("@modern-js/plugin").AsyncWorkflow<{
71
107
  program: import("@modern-js/utils").Command;
72
108
  }, void>;
@@ -79,6 +115,12 @@ export declare const createPlugin: (setup?: import("@modern-js/plugin").AsyncSet
79
115
  }>;
80
116
  validateSchema: import("@modern-js/plugin").ParallelWorkflow<void, any>;
81
117
  prepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
118
+ afterPrepare: import("@modern-js/plugin").AsyncWorkflow<void, void>;
119
+ watchFiles: import("@modern-js/plugin").ParallelWorkflow<void, any>;
120
+ fileChange: import("@modern-js/plugin").AsyncWorkflow<{
121
+ filename: string;
122
+ eventType: "add" | "unlink" | "change";
123
+ }, void>;
82
124
  commands: import("@modern-js/plugin").AsyncWorkflow<{
83
125
  program: import("@modern-js/utils").Command;
84
126
  }, void>;
package/dist/manager.js CHANGED
@@ -9,7 +9,10 @@ const baseHooks = {
9
9
  resolvedConfig: (0, plugin_1.createAsyncWaterfall)(),
10
10
  validateSchema: (0, plugin_1.createParallelWorkflow)(),
11
11
  prepare: (0, plugin_1.createAsyncWorkflow)(),
12
+ afterPrepare: (0, plugin_1.createAsyncWorkflow)(),
12
13
  commands: (0, plugin_1.createAsyncWorkflow)(),
14
+ watchFiles: (0, plugin_1.createParallelWorkflow)(),
15
+ fileChange: (0, plugin_1.createAsyncWorkflow)(),
13
16
  beforeExit: (0, plugin_1.createAsyncWorkflow)(),
14
17
  addRuntimeExports: (0, plugin_1.createAsyncWaterfall)(),
15
18
  };
@@ -12,6 +12,12 @@ export type BaseHooks<Extends extends {
12
12
  }>;
13
13
  validateSchema: ParallelWorkflow<void>;
14
14
  prepare: AsyncWorkflow<void, void>;
15
+ afterPrepare: AsyncWorkflow<void, void>;
16
+ watchFiles: ParallelWorkflow<void>;
17
+ fileChange: AsyncWorkflow<{
18
+ filename: string;
19
+ eventType: 'add' | 'change' | 'unlink';
20
+ }, void>;
15
21
  commands: AsyncWorkflow<{
16
22
  program: Command;
17
23
  }, void>;
@@ -0,0 +1,2 @@
1
+ import type { CliHooksRunner, IAppContext } from '../types';
2
+ export declare const createFileWatcher: (appContext: IAppContext, hooksRunner: CliHooksRunner) => Promise<import("@modern-js/utils").FSWatcher | undefined>;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.createFileWatcher = void 0;
30
+ const crypto_1 = __importDefault(require("crypto"));
31
+ const path = __importStar(require("path"));
32
+ const fs = __importStar(require("fs"));
33
+ const utils_1 = require("@modern-js/utils");
34
+ const debug = (0, utils_1.createDebugger)('watch-files');
35
+ const hashMap = new Map();
36
+ const md5 = (data) => crypto_1.default.createHash('md5').update(data).digest('hex');
37
+ const createFileWatcher = async (appContext, hooksRunner) => {
38
+ // only add fs watcher on dev mode.
39
+ if ((0, utils_1.isDev)() || (0, utils_1.isTest)()) {
40
+ const { appDirectory } = appContext;
41
+ const extraFiles = await hooksRunner.watchFiles();
42
+ const watched = extraFiles.filter(Boolean);
43
+ debug(`watched: %o`, watched);
44
+ const watcher = utils_1.chokidar.watch(watched, {
45
+ cwd: appDirectory,
46
+ ignoreInitial: true,
47
+ ignorePermissionErrors: true,
48
+ ignored: [
49
+ /node_modules/,
50
+ '**/__test__/**',
51
+ '**/*.test.(js|jsx|ts|tsx)',
52
+ '**/*.spec.(js|jsx|ts|tsx)',
53
+ '**/*.stories.(js|jsx|ts|tsx)',
54
+ ],
55
+ });
56
+ watcher.on('change', changed => {
57
+ const lastHash = hashMap.get(changed);
58
+ const currentHash = md5(fs.readFileSync(path.join(appDirectory, changed), 'utf8'));
59
+ if (currentHash !== lastHash) {
60
+ debug(`file change: %s`, changed);
61
+ hashMap.set(changed, currentHash);
62
+ hooksRunner.fileChange({ filename: changed, eventType: 'change' });
63
+ }
64
+ });
65
+ watcher.on('add', name => {
66
+ debug(`add file: %s`, name);
67
+ const currentHash = md5(fs.readFileSync(path.join(appDirectory, name), 'utf8'));
68
+ hashMap.set(name, currentHash);
69
+ hooksRunner.fileChange({ filename: name, eventType: 'add' });
70
+ });
71
+ watcher.on('unlink', name => {
72
+ debug(`remove file: %s`, name);
73
+ if (hashMap.has(name)) {
74
+ hashMap.delete(name);
75
+ }
76
+ hooksRunner.fileChange({ filename: name, eventType: 'unlink' });
77
+ });
78
+ watcher.on('error', err => {
79
+ throw err;
80
+ });
81
+ return watcher;
82
+ }
83
+ };
84
+ exports.createFileWatcher = createFileWatcher;
@@ -0,0 +1,4 @@
1
+ export * from './commander';
2
+ export * from './createFileWatcher';
3
+ export * from './mergeConfig';
4
+ export * from './repeatKeyWarning';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./commander"), exports);
18
+ __exportStar(require("./createFileWatcher"), exports);
19
+ __exportStar(require("./mergeConfig"), exports);
20
+ __exportStar(require("./repeatKeyWarning"), exports);
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "modern",
11
11
  "modern.js"
12
12
  ],
13
- "version": "2.1.0",
13
+ "version": "2.2.0",
14
14
  "jsnext:source": "./src/index.ts",
15
15
  "types": "./dist/index.d.ts",
16
16
  "main": "./dist/index.js",
@@ -51,9 +51,9 @@
51
51
  }
52
52
  },
53
53
  "dependencies": {
54
- "@modern-js/node-bundle-require": "2.1.0",
55
- "@modern-js/plugin": "2.1.0",
56
- "@modern-js/utils": "2.1.0"
54
+ "@modern-js/node-bundle-require": "2.2.0",
55
+ "@modern-js/plugin": "2.2.0",
56
+ "@modern-js/utils": "2.2.0"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@jest/types": "^27.0.6",
@@ -64,19 +64,18 @@
64
64
  "@types/node": "^14",
65
65
  "autoprefixer": "^10.3.1",
66
66
  "btsm": "2.2.2",
67
- "electron-builder": "22.7.0",
68
67
  "html-webpack-plugin": "5.5.0",
69
68
  "jest": "^27",
70
- "postcss": "^8.4.14",
69
+ "postcss": "8.4.21",
71
70
  "sass": "^1.45.0",
72
71
  "terser-webpack-plugin": "^5.1.4",
73
72
  "typescript": "^4",
74
73
  "webpack": "^5.75.0",
75
- "@modern-js/builder-shared": "2.1.0",
76
- "@modern-js/babel-preset-app": "2.1.0",
77
- "@modern-js/types": "2.1.0",
78
- "@scripts/jest-config": "2.1.0",
79
- "@scripts/build": "2.1.0"
74
+ "@modern-js/builder-shared": "2.2.0",
75
+ "@modern-js/babel-preset-app": "2.2.0",
76
+ "@modern-js/types": "2.2.0",
77
+ "@scripts/jest-config": "2.2.0",
78
+ "@scripts/build": "2.2.0"
80
79
  },
81
80
  "sideEffects": false,
82
81
  "publishConfig": {