@modern-js/core 2.4.1-beta.0 → 2.5.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.
- package/CHANGELOG.md +9 -3
- package/dist/context.d.ts +1 -0
- package/dist/context.js +3 -1
- package/dist/load-configs/index.d.ts +34 -0
- package/dist/load-configs/index.js +124 -0
- package/dist/types/context.d.ts +2 -0
- package/package.json +10 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
# @modern-js/core
|
|
2
2
|
|
|
3
|
-
## 2.
|
|
3
|
+
## 2.5.0
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
+
- 30614fa: chore: modify package.json entry fields and build config
|
|
8
|
+
chore: 更改 package.json entry 字段以及构建配置
|
|
9
|
+
- Updated dependencies [89ca6cc]
|
|
10
|
+
- Updated dependencies [30614fa]
|
|
11
|
+
- Updated dependencies [1b0ce87]
|
|
7
12
|
- Updated dependencies [11c053b]
|
|
8
|
-
- @modern-js/
|
|
9
|
-
- @modern-js/
|
|
13
|
+
- @modern-js/node-bundle-require@2.5.0
|
|
14
|
+
- @modern-js/plugin@2.5.0
|
|
15
|
+
- @modern-js/utils@2.5.0
|
|
10
16
|
|
|
11
17
|
## 2.4.0
|
|
12
18
|
|
package/dist/context.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export declare const initAppContext: ({ appDirectory, plugins, configFile, optio
|
|
|
26
26
|
options?: {
|
|
27
27
|
metaName?: string | undefined;
|
|
28
28
|
srcDir?: string | undefined;
|
|
29
|
+
apiDir?: string | undefined;
|
|
29
30
|
distDir?: string | undefined;
|
|
30
31
|
sharedDir?: string | undefined;
|
|
31
32
|
} | undefined;
|
package/dist/context.js
CHANGED
|
@@ -32,7 +32,7 @@ exports.useConfigContext = useConfigContext;
|
|
|
32
32
|
const useResolvedConfigContext = () => exports.ResolvedConfigContext.use().value;
|
|
33
33
|
exports.useResolvedConfigContext = useResolvedConfigContext;
|
|
34
34
|
const initAppContext = ({ appDirectory, plugins, configFile, options, serverConfigFile, serverInternalPlugins, toolsType, }) => {
|
|
35
|
-
const { metaName = 'modern-js', srcDir = 'src', distDir = '', sharedDir = 'shared', } = options || {};
|
|
35
|
+
const { metaName = 'modern-js', srcDir = 'src', distDir = '', apiDir = 'api', sharedDir = 'shared', } = options || {};
|
|
36
36
|
return {
|
|
37
37
|
metaName,
|
|
38
38
|
appDirectory,
|
|
@@ -43,6 +43,8 @@ const initAppContext = ({ appDirectory, plugins, configFile, options, serverConf
|
|
|
43
43
|
port: 0,
|
|
44
44
|
packageName: require(path_1.default.resolve(appDirectory, './package.json')).name,
|
|
45
45
|
srcDirectory: path_1.default.resolve(appDirectory, srcDir),
|
|
46
|
+
apiDirectory: path_1.default.resolve(appDirectory, apiDir),
|
|
47
|
+
lambdaDirectory: path_1.default.resolve(appDirectory, apiDir, 'lambda'),
|
|
46
48
|
distDirectory: distDir,
|
|
47
49
|
sharedDirectory: path_1.default.resolve(appDirectory, sharedDir),
|
|
48
50
|
nodeModulesDirectory: path_1.default.resolve(appDirectory, './node_modules'),
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
export declare const CONFIG_FILE_NAME = "modern.config";
|
|
3
|
+
export declare const PACKAGE_JSON_CONFIG_NAME = "modernConfig";
|
|
4
|
+
/**
|
|
5
|
+
* Get user config from package.json.
|
|
6
|
+
* @param appDirectory - App root directory.
|
|
7
|
+
* @returns modernConfig or undefined
|
|
8
|
+
*/
|
|
9
|
+
export declare const getPackageConfig: <T>(appDirectory: string, packageJsonConfig?: string) => T | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Get the file dependencies by module.children, ignore file path in node_modules and this monorepo packages default.
|
|
12
|
+
* @param filePath - Absolute file path.
|
|
13
|
+
* @returns File dependencies array.
|
|
14
|
+
*/
|
|
15
|
+
export declare const getDependencies: (filePath: string) => string[];
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param targetDir target dir
|
|
19
|
+
* @param overtime Unit of second
|
|
20
|
+
*/
|
|
21
|
+
export declare const clearFilesOverTime: (targetDir: string, overtime: number) => Promise<void>;
|
|
22
|
+
export declare const getConfigFilePath: (appDirectory: string, filePath?: string) => string | false;
|
|
23
|
+
/**
|
|
24
|
+
* Parse and load user config file, support extensions like .ts, mjs, js, ejs.
|
|
25
|
+
* @param appDirectory - App root directory, from which start search user config file.
|
|
26
|
+
* @param filePath - Specific absolute config file path.
|
|
27
|
+
* @returns Object contain config file path, user config object and dependency files used by config file.
|
|
28
|
+
*/
|
|
29
|
+
export declare const loadConfig: <T>(appDirectory: string, filePath?: string, packageJsonConfig?: string) => Promise<{
|
|
30
|
+
path: string | false;
|
|
31
|
+
config?: T | undefined;
|
|
32
|
+
dependencies?: string[] | undefined;
|
|
33
|
+
pkgConfig?: T | undefined;
|
|
34
|
+
}>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.loadConfig = exports.getConfigFilePath = exports.clearFilesOverTime = exports.getDependencies = exports.getPackageConfig = exports.PACKAGE_JSON_CONFIG_NAME = exports.CONFIG_FILE_NAME = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const utils_1 = require("@modern-js/utils");
|
|
9
|
+
const node_bundle_require_1 = require("@modern-js/node-bundle-require");
|
|
10
|
+
const debug = (0, utils_1.createDebugger)('load-config');
|
|
11
|
+
exports.CONFIG_FILE_NAME = 'modern.config';
|
|
12
|
+
exports.PACKAGE_JSON_CONFIG_NAME = 'modernConfig';
|
|
13
|
+
/**
|
|
14
|
+
* Get user config from package.json.
|
|
15
|
+
* @param appDirectory - App root directory.
|
|
16
|
+
* @returns modernConfig or undefined
|
|
17
|
+
*/
|
|
18
|
+
const getPackageConfig = (appDirectory, packageJsonConfig) => {
|
|
19
|
+
const json = JSON.parse(utils_1.fs.readFileSync(path_1.default.resolve(appDirectory, './package.json'), 'utf8'));
|
|
20
|
+
return json[packageJsonConfig !== null && packageJsonConfig !== void 0 ? packageJsonConfig : exports.PACKAGE_JSON_CONFIG_NAME];
|
|
21
|
+
};
|
|
22
|
+
exports.getPackageConfig = getPackageConfig;
|
|
23
|
+
/**
|
|
24
|
+
* Get the file dependencies by module.children, ignore file path in node_modules and this monorepo packages default.
|
|
25
|
+
* @param filePath - Absolute file path.
|
|
26
|
+
* @returns File dependencies array.
|
|
27
|
+
*/
|
|
28
|
+
const getDependencies = (filePath) => {
|
|
29
|
+
const mod = require.cache[filePath];
|
|
30
|
+
if (!mod) {
|
|
31
|
+
debug(`${filePath} has not been required yet`);
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
const deps = [];
|
|
35
|
+
if (!/\/node_modules\/|\/modern-js\/packages\//.test(mod.id)) {
|
|
36
|
+
deps.push(mod.id);
|
|
37
|
+
for (const child of mod.children) {
|
|
38
|
+
deps.push(...(0, exports.getDependencies)(child.id));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return deps;
|
|
42
|
+
};
|
|
43
|
+
exports.getDependencies = getDependencies;
|
|
44
|
+
/**
|
|
45
|
+
*
|
|
46
|
+
* @param targetDir target dir
|
|
47
|
+
* @param overtime Unit of second
|
|
48
|
+
*/
|
|
49
|
+
const clearFilesOverTime = async (targetDir, overtime) => {
|
|
50
|
+
// when stats is true, globby return Stats[]
|
|
51
|
+
const files = (await (0, utils_1.globby)(`${targetDir}/**/*`, {
|
|
52
|
+
stats: true,
|
|
53
|
+
absolute: true,
|
|
54
|
+
}));
|
|
55
|
+
const currentTime = Date.now();
|
|
56
|
+
if (files.length > 0) {
|
|
57
|
+
for (const file of files) {
|
|
58
|
+
if (currentTime - file.stats.birthtimeMs >= overtime * 1000) {
|
|
59
|
+
utils_1.fs.unlinkSync(file.path);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
exports.clearFilesOverTime = clearFilesOverTime;
|
|
65
|
+
const bundleRequireWithCatch = async (configFile, { appDirectory }) => {
|
|
66
|
+
try {
|
|
67
|
+
const mod = await (0, node_bundle_require_1.bundleRequire)(configFile, {
|
|
68
|
+
autoClear: false,
|
|
69
|
+
getOutputFile: async (filePath) => {
|
|
70
|
+
const defaultOutputFileName = path_1.default.basename(await (0, node_bundle_require_1.defaultGetOutputFile)(filePath));
|
|
71
|
+
const outputPath = path_1.default.join(appDirectory, utils_1.CONFIG_CACHE_DIR);
|
|
72
|
+
// 10 min
|
|
73
|
+
const timeLimit = 10 * 60;
|
|
74
|
+
await (0, exports.clearFilesOverTime)(outputPath, timeLimit);
|
|
75
|
+
return path_1.default.join(outputPath, defaultOutputFileName);
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
return mod;
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
if (e instanceof Error) {
|
|
82
|
+
e.message = `Get Error while loading config file: ${configFile}, please check it and retry.\n${e.message || ''}`;
|
|
83
|
+
}
|
|
84
|
+
throw e;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const getConfigFilePath = (appDirectory, filePath) => {
|
|
88
|
+
if (filePath) {
|
|
89
|
+
if (path_1.default.isAbsolute(filePath)) {
|
|
90
|
+
return filePath;
|
|
91
|
+
}
|
|
92
|
+
return path_1.default.resolve(appDirectory, filePath);
|
|
93
|
+
}
|
|
94
|
+
return (0, utils_1.findExists)(utils_1.CONFIG_FILE_EXTENSIONS.map(extension => path_1.default.resolve(appDirectory, `${exports.CONFIG_FILE_NAME}${extension}`)));
|
|
95
|
+
};
|
|
96
|
+
exports.getConfigFilePath = getConfigFilePath;
|
|
97
|
+
/**
|
|
98
|
+
* Parse and load user config file, support extensions like .ts, mjs, js, ejs.
|
|
99
|
+
* @param appDirectory - App root directory, from which start search user config file.
|
|
100
|
+
* @param filePath - Specific absolute config file path.
|
|
101
|
+
* @returns Object contain config file path, user config object and dependency files used by config file.
|
|
102
|
+
*/
|
|
103
|
+
const loadConfig = async (appDirectory, filePath, packageJsonConfig) => {
|
|
104
|
+
const configFile = (0, exports.getConfigFilePath)(appDirectory, filePath);
|
|
105
|
+
const pkgConfig = (0, exports.getPackageConfig)(appDirectory, packageJsonConfig);
|
|
106
|
+
let config;
|
|
107
|
+
const dependencies = pkgConfig
|
|
108
|
+
? [path_1.default.resolve(appDirectory, './package.json')]
|
|
109
|
+
: [];
|
|
110
|
+
if (configFile) {
|
|
111
|
+
delete require.cache[configFile];
|
|
112
|
+
const mod = await bundleRequireWithCatch(configFile, { appDirectory });
|
|
113
|
+
config = mod.default || mod;
|
|
114
|
+
// TODO: get deps.
|
|
115
|
+
// dependencies = dependencies.concat(getDependencies(configFile));
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
path: configFile,
|
|
119
|
+
config,
|
|
120
|
+
pkgConfig,
|
|
121
|
+
dependencies,
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
exports.loadConfig = loadConfig;
|
package/dist/types/context.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -10,12 +10,11 @@
|
|
|
10
10
|
"modern",
|
|
11
11
|
"modern.js"
|
|
12
12
|
],
|
|
13
|
-
"version": "2.
|
|
13
|
+
"version": "2.5.1-alpha.0",
|
|
14
14
|
"jsnext:source": "./src/index.ts",
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
16
|
"main": "./dist/index.js",
|
|
17
17
|
"module": "./dist/index.js",
|
|
18
|
-
"jsnext:modern": "./dist/index.js",
|
|
19
18
|
"exports": {
|
|
20
19
|
".": {
|
|
21
20
|
"node": {
|
|
@@ -51,12 +50,17 @@
|
|
|
51
50
|
}
|
|
52
51
|
},
|
|
53
52
|
"dependencies": {
|
|
54
|
-
"@modern-js/node-bundle-require": "2.
|
|
55
|
-
"@modern-js/
|
|
56
|
-
"@modern-js/
|
|
53
|
+
"@modern-js/node-bundle-require": "2.5.0",
|
|
54
|
+
"@modern-js/plugin": "2.5.0",
|
|
55
|
+
"@modern-js/utils": "2.5.0"
|
|
57
56
|
},
|
|
58
57
|
"devDependencies": {
|
|
59
58
|
"@jest/types": "^27.0.6",
|
|
59
|
+
"@modern-js/builder-shared": "2.5.0",
|
|
60
|
+
"@modern-js/babel-preset-app": "2.5.0",
|
|
61
|
+
"@modern-js/types": "2.5.0",
|
|
62
|
+
"@scripts/build": "2.5.0",
|
|
63
|
+
"@scripts/jest-config": "2.5.0",
|
|
60
64
|
"@types/babel__code-frame": "^7.0.3",
|
|
61
65
|
"@types/babel__core": "^7.1.16",
|
|
62
66
|
"@types/jest": "^27",
|
|
@@ -70,12 +74,7 @@
|
|
|
70
74
|
"sass": "^1.45.0",
|
|
71
75
|
"terser-webpack-plugin": "^5.1.4",
|
|
72
76
|
"typescript": "^4",
|
|
73
|
-
"webpack": "^5.75.0"
|
|
74
|
-
"@modern-js/builder-shared": "2.5.0-beta.0",
|
|
75
|
-
"@modern-js/babel-preset-app": "2.4.1-beta.0",
|
|
76
|
-
"@modern-js/types": "2.4.0",
|
|
77
|
-
"@scripts/jest-config": "2.4.0",
|
|
78
|
-
"@scripts/build": "2.4.0"
|
|
77
|
+
"webpack": "^5.75.0"
|
|
79
78
|
},
|
|
80
79
|
"sideEffects": false,
|
|
81
80
|
"publishConfig": {
|