@modern-js/module-tools 1.1.5 → 1.3.2
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/.eslintrc.js +4 -1
- package/CHANGELOG.md +85 -0
- package/dist/js/modern/cli/build.js +1 -1
- package/dist/js/modern/cli/dev.js +2 -2
- package/dist/js/modern/commands/dev.js +13 -1
- package/dist/js/modern/features/dev/index.js +11 -0
- package/dist/js/node/cli/build.js +1 -1
- package/dist/js/node/cli/dev.js +2 -2
- package/dist/js/node/commands/dev.js +12 -1
- package/dist/js/node/features/dev/index.js +16 -2
- package/dist/types/commands/build.d.ts +1 -1
- package/dist/types/commands/dev.d.ts +1 -1
- package/dist/types/features/dev/index.d.ts +2 -1
- package/dist/types/index.d.ts +3 -1
- package/jest.config.js +8 -0
- package/lib/types.d.ts +0 -11
- package/package.json +30 -22
- package/tests/dev-cli.test.ts +25 -0
- package/tests/dev-command.test.ts +44 -0
- package/tests/dev-feature.test.ts +30 -0
- package/tests/fixtures/tspaths/a.ts +1 -0
- package/tests/fixtures/tspaths/b.ts +1 -0
- package/tests/index.test.ts +7 -0
- package/tests/tsconfig.json +11 -0
- package/tests/tspaths-transform.test.ts +21 -0
- package/tsconfig.json +2 -3
- package/src/.eslintrc.json +0 -5
- package/src/cli/build.ts +0 -39
- package/src/cli/dev.ts +0 -24
- package/src/cli/index.ts +0 -3
- package/src/cli/new.ts +0 -32
- package/src/commands/build.ts +0 -74
- package/src/commands/dev.ts +0 -43
- package/src/commands/index.ts +0 -2
- package/src/features/build/build-platform.ts +0 -83
- package/src/features/build/build-watch.ts +0 -99
- package/src/features/build/build.ts +0 -111
- package/src/features/build/constants.ts +0 -52
- package/src/features/build/index.ts +0 -54
- package/src/features/build/logger/index.ts +0 -2
- package/src/features/build/logger/logText.ts +0 -80
- package/src/features/build/logger/loggerManager.ts +0 -132
- package/src/features/build/utils.ts +0 -235
- package/src/features/dev/index.ts +0 -62
- package/src/index.ts +0 -55
- package/src/locale/en.ts +0 -21
- package/src/locale/index.ts +0 -15
- package/src/locale/zh.ts +0 -21
- package/src/schema/index.ts +0 -4
- package/src/schema/output.ts +0 -41
- package/src/schema/schema.d.ts +0 -13
- package/src/schema/source.ts +0 -16
- package/src/tasks/build-source-code.ts +0 -234
- package/src/tasks/build-style.ts +0 -194
- package/src/tasks/build-watch-source-code.ts +0 -186
- package/src/tasks/build-watch-style.ts +0 -271
- package/src/tasks/constants.ts +0 -1
- package/src/tasks/copy-assets.ts +0 -123
- package/src/tasks/generator-dts.ts +0 -277
- package/src/type.d.ts +0 -1
- package/src/types.ts +0 -65
- package/src/utils/babel.ts +0 -104
- package/src/utils/color.ts +0 -3
- package/src/utils/copy.ts +0 -71
- package/src/utils/init-env.ts +0 -31
- package/src/utils/json.ts +0 -13
- package/src/utils/language.ts +0 -9
- package/src/utils/logger.ts +0 -141
- package/src/utils/readline.ts +0 -28
- package/src/utils/tsconfig.ts +0 -37
- package/src/utils/tspaths-transform/constants.ts +0 -19
- package/src/utils/tspaths-transform/index.ts +0 -139
- package/src/utils/tspaths-transform/utils.ts +0 -25
- package/src/utils/valide.ts +0 -37
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { Import, fs } from '@modern-js/utils';
|
|
2
|
-
import type { NormalizedConfig, CoreOptions } from '@modern-js/core';
|
|
3
|
-
import type { BabelOptions, IVirtualDist } from '@modern-js/babel-compiler';
|
|
4
|
-
import type { ITsconfig } from '../types';
|
|
5
|
-
import { initEnv } from '../utils/init-env';
|
|
6
|
-
|
|
7
|
-
const babelCompiler: typeof import('@modern-js/babel-compiler') = Import.lazy(
|
|
8
|
-
'@modern-js/babel-compiler',
|
|
9
|
-
require,
|
|
10
|
-
);
|
|
11
|
-
const glob: typeof import('glob') = Import.lazy('glob', require);
|
|
12
|
-
const argv: typeof import('process.argv').default = Import.lazy(
|
|
13
|
-
'process.argv',
|
|
14
|
-
require,
|
|
15
|
-
);
|
|
16
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
17
|
-
'@modern-js/core',
|
|
18
|
-
require,
|
|
19
|
-
);
|
|
20
|
-
const bc: typeof import('../utils/babel') = Import.lazy(
|
|
21
|
-
'../utils/babel',
|
|
22
|
-
require,
|
|
23
|
-
);
|
|
24
|
-
const ts: typeof import('../utils/tsconfig') = Import.lazy(
|
|
25
|
-
'../utils/tsconfig',
|
|
26
|
-
require,
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
export enum Compiler {
|
|
30
|
-
babel,
|
|
31
|
-
esbuild,
|
|
32
|
-
swc,
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface IBuildSourceCodeConfig {
|
|
36
|
-
babelConfig: BabelOptions;
|
|
37
|
-
srcRootDir: string;
|
|
38
|
-
willCompilerDirOrFile: string;
|
|
39
|
-
distDir: string;
|
|
40
|
-
compiler: Compiler;
|
|
41
|
-
projectData: {
|
|
42
|
-
appDirectory: string;
|
|
43
|
-
};
|
|
44
|
-
sourceMaps?: boolean;
|
|
45
|
-
tsconfigPath: string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const runBabelCompiler = async (
|
|
49
|
-
willCompilerFiles: string[],
|
|
50
|
-
config: IBuildSourceCodeConfig,
|
|
51
|
-
babelConfig: BabelOptions = {},
|
|
52
|
-
) => {
|
|
53
|
-
const { srcRootDir, distDir } = config;
|
|
54
|
-
// TODO: 判断lynx模式下,修改distFileExtMap: {'js': 'js', 'jsx': 'jsx', 'ts': 'js', 'tsx': 'jsx'}
|
|
55
|
-
return babelCompiler.compiler(
|
|
56
|
-
{
|
|
57
|
-
quiet: true,
|
|
58
|
-
enableVirtualDist: true,
|
|
59
|
-
rootDir: srcRootDir,
|
|
60
|
-
filenames: willCompilerFiles,
|
|
61
|
-
distDir,
|
|
62
|
-
},
|
|
63
|
-
babelConfig,
|
|
64
|
-
);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const getWillCompilerCode = (
|
|
68
|
-
srcDirOrFile: string,
|
|
69
|
-
option: { tsconfig: ITsconfig | null; isTsProject: boolean },
|
|
70
|
-
) => {
|
|
71
|
-
const { tsconfig, isTsProject } = option;
|
|
72
|
-
// 如果是一个文件路径,则直接返回
|
|
73
|
-
if (fs.existsSync(srcDirOrFile) && fs.lstatSync(srcDirOrFile).isFile()) {
|
|
74
|
-
return [srcDirOrFile];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const getExts = (isTs: boolean) => {
|
|
78
|
-
// TODO: 是否受控tsconfig.json 里的jsx配置
|
|
79
|
-
let exts = [];
|
|
80
|
-
if (isTs) {
|
|
81
|
-
exts = tsconfig?.compilerOptions?.allowJs
|
|
82
|
-
? ['.ts', '.tsx', '.js', '.jsx']
|
|
83
|
-
: ['.ts', '.tsx'];
|
|
84
|
-
} else {
|
|
85
|
-
exts = ['.js', '.jsx'];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return exts;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const exts = getExts(isTsProject);
|
|
92
|
-
const globPattern = `${srcDirOrFile}/**/*{${exts.join(',')}}`;
|
|
93
|
-
const files = glob.sync(globPattern, {
|
|
94
|
-
ignore: [`${srcDirOrFile}/**/*.d.ts`],
|
|
95
|
-
absolute: true,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
return files;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
export const buildSourceCode = async (config: IBuildSourceCodeConfig) => {
|
|
102
|
-
const {
|
|
103
|
-
compiler,
|
|
104
|
-
willCompilerDirOrFile,
|
|
105
|
-
tsconfigPath,
|
|
106
|
-
sourceMaps,
|
|
107
|
-
babelConfig,
|
|
108
|
-
} = config;
|
|
109
|
-
const tsconfig = ts.readTsConfig(tsconfigPath);
|
|
110
|
-
const willCompilerFiles = getWillCompilerCode(willCompilerDirOrFile, {
|
|
111
|
-
tsconfig,
|
|
112
|
-
isTsProject: Boolean(tsconfig),
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
let distSet = null;
|
|
116
|
-
if (compiler === Compiler.babel) {
|
|
117
|
-
distSet = await runBabelCompiler(willCompilerFiles, config, {
|
|
118
|
-
...babelConfig,
|
|
119
|
-
sourceMaps,
|
|
120
|
-
});
|
|
121
|
-
} else {
|
|
122
|
-
distSet = {
|
|
123
|
-
code: 1,
|
|
124
|
-
message: 'no compiler',
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return distSet;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const generatorRealFiles = (virtualDists: IVirtualDist[]) => {
|
|
132
|
-
for (const virtualDist of virtualDists) {
|
|
133
|
-
const { distPath, code, sourcemap, sourceMapPath } = virtualDist;
|
|
134
|
-
fs.ensureFileSync(distPath);
|
|
135
|
-
fs.writeFileSync(distPath, code);
|
|
136
|
-
if (sourcemap.length > 0) {
|
|
137
|
-
fs.ensureFileSync(sourceMapPath);
|
|
138
|
-
fs.writeFileSync(sourceMapPath, sourcemap);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
interface ITaskConfig {
|
|
144
|
-
srcRootDir: string; // 源码的根目录
|
|
145
|
-
willCompilerDirOrFile: string; // 用于编译的源码文件或者源码目录
|
|
146
|
-
distDir: string;
|
|
147
|
-
appDirectory: string;
|
|
148
|
-
sourceMaps: boolean;
|
|
149
|
-
syntax: 'es5' | 'es6+';
|
|
150
|
-
type: 'module' | 'commonjs';
|
|
151
|
-
tsconfigPath: string;
|
|
152
|
-
copyDirs?: string;
|
|
153
|
-
compiler?: 'babel' | 'esbuild' | 'swc';
|
|
154
|
-
watch: boolean;
|
|
155
|
-
}
|
|
156
|
-
const defaultConfig: ITaskConfig = {
|
|
157
|
-
srcRootDir: `${process.cwd()}/src`,
|
|
158
|
-
willCompilerDirOrFile: `${process.cwd()}/src`,
|
|
159
|
-
distDir: './dist/js/temp',
|
|
160
|
-
compiler: 'babel',
|
|
161
|
-
appDirectory: '',
|
|
162
|
-
sourceMaps: false,
|
|
163
|
-
tsconfigPath: '',
|
|
164
|
-
syntax: 'es5',
|
|
165
|
-
type: 'module',
|
|
166
|
-
watch: false,
|
|
167
|
-
};
|
|
168
|
-
const taskMain = async ({
|
|
169
|
-
modernConfig,
|
|
170
|
-
}: {
|
|
171
|
-
modernConfig: NormalizedConfig;
|
|
172
|
-
}) => {
|
|
173
|
-
// Execution of the script's parameter handling and related required configuration acquisition
|
|
174
|
-
const processArgv = argv(process.argv.slice(2));
|
|
175
|
-
const config = processArgv<ITaskConfig>(defaultConfig);
|
|
176
|
-
process.env.BUILD_FORMAT = initEnv(config);
|
|
177
|
-
const compiler = Compiler.babel; // Currently, only babel is supported.
|
|
178
|
-
const babelConfig = bc.resolveBabelConfig(config.appDirectory, modernConfig, {
|
|
179
|
-
sourceAbsDir: config.srcRootDir,
|
|
180
|
-
tsconfigPath: config.tsconfigPath,
|
|
181
|
-
syntax: config.syntax,
|
|
182
|
-
type: config.type,
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
const {
|
|
186
|
-
code,
|
|
187
|
-
message,
|
|
188
|
-
messageDetails,
|
|
189
|
-
virtualDists = [],
|
|
190
|
-
} = await buildSourceCode({
|
|
191
|
-
distDir: config.distDir,
|
|
192
|
-
srcRootDir: config.srcRootDir,
|
|
193
|
-
willCompilerDirOrFile: config.willCompilerDirOrFile,
|
|
194
|
-
sourceMaps: config.sourceMaps,
|
|
195
|
-
compiler,
|
|
196
|
-
projectData: { appDirectory: config.appDirectory },
|
|
197
|
-
tsconfigPath: config.tsconfigPath,
|
|
198
|
-
babelConfig,
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
if (code === 0) {
|
|
202
|
-
generatorRealFiles(virtualDists);
|
|
203
|
-
// 执行成功log使用 console.info
|
|
204
|
-
console.info('[Babel Compiler]: Successfully');
|
|
205
|
-
} else if (messageDetails && messageDetails.length > 0) {
|
|
206
|
-
console.error(message);
|
|
207
|
-
for (const detail of messageDetails || []) {
|
|
208
|
-
console.error(detail.content);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (code === 0 && config.copyDirs) {
|
|
213
|
-
const copyList = config.copyDirs.split(',');
|
|
214
|
-
for (const copyDir of copyList) {
|
|
215
|
-
fs.ensureDirSync(copyDir);
|
|
216
|
-
fs.copySync(config.distDir, copyDir);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
(async () => {
|
|
222
|
-
let options: CoreOptions | undefined;
|
|
223
|
-
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
224
|
-
({ options } = require(process.env.CORE_INIT_OPTION_FILE));
|
|
225
|
-
}
|
|
226
|
-
const { resolved } = await core.cli.init([], options);
|
|
227
|
-
await core.manager.run(async () => {
|
|
228
|
-
try {
|
|
229
|
-
await taskMain({ modernConfig: resolved });
|
|
230
|
-
} catch (e) {
|
|
231
|
-
console.error(e);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
})();
|
package/src/tasks/build-style.ts
DELETED
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import { fs, Import } from '@modern-js/utils';
|
|
3
|
-
import type {
|
|
4
|
-
NormalizedConfig,
|
|
5
|
-
IAppContext,
|
|
6
|
-
CoreOptions,
|
|
7
|
-
} from '@modern-js/core';
|
|
8
|
-
import type { ICompilerResult, PostcssOption } from '@modern-js/style-compiler';
|
|
9
|
-
import type { ModuleToolsOutput } from '../types';
|
|
10
|
-
|
|
11
|
-
const cssConfig: typeof import('@modern-js/css-config') = Import.lazy(
|
|
12
|
-
'@modern-js/css-config',
|
|
13
|
-
require,
|
|
14
|
-
);
|
|
15
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
16
|
-
'@modern-js/core',
|
|
17
|
-
require,
|
|
18
|
-
);
|
|
19
|
-
const compiler: typeof import('@modern-js/style-compiler') = Import.lazy(
|
|
20
|
-
'@modern-js/style-compiler',
|
|
21
|
-
require,
|
|
22
|
-
);
|
|
23
|
-
const glob: typeof import('glob') = Import.lazy('glob', require);
|
|
24
|
-
const hooks: typeof import('@modern-js/module-tools-hooks') = Import.lazy(
|
|
25
|
-
'@modern-js/module-tools-hooks',
|
|
26
|
-
require,
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
const STYLE_DIRS = 'styles';
|
|
30
|
-
const SRC_STYLE_DIRS = 'src';
|
|
31
|
-
|
|
32
|
-
const checkStylesDirExist = (option: { appDirectory: string }) => {
|
|
33
|
-
const { appDirectory } = option;
|
|
34
|
-
return fs.existsSync(path.join(appDirectory, STYLE_DIRS));
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const generatorFileOrLogError = (
|
|
38
|
-
result: ICompilerResult,
|
|
39
|
-
successMessage = '',
|
|
40
|
-
) => {
|
|
41
|
-
if (result.code === 0 && result.dists.length > 0) {
|
|
42
|
-
for (const file of result.dists) {
|
|
43
|
-
fs.ensureFileSync(file.filename);
|
|
44
|
-
fs.writeFileSync(file.filename, file.content);
|
|
45
|
-
}
|
|
46
|
-
if (successMessage) {
|
|
47
|
-
console.info(successMessage);
|
|
48
|
-
}
|
|
49
|
-
} else {
|
|
50
|
-
for (const file of result.errors) {
|
|
51
|
-
console.error(file.error);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const getPostcssOption = (
|
|
57
|
-
appDirectory: string,
|
|
58
|
-
modernConfig: NormalizedConfig,
|
|
59
|
-
): PostcssOption => {
|
|
60
|
-
const postcssOption = cssConfig.getPostcssConfig(
|
|
61
|
-
appDirectory,
|
|
62
|
-
modernConfig,
|
|
63
|
-
false,
|
|
64
|
-
);
|
|
65
|
-
return {
|
|
66
|
-
plugins: postcssOption?.postcssOptions?.plugins || [],
|
|
67
|
-
enableSourceMap: (postcssOption as any)?.sourceMap || false,
|
|
68
|
-
options: {},
|
|
69
|
-
} as any;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const copyOriginStyleFiles = ({
|
|
73
|
-
targetDir,
|
|
74
|
-
outputDir,
|
|
75
|
-
}: {
|
|
76
|
-
targetDir: string;
|
|
77
|
-
outputDir: string;
|
|
78
|
-
}) => {
|
|
79
|
-
const styleFiles = glob.sync(`${targetDir}/**/*.{css,sass,scss,less}`);
|
|
80
|
-
if (styleFiles.length > 0) {
|
|
81
|
-
fs.ensureDirSync(outputDir);
|
|
82
|
-
}
|
|
83
|
-
for (const styleFile of styleFiles) {
|
|
84
|
-
const file = path.relative(targetDir, styleFile);
|
|
85
|
-
fs.ensureDirSync(path.dirname(path.join(outputDir, file)));
|
|
86
|
-
fs.copyFileSync(styleFile, path.join(outputDir, file));
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const taskMain = async ({
|
|
91
|
-
modernConfig,
|
|
92
|
-
appContext,
|
|
93
|
-
}: {
|
|
94
|
-
modernConfig: NormalizedConfig;
|
|
95
|
-
appContext: IAppContext;
|
|
96
|
-
}) => {
|
|
97
|
-
const {
|
|
98
|
-
assetsPath = 'styles',
|
|
99
|
-
path: outputPath = 'dist',
|
|
100
|
-
jsPath = 'js',
|
|
101
|
-
} = modernConfig.output as ModuleToolsOutput;
|
|
102
|
-
const { appDirectory } = appContext;
|
|
103
|
-
|
|
104
|
-
const lessOption = await core
|
|
105
|
-
.mountHook()
|
|
106
|
-
.moduleLessConfig(
|
|
107
|
-
{ modernConfig },
|
|
108
|
-
{ onLast: async (_: any) => undefined },
|
|
109
|
-
);
|
|
110
|
-
const sassOption = await core
|
|
111
|
-
.mountHook()
|
|
112
|
-
.moduleSassConfig(
|
|
113
|
-
{ modernConfig },
|
|
114
|
-
{ onLast: async (_: any) => undefined },
|
|
115
|
-
);
|
|
116
|
-
const tailwindPlugin = await core
|
|
117
|
-
.mountHook()
|
|
118
|
-
.moduleTailwindConfig(
|
|
119
|
-
{ modernConfig },
|
|
120
|
-
{ onLast: async (_: any) => undefined },
|
|
121
|
-
);
|
|
122
|
-
const postcssOption = getPostcssOption(appDirectory, modernConfig);
|
|
123
|
-
if (tailwindPlugin) {
|
|
124
|
-
postcssOption.plugins?.push(tailwindPlugin);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const { importStyle } = modernConfig.output as ModuleToolsOutput;
|
|
128
|
-
const existStylesDir = checkStylesDirExist({ appDirectory });
|
|
129
|
-
// 编译 styles 目录样式
|
|
130
|
-
if (existStylesDir) {
|
|
131
|
-
const styleResult = await compiler.styleCompiler({
|
|
132
|
-
projectDir: appDirectory,
|
|
133
|
-
stylesDir: path.resolve(appDirectory, STYLE_DIRS),
|
|
134
|
-
outDir: path.join(appDirectory, outputPath, assetsPath),
|
|
135
|
-
enableVirtualDist: true,
|
|
136
|
-
compilerOption: {
|
|
137
|
-
less: lessOption,
|
|
138
|
-
sass: sassOption,
|
|
139
|
-
postcss: postcssOption,
|
|
140
|
-
},
|
|
141
|
-
});
|
|
142
|
-
generatorFileOrLogError(
|
|
143
|
-
styleResult!,
|
|
144
|
-
`[Style Compiler] Successfully for 'styles' dir`,
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// 编译 src 内的样式代码
|
|
149
|
-
const srcDir = path.resolve(appDirectory, SRC_STYLE_DIRS);
|
|
150
|
-
const outputDirtoSrc = path.join(
|
|
151
|
-
appDirectory,
|
|
152
|
-
outputPath,
|
|
153
|
-
jsPath,
|
|
154
|
-
assetsPath,
|
|
155
|
-
);
|
|
156
|
-
if (importStyle === 'compiled-code') {
|
|
157
|
-
const srcStyleResult = await compiler.styleCompiler({
|
|
158
|
-
projectDir: appDirectory,
|
|
159
|
-
stylesDir: srcDir,
|
|
160
|
-
outDir: outputDirtoSrc,
|
|
161
|
-
enableVirtualDist: true,
|
|
162
|
-
compilerOption: {
|
|
163
|
-
less: lessOption,
|
|
164
|
-
sass: sassOption,
|
|
165
|
-
postcss: postcssOption,
|
|
166
|
-
},
|
|
167
|
-
});
|
|
168
|
-
generatorFileOrLogError(
|
|
169
|
-
srcStyleResult!,
|
|
170
|
-
`[Style Compiler] Successfully for 'src' dir`,
|
|
171
|
-
);
|
|
172
|
-
} else {
|
|
173
|
-
copyOriginStyleFiles({ targetDir: srcDir, outputDir: outputDirtoSrc });
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
(async () => {
|
|
178
|
-
let options: CoreOptions | undefined;
|
|
179
|
-
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
180
|
-
({ options } = require(process.env.CORE_INIT_OPTION_FILE));
|
|
181
|
-
}
|
|
182
|
-
hooks.buildLifeCycle();
|
|
183
|
-
const { resolved: modernConfig, appContext } = await core.cli.init(
|
|
184
|
-
[],
|
|
185
|
-
options,
|
|
186
|
-
);
|
|
187
|
-
await core.manager.run(async () => {
|
|
188
|
-
try {
|
|
189
|
-
await taskMain({ modernConfig, appContext });
|
|
190
|
-
} catch (e: any) {
|
|
191
|
-
console.error(e.toString());
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
})();
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { Import, fs } from '@modern-js/utils';
|
|
2
|
-
import type { NormalizedConfig, CoreOptions } from '@modern-js/core';
|
|
3
|
-
import type { ICompilerResult, IVirtualDist } from '@modern-js/babel-compiler';
|
|
4
|
-
import type { ITsconfig } from '../types';
|
|
5
|
-
import { initEnv } from '../utils/init-env';
|
|
6
|
-
|
|
7
|
-
const babelCompiler: typeof import('@modern-js/babel-compiler') = Import.lazy(
|
|
8
|
-
'@modern-js/babel-compiler',
|
|
9
|
-
require,
|
|
10
|
-
);
|
|
11
|
-
const logger: typeof import('../features/build/logger') = Import.lazy(
|
|
12
|
-
'../features/build/logger',
|
|
13
|
-
require,
|
|
14
|
-
);
|
|
15
|
-
const ts: typeof import('../utils/tsconfig') = Import.lazy(
|
|
16
|
-
'../utils/tsconfig',
|
|
17
|
-
require,
|
|
18
|
-
);
|
|
19
|
-
const babel: typeof import('../utils/babel') = Import.lazy(
|
|
20
|
-
'../utils/babel',
|
|
21
|
-
require,
|
|
22
|
-
);
|
|
23
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
24
|
-
'@modern-js/core',
|
|
25
|
-
require,
|
|
26
|
-
);
|
|
27
|
-
const argv: typeof import('process.argv').default = Import.lazy(
|
|
28
|
-
'process.argv',
|
|
29
|
-
require,
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
const generatorRealFiles = (virtualDists: IVirtualDist[]) => {
|
|
33
|
-
for (const virtualDist of virtualDists) {
|
|
34
|
-
const { distPath, code, sourcemap, sourceMapPath } = virtualDist;
|
|
35
|
-
fs.ensureFileSync(distPath);
|
|
36
|
-
fs.writeFileSync(distPath, code);
|
|
37
|
-
if (sourcemap.length > 0) {
|
|
38
|
-
fs.ensureFileSync(sourceMapPath);
|
|
39
|
-
fs.writeFileSync(sourceMapPath, sourcemap);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const runBabelCompiler = async (
|
|
45
|
-
config: ITaskConfig,
|
|
46
|
-
modernConfig: NormalizedConfig,
|
|
47
|
-
) => {
|
|
48
|
-
const { tsconfigPath } = config;
|
|
49
|
-
const babelConfig = babel.resolveBabelConfig(
|
|
50
|
-
config.appDirectory,
|
|
51
|
-
modernConfig,
|
|
52
|
-
{
|
|
53
|
-
sourceAbsDir: config.srcRootDir,
|
|
54
|
-
tsconfigPath,
|
|
55
|
-
syntax: config.syntax,
|
|
56
|
-
type: config.type,
|
|
57
|
-
},
|
|
58
|
-
);
|
|
59
|
-
const tsconfig = ts.readTsConfig<ITsconfig>(tsconfigPath || '', {});
|
|
60
|
-
const isTs = Boolean(tsconfig);
|
|
61
|
-
|
|
62
|
-
const getExts = (isTsProject: boolean) => {
|
|
63
|
-
// TODO: 是否受控tsconfig.json 里的jsx配置
|
|
64
|
-
let exts = [];
|
|
65
|
-
if (isTsProject) {
|
|
66
|
-
exts = tsconfig?.compilerOptions?.allowJs
|
|
67
|
-
? ['.ts', '.tsx', '.js', '.jsx']
|
|
68
|
-
: ['.ts', '.tsx'];
|
|
69
|
-
} else {
|
|
70
|
-
exts = ['.js', '.jsx'];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return exts;
|
|
74
|
-
};
|
|
75
|
-
const emitter = await babelCompiler.compiler(
|
|
76
|
-
{
|
|
77
|
-
enableVirtualDist: true,
|
|
78
|
-
quiet: true,
|
|
79
|
-
enableWatch: true,
|
|
80
|
-
rootDir: config.srcRootDir,
|
|
81
|
-
distDir: config.distDir,
|
|
82
|
-
watchDir: config.srcRootDir,
|
|
83
|
-
extensions: getExts(isTs),
|
|
84
|
-
},
|
|
85
|
-
{ ...babelConfig, sourceMaps: config.sourceMaps },
|
|
86
|
-
);
|
|
87
|
-
emitter.on(babelCompiler.BuildWatchEvent.compiling, () => {
|
|
88
|
-
console.info(logger.clearFlag, `Compiling...`);
|
|
89
|
-
});
|
|
90
|
-
emitter.on(
|
|
91
|
-
babelCompiler.BuildWatchEvent.firstCompiler,
|
|
92
|
-
(result: ICompilerResult) => {
|
|
93
|
-
if (result.code === 1) {
|
|
94
|
-
console.error(logger.clearFlag);
|
|
95
|
-
console.error(result.message);
|
|
96
|
-
for (const detail of result.messageDetails || []) {
|
|
97
|
-
console.error(detail.content);
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
generatorRealFiles(result.virtualDists!);
|
|
101
|
-
console.info(logger.clearFlag, '[Babel Compiler]: Successfully');
|
|
102
|
-
}
|
|
103
|
-
},
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
emitter.on(
|
|
107
|
-
babelCompiler.BuildWatchEvent.watchingCompiler,
|
|
108
|
-
(result: ICompilerResult) => {
|
|
109
|
-
if (result.code === 1) {
|
|
110
|
-
console.error(logger.clearFlag);
|
|
111
|
-
console.error(result.message);
|
|
112
|
-
for (const detail of result.messageDetails || []) {
|
|
113
|
-
console.error(detail.content);
|
|
114
|
-
}
|
|
115
|
-
if (
|
|
116
|
-
Array.isArray(result.virtualDists) &&
|
|
117
|
-
result.virtualDists?.length > 0
|
|
118
|
-
) {
|
|
119
|
-
generatorRealFiles(result.virtualDists);
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
generatorRealFiles(result.virtualDists!);
|
|
123
|
-
console.info(result.message);
|
|
124
|
-
}
|
|
125
|
-
},
|
|
126
|
-
);
|
|
127
|
-
await emitter.watch();
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
const buildSourceCode = async (
|
|
131
|
-
config: ITaskConfig,
|
|
132
|
-
modernConfig: NormalizedConfig,
|
|
133
|
-
) => {
|
|
134
|
-
const { compiler } = config;
|
|
135
|
-
if (compiler === 'babel') {
|
|
136
|
-
await runBabelCompiler(config, modernConfig);
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
interface ITaskConfig {
|
|
141
|
-
srcRootDir: string; // 源码的根目录
|
|
142
|
-
distDir: string;
|
|
143
|
-
appDirectory: string;
|
|
144
|
-
sourceMaps: boolean;
|
|
145
|
-
syntax: 'es5' | 'es6+';
|
|
146
|
-
type: 'module' | 'commonjs';
|
|
147
|
-
tsconfigPath: string;
|
|
148
|
-
copyDirs?: string;
|
|
149
|
-
compiler?: 'babel' | 'esbuild' | 'swc';
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const taskMain = async ({
|
|
153
|
-
modernConfig,
|
|
154
|
-
}: {
|
|
155
|
-
modernConfig: NormalizedConfig;
|
|
156
|
-
}) => {
|
|
157
|
-
const processArgv = argv(process.argv.slice(2));
|
|
158
|
-
const config = processArgv<ITaskConfig>({
|
|
159
|
-
srcRootDir: `${process.cwd()}/src`,
|
|
160
|
-
distDir: '',
|
|
161
|
-
compiler: 'babel',
|
|
162
|
-
appDirectory: '',
|
|
163
|
-
sourceMaps: false,
|
|
164
|
-
tsconfigPath: '',
|
|
165
|
-
syntax: 'es5',
|
|
166
|
-
type: 'module',
|
|
167
|
-
});
|
|
168
|
-
process.env.BUILD_FORMAT = initEnv(config);
|
|
169
|
-
|
|
170
|
-
await buildSourceCode(config, modernConfig);
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
(async () => {
|
|
174
|
-
let options: CoreOptions | undefined;
|
|
175
|
-
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
176
|
-
({ options } = require(process.env.CORE_INIT_OPTION_FILE));
|
|
177
|
-
}
|
|
178
|
-
const { resolved } = await core.cli.init([], options);
|
|
179
|
-
await core.manager.run(async () => {
|
|
180
|
-
try {
|
|
181
|
-
await taskMain({ modernConfig: resolved });
|
|
182
|
-
} catch (e) {
|
|
183
|
-
console.error(e);
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
})();
|