@modern-js/plugin-v2 2.63.7 → 2.63.8-alpha.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 (101) hide show
  1. package/dist/cjs/cli/run/create.js +25 -14
  2. package/dist/cjs/hooks.js +74 -2
  3. package/dist/cjs/index.js +8 -2
  4. package/dist/cjs/manager.js +3 -5
  5. package/dist/cjs/runtime/api.js +79 -0
  6. package/dist/cjs/runtime/context.js +51 -0
  7. package/dist/cjs/runtime/hooks.js +36 -0
  8. package/dist/cjs/runtime/index.js +39 -0
  9. package/dist/cjs/runtime/run/create.js +74 -0
  10. package/dist/cjs/runtime/run/index.js +29 -0
  11. package/dist/cjs/runtime/run/types.js +16 -0
  12. package/dist/cjs/types/runtime/api.js +16 -0
  13. package/dist/cjs/types/runtime/context.js +16 -0
  14. package/dist/cjs/types/runtime/hooks.js +16 -0
  15. package/dist/cjs/types/runtime/index.js +16 -0
  16. package/dist/cjs/types/runtime/plugin.js +16 -0
  17. package/dist/esm/cli/api.js +103 -0
  18. package/dist/esm/cli/context.js +53 -0
  19. package/dist/esm/cli/hooks.js +42 -0
  20. package/dist/esm/cli/index.js +14 -0
  21. package/dist/esm/cli/run/config/createLoadedConfig.js +190 -0
  22. package/dist/esm/cli/run/config/createResolvedConfig.js +30 -0
  23. package/dist/esm/cli/run/config/loadConfig.js +210 -0
  24. package/dist/esm/cli/run/create.js +300 -0
  25. package/dist/esm/cli/run/index.js +10 -0
  26. package/dist/esm/cli/run/run.js +60 -0
  27. package/dist/esm/cli/run/types.js +0 -0
  28. package/dist/esm/cli/run/utils/checkIsDuplicationPlugin.js +24 -0
  29. package/dist/esm/cli/run/utils/commander.js +42 -0
  30. package/dist/esm/cli/run/utils/createFileWatcher.js +110 -0
  31. package/dist/esm/cli/run/utils/debug.js +5 -0
  32. package/dist/esm/cli/run/utils/initAppDir.js +36 -0
  33. package/dist/esm/cli/run/utils/loadEnv.js +24 -0
  34. package/dist/esm/cli/run/utils/mergeConfig.js +43 -0
  35. package/dist/esm/hooks.js +378 -0
  36. package/dist/esm/index.js +6 -357
  37. package/dist/esm/manager.js +161 -0
  38. package/dist/esm/runtime/api.js +59 -0
  39. package/dist/esm/runtime/context.js +25 -0
  40. package/dist/esm/runtime/hooks.js +12 -0
  41. package/dist/esm/runtime/index.js +11 -0
  42. package/dist/esm/runtime/run/create.js +72 -0
  43. package/dist/esm/runtime/run/index.js +5 -0
  44. package/dist/esm/runtime/run/types.js +0 -0
  45. package/dist/esm/types/cli/api.js +0 -0
  46. package/dist/esm/types/cli/context.js +0 -0
  47. package/dist/esm/types/cli/hooks.js +0 -0
  48. package/dist/esm/types/cli/index.js +0 -0
  49. package/dist/esm/types/cli/plugin.js +0 -0
  50. package/dist/esm/types/hooks.js +0 -0
  51. package/dist/esm/types/index.js +0 -0
  52. package/dist/esm/types/plugin.js +0 -0
  53. package/dist/esm/types/runtime/api.js +0 -0
  54. package/dist/esm/types/runtime/context.js +0 -0
  55. package/dist/esm/types/runtime/hooks.js +0 -0
  56. package/dist/esm/types/runtime/index.js +0 -0
  57. package/dist/esm/types/runtime/plugin.js +0 -0
  58. package/dist/esm/types/utils.js +0 -0
  59. package/dist/esm-node/cli/run/create.js +26 -15
  60. package/dist/esm-node/hooks.js +70 -1
  61. package/dist/esm-node/index.js +5 -2
  62. package/dist/esm-node/manager.js +2 -4
  63. package/dist/esm-node/runtime/api.js +55 -0
  64. package/dist/esm-node/runtime/context.js +26 -0
  65. package/dist/esm-node/runtime/hooks.js +12 -0
  66. package/dist/esm-node/runtime/index.js +11 -0
  67. package/dist/esm-node/runtime/run/create.js +50 -0
  68. package/dist/esm-node/runtime/run/index.js +5 -0
  69. package/dist/esm-node/runtime/run/types.js +0 -0
  70. package/dist/esm-node/types/runtime/api.js +0 -0
  71. package/dist/esm-node/types/runtime/context.js +0 -0
  72. package/dist/esm-node/types/runtime/hooks.js +0 -0
  73. package/dist/esm-node/types/runtime/index.js +0 -0
  74. package/dist/esm-node/types/runtime/plugin.js +0 -0
  75. package/dist/types/cli/hooks.d.ts +3 -3
  76. package/dist/types/cli/run/create.d.ts +2 -1
  77. package/dist/types/cli/run/index.d.ts +4 -4
  78. package/dist/types/hooks.d.ts +4 -1
  79. package/dist/types/index.d.ts +2 -1
  80. package/dist/types/manager.d.ts +3 -0
  81. package/dist/types/runtime/api.d.ts +9 -0
  82. package/dist/types/runtime/context.d.ts +11 -0
  83. package/dist/types/runtime/hooks.d.ts +2 -0
  84. package/dist/types/runtime/index.d.ts +5 -0
  85. package/dist/types/runtime/run/create.d.ts +7 -0
  86. package/dist/types/runtime/run/index.d.ts +5 -0
  87. package/dist/types/runtime/run/types.d.ts +6 -0
  88. package/dist/types/types/cli/api.d.ts +2 -2
  89. package/dist/types/types/cli/context.d.ts +1 -1
  90. package/dist/types/types/cli/hooks.d.ts +3 -3
  91. package/dist/types/types/cli/plugin.d.ts +2 -1
  92. package/dist/types/types/hooks.d.ts +14 -2
  93. package/dist/types/types/index.d.ts +1 -0
  94. package/dist/types/types/plugin.d.ts +1 -1
  95. package/dist/types/types/runtime/api.d.ts +15 -0
  96. package/dist/types/types/runtime/context.d.ts +13 -0
  97. package/dist/types/types/runtime/hooks.d.ts +12 -0
  98. package/dist/types/types/runtime/index.d.ts +4 -0
  99. package/dist/types/types/runtime/plugin.d.ts +14 -0
  100. package/dist/types/types/utils.d.ts +1 -0
  101. package/package.json +13 -9
