@modern-js/utils 2.26.0 → 2.28.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +20 -0
- package/dist/cjs/cli/alias.js +3 -3
- package/dist/cjs/cli/constants/chainId.js +4 -0
- package/dist/cjs/cli/constants/index.js +0 -1
- package/dist/cjs/cli/is/config.js +11 -8
- package/dist/cjs/cli/prettyInstructions.js +3 -2
- package/dist/cjs/cli/require.js +2 -1
- package/dist/cjs/runtime/nestedRoutes.js +9 -5
- package/dist/cjs/runtime-browser/parsed.js +2 -1
- package/dist/cjs/universal/formatWebpack.js +55 -12
- package/dist/esm/cli/alias.js +27 -23
- package/dist/esm/cli/applyOptionsChain.js +8 -6
- package/dist/esm/cli/babel.js +50 -35
- package/dist/esm/cli/commands.js +7 -7
- package/dist/esm/cli/common.js +10 -5
- package/dist/esm/cli/constants/chainId.js +5 -1
- package/dist/esm/cli/constants/index.js +33 -38
- package/dist/esm/cli/ensure.js +4 -2
- package/dist/esm/cli/fs.js +56 -9
- package/dist/esm/cli/get/config.js +7 -7
- package/dist/esm/cli/get/data.js +102 -55
- package/dist/esm/cli/get/index.js +21 -6
- package/dist/esm/cli/is/config.js +43 -21
- package/dist/esm/cli/is/env.js +18 -6
- package/dist/esm/cli/is/platform.js +6 -2
- package/dist/esm/cli/is/project.js +73 -36
- package/dist/esm/cli/is/type.js +3 -1
- package/dist/esm/cli/logger.js +49 -38
- package/dist/esm/cli/monorepo.js +49 -36
- package/dist/esm/cli/package.js +156 -38
- package/dist/esm/cli/path.js +44 -20
- package/dist/esm/cli/pathSerializer.js +29 -15
- package/dist/esm/cli/port.js +89 -40
- package/dist/esm/cli/prettyInstructions.js +44 -32
- package/dist/esm/cli/require.js +22 -16
- package/dist/esm/cli/runtimeExports.js +21 -15
- package/dist/esm/cli/watch.js +94 -27
- package/dist/esm/compiled.js +3 -3
- package/dist/esm/import.js +4 -4
- package/dist/esm/runtime/nestedRoutes.js +49 -32
- package/dist/esm/runtime-browser/parsed.js +6 -5
- package/dist/esm/runtime-node/index.js +1 -1
- package/dist/esm/runtime-node/nestedRoutes.js +6 -4
- package/dist/esm/runtime-node/serialize.js +1 -1
- package/dist/esm/runtime-node/storage.js +12 -14
- package/dist/esm/universal/constants.js +3 -3
- package/dist/esm/universal/formatWebpack.js +54 -28
- package/dist/esm/universal/pluginDagSort.js +49 -26
- package/dist/esm-node/cli/alias.js +51 -0
- package/dist/esm-node/cli/applyOptionsChain.js +24 -0
- package/dist/esm-node/cli/babel.js +103 -0
- package/dist/esm-node/cli/commands.js +16 -0
- package/dist/esm-node/cli/common.js +10 -0
- package/dist/esm-node/cli/constants/chainId.js +184 -0
- package/dist/esm-node/cli/constants/index.js +102 -0
- package/dist/esm-node/cli/ensure.js +10 -0
- package/dist/esm-node/cli/fs.js +14 -0
- package/dist/esm-node/cli/get/config.js +16 -0
- package/dist/esm-node/cli/get/data.js +86 -0
- package/dist/esm-node/cli/get/index.js +13 -0
- package/dist/esm-node/cli/index.js +20 -0
- package/dist/esm-node/cli/is/config.js +43 -0
- package/dist/esm-node/cli/is/env.js +6 -0
- package/dist/esm-node/cli/is/index.js +5 -0
- package/dist/esm-node/cli/is/platform.js +2 -0
- package/dist/esm-node/cli/is/project.js +73 -0
- package/dist/esm-node/cli/is/type.js +25 -0
- package/dist/esm-node/cli/logger.js +103 -0
- package/dist/esm-node/cli/monorepo.js +70 -0
- package/dist/esm-node/cli/package.js +43 -0
- package/dist/esm-node/cli/path.js +49 -0
- package/dist/esm-node/cli/pathSerializer.js +46 -0
- package/dist/esm-node/cli/port.js +44 -0
- package/dist/esm-node/cli/prettyInstructions.js +78 -0
- package/dist/esm-node/cli/require.js +53 -0
- package/dist/esm-node/cli/runtimeExports.js +35 -0
- package/dist/esm-node/cli/watch.js +41 -0
- package/dist/esm-node/compiled.js +33 -0
- package/dist/esm-node/import.js +8 -0
- package/dist/esm-node/index.js +3 -0
- package/dist/esm-node/runtime/nestedRoutes.js +116 -0
- package/dist/esm-node/runtime/remixRouter.js +1 -0
- package/dist/esm-node/runtime/router.js +1 -0
- package/dist/esm-node/runtime-browser/index.js +1 -0
- package/dist/esm-node/runtime-browser/parsed.js +18 -0
- package/dist/esm-node/runtime-node/index.js +5 -0
- package/dist/esm-node/runtime-node/nestedRoutes.js +18 -0
- package/dist/esm-node/runtime-node/router.js +1 -0
- package/dist/esm-node/runtime-node/serialize.js +6 -0
- package/dist/esm-node/runtime-node/storage.js +38 -0
- package/dist/esm-node/universal/constants.js +3 -0
- package/dist/esm-node/universal/formatWebpack.js +99 -0
- package/dist/esm-node/universal/pluginDagSort.js +50 -0
- package/dist/types/cli/constants/chainId.d.ts +4 -0
- package/dist/types/universal/formatWebpack.d.ts +14 -1
- package/package.json +4 -4
@@ -0,0 +1,51 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import path from "path";
|
3
|
+
import { chalk } from "../compiled";
|
4
|
+
import { readTsConfigByFile } from "./get";
|
5
|
+
import { applyOptionsChain } from "./applyOptionsChain";
|
6
|
+
export const validAlias = (modernConfig, { tsconfigPath }) => {
|
7
|
+
const { source: { alias } } = modernConfig;
|
8
|
+
if (!alias) {
|
9
|
+
return null;
|
10
|
+
}
|
11
|
+
const isTsProject = fs.existsSync(tsconfigPath);
|
12
|
+
if (!isTsProject) {
|
13
|
+
return null;
|
14
|
+
}
|
15
|
+
const userAlias = getUserAlias(alias);
|
16
|
+
if (Object.keys(userAlias).length > 0) {
|
17
|
+
return chalk.red('Note: Please use `compilerOptions.paths` in "tsconfig.json" file replace `source.alias` config in "modern.config.js/ts" when project is typescript');
|
18
|
+
}
|
19
|
+
return null;
|
20
|
+
};
|
21
|
+
export const mergeAlias = (alias) => applyOptionsChain({}, alias);
|
22
|
+
export const getAliasConfig = (aliasOption, option) => {
|
23
|
+
var _tsconfig_compilerOptions, _tsconfig, _tsconfig_compilerOptions1, _tsconfig1;
|
24
|
+
const isTsProject = fs.existsSync(option.tsconfigPath);
|
25
|
+
const alias = mergeAlias(aliasOption);
|
26
|
+
if (!isTsProject) {
|
27
|
+
return {
|
28
|
+
absoluteBaseUrl: option.appDirectory,
|
29
|
+
paths: alias,
|
30
|
+
isTsPath: false,
|
31
|
+
isTsProject
|
32
|
+
};
|
33
|
+
}
|
34
|
+
const tsconfig = readTsConfigByFile(option.tsconfigPath);
|
35
|
+
const baseUrl = (_tsconfig = tsconfig) === null || _tsconfig === void 0 ? void 0 : (_tsconfig_compilerOptions = _tsconfig.compilerOptions) === null || _tsconfig_compilerOptions === void 0 ? void 0 : _tsconfig_compilerOptions.baseUrl;
|
36
|
+
return {
|
37
|
+
absoluteBaseUrl: baseUrl ? path.join(option.appDirectory, baseUrl) : option.appDirectory,
|
38
|
+
paths: {
|
39
|
+
...alias,
|
40
|
+
...(_tsconfig1 = tsconfig) === null || _tsconfig1 === void 0 ? void 0 : (_tsconfig_compilerOptions1 = _tsconfig1.compilerOptions) === null || _tsconfig_compilerOptions1 === void 0 ? void 0 : _tsconfig_compilerOptions1.paths
|
41
|
+
},
|
42
|
+
isTsPath: true,
|
43
|
+
isTsProject
|
44
|
+
};
|
45
|
+
};
|
46
|
+
export const getUserAlias = (alias = {}) => Object.keys(alias).reduce((o, k) => {
|
47
|
+
if (Array.isArray(alias[k])) {
|
48
|
+
o[k] = alias[k];
|
49
|
+
}
|
50
|
+
return o;
|
51
|
+
}, {});
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { isFunction, isPlainObject } from "./is";
|
2
|
+
import { logger } from "./logger";
|
3
|
+
export function applyOptionsChain(defaults, options, utils, mergeFn = Object.assign) {
|
4
|
+
if (!options) {
|
5
|
+
return defaults;
|
6
|
+
}
|
7
|
+
if (isPlainObject(options)) {
|
8
|
+
return mergeFn(defaults, options);
|
9
|
+
} else if (isFunction(options)) {
|
10
|
+
const ret = options(defaults, utils);
|
11
|
+
if (ret) {
|
12
|
+
if (!isPlainObject(ret)) {
|
13
|
+
logger.warn(`${options.name}: Function should mutate the config and return nothing, Or return a cloned or merged version of config object.`);
|
14
|
+
}
|
15
|
+
return ret;
|
16
|
+
}
|
17
|
+
} else if (Array.isArray(options)) {
|
18
|
+
return options.reduce((memo, cur) => applyOptionsChain(memo, cur, utils, mergeFn), defaults);
|
19
|
+
} else {
|
20
|
+
throw new Error(`applyOptionsChain error:
|
21
|
+
default options is: ${JSON.stringify(defaults)}`);
|
22
|
+
}
|
23
|
+
return defaults;
|
24
|
+
}
|
@@ -0,0 +1,103 @@
|
|
1
|
+
import { isAbsolute, sep } from "path";
|
2
|
+
import { applyOptionsChain } from "./applyOptionsChain";
|
3
|
+
import { ensureArray } from "./ensure";
|
4
|
+
import { normalizeToPosixPath } from "./path";
|
5
|
+
const formatPath = (originPath) => {
|
6
|
+
if (isAbsolute(originPath)) {
|
7
|
+
return originPath.split(sep).join("/");
|
8
|
+
}
|
9
|
+
return originPath;
|
10
|
+
};
|
11
|
+
const getPluginItemName = (item) => {
|
12
|
+
if (typeof item === "string") {
|
13
|
+
return formatPath(item);
|
14
|
+
}
|
15
|
+
if (Array.isArray(item) && typeof item[0] === "string") {
|
16
|
+
return formatPath(item[0]);
|
17
|
+
}
|
18
|
+
return null;
|
19
|
+
};
|
20
|
+
const addPlugins = (plugins, config) => {
|
21
|
+
if (config.plugins) {
|
22
|
+
config.plugins.push(...plugins);
|
23
|
+
} else {
|
24
|
+
config.plugins = plugins;
|
25
|
+
}
|
26
|
+
};
|
27
|
+
const addPresets = (presets, config) => {
|
28
|
+
if (config.presets) {
|
29
|
+
config.presets.push(...presets);
|
30
|
+
} else {
|
31
|
+
config.presets = presets;
|
32
|
+
}
|
33
|
+
};
|
34
|
+
const removePlugins = (plugins, config) => {
|
35
|
+
if (!config.plugins) {
|
36
|
+
return;
|
37
|
+
}
|
38
|
+
const removeList = ensureArray(plugins);
|
39
|
+
config.plugins = config.plugins.filter((item) => {
|
40
|
+
const name = getPluginItemName(item);
|
41
|
+
if (name) {
|
42
|
+
return !removeList.find((removeItem) => name.includes(removeItem));
|
43
|
+
}
|
44
|
+
return true;
|
45
|
+
});
|
46
|
+
};
|
47
|
+
const removePresets = (presets, config) => {
|
48
|
+
if (!config.presets) {
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
const removeList = ensureArray(presets);
|
52
|
+
config.presets = config.presets.filter((item) => {
|
53
|
+
const name = getPluginItemName(item);
|
54
|
+
if (name) {
|
55
|
+
return !removeList.find((removeItem) => name.includes(removeItem));
|
56
|
+
}
|
57
|
+
return true;
|
58
|
+
});
|
59
|
+
};
|
60
|
+
const modifyPresetOptions = (presetName, options, presets = []) => {
|
61
|
+
presets.forEach((preset, index) => {
|
62
|
+
if (Array.isArray(preset)) {
|
63
|
+
if (typeof preset[0] === "string" && normalizeToPosixPath(preset[0]).includes(presetName)) {
|
64
|
+
preset[1] = {
|
65
|
+
...preset[1] || {},
|
66
|
+
...options
|
67
|
+
};
|
68
|
+
}
|
69
|
+
} else if (typeof preset === "string" && normalizeToPosixPath(preset).includes(presetName)) {
|
70
|
+
presets[index] = [
|
71
|
+
preset,
|
72
|
+
options
|
73
|
+
];
|
74
|
+
}
|
75
|
+
});
|
76
|
+
};
|
77
|
+
export const getBabelUtils = (config) => {
|
78
|
+
const noop = () => {
|
79
|
+
};
|
80
|
+
return {
|
81
|
+
addPlugins: (plugins) => addPlugins(plugins, config),
|
82
|
+
addPresets: (presets) => addPresets(presets, config),
|
83
|
+
removePlugins: (plugins) => removePlugins(plugins, config),
|
84
|
+
removePresets: (presets) => removePresets(presets, config),
|
85
|
+
// `addIncludes` and `addExcludes` are noop functions by default,
|
86
|
+
// It can be overridden by `extraBabelUtils`.
|
87
|
+
addIncludes: noop,
|
88
|
+
addExcludes: noop,
|
89
|
+
// Compat `presetEnvOptions` and `presetReactOptions` in Eden.
|
90
|
+
modifyPresetEnvOptions: (options) => modifyPresetOptions("@babel/preset-env", options, config.presets || []),
|
91
|
+
modifyPresetReactOptions: (options) => modifyPresetOptions("@babel/preset-react", options, config.presets || [])
|
92
|
+
};
|
93
|
+
};
|
94
|
+
export const applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils) => {
|
95
|
+
if (userBabelConfig) {
|
96
|
+
const babelUtils = {
|
97
|
+
...getBabelUtils(defaultOptions),
|
98
|
+
...extraBabelUtils
|
99
|
+
};
|
100
|
+
return applyOptionsChain(defaultOptions, userBabelConfig || {}, babelUtils);
|
101
|
+
}
|
102
|
+
return defaultOptions;
|
103
|
+
};
|
@@ -0,0 +1,16 @@
|
|
1
|
+
export const getFullArgv = () => {
|
2
|
+
var _process_env_MODERN_ARGV;
|
3
|
+
return ((_process_env_MODERN_ARGV = process.env.MODERN_ARGV) === null || _process_env_MODERN_ARGV === void 0 ? void 0 : _process_env_MODERN_ARGV.split(" ")) || process.argv;
|
4
|
+
};
|
5
|
+
export const getArgv = () => {
|
6
|
+
return getFullArgv().slice(2);
|
7
|
+
};
|
8
|
+
export const getCommand = () => {
|
9
|
+
const args = getArgv();
|
10
|
+
const command = args[0];
|
11
|
+
return command;
|
12
|
+
};
|
13
|
+
export const isDevCommand = () => {
|
14
|
+
const command = getCommand();
|
15
|
+
return command === "dev" || command === "start";
|
16
|
+
};
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { debug } from "../../compiled/debug";
|
2
|
+
export const createDebugger = (scope) => debug(`modern-js:${scope}`);
|
3
|
+
export const clearConsole = () => {
|
4
|
+
if (process.stdout.isTTY && !process.env.DEBUG) {
|
5
|
+
process.stdout.write("\x1B[H\x1B[2J");
|
6
|
+
}
|
7
|
+
};
|
8
|
+
export const wait = (time = 0) => new Promise((resolve) => {
|
9
|
+
setTimeout(resolve, time);
|
10
|
+
});
|
@@ -0,0 +1,184 @@
|
|
1
|
+
export const CHAIN_ID = {
|
2
|
+
/** Predefined rules */
|
3
|
+
RULE: {
|
4
|
+
/** Rule for .mjs */
|
5
|
+
MJS: "mjs",
|
6
|
+
/** Rule for predefined loaders */
|
7
|
+
LOADERS: "loaders",
|
8
|
+
/** Rule for fonts */
|
9
|
+
FONT: "font",
|
10
|
+
/** Rule for images */
|
11
|
+
IMAGE: "image",
|
12
|
+
/** Rule for media */
|
13
|
+
MEDIA: "media",
|
14
|
+
/** Rule for js */
|
15
|
+
JS: "js",
|
16
|
+
/** Rule for data uri encoded javascript */
|
17
|
+
JS_DATA_URI: "js-data-uri",
|
18
|
+
/** Rule for ts */
|
19
|
+
TS: "ts",
|
20
|
+
/** Rule for css */
|
21
|
+
CSS: "css",
|
22
|
+
/** Rule for less */
|
23
|
+
LESS: "less",
|
24
|
+
/** Rule for sass */
|
25
|
+
SASS: "sass",
|
26
|
+
/** Rule for stylus */
|
27
|
+
STYLUS: "stylus",
|
28
|
+
/** Rule for svg */
|
29
|
+
SVG: "svg",
|
30
|
+
/** Rule for pug */
|
31
|
+
PUG: "pug",
|
32
|
+
/** Rule for Vue */
|
33
|
+
VUE: "vue",
|
34
|
+
/** Rule for toml */
|
35
|
+
TOML: "toml",
|
36
|
+
/** Rule for yaml */
|
37
|
+
YAML: "yaml",
|
38
|
+
/** Rule for wasm */
|
39
|
+
WASM: "wasm",
|
40
|
+
/** Rule for node */
|
41
|
+
NODE: "node",
|
42
|
+
/** Rule for bff */
|
43
|
+
JS_BFF_API: "js-bff-api"
|
44
|
+
},
|
45
|
+
/** Predefined rule groups */
|
46
|
+
ONE_OF: {
|
47
|
+
SVG: "svg",
|
48
|
+
SVG_URL: "svg-url",
|
49
|
+
SVG_ASSET: "svg-asset",
|
50
|
+
SVG_INLINE: "svg-inline"
|
51
|
+
},
|
52
|
+
/** Predefined loaders */
|
53
|
+
USE: {
|
54
|
+
/** ts-loader */
|
55
|
+
TS: "ts",
|
56
|
+
/** css-loader */
|
57
|
+
CSS: "css",
|
58
|
+
/** sass-loader */
|
59
|
+
SASS: "sass",
|
60
|
+
/** less-loader */
|
61
|
+
LESS: "less",
|
62
|
+
/** stylus-loader */
|
63
|
+
STYLUS: "stylus",
|
64
|
+
/** url-loader */
|
65
|
+
URL: "url",
|
66
|
+
/** pug-loader */
|
67
|
+
PUG: "pug",
|
68
|
+
/** vue-loader */
|
69
|
+
VUE: "vue",
|
70
|
+
/** file-loader */
|
71
|
+
FILE: "file",
|
72
|
+
/** @svgr/webpack */
|
73
|
+
SVGR: "svgr",
|
74
|
+
/** yaml-loader */
|
75
|
+
YAML: "yaml",
|
76
|
+
/** toml-loader */
|
77
|
+
TOML: "toml",
|
78
|
+
/** html-loader */
|
79
|
+
HTML: "html",
|
80
|
+
/** node-loader */
|
81
|
+
NODE: "html",
|
82
|
+
/** babel-loader */
|
83
|
+
BABEL: "babel",
|
84
|
+
/** esbuild-loader */
|
85
|
+
ESBUILD: "esbuild",
|
86
|
+
/** swc-loader */
|
87
|
+
SWC: "swc",
|
88
|
+
/** style-loader */
|
89
|
+
STYLE: "style-loader",
|
90
|
+
/** postcss-loader */
|
91
|
+
POSTCSS: "postcss",
|
92
|
+
/** markdown-loader */
|
93
|
+
MARKDOWN: "markdown",
|
94
|
+
/** ignore-css-loader */
|
95
|
+
IGNORE_CSS: "ignore-css",
|
96
|
+
/** css-modules-typescript-loader */
|
97
|
+
CSS_MODULES_TS: "css-modules-typescript",
|
98
|
+
/** mini-css-extract-plugin.loader */
|
99
|
+
MINI_CSS_EXTRACT: "mini-css-extract",
|
100
|
+
/** builder-plugin-image-compress.loader */
|
101
|
+
IMAGE_COMPRESS: "image-compress",
|
102
|
+
/** builder-plugin-image-compress svgo-loader */
|
103
|
+
SVGO: "svgo",
|
104
|
+
/** builder-plugin-imagex */
|
105
|
+
IMAGEX: "imagex"
|
106
|
+
},
|
107
|
+
/** Predefined plugins */
|
108
|
+
PLUGIN: {
|
109
|
+
/** HotModuleReplacementPlugin */
|
110
|
+
HMR: "hmr",
|
111
|
+
/** CopyWebpackPlugin */
|
112
|
+
COPY: "copy",
|
113
|
+
/** HtmlWebpackPlugin */
|
114
|
+
HTML: "html",
|
115
|
+
/** DefinePlugin */
|
116
|
+
DEFINE: "define",
|
117
|
+
/** IgnorePlugin */
|
118
|
+
IGNORE: "ignore",
|
119
|
+
/** BannerPlugin */
|
120
|
+
BANNER: "banner",
|
121
|
+
/** Webpackbar */
|
122
|
+
PROGRESS: "progress",
|
123
|
+
/** Inspector */
|
124
|
+
INSPECTOR: "inspector",
|
125
|
+
/** AppIconPlugin */
|
126
|
+
APP_ICON: "app-icon",
|
127
|
+
/** FaviconUrlPlugin */
|
128
|
+
FAVICON_URL: "favicon-url",
|
129
|
+
/** LoadableWebpackPlugin */
|
130
|
+
LOADABLE: "loadable",
|
131
|
+
/** WebpackManifestPlugin */
|
132
|
+
MANIFEST: "webpack-manifest",
|
133
|
+
/** ForkTsCheckerWebpackPlugin */
|
134
|
+
TS_CHECKER: "ts-checker",
|
135
|
+
/** InlineChunkHtmlPlugin */
|
136
|
+
INLINE_HTML: "inline-html",
|
137
|
+
/** WebpackBundleAnalyzer */
|
138
|
+
BUNDLE_ANALYZER: "bundle-analyze",
|
139
|
+
/** BottomTemplatePlugin */
|
140
|
+
BOTTOM_TEMPLATE: "bottom-template",
|
141
|
+
/** HtmlTagsPlugin */
|
142
|
+
HTML_TAGS: "html-tags",
|
143
|
+
/** HtmlNoncePlugin */
|
144
|
+
HTML_NONCE: "html-nonce",
|
145
|
+
/** HtmlCrossOriginPlugin */
|
146
|
+
HTML_CROSS_ORIGIN: "html-cross-origin",
|
147
|
+
/** MiniCssExtractPlugin */
|
148
|
+
MINI_CSS_EXTRACT: "mini-css-extract",
|
149
|
+
/** VueLoaderPlugin */
|
150
|
+
VUE_LOADER_PLUGIN: "vue-loader-plugin",
|
151
|
+
/** ReactFastRefreshPlugin */
|
152
|
+
REACT_FAST_REFRESH: "react-fast-refresh",
|
153
|
+
/** ProvidePlugin for node polyfill */
|
154
|
+
NODE_POLYFILL_PROVIDE: "node-polyfill-provide",
|
155
|
+
/** WebpackSRIPlugin */
|
156
|
+
SUBRESOURCE_INTEGRITY: "subresource-integrity",
|
157
|
+
/** WebpackAssetsRetryPlugin */
|
158
|
+
ASSETS_RETRY: "ASSETS_RETRY",
|
159
|
+
/** AutoSetRootFontSizePlugin */
|
160
|
+
AUTO_SET_ROOT_SIZE: "auto-set-root-size",
|
161
|
+
/** HtmlAsyncChunkPlugin */
|
162
|
+
HTML_ASYNC_CHUNK: "html-async-chunk",
|
163
|
+
/** SWC_POLYFILL_CHECKER */
|
164
|
+
SWC_POLYFILL_CHECKER: "swc-polyfill-checker-plugin"
|
165
|
+
},
|
166
|
+
/** Predefined minimizers */
|
167
|
+
MINIMIZER: {
|
168
|
+
/** TerserWebpackPlugin */
|
169
|
+
JS: "js",
|
170
|
+
/** CssMinimizerWebpackPlugin */
|
171
|
+
CSS: "css",
|
172
|
+
/** ESBuildPlugin */
|
173
|
+
ESBUILD: "js-css",
|
174
|
+
/** SWCPlugin */
|
175
|
+
SWC: "swc"
|
176
|
+
},
|
177
|
+
/** Predefined resolve plugins */
|
178
|
+
RESOLVE_PLUGIN: {
|
179
|
+
/** ModuleScopePlugin */
|
180
|
+
MODULE_SCOPE: "module-scope",
|
181
|
+
/** TsConfigPathsPlugin */
|
182
|
+
TS_CONFIG_PATHS: "ts-config-paths"
|
183
|
+
}
|
184
|
+
};
|
@@ -0,0 +1,102 @@
|
|
1
|
+
export const ROUTE_SPEC_FILE = "route.json";
|
2
|
+
export const NESTED_ROUTE_SPEC_FILE = "nestedRoutes.json";
|
3
|
+
export const MAIN_ENTRY_NAME = "main";
|
4
|
+
export const SERVER_BUNDLE_DIRECTORY = "bundles";
|
5
|
+
export const SERVER_WORKER_BUNDLE_DIRECTORY = "worker";
|
6
|
+
export const ENTRY_NAME_PATTERN = "^[a-zA-Z0-9_-]+$";
|
7
|
+
export const SERVER_RENDER_FUNCTION_NAME = "serverRender";
|
8
|
+
export const LOADABLE_STATS_FILE = "loadable-stats.json";
|
9
|
+
export const API_DIR = "api";
|
10
|
+
export const SERVER_DIR = "server";
|
11
|
+
export const SHARED_DIR = "shared";
|
12
|
+
export const CONFIG_CACHE_DIR = "./node_modules/.cache/node-bundle-require";
|
13
|
+
export const CONFIG_FILE_EXTENSIONS = [
|
14
|
+
".js",
|
15
|
+
".ts",
|
16
|
+
".mjs"
|
17
|
+
];
|
18
|
+
export const OUTPUT_CONFIG_FILE = "modern.config.json";
|
19
|
+
export const DEFAULT_SERVER_CONFIG = "modern.server-runtime.config";
|
20
|
+
export const ROUTE_MANIFEST_FILE = "routes-manifest.json";
|
21
|
+
export const LOADER_ROUTES_DIR = `loader-routes`;
|
22
|
+
export const DEFAULT_DEV_HOST = "0.0.0.0";
|
23
|
+
export const INTERNAL_APP_TOOLS_PLUGINS = {
|
24
|
+
"@modern-js/app-tools": "@modern-js/app-tools/cli",
|
25
|
+
"@modern-js/plugin-proxy": "@modern-js/plugin-proxy/cli",
|
26
|
+
"@modern-js/plugin-ssg": "@modern-js/plugin-ssg/cli",
|
27
|
+
"@modern-js/plugin-bff": "@modern-js/plugin-bff/cli",
|
28
|
+
"@modern-js/plugin-testing": "@modern-js/plugin-testing/cli",
|
29
|
+
"@modern-js/plugin-storybook": "@modern-js/plugin-storybook/cli",
|
30
|
+
"@modern-js/plugin-express": "@modern-js/plugin-express/cli",
|
31
|
+
"@modern-js/plugin-koa": "@modern-js/plugin-koa/cli",
|
32
|
+
"@modern-js/plugin-server": "@modern-js/plugin-server/cli",
|
33
|
+
"@modern-js/plugin-garfish": "@modern-js/plugin-garfish/cli",
|
34
|
+
"@modern-js/plugin-tailwindcss": "@modern-js/plugin-tailwindcss/cli",
|
35
|
+
"@modern-js/plugin-polyfill": "@modern-js/plugin-polyfill/cli",
|
36
|
+
// legacy router (inner react-router-dom v5)
|
37
|
+
"@modern-js/plugin-router-v5": "@modern-js/plugin-router-v5/cli"
|
38
|
+
};
|
39
|
+
export const INTERNAL_APP_TOOLS_RUNTIME_PLUGINS = {
|
40
|
+
"@modern-js/runtime": "@modern-js/runtime/cli"
|
41
|
+
};
|
42
|
+
export const INTERNAL_MODULE_TOOLS_PLUGINS = {
|
43
|
+
"@modern-js/module-tools": "@modern-js/module-tools",
|
44
|
+
"@modern-js/runtime": "@modern-js/runtime/cli",
|
45
|
+
"@modern-js/plugin-testing": "@modern-js/plugin-testing/cli",
|
46
|
+
"@modern-js/plugin-storybook": "@modern-js/plugin-storybook/cli",
|
47
|
+
"@modern-js/plugin-tailwindcss": "@modern-js/plugin-tailwindcss/cli",
|
48
|
+
// legacy router (inner react-router-dom v5)
|
49
|
+
"@modern-js/plugin-router-legacy": "@modern-js/plugin-router-legacy/cli"
|
50
|
+
};
|
51
|
+
export const INTERNAL_MONOREPO_TOOLS_PLUGINS = {
|
52
|
+
"@modern-js/monorepo-tools": "@modern-js/monorepo-tools/cli",
|
53
|
+
"@modern-js/plugin-testing": "@modern-js/plugin-testing/cli"
|
54
|
+
};
|
55
|
+
export const INTERNAL_DOC_TOOLS_PLUGINS = {
|
56
|
+
"@modern-js/doc-tools": "@modern-js/doc-tools",
|
57
|
+
"@modern-js/runtime": "@modern-js/runtime/cli",
|
58
|
+
"@modern-js/plugin-testing": "@modern-js/plugin-testing/cli"
|
59
|
+
};
|
60
|
+
export const INTERNAL_CLI_PLUGINS = {
|
61
|
+
"@modern-js/app-tools": "@modern-js/app-tools/cli",
|
62
|
+
"@modern-js/monorepo-tools": "@modern-js/monorepo-tools/cli",
|
63
|
+
"@modern-js/module-tools": "@modern-js/module-tools",
|
64
|
+
"@modern-js/doc-tools": "@modern-js/doc-tools",
|
65
|
+
"@modern-js/runtime": "@modern-js/runtime/cli",
|
66
|
+
"@modern-js/plugin-proxy": "@modern-js/plugin-proxy/cli",
|
67
|
+
"@modern-js/plugin-ssg": "@modern-js/plugin-ssg/cli",
|
68
|
+
"@modern-js/plugin-bff": "@modern-js/plugin-bff/cli",
|
69
|
+
"@modern-js/plugin-testing": "@modern-js/plugin-testing/cli",
|
70
|
+
"@modern-js/plugin-storybook": "@modern-js/plugin-storybook/cli",
|
71
|
+
"@modern-js/plugin-express": "@modern-js/plugin-express/cli",
|
72
|
+
"@modern-js/plugin-koa": "@modern-js/plugin-koa/cli",
|
73
|
+
"@modern-js/plugin-server": "@modern-js/plugin-server/cli",
|
74
|
+
"@modern-js/plugin-swc": "@modern-js/plugin-swc",
|
75
|
+
"@modern-js/plugin-garfish": "@modern-js/plugin-garfish/cli",
|
76
|
+
"@modern-js/plugin-tailwindcss": "@modern-js/plugin-tailwindcss/cli",
|
77
|
+
"@modern-js/plugin-polyfill": "@modern-js/plugin-polyfill/cli",
|
78
|
+
// legacy router (inner react-router-dom v5)
|
79
|
+
"@modern-js/plugin-router-v5": "@modern-js/plugin-router-v5/cli"
|
80
|
+
};
|
81
|
+
export const SERVER_PLUGIN_BFF = "@modern-js/plugin-bff";
|
82
|
+
export const SERVER_PLUGIN_EXPRESS = "@modern-js/plugin-express";
|
83
|
+
export const SERVER_PLUGIN_KOA = "@modern-js/plugin-koa";
|
84
|
+
export const SERVER_PLUGIN_SERVER = "@modern-js/plugin-server";
|
85
|
+
export const SERVER_PLUGIN_POLYFILL = "@modern-js/plugin-polyfill";
|
86
|
+
export const INTERNAL_SERVER_PLUGINS = {
|
87
|
+
[SERVER_PLUGIN_BFF]: "@modern-js/plugin-bff/server",
|
88
|
+
[SERVER_PLUGIN_EXPRESS]: "@modern-js/plugin-express/server",
|
89
|
+
[SERVER_PLUGIN_KOA]: "@modern-js/plugin-koa/server",
|
90
|
+
[SERVER_PLUGIN_SERVER]: "@modern-js/plugin-server/server",
|
91
|
+
[SERVER_PLUGIN_POLYFILL]: "@modern-js/plugin-polyfill/server"
|
92
|
+
};
|
93
|
+
export const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
|
94
|
+
allowNamespaces: true,
|
95
|
+
allExtensions: true,
|
96
|
+
allowDeclareFields: true,
|
97
|
+
// aligns Babel's behavior with TypeScript's default behavior.
|
98
|
+
// https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums
|
99
|
+
optimizeConstEnums: true,
|
100
|
+
isTSX: true
|
101
|
+
};
|
102
|
+
export * from "./chainId";
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import path from "path";
|
2
|
+
export const ensureAbsolutePath = (base, filePath) => path.isAbsolute(filePath) ? filePath : path.resolve(base, filePath);
|
3
|
+
export const ensureArray = (params) => {
|
4
|
+
if (Array.isArray(params)) {
|
5
|
+
return params;
|
6
|
+
}
|
7
|
+
return [
|
8
|
+
params
|
9
|
+
];
|
10
|
+
};
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { fs } from "../compiled";
|
2
|
+
export const findExists = (files) => {
|
3
|
+
for (const file of files) {
|
4
|
+
if (fs.existsSync(file) && fs.statSync(file).isFile()) {
|
5
|
+
return file;
|
6
|
+
}
|
7
|
+
}
|
8
|
+
return false;
|
9
|
+
};
|
10
|
+
export const emptyDir = async (dir) => {
|
11
|
+
if (await fs.pathExists(dir)) {
|
12
|
+
await fs.emptyDir(dir);
|
13
|
+
}
|
14
|
+
};
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { isPlainObject } from "../is";
|
2
|
+
export const getEntryOptions = (name, isMainEntry, baseOptions, optionsByEntries, packageName) => {
|
3
|
+
if (optionsByEntries) {
|
4
|
+
let optionsByEntry = getOptionsByEntryName(name, optionsByEntries);
|
5
|
+
if (optionsByEntry === void 0 && isMainEntry && packageName) {
|
6
|
+
optionsByEntry = getOptionsByEntryName(packageName, optionsByEntries);
|
7
|
+
}
|
8
|
+
return optionsByEntry !== void 0 ? isPlainObject(optionsByEntry) && isPlainObject(baseOptions) ? {
|
9
|
+
...baseOptions,
|
10
|
+
...optionsByEntry
|
11
|
+
} : optionsByEntry : baseOptions;
|
12
|
+
} else {
|
13
|
+
return baseOptions;
|
14
|
+
}
|
15
|
+
};
|
16
|
+
const getOptionsByEntryName = (name, optionsByEntries) => optionsByEntries.hasOwnProperty(name) ? optionsByEntries[name] : void 0;
|
@@ -0,0 +1,86 @@
|
|
1
|
+
import os from "os";
|
2
|
+
import path from "path";
|
3
|
+
import { browserslist, fs, json5 } from "../../compiled";
|
4
|
+
import { INTERNAL_CLI_PLUGINS } from "../constants";
|
5
|
+
import { isDepExists } from "../is";
|
6
|
+
import { canUsePnpm, canUseYarn } from "../package";
|
7
|
+
const MAX_TIMES = 5;
|
8
|
+
export async function getPackageManager(cwd = process.cwd()) {
|
9
|
+
let appDirectory = cwd;
|
10
|
+
let times = 0;
|
11
|
+
while (os.homedir() !== appDirectory && times < MAX_TIMES) {
|
12
|
+
times++;
|
13
|
+
if (fs.existsSync(path.resolve(appDirectory, "pnpm-lock.yaml"))) {
|
14
|
+
return "pnpm";
|
15
|
+
}
|
16
|
+
if (fs.existsSync(path.resolve(appDirectory, "yarn.lock"))) {
|
17
|
+
return "yarn";
|
18
|
+
}
|
19
|
+
if (fs.existsSync(path.resolve(appDirectory, "package-lock.json"))) {
|
20
|
+
return "npm";
|
21
|
+
}
|
22
|
+
appDirectory = path.join(appDirectory, "..");
|
23
|
+
}
|
24
|
+
if (await canUsePnpm()) {
|
25
|
+
return "pnpm";
|
26
|
+
}
|
27
|
+
if (await canUseYarn()) {
|
28
|
+
return "yarn";
|
29
|
+
}
|
30
|
+
return "npm";
|
31
|
+
}
|
32
|
+
export const getCoreJsVersion = (corejsPkgPath) => {
|
33
|
+
try {
|
34
|
+
const { version } = fs.readJSONSync(corejsPkgPath);
|
35
|
+
const [major, minor] = version.split(".");
|
36
|
+
return `${major}.${minor}`;
|
37
|
+
} catch (err) {
|
38
|
+
return "3";
|
39
|
+
}
|
40
|
+
};
|
41
|
+
export const getAntdMajorVersion = (appDirectory) => {
|
42
|
+
try {
|
43
|
+
const pkgJsonPath = require.resolve("antd/package.json", {
|
44
|
+
paths: [
|
45
|
+
appDirectory
|
46
|
+
]
|
47
|
+
});
|
48
|
+
const { version } = require(pkgJsonPath);
|
49
|
+
return Number(version.split(".")[0]);
|
50
|
+
} catch (err) {
|
51
|
+
return null;
|
52
|
+
}
|
53
|
+
};
|
54
|
+
export const defaults = [
|
55
|
+
"> 0.01%",
|
56
|
+
"not dead",
|
57
|
+
"not op_mini all"
|
58
|
+
];
|
59
|
+
export const getBrowserslist = (appDirectory) => browserslist.loadConfig({
|
60
|
+
path: appDirectory
|
61
|
+
}) || defaults;
|
62
|
+
export function getInternalPlugins(appDirectory, internalPlugins = INTERNAL_CLI_PLUGINS) {
|
63
|
+
return [
|
64
|
+
...Object.keys(internalPlugins).filter((name) => {
|
65
|
+
const config = internalPlugins[name];
|
66
|
+
if (typeof config !== "string" && config.forced === true) {
|
67
|
+
return true;
|
68
|
+
}
|
69
|
+
return isDepExists(appDirectory, name);
|
70
|
+
}).map((name) => {
|
71
|
+
const config = internalPlugins[name];
|
72
|
+
if (typeof config !== "string") {
|
73
|
+
return config.path;
|
74
|
+
} else {
|
75
|
+
return config;
|
76
|
+
}
|
77
|
+
})
|
78
|
+
];
|
79
|
+
}
|
80
|
+
export const readTsConfig = (root) => {
|
81
|
+
return readTsConfigByFile(path.resolve(root, "./tsconfig.json"));
|
82
|
+
};
|
83
|
+
export const readTsConfigByFile = (filename) => {
|
84
|
+
const content = fs.readFileSync(path.resolve(filename), "utf-8");
|
85
|
+
return json5.parse(content);
|
86
|
+
};
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import path from "path";
|
2
|
+
import { CONFIG_FILE_EXTENSIONS } from "../constants";
|
3
|
+
import { findExists } from "../fs";
|
4
|
+
export const getServerConfig = async (appDirectory, configFile) => {
|
5
|
+
const configFilePath = findExists(CONFIG_FILE_EXTENSIONS.map((extension) => path.resolve(appDirectory, `${configFile}${extension}`)));
|
6
|
+
return configFilePath;
|
7
|
+
};
|
8
|
+
export const getTargetDir = (from, baseDir, targetBaseDir) => {
|
9
|
+
const relativePath = path.relative(baseDir, from);
|
10
|
+
return path.resolve(targetBaseDir, relativePath);
|
11
|
+
};
|
12
|
+
export * from "./data";
|
13
|
+
export * from "./config";
|
@@ -0,0 +1,20 @@
|
|
1
|
+
export * from "./constants";
|
2
|
+
export * from "./get";
|
3
|
+
export * from "./is";
|
4
|
+
export * from "./alias";
|
5
|
+
export * from "./applyOptionsChain";
|
6
|
+
export * from "./babel";
|
7
|
+
export * from "./commands";
|
8
|
+
export * from "./common";
|
9
|
+
export * from "./ensure";
|
10
|
+
export * from "./fs";
|
11
|
+
export * from "./logger";
|
12
|
+
export * from "./monorepo";
|
13
|
+
export * from "./package";
|
14
|
+
export * from "./path";
|
15
|
+
export * from "./pathSerializer";
|
16
|
+
export * from "./port";
|
17
|
+
export * from "./prettyInstructions";
|
18
|
+
export * from "./require";
|
19
|
+
export * from "./runtimeExports";
|
20
|
+
export * from "./watch";
|