@modern-js/plugin-v2 2.63.8-alpha.2 → 2.64.1-alpha.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.
@@ -35,8 +35,8 @@ __export(loadConfig_exports, {
35
35
  });
36
36
  module.exports = __toCommonJS(loadConfig_exports);
37
37
  var import_path = __toESM(require("path"));
38
- var import_node_bundle_require = require("@modern-js/node-bundle-require");
39
38
  var import_utils = require("@modern-js/utils");
39
+ var import_jiti = __toESM(require("jiti"));
40
40
  const getPackageConfig = (appDirectory, packageJsonConfig) => {
41
41
  const json = JSON.parse(import_utils.fs.readFileSync(import_path.default.resolve(appDirectory, "./package.json"), "utf8"));
42
42
  return json[packageJsonConfig];
@@ -64,19 +64,19 @@ const clearFilesOverTime = async (targetDir, overtime) => {
64
64
  } catch (err) {
65
65
  }
66
66
  };
67
- const bundleRequireWithCatch = async (configFile, { appDirectory }) => {
67
+ function loadConfigContent(configFile) {
68
+ const _require = (0, import_jiti.default)(__filename, {
69
+ esmResolve: true,
70
+ // disable require cache to support restart CLI and read the new config
71
+ requireCache: false,
72
+ interopDefault: true
73
+ });
74
+ if (!import_utils.fs.existsSync(configFile)) {
75
+ throw new Error(`Configuration file does not exist: ${configFile}`);
76
+ }
68
77
  try {
69
- const mod = await (0, import_node_bundle_require.bundleRequire)(configFile, {
70
- autoClear: false,
71
- getOutputFile: async (filePath) => {
72
- const defaultOutputFileName = import_path.default.basename(await (0, import_node_bundle_require.defaultGetOutputFile)(filePath));
73
- const outputPath = import_path.default.join(appDirectory, import_utils.CONFIG_CACHE_DIR);
74
- const timeLimit = 10 * 60;
75
- await clearFilesOverTime(outputPath, timeLimit);
76
- return import_path.default.join(outputPath, defaultOutputFileName);
77
- }
78
- });
79
- return mod;
78
+ const config = _require(configFile);
79
+ return config.default || config;
80
80
  } catch (e) {
81
81
  if (e instanceof Error) {
82
82
  e.message = `Get Error while loading config file: ${configFile}, please check it and retry.
@@ -84,7 +84,7 @@ ${e.message || ""}`;
84
84
  }
85
85
  throw e;
86
86
  }
87
- };
87
+ }
88
88
  const loadConfig = async (appDirectory, configFile, packageJsonConfig) => {
89
89
  let pkgConfig;
90
90
  if (packageJsonConfig) {
@@ -93,11 +93,7 @@ const loadConfig = async (appDirectory, configFile, packageJsonConfig) => {
93
93
  const packageName = require(import_path.default.resolve(appDirectory, "./package.json")).name;
94
94
  let config;
95
95
  if (configFile) {
96
- delete require.cache[configFile];
97
- const mod = await bundleRequireWithCatch(configFile, {
98
- appDirectory
99
- });
100
- config = mod.default || mod;
96
+ config = loadConfigContent(configFile);
101
97
  }
102
98
  return {
103
99
  packageName,
@@ -26,7 +26,7 @@ function initPluginAPI({ context, plugins }) {
26
26
  const { hooks, extendsHooks } = context;
27
27
  function getRuntimeContext() {
28
28
  if (context) {
29
- const { hooks: hooks2, extendsHooks: extendsHooks2, config, pluginAPI, ...runtimeContext } = context;
29
+ const { hooks: hooks2, extendsHooks: extendsHooks2, config, pluginAPI: pluginAPI2, ...runtimeContext } = context;
30
30
  runtimeContext._internalContext = context;
31
31
  return runtimeContext;
32
32
  }
@@ -62,7 +62,7 @@ function initPluginAPI({ context, plugins }) {
62
62
  extendsPluginApi[hookName] = extendsHooks[hookName].tap;
63
63
  });
64
64
  }
65
- return {
65
+ const pluginAPI = {
66
66
  updateRuntimeContext,
67
67
  getHooks,
68
68
  getRuntimeConfig,
@@ -72,6 +72,19 @@ function initPluginAPI({ context, plugins }) {
72
72
  pickContext: hooks.pickContext.tap,
73
73
  ...extendsPluginApi
74
74
  };
75
+ return new Proxy(pluginAPI, {
76
+ get(target, prop) {
77
+ if (prop === "then") {
78
+ return void 0;
79
+ }
80
+ if (prop in target) {
81
+ return target[prop];
82
+ }
83
+ return () => {
84
+ console.warn(`api.${prop.toString()} not exist`);
85
+ };
86
+ }
87
+ });
75
88
  }
76
89
  // Annotate the CommonJS export names for ESM import in node:
77
90
  0 && (module.exports = {
@@ -2,8 +2,8 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
3
3
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import path from "path";
5
- import { bundleRequire, defaultGetOutputFile } from "@modern-js/node-bundle-require";
6
- import { fs, CONFIG_CACHE_DIR, globby } from "@modern-js/utils";
5
+ import { fs, globby } from "@modern-js/utils";
6
+ import jiti from "jiti";
7
7
  var getPackageConfig = function(appDirectory, packageJsonConfig) {
8
8
  var json = JSON.parse(fs.readFileSync(path.resolve(appDirectory, "./package.json"), "utf8"));
9
9
  return json[packageJsonConfig];
@@ -81,121 +81,46 @@ var clearFilesOverTime = function() {
81
81
  return _ref.apply(this, arguments);
82
82
  };
83
83
  }();
84
- var bundleRequireWithCatch = function() {
85
- var _ref = _async_to_generator(function(configFile, param) {
86
- var appDirectory, mod, e;
87
- return _ts_generator(this, function(_state) {
88
- switch (_state.label) {
89
- case 0:
90
- appDirectory = param.appDirectory;
91
- _state.label = 1;
92
- case 1:
93
- _state.trys.push([
94
- 1,
95
- 3,
96
- ,
97
- 4
98
- ]);
99
- return [
100
- 4,
101
- bundleRequire(configFile, {
102
- autoClear: false,
103
- getOutputFile: function() {
104
- var _ref2 = _async_to_generator(function(filePath) {
105
- var defaultOutputFileName, _, outputPath, timeLimit;
106
- return _ts_generator(this, function(_state2) {
107
- switch (_state2.label) {
108
- case 0:
109
- _ = path.basename;
110
- return [
111
- 4,
112
- defaultGetOutputFile(filePath)
113
- ];
114
- case 1:
115
- defaultOutputFileName = _.apply(path, [
116
- _state2.sent()
117
- ]);
118
- outputPath = path.join(appDirectory, CONFIG_CACHE_DIR);
119
- timeLimit = 10 * 60;
120
- return [
121
- 4,
122
- clearFilesOverTime(outputPath, timeLimit)
123
- ];
124
- case 2:
125
- _state2.sent();
126
- return [
127
- 2,
128
- path.join(outputPath, defaultOutputFileName)
129
- ];
130
- }
131
- });
132
- });
133
- return function(filePath) {
134
- return _ref2.apply(this, arguments);
135
- };
136
- }()
137
- })
138
- ];
139
- case 2:
140
- mod = _state.sent();
141
- return [
142
- 2,
143
- mod
144
- ];
145
- case 3:
146
- e = _state.sent();
147
- if (_instanceof(e, Error)) {
148
- e.message = "Get Error while loading config file: ".concat(configFile, ", please check it and retry.\n").concat(e.message || "");
149
- }
150
- throw e;
151
- case 4:
152
- return [
153
- 2
154
- ];
155
- }
156
- });
84
+ function loadConfigContent(configFile) {
85
+ var _require = jiti(__filename, {
86
+ esmResolve: true,
87
+ // disable require cache to support restart CLI and read the new config
88
+ requireCache: false,
89
+ interopDefault: true
157
90
  });
158
- return function bundleRequireWithCatch2(configFile, _) {
159
- return _ref.apply(this, arguments);
160
- };
161
- }();
91
+ if (!fs.existsSync(configFile)) {
92
+ throw new Error("Configuration file does not exist: ".concat(configFile));
93
+ }
94
+ try {
95
+ var config = _require(configFile);
96
+ return config.default || config;
97
+ } catch (e) {
98
+ if (_instanceof(e, Error)) {
99
+ e.message = "Get Error while loading config file: ".concat(configFile, ", please check it and retry.\n").concat(e.message || "");
100
+ }
101
+ throw e;
102
+ }
103
+ }
162
104
  var loadConfig = function() {
163
105
  var _ref = _async_to_generator(function(appDirectory, configFile, packageJsonConfig) {
164
- var pkgConfig, packageName, config, mod;
106
+ var pkgConfig, packageName, config;
165
107
  return _ts_generator(this, function(_state) {
166
- switch (_state.label) {
167
- case 0:
168
- if (packageJsonConfig) {
169
- pkgConfig = getPackageConfig(appDirectory, packageJsonConfig);
170
- }
171
- packageName = require(path.resolve(appDirectory, "./package.json")).name;
172
- if (!configFile)
173
- return [
174
- 3,
175
- 2
176
- ];
177
- delete require.cache[configFile];
178
- return [
179
- 4,
180
- bundleRequireWithCatch(configFile, {
181
- appDirectory
182
- })
183
- ];
184
- case 1:
185
- mod = _state.sent();
186
- config = mod.default || mod;
187
- _state.label = 2;
188
- case 2:
189
- return [
190
- 2,
191
- {
192
- packageName,
193
- configFile,
194
- config,
195
- pkgConfig
196
- }
197
- ];
108
+ if (packageJsonConfig) {
109
+ pkgConfig = getPackageConfig(appDirectory, packageJsonConfig);
110
+ }
111
+ packageName = require(path.resolve(appDirectory, "./package.json")).name;
112
+ if (configFile) {
113
+ config = loadConfigContent(configFile);
198
114
  }
115
+ return [
116
+ 2,
117
+ {
118
+ packageName,
119
+ configFile,
120
+ config,
121
+ pkgConfig
122
+ }
123
+ ];
199
124
  });
200
125
  });
201
126
  return function loadConfig2(appDirectory, configFile, packageJsonConfig) {
@@ -6,7 +6,7 @@ function initPluginAPI(param) {
6
6
  var hooks = context.hooks, extendsHooks = context.extendsHooks;
7
7
  function getRuntimeContext() {
8
8
  if (context) {
9
- var hooks2 = context.hooks, extendsHooks2 = context.extendsHooks, config = context.config, pluginAPI = context.pluginAPI, runtimeContext = _object_without_properties(context, [
9
+ var hooks2 = context.hooks, extendsHooks2 = context.extendsHooks, config = context.config, pluginAPI2 = context.pluginAPI, runtimeContext = _object_without_properties(context, [
10
10
  "hooks",
11
11
  "extendsHooks",
12
12
  "config",
@@ -44,7 +44,7 @@ function initPluginAPI(param) {
44
44
  extendsPluginApi[hookName] = extendsHooks[hookName].tap;
45
45
  });
46
46
  }
47
- return _object_spread({
47
+ var pluginAPI = _object_spread({
48
48
  updateRuntimeContext,
49
49
  getHooks,
50
50
  getRuntimeConfig,
@@ -53,6 +53,19 @@ function initPluginAPI(param) {
53
53
  wrapRoot: hooks.wrapRoot.tap,
54
54
  pickContext: hooks.pickContext.tap
55
55
  }, extendsPluginApi);
56
+ return new Proxy(pluginAPI, {
57
+ get: function get(target, prop) {
58
+ if (prop === "then") {
59
+ return void 0;
60
+ }
61
+ if (prop in target) {
62
+ return target[prop];
63
+ }
64
+ return function() {
65
+ console.warn("api.".concat(prop.toString(), " not exist"));
66
+ };
67
+ }
68
+ });
56
69
  }
57
70
  export {
58
71
  initPluginAPI
@@ -1,6 +1,6 @@
1
1
  import path from "path";
2
- import { bundleRequire, defaultGetOutputFile } from "@modern-js/node-bundle-require";
3
- import { fs, CONFIG_CACHE_DIR, globby } from "@modern-js/utils";
2
+ import { fs, globby } from "@modern-js/utils";
3
+ import jiti from "jiti";
4
4
  const getPackageConfig = (appDirectory, packageJsonConfig) => {
5
5
  const json = JSON.parse(fs.readFileSync(path.resolve(appDirectory, "./package.json"), "utf8"));
6
6
  return json[packageJsonConfig];
@@ -28,19 +28,19 @@ const clearFilesOverTime = async (targetDir, overtime) => {
28
28
  } catch (err) {
29
29
  }
30
30
  };
31
- const bundleRequireWithCatch = async (configFile, { appDirectory }) => {
31
+ function loadConfigContent(configFile) {
32
+ const _require = jiti(__filename, {
33
+ esmResolve: true,
34
+ // disable require cache to support restart CLI and read the new config
35
+ requireCache: false,
36
+ interopDefault: true
37
+ });
38
+ if (!fs.existsSync(configFile)) {
39
+ throw new Error(`Configuration file does not exist: ${configFile}`);
40
+ }
32
41
  try {
33
- const mod = await bundleRequire(configFile, {
34
- autoClear: false,
35
- getOutputFile: async (filePath) => {
36
- const defaultOutputFileName = path.basename(await defaultGetOutputFile(filePath));
37
- const outputPath = path.join(appDirectory, CONFIG_CACHE_DIR);
38
- const timeLimit = 10 * 60;
39
- await clearFilesOverTime(outputPath, timeLimit);
40
- return path.join(outputPath, defaultOutputFileName);
41
- }
42
- });
43
- return mod;
42
+ const config = _require(configFile);
43
+ return config.default || config;
44
44
  } catch (e) {
45
45
  if (e instanceof Error) {
46
46
  e.message = `Get Error while loading config file: ${configFile}, please check it and retry.
@@ -48,7 +48,7 @@ ${e.message || ""}`;
48
48
  }
49
49
  throw e;
50
50
  }
51
- };
51
+ }
52
52
  const loadConfig = async (appDirectory, configFile, packageJsonConfig) => {
53
53
  let pkgConfig;
54
54
  if (packageJsonConfig) {
@@ -57,11 +57,7 @@ const loadConfig = async (appDirectory, configFile, packageJsonConfig) => {
57
57
  const packageName = require(path.resolve(appDirectory, "./package.json")).name;
58
58
  let config;
59
59
  if (configFile) {
60
- delete require.cache[configFile];
61
- const mod = await bundleRequireWithCatch(configFile, {
62
- appDirectory
63
- });
64
- config = mod.default || mod;
60
+ config = loadConfigContent(configFile);
65
61
  }
66
62
  return {
67
63
  packageName,
@@ -3,7 +3,7 @@ function initPluginAPI({ context, plugins }) {
3
3
  const { hooks, extendsHooks } = context;
4
4
  function getRuntimeContext() {
5
5
  if (context) {
6
- const { hooks: hooks2, extendsHooks: extendsHooks2, config, pluginAPI, ...runtimeContext } = context;
6
+ const { hooks: hooks2, extendsHooks: extendsHooks2, config, pluginAPI: pluginAPI2, ...runtimeContext } = context;
7
7
  runtimeContext._internalContext = context;
8
8
  return runtimeContext;
9
9
  }
@@ -39,7 +39,7 @@ function initPluginAPI({ context, plugins }) {
39
39
  extendsPluginApi[hookName] = extendsHooks[hookName].tap;
40
40
  });
41
41
  }
42
- return {
42
+ const pluginAPI = {
43
43
  updateRuntimeContext,
44
44
  getHooks,
45
45
  getRuntimeConfig,
@@ -49,6 +49,19 @@ function initPluginAPI({ context, plugins }) {
49
49
  pickContext: hooks.pickContext.tap,
50
50
  ...extendsPluginApi
51
51
  };
52
+ return new Proxy(pluginAPI, {
53
+ get(target, prop) {
54
+ if (prop === "then") {
55
+ return void 0;
56
+ }
57
+ if (prop in target) {
58
+ return target[prop];
59
+ }
60
+ return () => {
61
+ console.warn(`api.${prop.toString()} not exist`);
62
+ };
63
+ }
64
+ });
52
65
  }
53
66
  export {
54
67
  initPluginAPI
@@ -1,6 +1,6 @@
1
1
  import type { OnAfterBuildFn, OnAfterCreateCompilerFn, OnBeforeBuildFn, OnBeforeCreateCompilerFn, OnDevCompileDoneFn } from '@rsbuild/core';
2
2
  import type { Hooks } from '../../cli/hooks';
3
- import type { PluginHookTap } from '../hooks';
3
+ import type { PluginHook, PluginHookTap } from '../hooks';
4
4
  import type { DeepPartial } from '../utils';
5
5
  import type { AppContext } from './context';
6
6
  import type { AddCommandFn, AddWatchFilesFn, ConfigFn, ModifyBundlerChainFn, ModifyConfigFn, ModifyHtmlPartialsFn, ModifyResolvedConfigFn, ModifyRsbuildConfigFn, ModifyRspackConfigFn, ModifyWebpackChainFn, ModifyWebpackConfigFn, OnAfterDeployFn, OnAfterDevFn, OnBeforeDeployFn, OnBeforeDevFn, OnBeforeExitFn, OnBeforeRestartFn, OnFileChangedFn, OnPrepareFn } from './hooks';
@@ -42,4 +42,9 @@ export type CLIPluginAPI<Extends extends CLIPluginExtends> = Readonly<{
42
42
  onBeforeDeploy: PluginHookTap<OnBeforeDeployFn>;
43
43
  onAfterDeploy: PluginHookTap<OnAfterDeployFn>;
44
44
  onBeforeExit: PluginHookTap<OnBeforeExitFn>;
45
- }>;
45
+ } & CLIPluginExtendsAPI<Extends>>;
46
+ export type CLIPluginExtendsAPI<Extends extends CLIPluginExtends> = {
47
+ [K in keyof Extends['extendHooks']]: PluginHookTap<Extends['extendHooks'][K] extends PluginHook<infer Args> ? Args extends (...args: any[]) => any ? Args : (...args: any[]) => any : (...args: any[]) => any>;
48
+ } & Extends['extendApi'];
49
+ export type AllKeysForCLIPluginExtendsAPI<Extends extends CLIPluginExtends> = keyof CLIPluginExtendsAPI<Extends>;
50
+ export type AllValueForCLIPluginExtendsAPI<Extends extends CLIPluginExtends> = CLIPluginExtendsAPI<Extends>[AllKeysForCLIPluginExtendsAPI<Extends>];
@@ -5,11 +5,11 @@ export type SyncHook<Callback extends (...args: any[]) => any> = {
5
5
  };
6
6
  export type AsyncHook<Callback extends (...args: any[]) => any> = {
7
7
  tap: (cb: Callback) => void;
8
- call: (...args: Parameters<Callback>) => Promise<ReturnType<Callback>>;
8
+ call: (...args: Parameters<Callback>) => Promise<ReturnType<UnwrapPromise<Callback>>>;
9
9
  };
10
10
  export type AsyncInterruptHook<Callback extends (...args: any[]) => any> = {
11
11
  tap: (cb: Callback) => void;
12
- call: (...args: Tail<Parameters<Callback>>) => Promise<ReturnType<Callback>>;
12
+ call: (...args: Tail<Parameters<Callback>>) => Promise<ReturnType<UnwrapPromise<Callback>>>;
13
13
  };
14
14
  export type CollectAsyncHook<Callback extends (...params: any[]) => any> = {
15
15
  tap: (cb: Callback) => void;
@@ -1,4 +1,4 @@
1
- import type { PluginHookTap } from '../hooks';
1
+ import type { PluginHook, PluginHookTap } from '../hooks';
2
2
  import type { DeepPartial } from '../utils';
3
3
  import type { RuntimeContext } from './context';
4
4
  import type { Hooks } from './hooks';
@@ -12,4 +12,9 @@ export type RuntimePluginAPI<Extends extends RuntimePluginExtends> = Readonly<{
12
12
  wrapRoot: PluginHookTap<WrapRootFn>;
13
13
  pickContext: PluginHookTap<PickContextFn<RuntimeContext>>;
14
14
  modifyRuntimeConfig: PluginHookTap<ModifyRuntimeConfigFn<Extends['config']>>;
15
- }>;
15
+ } & RuntimePluginExtendsAPI<Extends>>;
16
+ export type RuntimePluginExtendsAPI<Extends extends RuntimePluginExtends> = {
17
+ [K in keyof Extends['extendHooks']]: PluginHookTap<Extends['extendHooks'][K] extends PluginHook<infer Args> ? Args extends (...args: any[]) => any ? Args : (...args: any[]) => any : (...args: any[]) => any>;
18
+ } & Extends['extendApi'];
19
+ export type AllKeysForRuntimePluginExtendsAPI<Extends extends RuntimePluginExtends> = keyof RuntimePluginExtendsAPI<Extends>;
20
+ export type AllValueForRuntimePluginExtendsAPI<Extends extends RuntimePluginExtends> = RuntimePluginExtendsAPI<Extends>[AllKeysForRuntimePluginExtendsAPI<Extends>];
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.63.8-alpha.2",
18
+ "version": "2.64.1-alpha.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -63,10 +63,10 @@
63
63
  }
64
64
  },
65
65
  "dependencies": {
66
+ "jiti": "1.21.7",
66
67
  "@swc/helpers": "0.5.13",
67
- "@modern-js/node-bundle-require": "2.63.7",
68
- "@modern-js/utils": "2.63.7",
69
- "@modern-js/runtime-utils": "2.63.7"
68
+ "@modern-js/utils": "2.64.0",
69
+ "@modern-js/runtime-utils": "2.64.0"
70
70
  },
71
71
  "devDependencies": {
72
72
  "@rsbuild/core": "1.1.13",
@@ -75,10 +75,10 @@
75
75
  "@types/node": "^14",
76
76
  "jest": "^29",
77
77
  "typescript": "^5",
78
- "@modern-js/uni-builder": "2.63.7",
79
- "@modern-js/types": "2.63.7",
80
- "@scripts/build": "2.63.7",
81
- "@scripts/jest-config": "2.63.7"
78
+ "@modern-js/uni-builder": "2.64.0",
79
+ "@scripts/build": "2.64.0",
80
+ "@modern-js/types": "2.64.0",
81
+ "@scripts/jest-config": "2.64.0"
82
82
  },
83
83
  "sideEffects": false,
84
84
  "publishConfig": {