@modern-js/module-tools 1.3.0 → 1.3.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.
- package/CHANGELOG.md +23 -0
- package/package.json +15 -15
- 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 -52
- 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 -74
- 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,271 +0,0 @@
|
|
|
1
|
-
/* eslint-disable max-statements */
|
|
2
|
-
import * as path from 'path';
|
|
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 { fs, watch, WatchChangeType, Import } from '@modern-js/utils';
|
|
10
|
-
import type { ModuleToolsOutput } from '../types';
|
|
11
|
-
|
|
12
|
-
const logger: typeof import('../features/build/logger') = Import.lazy(
|
|
13
|
-
'../features/build/logger',
|
|
14
|
-
require,
|
|
15
|
-
);
|
|
16
|
-
const cssConfig: typeof import('@modern-js/css-config') = Import.lazy(
|
|
17
|
-
'@modern-js/css-config',
|
|
18
|
-
require,
|
|
19
|
-
);
|
|
20
|
-
const hooks: typeof import('@modern-js/module-tools-hooks') = Import.lazy(
|
|
21
|
-
'@modern-js/module-tools-hooks',
|
|
22
|
-
require,
|
|
23
|
-
);
|
|
24
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
25
|
-
'@modern-js/core',
|
|
26
|
-
require,
|
|
27
|
-
);
|
|
28
|
-
const compiler: typeof import('@modern-js/style-compiler') = Import.lazy(
|
|
29
|
-
'@modern-js/style-compiler',
|
|
30
|
-
require,
|
|
31
|
-
);
|
|
32
|
-
const glob: typeof import('glob') = Import.lazy('glob', require);
|
|
33
|
-
|
|
34
|
-
const STYLE_DIRS = 'styles';
|
|
35
|
-
const SRC_STYLE_DIRS = 'src';
|
|
36
|
-
|
|
37
|
-
const checkStylesDirExist = (option: { appDirectory: string }) => {
|
|
38
|
-
const { appDirectory } = option;
|
|
39
|
-
return fs.existsSync(path.join(appDirectory, STYLE_DIRS));
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const generatorFileAndReturnLog = (
|
|
43
|
-
result: ICompilerResult,
|
|
44
|
-
successMessage = '',
|
|
45
|
-
) => {
|
|
46
|
-
if (result.code === 0) {
|
|
47
|
-
for (const file of result.dists) {
|
|
48
|
-
fs.ensureFileSync(file.filename);
|
|
49
|
-
fs.writeFileSync(file.filename, file.content);
|
|
50
|
-
}
|
|
51
|
-
return successMessage;
|
|
52
|
-
} else {
|
|
53
|
-
return result.errors.join('\n');
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const getPostcssOption = (
|
|
58
|
-
appDirectory: string,
|
|
59
|
-
modernConfig: NormalizedConfig,
|
|
60
|
-
): PostcssOption => {
|
|
61
|
-
const postcssOption = cssConfig.getPostcssConfig(
|
|
62
|
-
appDirectory,
|
|
63
|
-
modernConfig,
|
|
64
|
-
false,
|
|
65
|
-
);
|
|
66
|
-
return {
|
|
67
|
-
plugins: postcssOption?.postcssOptions?.plugins || [],
|
|
68
|
-
enableSourceMap: (postcssOption as any)?.sourceMap || false,
|
|
69
|
-
options: {},
|
|
70
|
-
} as any;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const copyOriginStyleFiles = ({
|
|
74
|
-
targetDir,
|
|
75
|
-
outputDir,
|
|
76
|
-
}: {
|
|
77
|
-
targetDir: string;
|
|
78
|
-
outputDir: string;
|
|
79
|
-
}) => {
|
|
80
|
-
const styleFiles = glob.sync(`${targetDir}/**/*.{css,sass,scss,less}`);
|
|
81
|
-
if (styleFiles.length > 0) {
|
|
82
|
-
fs.ensureDirSync(outputDir);
|
|
83
|
-
}
|
|
84
|
-
for (const styleFile of styleFiles) {
|
|
85
|
-
const file = path.relative(targetDir, styleFile);
|
|
86
|
-
fs.ensureFileSync(path.join(outputDir, file));
|
|
87
|
-
fs.copyFileSync(styleFile, path.join(outputDir, file));
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const logCompilerMessage = (compilerMessage: {
|
|
92
|
-
src: string;
|
|
93
|
-
styles: string;
|
|
94
|
-
}) => {
|
|
95
|
-
console.info(logger.clearFlag);
|
|
96
|
-
console.info(compilerMessage.src);
|
|
97
|
-
console.info(compilerMessage.styles);
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const taskMain = async ({
|
|
101
|
-
modernConfig,
|
|
102
|
-
appContext,
|
|
103
|
-
}: {
|
|
104
|
-
modernConfig: NormalizedConfig;
|
|
105
|
-
appContext: IAppContext;
|
|
106
|
-
}) => {
|
|
107
|
-
const {
|
|
108
|
-
assetsPath = 'styles',
|
|
109
|
-
path: outputPath = 'dist',
|
|
110
|
-
jsPath = 'js',
|
|
111
|
-
importStyle,
|
|
112
|
-
} = modernConfig.output as ModuleToolsOutput;
|
|
113
|
-
const { appDirectory } = appContext;
|
|
114
|
-
|
|
115
|
-
const lessOption = await (core.mountHook() as any).moduleLessConfig(
|
|
116
|
-
{ modernConfig },
|
|
117
|
-
{
|
|
118
|
-
onLast: async (_: any) => null as any,
|
|
119
|
-
},
|
|
120
|
-
);
|
|
121
|
-
const sassOption = await (core.mountHook() as any).moduleSassConfig(
|
|
122
|
-
{ modernConfig },
|
|
123
|
-
{
|
|
124
|
-
onLast: async (_: any) => null as any,
|
|
125
|
-
},
|
|
126
|
-
);
|
|
127
|
-
const postcssOption = getPostcssOption(appDirectory, modernConfig);
|
|
128
|
-
const existStylesDir = checkStylesDirExist({ appDirectory });
|
|
129
|
-
const compilerMessage = {
|
|
130
|
-
src: '',
|
|
131
|
-
styles: '',
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
// 编译 styles 目录样式
|
|
135
|
-
let styleEmitter = null;
|
|
136
|
-
if (existStylesDir) {
|
|
137
|
-
styleEmitter = compiler.styleCompiler({
|
|
138
|
-
watch: true,
|
|
139
|
-
projectDir: appDirectory,
|
|
140
|
-
stylesDir: path.resolve(appDirectory, STYLE_DIRS),
|
|
141
|
-
outDir: path.join(appDirectory, outputPath, assetsPath),
|
|
142
|
-
enableVirtualDist: true,
|
|
143
|
-
compilerOption: {
|
|
144
|
-
less: lessOption,
|
|
145
|
-
sass: sassOption,
|
|
146
|
-
postcss: postcssOption,
|
|
147
|
-
},
|
|
148
|
-
});
|
|
149
|
-
styleEmitter.on(
|
|
150
|
-
compiler.BuildWatchEvent.firstCompiler,
|
|
151
|
-
(styleResult: ICompilerResult) => {
|
|
152
|
-
compilerMessage.styles = generatorFileAndReturnLog(
|
|
153
|
-
styleResult,
|
|
154
|
-
`[Style Compiler] Successfully for 'styles' dir`,
|
|
155
|
-
);
|
|
156
|
-
logCompilerMessage(compilerMessage);
|
|
157
|
-
},
|
|
158
|
-
);
|
|
159
|
-
styleEmitter.on(compiler.BuildWatchEvent.compilering, () => {
|
|
160
|
-
compilerMessage.styles = `[${assetsPath}] Compiling...`;
|
|
161
|
-
logCompilerMessage(compilerMessage);
|
|
162
|
-
});
|
|
163
|
-
styleEmitter.on(
|
|
164
|
-
compiler.BuildWatchEvent.watchingCompiler,
|
|
165
|
-
(styleResult: ICompilerResult) => {
|
|
166
|
-
compilerMessage.styles = generatorFileAndReturnLog(
|
|
167
|
-
styleResult,
|
|
168
|
-
`[Style Compiler] Successfully for 'styles' dir`,
|
|
169
|
-
);
|
|
170
|
-
logCompilerMessage(compilerMessage);
|
|
171
|
-
},
|
|
172
|
-
);
|
|
173
|
-
// await styleEmitter.watch();
|
|
174
|
-
}
|
|
175
|
-
// 编译 src 内的样式代码
|
|
176
|
-
const srcDir = path.resolve(appDirectory, SRC_STYLE_DIRS);
|
|
177
|
-
const outputDirToSrc = path.join(
|
|
178
|
-
appDirectory,
|
|
179
|
-
outputPath,
|
|
180
|
-
jsPath,
|
|
181
|
-
assetsPath,
|
|
182
|
-
);
|
|
183
|
-
if (importStyle === 'compiled-code') {
|
|
184
|
-
compilerMessage.src = `[src] Compiling`;
|
|
185
|
-
const srcStyleEmitter = compiler.styleCompiler({
|
|
186
|
-
watch: true,
|
|
187
|
-
projectDir: appDirectory,
|
|
188
|
-
stylesDir: srcDir,
|
|
189
|
-
outDir: outputDirToSrc,
|
|
190
|
-
enableVirtualDist: true,
|
|
191
|
-
compilerOption: {
|
|
192
|
-
less: lessOption,
|
|
193
|
-
sass: sassOption,
|
|
194
|
-
postcss: postcssOption,
|
|
195
|
-
},
|
|
196
|
-
});
|
|
197
|
-
srcStyleEmitter.on(
|
|
198
|
-
compiler.BuildWatchEvent.firstCompiler,
|
|
199
|
-
(srcStyleResult: ICompilerResult) => {
|
|
200
|
-
compilerMessage.src = generatorFileAndReturnLog(
|
|
201
|
-
srcStyleResult,
|
|
202
|
-
`[Style Compiler] Successfully for 'src' dir`,
|
|
203
|
-
);
|
|
204
|
-
logCompilerMessage(compilerMessage);
|
|
205
|
-
},
|
|
206
|
-
);
|
|
207
|
-
srcStyleEmitter.on(compiler.BuildWatchEvent.compilering, () => {
|
|
208
|
-
compilerMessage.src = `[src] Compiling`;
|
|
209
|
-
logCompilerMessage(compilerMessage);
|
|
210
|
-
});
|
|
211
|
-
srcStyleEmitter.on(
|
|
212
|
-
compiler.BuildWatchEvent.watchingCompiler,
|
|
213
|
-
(srcStyleResult: ICompilerResult) => {
|
|
214
|
-
compilerMessage.src = generatorFileAndReturnLog(
|
|
215
|
-
srcStyleResult,
|
|
216
|
-
`[Style Compiler] Successfully for 'src' dir`,
|
|
217
|
-
);
|
|
218
|
-
logCompilerMessage(compilerMessage);
|
|
219
|
-
},
|
|
220
|
-
);
|
|
221
|
-
styleEmitter && (await styleEmitter.watch());
|
|
222
|
-
await srcStyleEmitter.watch();
|
|
223
|
-
} else {
|
|
224
|
-
compilerMessage.src = `['src' dir] Copying in progress`;
|
|
225
|
-
styleEmitter && (await styleEmitter.watch());
|
|
226
|
-
logCompilerMessage(compilerMessage);
|
|
227
|
-
copyOriginStyleFiles({ targetDir: srcDir, outputDir: outputDirToSrc });
|
|
228
|
-
compilerMessage.src = `[Style Compiler] Successfully for 'src' dir`;
|
|
229
|
-
logCompilerMessage(compilerMessage);
|
|
230
|
-
watch(
|
|
231
|
-
`${srcDir}/**/*.{css,less,sass,scss}`,
|
|
232
|
-
({ changeType, changedFilePath }) => {
|
|
233
|
-
compilerMessage.src = `['src' dir] Copying in progress`;
|
|
234
|
-
logCompilerMessage(compilerMessage);
|
|
235
|
-
if (changeType === WatchChangeType.UNLINK) {
|
|
236
|
-
const removeFile = path.normalize(
|
|
237
|
-
`${outputDirToSrc}/${path.relative(srcDir, changedFilePath)}`,
|
|
238
|
-
);
|
|
239
|
-
fs.removeSync(removeFile);
|
|
240
|
-
} else {
|
|
241
|
-
copyOriginStyleFiles({
|
|
242
|
-
targetDir: srcDir,
|
|
243
|
-
outputDir: outputDirToSrc,
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
compilerMessage.src = `[Style Compiler] Successfully for 'src' dir`;
|
|
247
|
-
logCompilerMessage(compilerMessage);
|
|
248
|
-
},
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
(async () => {
|
|
254
|
-
let options: CoreOptions | undefined;
|
|
255
|
-
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
256
|
-
({ options } = require(process.env.CORE_INIT_OPTION_FILE));
|
|
257
|
-
}
|
|
258
|
-
hooks.buildLifeCycle();
|
|
259
|
-
const { resolved: modernConfig, appContext } = await core.cli.init(
|
|
260
|
-
[],
|
|
261
|
-
options,
|
|
262
|
-
);
|
|
263
|
-
await core.manager.run(async () => {
|
|
264
|
-
try {
|
|
265
|
-
await taskMain({ modernConfig, appContext });
|
|
266
|
-
} catch (e: any) {
|
|
267
|
-
console.error(e);
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
})();
|
|
271
|
-
/* eslint-enable max-statements */
|
package/src/tasks/constants.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const tempTsconfigName = './tsconfig.temp.json';
|
package/src/tasks/copy-assets.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import { fs, watch, WatchChangeType, Import } from '@modern-js/utils';
|
|
3
|
-
import type {
|
|
4
|
-
NormalizedConfig,
|
|
5
|
-
IAppContext,
|
|
6
|
-
CoreOptions,
|
|
7
|
-
} from '@modern-js/core';
|
|
8
|
-
import type { ModuleToolsOutput } from '../types';
|
|
9
|
-
|
|
10
|
-
const argv: typeof import('process.argv').default = Import.lazy(
|
|
11
|
-
'process.argv',
|
|
12
|
-
require,
|
|
13
|
-
);
|
|
14
|
-
const glob: typeof import('glob') = Import.lazy('glob', require);
|
|
15
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
16
|
-
'@modern-js/core',
|
|
17
|
-
require,
|
|
18
|
-
);
|
|
19
|
-
const copyUtils: typeof import('../utils/copy') = Import.lazy(
|
|
20
|
-
'../utils/copy',
|
|
21
|
-
require,
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const STYLE_DIRS = 'styles';
|
|
25
|
-
const SRC_DIRS = 'src';
|
|
26
|
-
|
|
27
|
-
const copyAssets = ({
|
|
28
|
-
targetDir,
|
|
29
|
-
outputDir,
|
|
30
|
-
}: {
|
|
31
|
-
targetDir: string;
|
|
32
|
-
outputDir: string;
|
|
33
|
-
}) => {
|
|
34
|
-
const assetsFiles = glob.sync(`${targetDir}/**/*.*`, {
|
|
35
|
-
ignore: ['**/*.{js,jsx,ts,tsx,d.ts,scss,less,css,sass}'],
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
if (assetsFiles.length > 0) {
|
|
39
|
-
fs.ensureDirSync(outputDir);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
for (const resource of assetsFiles) {
|
|
43
|
-
const file = path.relative(targetDir, resource);
|
|
44
|
-
fs.ensureDirSync(path.dirname(path.resolve(outputDir, file)));
|
|
45
|
-
fs.copyFileSync(resource, path.resolve(outputDir, file));
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const watchAssets = ({
|
|
50
|
-
targetDir,
|
|
51
|
-
outputDir,
|
|
52
|
-
}: {
|
|
53
|
-
targetDir: string;
|
|
54
|
-
outputDir: string;
|
|
55
|
-
}) => {
|
|
56
|
-
watch(
|
|
57
|
-
`${targetDir}/**/*.*`,
|
|
58
|
-
({ changeType, changedFilePath }) => {
|
|
59
|
-
if (changeType === WatchChangeType.UNLINK) {
|
|
60
|
-
const removeFile = path.normalize(
|
|
61
|
-
`${outputDir}/${path.relative(targetDir, changedFilePath)}`,
|
|
62
|
-
);
|
|
63
|
-
fs.removeSync(removeFile);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
const file = path.relative(targetDir, changedFilePath);
|
|
67
|
-
fs.copyFileSync(changedFilePath, path.resolve(outputDir, file));
|
|
68
|
-
},
|
|
69
|
-
['**/*.{js,jsx,ts,tsx,d.ts,scss,less,css,sass}'],
|
|
70
|
-
);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const taskMain = ({
|
|
74
|
-
modernConfig,
|
|
75
|
-
appContext,
|
|
76
|
-
}: {
|
|
77
|
-
modernConfig: NormalizedConfig;
|
|
78
|
-
appContext: IAppContext;
|
|
79
|
-
}) => {
|
|
80
|
-
const processArgv = argv(process.argv.slice(2));
|
|
81
|
-
const config = processArgv<{ watch?: boolean }>({});
|
|
82
|
-
const { appDirectory } = appContext;
|
|
83
|
-
const {
|
|
84
|
-
jsPath = 'js',
|
|
85
|
-
assetsPath = 'styles',
|
|
86
|
-
path: outputPath = 'dist',
|
|
87
|
-
} = modernConfig.output as ModuleToolsOutput;
|
|
88
|
-
const srcDir = path.join(appDirectory, SRC_DIRS);
|
|
89
|
-
const outputDirToSrc = path.join(
|
|
90
|
-
appDirectory,
|
|
91
|
-
outputPath,
|
|
92
|
-
jsPath,
|
|
93
|
-
assetsPath,
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
const styleDir = path.join(appDirectory, STYLE_DIRS);
|
|
97
|
-
const outputDirToStyle = path.join(appDirectory, outputPath, assetsPath);
|
|
98
|
-
copyAssets({ targetDir: srcDir, outputDir: outputDirToSrc });
|
|
99
|
-
copyAssets({ targetDir: styleDir, outputDir: outputDirToStyle });
|
|
100
|
-
copyUtils.copyTask({ modernConfig, appContext });
|
|
101
|
-
if (config.watch) {
|
|
102
|
-
watchAssets({ targetDir: srcDir, outputDir: outputDirToSrc });
|
|
103
|
-
watchAssets({ targetDir: styleDir, outputDir: outputDirToStyle });
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
(async () => {
|
|
108
|
-
let options: CoreOptions | undefined;
|
|
109
|
-
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
110
|
-
({ options } = require(process.env.CORE_INIT_OPTION_FILE));
|
|
111
|
-
}
|
|
112
|
-
const { resolved: modernConfig, appContext } = await core.cli.init(
|
|
113
|
-
[],
|
|
114
|
-
options,
|
|
115
|
-
);
|
|
116
|
-
core.manager.run(() => {
|
|
117
|
-
try {
|
|
118
|
-
taskMain({ modernConfig, appContext });
|
|
119
|
-
} catch (e: any) {
|
|
120
|
-
console.error(e.message);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
})();
|
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
import type { ChildProcess } from 'child_process';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import * as os from 'os';
|
|
4
|
-
import { Import, fs } from '@modern-js/utils';
|
|
5
|
-
import type { NormalizedConfig, CoreOptions } from '@modern-js/core';
|
|
6
|
-
import type { ITsconfig } from '../types';
|
|
7
|
-
|
|
8
|
-
const tsPathsTransform: typeof import('../utils/tspaths-transform') =
|
|
9
|
-
Import.lazy('../utils/tspaths-transform', require);
|
|
10
|
-
const babel: typeof import('../utils/babel') = Import.lazy(
|
|
11
|
-
'../utils/babel',
|
|
12
|
-
require,
|
|
13
|
-
);
|
|
14
|
-
const constants: typeof import('./constants') = Import.lazy(
|
|
15
|
-
'./constants',
|
|
16
|
-
require,
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
20
|
-
'@modern-js/core',
|
|
21
|
-
require,
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const execa: typeof import('execa') = Import.lazy('execa', require);
|
|
25
|
-
const JSON5: typeof import('json5') = Import.lazy('json5', require);
|
|
26
|
-
const argv: typeof import('process.argv').default = Import.lazy(
|
|
27
|
-
'process.argv',
|
|
28
|
-
require,
|
|
29
|
-
);
|
|
30
|
-
const deepMerge: typeof import('lodash.merge') = Import.lazy(
|
|
31
|
-
'lodash.merge',
|
|
32
|
-
require,
|
|
33
|
-
);
|
|
34
|
-
const glob: typeof import('glob') = Import.lazy('glob', require);
|
|
35
|
-
|
|
36
|
-
let removeTsconfigPath = '';
|
|
37
|
-
|
|
38
|
-
const generatorTsConfig = (
|
|
39
|
-
projectTsconfig: ITsconfig,
|
|
40
|
-
{
|
|
41
|
-
appDirectory,
|
|
42
|
-
distDir,
|
|
43
|
-
sourceDir = 'src',
|
|
44
|
-
}: { appDirectory: string; distDir: string; sourceDir?: string },
|
|
45
|
-
) => {
|
|
46
|
-
const tempPath = path.resolve(appDirectory, './node_modules');
|
|
47
|
-
const resolvePath = path.relative(tempPath, appDirectory);
|
|
48
|
-
// const rootDir = projectTsconfig.compilerOptions?.rootDir
|
|
49
|
-
// ? path.join(resolvePath, projectTsconfig.compilerOptions?.rootDir)
|
|
50
|
-
// : resolvePath;
|
|
51
|
-
// 目前限制 rootDir 的路径为 sourceDir
|
|
52
|
-
const rootDir = path.join(resolvePath, sourceDir);
|
|
53
|
-
const baseUrl = projectTsconfig.compilerOptions?.baseUrl
|
|
54
|
-
? path.join(appDirectory, projectTsconfig.compilerOptions?.baseUrl)
|
|
55
|
-
: appDirectory;
|
|
56
|
-
// if include = ['src'], final include should be ['../src']
|
|
57
|
-
const include = projectTsconfig.include?.map(includePath =>
|
|
58
|
-
path.join(resolvePath, includePath),
|
|
59
|
-
);
|
|
60
|
-
const exclude = projectTsconfig.exclude?.map(excludePath =>
|
|
61
|
-
path.join(resolvePath, excludePath),
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
const resetConfig: ITsconfig = {
|
|
65
|
-
compilerOptions: {
|
|
66
|
-
rootDir,
|
|
67
|
-
baseUrl,
|
|
68
|
-
// Ensure that .d.ts files are created by tsc, but not .js files
|
|
69
|
-
declaration: true,
|
|
70
|
-
emitDeclarationOnly: true,
|
|
71
|
-
outDir: distDir,
|
|
72
|
-
},
|
|
73
|
-
include,
|
|
74
|
-
exclude,
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const recommendOption = {
|
|
78
|
-
// Ensure that Babel can safely transpile files in the TypeScript project
|
|
79
|
-
compilerOptions: {
|
|
80
|
-
isolatedModules: true,
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const tempTsconfigPath = path.join(tempPath, constants.tempTsconfigName);
|
|
85
|
-
fs.ensureFileSync(tempTsconfigPath);
|
|
86
|
-
fs.writeJSONSync(
|
|
87
|
-
tempTsconfigPath,
|
|
88
|
-
deepMerge(
|
|
89
|
-
recommendOption,
|
|
90
|
-
projectTsconfig,
|
|
91
|
-
// 此处是必须要覆盖用户默认配置
|
|
92
|
-
resetConfig,
|
|
93
|
-
),
|
|
94
|
-
);
|
|
95
|
-
return tempTsconfigPath;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const getProjectTsconfig = (tsconfigPath: string | undefined): ITsconfig => {
|
|
99
|
-
if (!tsconfigPath || !fs.existsSync(tsconfigPath)) {
|
|
100
|
-
return {};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return JSON5.parse(fs.readFileSync(tsconfigPath, 'utf-8'));
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const resolveLog = (
|
|
107
|
-
childProgress: ChildProcess,
|
|
108
|
-
{ tsCheck = false, watch = false } = {},
|
|
109
|
-
) => {
|
|
110
|
-
/**
|
|
111
|
-
* tsc 所有的log信息都是从stdout data 事件中获取
|
|
112
|
-
* 正常模式下,如果有报错信息,交给 resolveLog 后面的逻辑来处理
|
|
113
|
-
* watch 模式下,则使用这里的信息
|
|
114
|
-
*/
|
|
115
|
-
childProgress.stdout?.on('data', data => {
|
|
116
|
-
if (!tsCheck) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
if (watch) {
|
|
120
|
-
console.info(data.toString());
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
// 正常以下内容都不会触发,因为tsc 不会产生以下类型的log信息,不过防止意外情况
|
|
124
|
-
childProgress.stdout?.on('error', error => {
|
|
125
|
-
console.error(error.message);
|
|
126
|
-
});
|
|
127
|
-
childProgress.stderr?.on('data', chunk => {
|
|
128
|
-
console.error(chunk.toString());
|
|
129
|
-
});
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
interface IGeneratorConfig {
|
|
133
|
-
sourceDirName?: string;
|
|
134
|
-
srcDir: string;
|
|
135
|
-
distDir: string;
|
|
136
|
-
projectData: {
|
|
137
|
-
appDirectory: string;
|
|
138
|
-
};
|
|
139
|
-
tsconfigPath?: string;
|
|
140
|
-
tsCheck?: boolean;
|
|
141
|
-
watch?: boolean;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const generatorDts = async (_: NormalizedConfig, config: IGeneratorConfig) => {
|
|
145
|
-
const {
|
|
146
|
-
tsconfigPath,
|
|
147
|
-
distDir,
|
|
148
|
-
sourceDirName = 'src',
|
|
149
|
-
projectData: { appDirectory },
|
|
150
|
-
tsCheck = false,
|
|
151
|
-
watch = false,
|
|
152
|
-
} = config;
|
|
153
|
-
|
|
154
|
-
const userTsconfig = getProjectTsconfig(tsconfigPath);
|
|
155
|
-
const willDeleteTsconfigPath = generatorTsConfig(userTsconfig, {
|
|
156
|
-
appDirectory,
|
|
157
|
-
distDir,
|
|
158
|
-
sourceDir: sourceDirName,
|
|
159
|
-
});
|
|
160
|
-
removeTsconfigPath = willDeleteTsconfigPath;
|
|
161
|
-
const tscBinFile = path.join(appDirectory, './node_modules/.bin/tsc');
|
|
162
|
-
const watchParams = watch ? ['-w'] : [];
|
|
163
|
-
const childProgress = execa(
|
|
164
|
-
tscBinFile,
|
|
165
|
-
['-p', willDeleteTsconfigPath, ...watchParams],
|
|
166
|
-
{
|
|
167
|
-
stdio: 'pipe',
|
|
168
|
-
cwd: appDirectory,
|
|
169
|
-
},
|
|
170
|
-
);
|
|
171
|
-
resolveLog(childProgress, { tsCheck, watch });
|
|
172
|
-
try {
|
|
173
|
-
await childProgress;
|
|
174
|
-
console.info('[Tsc Compiler]: Successfully');
|
|
175
|
-
} catch (e) {
|
|
176
|
-
if (!tsCheck) {
|
|
177
|
-
console.warn(
|
|
178
|
-
`There are some type issues, which can be checked by configuring 'source.enableTsChecker = true' ${os.EOL} in modern.config.js`,
|
|
179
|
-
);
|
|
180
|
-
} else {
|
|
181
|
-
const isRecord = (input: any): input is Record<string, any> =>
|
|
182
|
-
typeof input === 'object';
|
|
183
|
-
// 通过使用 execa,可以将tsc 的 data 类型的报错信息变为异常错误信息
|
|
184
|
-
if (isRecord(e)) {
|
|
185
|
-
console.error(e.stdout);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
fs.removeSync(willDeleteTsconfigPath);
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
const resolveAlias = (
|
|
193
|
-
modernConfig: NormalizedConfig,
|
|
194
|
-
config: IGeneratorConfig,
|
|
195
|
-
watchFilenames: string[] = [],
|
|
196
|
-
) => {
|
|
197
|
-
const { output } = modernConfig;
|
|
198
|
-
const defaultPaths = { '@': ['./src'] };
|
|
199
|
-
const dtsDistPath = `${config.distDir}/**/*.d.ts`;
|
|
200
|
-
const dtsFilenames =
|
|
201
|
-
watchFilenames.length > 0
|
|
202
|
-
? watchFilenames
|
|
203
|
-
: glob.sync(dtsDistPath, { absolute: true });
|
|
204
|
-
const alias = babel.getFinalAlias(modernConfig, {
|
|
205
|
-
appDirectory: config.projectData.appDirectory,
|
|
206
|
-
tsconfigPath:
|
|
207
|
-
config.tsconfigPath ||
|
|
208
|
-
path.join(config.projectData.appDirectory, './tsconfig.json'),
|
|
209
|
-
sourceAbsDir: config.distDir,
|
|
210
|
-
});
|
|
211
|
-
const mergedPaths = alias.isTsPath
|
|
212
|
-
? alias.paths || {}
|
|
213
|
-
: { ...defaultPaths, ...(alias.paths || {}) };
|
|
214
|
-
const result = tsPathsTransform.transformDtsAlias({
|
|
215
|
-
filenames: dtsFilenames,
|
|
216
|
-
baseUrl: path.join(config.projectData.appDirectory, output.path || 'dist'),
|
|
217
|
-
paths: mergedPaths,
|
|
218
|
-
});
|
|
219
|
-
for (const r of result) {
|
|
220
|
-
fs.writeFileSync(r.path, r.content);
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
interface ITaskConfig {
|
|
225
|
-
srcDir: string;
|
|
226
|
-
sourceDirName: string;
|
|
227
|
-
distDir: string;
|
|
228
|
-
appDirectory: string;
|
|
229
|
-
tsconfigPath?: string;
|
|
230
|
-
tsCheck?: boolean;
|
|
231
|
-
watch?: boolean;
|
|
232
|
-
debug?: boolean;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const taskMain = async ({
|
|
236
|
-
modernConfig,
|
|
237
|
-
}: {
|
|
238
|
-
modernConfig: NormalizedConfig;
|
|
239
|
-
}) => {
|
|
240
|
-
const processArgv = argv(process.argv.slice(2));
|
|
241
|
-
const config = processArgv<ITaskConfig>({
|
|
242
|
-
appDirectory: process.cwd(),
|
|
243
|
-
srcDir: 'src',
|
|
244
|
-
distDir: 'dist/types',
|
|
245
|
-
tsconfigPath: './tsconfig.json',
|
|
246
|
-
sourceDirName: 'src',
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
const option = {
|
|
250
|
-
srcDir: config.srcDir,
|
|
251
|
-
distDir: config.distDir,
|
|
252
|
-
projectData: { appDirectory: config.appDirectory },
|
|
253
|
-
tsconfigPath: config.tsconfigPath,
|
|
254
|
-
watch: config.watch,
|
|
255
|
-
tsCheck: config.tsCheck,
|
|
256
|
-
sourceDirName: config.sourceDirName,
|
|
257
|
-
};
|
|
258
|
-
await generatorDts(modernConfig, option);
|
|
259
|
-
// TODO: watch 模式下无法转换
|
|
260
|
-
resolveAlias(modernConfig, option);
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
(async () => {
|
|
264
|
-
let options: CoreOptions | undefined;
|
|
265
|
-
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
266
|
-
({ options } = require(process.env.CORE_INIT_OPTION_FILE));
|
|
267
|
-
}
|
|
268
|
-
const { resolved } = await core.cli.init([], options);
|
|
269
|
-
await core.manager.run(async () => {
|
|
270
|
-
try {
|
|
271
|
-
await taskMain({ modernConfig: resolved });
|
|
272
|
-
} catch (e: any) {
|
|
273
|
-
console.error(e.message);
|
|
274
|
-
fs.removeSync(removeTsconfigPath);
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
})();
|
package/src/type.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/// <reference types="@modern-js/module-tools-hooks" />
|