@@ -0,0 +1,103 @@
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
3
+ import { merge } from "@modern-js/utils/lodash";
4
+ import { debug } from "./run/utils/debug";
5
+ function initPluginAPI(param) {
6
+ var context = param.context, pluginManager = param.pluginManager;
7
+ var hooks = context.hooks, extendsHooks = context.extendsHooks, plugins = context.plugins;
8
+ function getAppContext() {
9
+ if (context) {
10
+ var hooks2 = context.hooks, extendsHooks2 = context.extendsHooks, config = context.config, normalizedConfig = context.normalizedConfig, pluginAPI2 = context.pluginAPI, appContext = _object_without_properties(context, [
11
+ "hooks",
12
+ "extendsHooks",
13
+ "config",
14
+ "normalizedConfig",
15
+ "pluginAPI"
16
+ ]);
17
+ appContext._internalContext = context;
18
+ return appContext;
19
+ }
20
+ throw new Error("Cannot access context");
21
+ }
22
+ function getConfig() {
23
+ if (context.config) {
24
+ return context.config;
25
+ }
26
+ throw new Error("Cannot access config");
27
+ }
28
+ function getNormalizedConfig() {
29
+ if (context.normalizedConfig) {
30
+ return context.normalizedConfig;
31
+ }
32
+ throw new Error("Cannot access normalized config");
33
+ }
34
+ function getHooks() {
35
+ return context.hooks;
36
+ }
37
+ var extendsPluginApi = {};
38
+ plugins.forEach(function(plugin) {
39
+ var _registryApi = plugin._registryApi;
40
+ if (_registryApi) {
41
+ var apis = _registryApi(getAppContext, updateAppContext);
42
+ Object.keys(apis).forEach(function(apiName) {
43
+ extendsPluginApi[apiName] = apis[apiName];
44
+ });
45
+ }
46
+ });
47
+ if (extendsHooks) {
48
+ Object.keys(extendsHooks).forEach(function(hookName) {
49
+ extendsPluginApi[hookName] = extendsHooks[hookName].tap;
50
+ });
51
+ }
52
+ function updateAppContext(updateContext) {
53
+ context = merge(context, updateContext);
54
+ }
55
+ var pluginAPI = _object_spread({
56
+ isPluginExists: pluginManager.isPluginExists,
57
+ getAppContext,
58
+ getConfig,
59
+ getNormalizedConfig,
60
+ getHooks,
61
+ updateAppContext,
62
+ config: hooks.config.tap,
63
+ modifyConfig: hooks.modifyConfig.tap,
64
+ modifyResolvedConfig: hooks.modifyResolvedConfig.tap,
65
+ modifyRsbuildConfig: hooks.modifyRsbuildConfig.tap,
66
+ modifyBundlerChain: hooks.modifyBundlerChain.tap,
67
+ modifyRspackConfig: hooks.modifyRspackConfig.tap,
68
+ modifyWebpackChain: hooks.modifyWebpackChain.tap,
69
+ modifyWebpackConfig: hooks.modifyWebpackConfig.tap,
70
+ modifyHtmlPartials: hooks.modifyHtmlPartials.tap,
71
+ addCommand: hooks.addCommand.tap,
72
+ onPrepare: hooks.onPrepare.tap,
73
+ addWatchFiles: hooks.addWatchFiles.tap,
74
+ onFileChanged: hooks.onFileChanged.tap,
75
+ onBeforeRestart: hooks.onBeforeRestart.tap,
76
+ onBeforeCreateCompiler: hooks.onBeforeCreateCompiler.tap,
77
+ onDevCompileDone: hooks.onDevCompileDone.tap,
78
+ onAfterCreateCompiler: hooks.onAfterCreateCompiler.tap,
79
+ onBeforeBuild: hooks.onBeforeBuild.tap,
80
+ onAfterBuild: hooks.onAfterBuild.tap,
81
+ onBeforeDev: hooks.onBeforeDev.tap,
82
+ onAfterDev: hooks.onAfterDev.tap,
83
+ onBeforeDeploy: hooks.onBeforeDeploy.tap,
84
+ onAfterDeploy: hooks.onAfterDeploy.tap,
85
+ onBeforeExit: hooks.onBeforeExit.tap
86
+ }, extendsPluginApi);
87
+ return new Proxy(pluginAPI, {
88
+ get: function get(target, prop) {
89
+ if (prop === "then") {
90
+ return void 0;
91
+ }
92
+ if (prop in target) {
93
+ return target[prop];
94
+ }
95
+ return function() {
96
+ debug("api.".concat(prop.toString(), " not exist"));
97
+ };
98
+ }
99
+ });
100
+ }
101
+ export {
102
+ initPluginAPI
103
+ };
@@ -0,0 +1,53 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
+ import path from "path";
6
+ import { initHooks } from "./hooks";
7
+ function initAppContext(params) {
8
+ var appDirectory = params.appDirectory, _params_srcDir = params.srcDir, srcDir = _params_srcDir === void 0 ? "src" : _params_srcDir, _params_distDir = params.distDir, distDir = _params_distDir === void 0 ? "dist" : _params_distDir;
9
+ return {
10
+ packageName: params.packageName,
11
+ configFile: params.configFile,
12
+ command: params.command,
13
+ isProd: process.env.NODE_ENV === "production",
14
+ appDirectory,
15
+ srcDirectory: path.resolve(appDirectory, srcDir),
16
+ distDirectory: "",
17
+ nodeModulesDirectory: path.resolve(appDirectory, "node_modules"),
18
+ plugins: params.plugins
19
+ };
20
+ }
21
+ function createContext(_) {
22
+ return _createContext.apply(this, arguments);
23
+ }
24
+ function _createContext() {
25
+ _createContext = _async_to_generator(function(param) {
26
+ var appContext, config, normalizedConfig, plugins, extendsHooks;
27
+ return _ts_generator(this, function(_state) {
28
+ appContext = param.appContext, config = param.config, normalizedConfig = param.normalizedConfig;
29
+ plugins = appContext.plugins;
30
+ extendsHooks = {};
31
+ plugins.forEach(function(plugin) {
32
+ var _plugin_registryHooks = plugin.registryHooks, registryHooks = _plugin_registryHooks === void 0 ? {} : _plugin_registryHooks;
33
+ Object.keys(registryHooks).forEach(function(hookName) {
34
+ extendsHooks[hookName] = registryHooks[hookName];
35
+ });
36
+ });
37
+ return [
38
+ 2,
39
+ _object_spread_props(_object_spread({}, appContext), {
40
+ hooks: _object_spread({}, initHooks(), extendsHooks),
41
+ extendsHooks,
42
+ config,
43
+ normalizedConfig
44
+ })
45
+ ];
46
+ });
47
+ });
48
+ return _createContext.apply(this, arguments);
49
+ }
50
+ export {
51
+ createContext,
52
+ initAppContext
53
+ };
@@ -0,0 +1,42 @@
1
+ import { createAsyncHook, createCollectAsyncHook } from "../hooks";
2
+ function initHooks() {
3
+ return {
4
+ /**
5
+ * add config for this cli plugin
6
+ */
7
+ config: createCollectAsyncHook(),
8
+ /**
9
+ * @private
10
+ * modify config for this cli plugin
11
+ */
12
+ modifyConfig: createAsyncHook(),
13
+ /**
14
+ * modify final config
15
+ */
16
+ modifyResolvedConfig: createAsyncHook(),
17
+ modifyRsbuildConfig: createAsyncHook(),
18
+ modifyBundlerChain: createAsyncHook(),
19
+ modifyRspackConfig: createAsyncHook(),
20
+ modifyWebpackChain: createAsyncHook(),
21
+ modifyWebpackConfig: createAsyncHook(),
22
+ modifyHtmlPartials: createAsyncHook(),
23
+ addCommand: createAsyncHook(),
24
+ addWatchFiles: createCollectAsyncHook(),
25
+ onPrepare: createAsyncHook(),
26
+ onFileChanged: createAsyncHook(),
27
+ onBeforeRestart: createAsyncHook(),
28
+ onBeforeCreateCompiler: createAsyncHook(),
29
+ onAfterCreateCompiler: createAsyncHook(),
30
+ onDevCompileDone: createAsyncHook(),
31
+ onBeforeBuild: createAsyncHook(),
32
+ onAfterBuild: createAsyncHook(),
33
+ onBeforeDev: createAsyncHook(),
34
+ onAfterDev: createAsyncHook(),
35
+ onBeforeDeploy: createAsyncHook(),
36
+ onAfterDeploy: createAsyncHook(),
37
+ onBeforeExit: createAsyncHook()
38
+ };
39
+ }
40
+ export {
41
+ initHooks
42
+ };
@@ -0,0 +1,14 @@
1
+ import { initPluginAPI } from "./api";
2
+ import { initAppContext, createContext } from "./context";
3
+ import { initHooks } from "./hooks";
4
+ import { cli, createLoadedConfig, initAppDir, createCli } from "./run";
5
+ export {
6
+ cli,
7
+ createCli,
8
+ createContext,
9
+ createLoadedConfig,
10
+ initAppContext,
11
+ initAppDir,
12
+ initHooks,
13
+ initPluginAPI
14
+ };
@@ -0,0 +1,190 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import { fs, CONFIG_FILE_EXTENSIONS, chalk, getCommand, getNodeEnv, isDevCommand, isPlainObject, logger } from "@modern-js/utils";
5
+ import { mergeWith } from "@modern-js/utils/lodash";
6
+ import { mergeConfig } from "../utils/mergeConfig";
7
+ import { getConfigFilePath, loadConfig } from "./loadConfig";
8
+ function getConfigObject(config) {
9
+ return _getConfigObject.apply(this, arguments);
10
+ }
11
+ function _getConfigObject() {
12
+ _getConfigObject = /**
13
+ * A modern config can export a function or an object
14
+ * If it's a function, it will be called and return a config object
15
+ */
16
+ _async_to_generator(function(config) {
17
+ return _ts_generator(this, function(_state) {
18
+ switch (_state.label) {
19
+ case 0:
20
+ if (!(typeof config === "function"))
21
+ return [
22
+ 3,
23
+ 2
24
+ ];
25
+ return [
26
+ 4,
27
+ config({
28
+ env: getNodeEnv(),
29
+ command: getCommand()
30
+ })
31
+ ];
32
+ case 1:
33
+ return [
34
+ 2,
35
+ _state.sent() || {}
36
+ ];
37
+ case 2:
38
+ return [
39
+ 2,
40
+ config || {}
41
+ ];
42
+ }
43
+ });
44
+ });
45
+ return _getConfigObject.apply(this, arguments);
46
+ }
47
+ function loadLocalConfig(appDirectory, configFile) {
48
+ return _loadLocalConfig.apply(this, arguments);
49
+ }
50
+ function _loadLocalConfig() {
51
+ _loadLocalConfig = _async_to_generator(function(appDirectory, configFile) {
52
+ var localConfigFile, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, ext, replacedPath, loaded;
53
+ return _ts_generator(this, function(_state) {
54
+ switch (_state.label) {
55
+ case 0:
56
+ localConfigFile = false;
57
+ if (typeof configFile === "string") {
58
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
59
+ try {
60
+ for (_iterator = CONFIG_FILE_EXTENSIONS[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
61
+ ext = _step.value;
62
+ if (configFile.endsWith(ext)) {
63
+ replacedPath = configFile.replace(ext, ".local".concat(ext));
64
+ if (fs.existsSync(replacedPath)) {
65
+ localConfigFile = replacedPath;
66
+ }
67
+ }
68
+ }
69
+ } catch (err) {
70
+ _didIteratorError = true;
71
+ _iteratorError = err;
72
+ } finally {
73
+ try {
74
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
75
+ _iterator.return();
76
+ }
77
+ } finally {
78
+ if (_didIteratorError) {
79
+ throw _iteratorError;
80
+ }
81
+ }
82
+ }
83
+ }
84
+ if (!localConfigFile)
85
+ return [
86
+ 3,
87
+ 2
88
+ ];
89
+ return [
90
+ 4,
91
+ loadConfig(appDirectory, localConfigFile)
92
+ ];
93
+ case 1:
94
+ loaded = _state.sent();
95
+ return [
96
+ 2,
97
+ getConfigObject(loaded.config)
98
+ ];
99
+ case 2:
100
+ return [
101
+ 2,
102
+ null
103
+ ];
104
+ }
105
+ });
106
+ });
107
+ return _loadLocalConfig.apply(this, arguments);
108
+ }
109
+ function assignPkgConfig(userConfig, pkgConfig) {
110
+ return mergeWith({}, userConfig, pkgConfig, function(objValue, srcValue) {
111
+ if (objValue === void 0 && isPlainObject(srcValue)) {
112
+ return _object_spread({}, srcValue);
113
+ }
114
+ return void 0;
115
+ });
116
+ }
117
+ function createLoadedConfig(appDirectory, configFilePath, packageJsonConfig, otherConfig) {
118
+ return _createLoadedConfig.apply(this, arguments);
119
+ }
120
+ function _createLoadedConfig() {
121
+ _createLoadedConfig = _async_to_generator(function(appDirectory, configFilePath, packageJsonConfig, otherConfig) {
122
+ var configFile, loaded, config, mergedConfig, localConfig;
123
+ return _ts_generator(this, function(_state) {
124
+ switch (_state.label) {
125
+ case 0:
126
+ configFile = getConfigFilePath(appDirectory, configFilePath);
127
+ return [
128
+ 4,
129
+ loadConfig(appDirectory, configFile, packageJsonConfig)
130
+ ];
131
+ case 1:
132
+ loaded = _state.sent();
133
+ if (!loaded.config && !loaded.pkgConfig) {
134
+ logger.warn("Can not find any config file in the current project, please check if you have a correct config file.");
135
+ logger.warn("Current project path: ".concat(chalk.yellow(appDirectory)));
136
+ }
137
+ return [
138
+ 4,
139
+ getConfigObject(loaded.config)
140
+ ];
141
+ case 2:
142
+ config = _state.sent();
143
+ mergedConfig = config;
144
+ if (loaded.pkgConfig) {
145
+ mergedConfig = assignPkgConfig(config, loaded === null || loaded === void 0 ? void 0 : loaded.pkgConfig);
146
+ }
147
+ if (!isDevCommand())
148
+ return [
149
+ 3,
150
+ 4
151
+ ];
152
+ return [
153
+ 4,
154
+ loadLocalConfig(appDirectory, configFile)
155
+ ];
156
+ case 3:
157
+ localConfig = _state.sent();
158
+ if (localConfig) {
159
+ mergedConfig = mergeConfig([
160
+ mergedConfig,
161
+ localConfig
162
+ ]);
163
+ }
164
+ _state.label = 4;
165
+ case 4:
166
+ if (otherConfig) {
167
+ mergedConfig = mergeConfig([
168
+ mergedConfig,
169
+ otherConfig
170
+ ]);
171
+ }
172
+ return [
173
+ 2,
174
+ {
175
+ packageName: loaded.packageName,
176
+ config: mergedConfig,
177
+ configFile: loaded.configFile,
178
+ pkgConfig: loaded.pkgConfig,
179
+ jsConfig: loaded.config
180
+ }
181
+ ];
182
+ }
183
+ });
184
+ });
185
+ return _createLoadedConfig.apply(this, arguments);
186
+ }
187
+ export {
188
+ assignPkgConfig,
189
+ createLoadedConfig
190
+ };
@@ -0,0 +1,30 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import { createDebugger } from "@modern-js/utils";
5
+ import { mergeConfig } from "../utils/mergeConfig";
6
+ var debug = createDebugger("resolve-config");
7
+ var createResolveConfig = function() {
8
+ var _ref = _async_to_generator(function(loaded, configs) {
9
+ var userConfig, resolved;
10
+ return _ts_generator(this, function(_state) {
11
+ userConfig = loaded.config;
12
+ resolved = mergeConfig([
13
+ {}
14
+ ].concat(_to_consumable_array(configs), [
15
+ userConfig
16
+ ]));
17
+ debug("resolved %o", resolved);
18
+ return [
19
+ 2,
20
+ resolved
21
+ ];
22
+ });
23
+ });
24
+ return function createResolveConfig2(loaded, configs) {
25
+ return _ref.apply(this, arguments);
26
+ };
27
+ }();
28
+ export {
29
+ createResolveConfig
30
+ };
@@ -0,0 +1,210 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
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";
7
+ var getPackageConfig = function(appDirectory, packageJsonConfig) {
8
+ var json = JSON.parse(fs.readFileSync(path.resolve(appDirectory, "./package.json"), "utf8"));
9
+ return json[packageJsonConfig];
10
+ };
11
+ var getConfigFilePath = function(appDirectory, configFilePath) {
12
+ if (path.isAbsolute(configFilePath)) {
13
+ return configFilePath;
14
+ }
15
+ return path.resolve(appDirectory, configFilePath);
16
+ };
17
+ var clearFilesOverTime = function() {
18
+ var _ref = _async_to_generator(function(targetDir, overtime) {
19
+ var files, currentTime, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, file, err;
20
+ return _ts_generator(this, function(_state) {
21
+ switch (_state.label) {
22
+ case 0:
23
+ _state.trys.push([
24
+ 0,
25
+ 2,
26
+ ,
27
+ 3
28
+ ]);
29
+ return [
30
+ 4,
31
+ globby("".concat(targetDir, "/**/*"), {
32
+ stats: true,
33
+ absolute: true
34
+ })
35
+ ];
36
+ case 1:
37
+ files = _state.sent();
38
+ currentTime = Date.now();
39
+ if (files.length > 0) {
40
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
41
+ try {
42
+ for (_iterator = files[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
43
+ file = _step.value;
44
+ if (currentTime - file.stats.birthtimeMs >= overtime * 1e3) {
45
+ fs.unlinkSync(file.path);
46
+ }
47
+ }
48
+ } catch (err2) {
49
+ _didIteratorError = true;
50
+ _iteratorError = err2;
51
+ } finally {
52
+ try {
53
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
54
+ _iterator.return();
55
+ }
56
+ } finally {
57
+ if (_didIteratorError) {
58
+ throw _iteratorError;
59
+ }
60
+ }
61
+ }
62
+ }
63
+ return [
64
+ 3,
65
+ 3
66
+ ];
67
+ case 2:
68
+ err = _state.sent();
69
+ return [
70
+ 3,
71
+ 3
72
+ ];
73
+ case 3:
74
+ return [
75
+ 2
76
+ ];
77
+ }
78
+ });
79
+ });
80
+ return function clearFilesOverTime2(targetDir, overtime) {
81
+ return _ref.apply(this, arguments);
82
+ };
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
+ });
157
+ });
158
+ return function bundleRequireWithCatch2(configFile, _) {
159
+ return _ref.apply(this, arguments);
160
+ };
161
+ }();
162
+ var loadConfig = function() {
163
+ var _ref = _async_to_generator(function(appDirectory, configFile, packageJsonConfig) {
164
+ var pkgConfig, packageName, config, mod;
165
+ 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
+ ];
198
+ }
199
+ });
200
+ });
201
+ return function loadConfig2(appDirectory, configFile, packageJsonConfig) {
202
+ return _ref.apply(this, arguments);
203
+ };
204
+ }();
205
+ export {
206
+ clearFilesOverTime,
207
+ getConfigFilePath,
208
+ getPackageConfig,
209
+ loadConfig
210
+ };