@modern-js/module-tools 1.0.0-alpha.3
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 +21 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/bin/modern.js +3 -0
- package/dist/js/modern/cli/build.js +9 -0
- package/dist/js/modern/cli/dev.js +8 -0
- package/dist/js/modern/cli/index.js +3 -0
- package/dist/js/modern/cli/new.js +16 -0
- package/dist/js/modern/commands/build.js +56 -0
- package/dist/js/modern/commands/dev.js +29 -0
- package/dist/js/modern/commands/index.js +2 -0
- package/dist/js/modern/features/build/build-platform.js +70 -0
- package/dist/js/modern/features/build/build-watch.js +96 -0
- package/dist/js/modern/features/build/build.js +110 -0
- package/dist/js/modern/features/build/constants.js +66 -0
- package/dist/js/modern/features/build/index.js +29 -0
- package/dist/js/modern/features/build/logger/index.js +2 -0
- package/dist/js/modern/features/build/logger/logText.js +63 -0
- package/dist/js/modern/features/build/logger/loggerManager.js +101 -0
- package/dist/js/modern/features/build/utils.js +198 -0
- package/dist/js/modern/features/dev/index.js +41 -0
- package/dist/js/modern/index.js +46 -0
- package/dist/js/modern/locale/en.js +23 -0
- package/dist/js/modern/locale/index.js +10 -0
- package/dist/js/modern/locale/zh.js +23 -0
- package/dist/js/modern/schema/index.js +3 -0
- package/dist/js/modern/schema/output.js +29 -0
- package/dist/js/modern/schema/source.js +15 -0
- package/dist/js/modern/tasks/build-source-code.js +213 -0
- package/dist/js/modern/tasks/build-style.js +152 -0
- package/dist/js/modern/tasks/build-watch-source-code.js +144 -0
- package/dist/js/modern/tasks/build-watch-style.js +213 -0
- package/dist/js/modern/tasks/constants.js +1 -0
- package/dist/js/modern/tasks/copy-assets.js +108 -0
- package/dist/js/modern/tasks/generator-dts.js +220 -0
- package/dist/js/modern/types.js +0 -0
- package/dist/js/modern/utils/babel.js +85 -0
- package/dist/js/modern/utils/color.js +2 -0
- package/dist/js/modern/utils/copy.js +80 -0
- package/dist/js/modern/utils/json.js +13 -0
- package/dist/js/modern/utils/language.js +6 -0
- package/dist/js/modern/utils/logger.js +110 -0
- package/dist/js/modern/utils/readline.js +19 -0
- package/dist/js/modern/utils/tsconfig.js +15 -0
- package/dist/js/modern/utils/tspaths-transform/constants.js +3 -0
- package/dist/js/modern/utils/tspaths-transform/index.js +130 -0
- package/dist/js/modern/utils/tspaths-transform/utils.js +21 -0
- package/dist/js/modern/utils/valide.js +28 -0
- package/dist/js/node/cli/build.js +21 -0
- package/dist/js/node/cli/dev.js +20 -0
- package/dist/js/node/cli/index.js +44 -0
- package/dist/js/node/cli/new.js +28 -0
- package/dist/js/node/commands/build.js +76 -0
- package/dist/js/node/commands/dev.js +49 -0
- package/dist/js/node/commands/index.js +21 -0
- package/dist/js/node/features/build/build-platform.js +84 -0
- package/dist/js/node/features/build/build-watch.js +118 -0
- package/dist/js/node/features/build/build.js +133 -0
- package/dist/js/node/features/build/constants.js +79 -0
- package/dist/js/node/features/build/index.js +41 -0
- package/dist/js/node/features/build/logger/index.js +31 -0
- package/dist/js/node/features/build/logger/logText.js +79 -0
- package/dist/js/node/features/build/logger/loggerManager.js +111 -0
- package/dist/js/node/features/build/utils.js +231 -0
- package/dist/js/node/features/dev/index.js +60 -0
- package/dist/js/node/index.js +64 -0
- package/dist/js/node/locale/en.js +30 -0
- package/dist/js/node/locale/index.js +22 -0
- package/dist/js/node/locale/zh.js +30 -0
- package/dist/js/node/schema/index.js +14 -0
- package/dist/js/node/schema/output.js +36 -0
- package/dist/js/node/schema/source.js +22 -0
- package/dist/js/node/tasks/build-source-code.js +240 -0
- package/dist/js/node/tasks/build-style.js +168 -0
- package/dist/js/node/tasks/build-watch-source-code.js +156 -0
- package/dist/js/node/tasks/build-watch-style.js +231 -0
- package/dist/js/node/tasks/constants.js +8 -0
- package/dist/js/node/tasks/copy-assets.js +126 -0
- package/dist/js/node/tasks/generator-dts.js +243 -0
- package/dist/js/node/types.js +0 -0
- package/dist/js/node/utils/babel.js +99 -0
- package/dist/js/node/utils/color.js +12 -0
- package/dist/js/node/utils/copy.js +99 -0
- package/dist/js/node/utils/json.js +22 -0
- package/dist/js/node/utils/language.js +15 -0
- package/dist/js/node/utils/logger.js +133 -0
- package/dist/js/node/utils/readline.js +33 -0
- package/dist/js/node/utils/tsconfig.js +30 -0
- package/dist/js/node/utils/tspaths-transform/constants.js +10 -0
- package/dist/js/node/utils/tspaths-transform/index.js +155 -0
- package/dist/js/node/utils/tspaths-transform/utils.js +32 -0
- package/dist/js/node/utils/valide.js +41 -0
- package/dist/types/cli/build.d.ts +2 -0
- package/dist/types/cli/dev.d.ts +2 -0
- package/dist/types/cli/index.d.ts +3 -0
- package/dist/types/cli/new.d.ts +2 -0
- package/dist/types/commands/build.d.ts +16 -0
- package/dist/types/commands/dev.d.ts +4 -0
- package/dist/types/commands/index.d.ts +2 -0
- package/dist/types/features/build/build-platform.d.ts +6 -0
- package/dist/types/features/build/build-watch.d.ts +3 -0
- package/dist/types/features/build/build.d.ts +3 -0
- package/dist/types/features/build/constants.d.ts +14 -0
- package/dist/types/features/build/index.d.ts +3 -0
- package/dist/types/features/build/logger/index.d.ts +2 -0
- package/dist/types/features/build/logger/logText.d.ts +39 -0
- package/dist/types/features/build/logger/loggerManager.d.ts +39 -0
- package/dist/types/features/build/utils.d.ts +58 -0
- package/dist/types/features/dev/index.d.ts +7 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/locale/en.d.ts +23 -0
- package/dist/types/locale/index.d.ts +47 -0
- package/dist/types/locale/zh.d.ts +23 -0
- package/dist/types/schema/index.d.ts +36 -0
- package/dist/types/schema/output.d.ts +22 -0
- package/dist/types/schema/source.d.ts +15 -0
- package/dist/types/tasks/build-source-code.d.ts +42 -0
- package/dist/types/tasks/build-style.d.ts +1 -0
- package/dist/types/tasks/build-watch-source-code.d.ts +1 -0
- package/dist/types/tasks/build-watch-style.d.ts +1 -0
- package/dist/types/tasks/constants.d.ts +1 -0
- package/dist/types/tasks/copy-assets.d.ts +1 -0
- package/dist/types/tasks/generator-dts.d.ts +1 -0
- package/dist/types/types.d.ts +48 -0
- package/dist/types/utils/babel.d.ts +8 -0
- package/dist/types/utils/color.d.ts +2 -0
- package/dist/types/utils/copy.d.ts +5 -0
- package/dist/types/utils/json.d.ts +4 -0
- package/dist/types/utils/language.d.ts +1 -0
- package/dist/types/utils/logger.d.ts +56 -0
- package/dist/types/utils/readline.d.ts +9 -0
- package/dist/types/utils/tsconfig.d.ts +16 -0
- package/dist/types/utils/tspaths-transform/constants.d.ts +1 -0
- package/dist/types/utils/tspaths-transform/index.d.ts +15 -0
- package/dist/types/utils/tspaths-transform/utils.d.ts +4 -0
- package/dist/types/utils/valide.d.ts +12 -0
- package/modern.config.js +6 -0
- package/package.json +95 -0
- package/src/.eslintrc.json +5 -0
- package/src/cli/build.ts +39 -0
- package/src/cli/dev.ts +24 -0
- package/src/cli/index.ts +3 -0
- package/src/cli/new.ts +32 -0
- package/src/commands/build.ts +81 -0
- package/src/commands/dev.ts +41 -0
- package/src/commands/index.ts +2 -0
- package/src/features/build/build-platform.ts +76 -0
- package/src/features/build/build-watch.ts +93 -0
- package/src/features/build/build.ts +111 -0
- package/src/features/build/constants.ts +52 -0
- package/src/features/build/index.ts +36 -0
- package/src/features/build/logger/index.ts +2 -0
- package/src/features/build/logger/logText.ts +80 -0
- package/src/features/build/logger/loggerManager.ts +132 -0
- package/src/features/build/utils.ts +250 -0
- package/src/features/dev/index.ts +62 -0
- package/src/index.ts +55 -0
- package/src/locale/en.ts +21 -0
- package/src/locale/index.ts +15 -0
- package/src/locale/zh.ts +21 -0
- package/src/schema/index.ts +4 -0
- package/src/schema/output.ts +34 -0
- package/src/schema/schema.d.ts +13 -0
- package/src/schema/source.ts +16 -0
- package/src/tasks/build-source-code.ts +245 -0
- package/src/tasks/build-style.ts +175 -0
- package/src/tasks/build-watch-source-code.ts +185 -0
- package/src/tasks/build-watch-style.ts +260 -0
- package/src/tasks/constants.ts +1 -0
- package/src/tasks/copy-assets.ts +117 -0
- package/src/tasks/generator-dts.ts +277 -0
- package/src/type.d.ts +1 -0
- package/src/types.ts +63 -0
- package/src/utils/babel.ts +100 -0
- package/src/utils/color.ts +3 -0
- package/src/utils/copy.ts +70 -0
- package/src/utils/json.ts +13 -0
- package/src/utils/language.ts +9 -0
- package/src/utils/logger.ts +144 -0
- package/src/utils/readline.ts +28 -0
- package/src/utils/tsconfig.ts +37 -0
- package/src/utils/tspaths-transform/constants.ts +19 -0
- package/src/utils/tspaths-transform/index.ts +139 -0
- package/src/utils/tspaths-transform/utils.ts +25 -0
- package/src/utils/valide.ts +37 -0
- package/tsconfig.json +13 -0
- package/types.d.ts +1 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { Import, fs } from '@modern-js/utils';
|
|
2
|
+
import type { NormalizedConfig } from '@modern-js/core';
|
|
3
|
+
import type { ICompilerResult, IVirtualDist } from '@modern-js/babel-compiler';
|
|
4
|
+
import type { ITsconfig } from '../types';
|
|
5
|
+
|
|
6
|
+
const babelCompiler: typeof import('@modern-js/babel-compiler') = Import.lazy(
|
|
7
|
+
'@modern-js/babel-compiler',
|
|
8
|
+
require,
|
|
9
|
+
);
|
|
10
|
+
const logger: typeof import('../features/build/logger') = Import.lazy(
|
|
11
|
+
'../features/build/logger',
|
|
12
|
+
require,
|
|
13
|
+
);
|
|
14
|
+
const ts: typeof import('../utils/tsconfig') = Import.lazy(
|
|
15
|
+
'../utils/tsconfig',
|
|
16
|
+
require,
|
|
17
|
+
);
|
|
18
|
+
const babel: typeof import('../utils/babel') = Import.lazy(
|
|
19
|
+
'../utils/babel',
|
|
20
|
+
require,
|
|
21
|
+
);
|
|
22
|
+
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
23
|
+
'@modern-js/core',
|
|
24
|
+
require,
|
|
25
|
+
);
|
|
26
|
+
const pluginNode: typeof import('@modern-js/plugin/node') = Import.lazy(
|
|
27
|
+
'@modern-js/plugin/node',
|
|
28
|
+
require,
|
|
29
|
+
);
|
|
30
|
+
const argv: typeof import('process.argv').default = Import.lazy(
|
|
31
|
+
'process.argv',
|
|
32
|
+
require,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const generatorRealFiles = (virtualDists: IVirtualDist[]) => {
|
|
36
|
+
for (const virtualDist of virtualDists) {
|
|
37
|
+
const { distPath, code, sourcemap, sourceMapPath } = virtualDist;
|
|
38
|
+
fs.ensureFileSync(distPath);
|
|
39
|
+
fs.writeFileSync(distPath, code);
|
|
40
|
+
if (sourcemap.length > 0) {
|
|
41
|
+
fs.ensureFileSync(sourceMapPath);
|
|
42
|
+
fs.writeFileSync(sourceMapPath, sourcemap);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const runBabelCompiler = async (
|
|
48
|
+
config: ITaskConfig,
|
|
49
|
+
modernConfig: NormalizedConfig,
|
|
50
|
+
) => {
|
|
51
|
+
const { tsconfigPath } = config;
|
|
52
|
+
const babelConfig = babel.resolveBabelConfig(
|
|
53
|
+
config.appDirectory,
|
|
54
|
+
modernConfig,
|
|
55
|
+
{
|
|
56
|
+
sourceAbsDir: config.srcRootDir,
|
|
57
|
+
tsconfigPath,
|
|
58
|
+
syntax: config.syntax,
|
|
59
|
+
type: config.type,
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
const tsconfig = ts.readTsConfig<ITsconfig>(tsconfigPath || '', {});
|
|
63
|
+
const isTs = Boolean(tsconfig);
|
|
64
|
+
|
|
65
|
+
const getExts = (isTsProject: boolean) => {
|
|
66
|
+
// TODO: 是否受控tsconfig.json 里的jsx配置
|
|
67
|
+
let exts = [];
|
|
68
|
+
if (isTsProject) {
|
|
69
|
+
exts = tsconfig?.compilerOptions?.allowJs
|
|
70
|
+
? ['.ts', '.tsx', '.js', '.jsx']
|
|
71
|
+
: ['.ts', '.tsx'];
|
|
72
|
+
} else {
|
|
73
|
+
exts = ['.js', '.jsx'];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return exts;
|
|
77
|
+
};
|
|
78
|
+
const emitter = await babelCompiler.compiler(
|
|
79
|
+
{
|
|
80
|
+
enableVirtualDist: true,
|
|
81
|
+
quiet: true,
|
|
82
|
+
enableWatch: true,
|
|
83
|
+
rootDir: config.srcRootDir,
|
|
84
|
+
distDir: config.distDir,
|
|
85
|
+
watchDir: config.srcRootDir,
|
|
86
|
+
extensions: getExts(isTs),
|
|
87
|
+
},
|
|
88
|
+
babelConfig,
|
|
89
|
+
);
|
|
90
|
+
emitter.on(babelCompiler.BuildWatchEvent.compiling, () => {
|
|
91
|
+
console.info(logger.clearFlag, `Compiling...`);
|
|
92
|
+
});
|
|
93
|
+
emitter.on(
|
|
94
|
+
babelCompiler.BuildWatchEvent.firstCompiler,
|
|
95
|
+
(result: ICompilerResult) => {
|
|
96
|
+
if (result.code === 1) {
|
|
97
|
+
console.error(logger.clearFlag);
|
|
98
|
+
console.error(result.message);
|
|
99
|
+
for (const detail of result.messageDetails || []) {
|
|
100
|
+
console.error(detail.content);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
generatorRealFiles(result.virtualDists!);
|
|
104
|
+
console.info(logger.clearFlag, '[Babel Compiler]: Successfully');
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
emitter.on(
|
|
110
|
+
babelCompiler.BuildWatchEvent.watchingCompiler,
|
|
111
|
+
(result: ICompilerResult) => {
|
|
112
|
+
if (result.code === 1) {
|
|
113
|
+
console.error(logger.clearFlag);
|
|
114
|
+
console.error(result.message);
|
|
115
|
+
for (const detail of result.messageDetails || []) {
|
|
116
|
+
console.error(detail.content);
|
|
117
|
+
}
|
|
118
|
+
if (
|
|
119
|
+
Array.isArray(result.virtualDists) &&
|
|
120
|
+
result.virtualDists?.length > 0
|
|
121
|
+
) {
|
|
122
|
+
generatorRealFiles(result.virtualDists);
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
generatorRealFiles(result.virtualDists!);
|
|
126
|
+
console.info(result.message);
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
);
|
|
130
|
+
await emitter.watch();
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const buildSourceCode = async (
|
|
134
|
+
config: ITaskConfig,
|
|
135
|
+
modernConfig: NormalizedConfig,
|
|
136
|
+
) => {
|
|
137
|
+
const { compiler } = config;
|
|
138
|
+
if (compiler === 'babel') {
|
|
139
|
+
await runBabelCompiler(config, modernConfig);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
interface ITaskConfig {
|
|
144
|
+
srcRootDir: string; // 源码的根目录
|
|
145
|
+
distDir: string;
|
|
146
|
+
appDirectory: string;
|
|
147
|
+
sourceMaps: boolean;
|
|
148
|
+
syntax: 'es5' | 'es6+';
|
|
149
|
+
type: 'module' | 'commonjs';
|
|
150
|
+
tsconfigPath: string;
|
|
151
|
+
copyDirs?: string;
|
|
152
|
+
compiler?: 'babel' | 'esbuild' | 'swc';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const taskMain = async ({
|
|
156
|
+
modernConfig,
|
|
157
|
+
}: {
|
|
158
|
+
modernConfig: NormalizedConfig;
|
|
159
|
+
}) => {
|
|
160
|
+
const processArgv = argv(process.argv.slice(2));
|
|
161
|
+
const config = processArgv<ITaskConfig>({
|
|
162
|
+
srcRootDir: `${process.cwd()}/src`,
|
|
163
|
+
distDir: '',
|
|
164
|
+
compiler: 'babel',
|
|
165
|
+
appDirectory: '',
|
|
166
|
+
sourceMaps: false,
|
|
167
|
+
tsconfigPath: '',
|
|
168
|
+
syntax: 'es5',
|
|
169
|
+
type: 'module',
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
await buildSourceCode(config, modernConfig);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
(async () => {
|
|
176
|
+
pluginNode.enable();
|
|
177
|
+
const { resolved } = await core.cli.init();
|
|
178
|
+
await core.manager.run(async () => {
|
|
179
|
+
try {
|
|
180
|
+
await taskMain({ modernConfig: resolved });
|
|
181
|
+
} catch (e) {
|
|
182
|
+
console.error(e);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
})();
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/* eslint-disable max-statements */
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import type { NormalizedConfig, IAppContext } from '@modern-js/core';
|
|
4
|
+
import type { ICompilerResult, PostcssOption } from '@modern-js/style-compiler';
|
|
5
|
+
import { fs, watch, WatchChangeType, Import } from '@modern-js/utils';
|
|
6
|
+
import type { ModuleToolsOutput } from '../types';
|
|
7
|
+
|
|
8
|
+
const logger: typeof import('../features/build/logger') = Import.lazy(
|
|
9
|
+
'../features/build/logger',
|
|
10
|
+
require,
|
|
11
|
+
);
|
|
12
|
+
const cssConfig: typeof import('@modern-js/css-config') = Import.lazy(
|
|
13
|
+
'@modern-js/css-config',
|
|
14
|
+
require,
|
|
15
|
+
);
|
|
16
|
+
const hooks: typeof import('@modern-js/module-tools-hooks') = Import.lazy(
|
|
17
|
+
'@modern-js/module-tools-hooks',
|
|
18
|
+
require,
|
|
19
|
+
);
|
|
20
|
+
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
21
|
+
'@modern-js/core',
|
|
22
|
+
require,
|
|
23
|
+
);
|
|
24
|
+
const pluginNode: typeof import('@modern-js/plugin/node') = Import.lazy(
|
|
25
|
+
'@modern-js/plugin/node',
|
|
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.copyFileSync(styleFile, path.join(outputDir, file));
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const logCompilerMessage = (compilerMessage: {
|
|
91
|
+
src: string;
|
|
92
|
+
styles: string;
|
|
93
|
+
}) => {
|
|
94
|
+
console.info(logger.clearFlag);
|
|
95
|
+
console.info(compilerMessage.src);
|
|
96
|
+
console.info(compilerMessage.styles);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const taskMain = async ({
|
|
100
|
+
modernConfig,
|
|
101
|
+
appContext,
|
|
102
|
+
}: {
|
|
103
|
+
modernConfig: NormalizedConfig;
|
|
104
|
+
appContext: IAppContext;
|
|
105
|
+
}) => {
|
|
106
|
+
const {
|
|
107
|
+
assetsPath = 'styles',
|
|
108
|
+
path: outputPath = 'dist',
|
|
109
|
+
jsPath = 'js',
|
|
110
|
+
importStyle,
|
|
111
|
+
} = modernConfig.output as ModuleToolsOutput;
|
|
112
|
+
const { appDirectory } = appContext;
|
|
113
|
+
|
|
114
|
+
const lessOption = await (core.mountHook() as any).moduleLessConfig(
|
|
115
|
+
{ modernConfig }, // eslint-disable-next-line @typescript-eslint/require-await
|
|
116
|
+
{ onLast: async (_: any) => null as any },
|
|
117
|
+
);
|
|
118
|
+
const sassOption = await (core.mountHook() as any).moduleSassConfig(
|
|
119
|
+
{ modernConfig }, // eslint-disable-next-line @typescript-eslint/require-await
|
|
120
|
+
{ onLast: async (_: any) => null as any },
|
|
121
|
+
);
|
|
122
|
+
const postcssOption = getPostcssOption(appDirectory, modernConfig);
|
|
123
|
+
const existStylesDir = checkStylesDirExist({ appDirectory });
|
|
124
|
+
const compilerMessage = {
|
|
125
|
+
src: '',
|
|
126
|
+
styles: '',
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// 编译 styles 目录样式
|
|
130
|
+
let styleEmitter = null;
|
|
131
|
+
if (existStylesDir) {
|
|
132
|
+
styleEmitter = compiler.styleCompiler({
|
|
133
|
+
watch: true,
|
|
134
|
+
projectDir: appDirectory,
|
|
135
|
+
stylesDir: path.resolve(appDirectory, STYLE_DIRS),
|
|
136
|
+
outDir: path.join(appDirectory, outputPath, assetsPath),
|
|
137
|
+
enableVirtualDist: true,
|
|
138
|
+
compilerOption: {
|
|
139
|
+
less: lessOption,
|
|
140
|
+
sass: sassOption,
|
|
141
|
+
postcss: postcssOption,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
styleEmitter.on(
|
|
145
|
+
compiler.BuildWatchEvent.firstCompiler,
|
|
146
|
+
(styleResult: ICompilerResult) => {
|
|
147
|
+
compilerMessage.styles = generatorFileAndReturnLog(
|
|
148
|
+
styleResult,
|
|
149
|
+
`[Style Compiler] Successfully for 'styles' dir`,
|
|
150
|
+
);
|
|
151
|
+
logCompilerMessage(compilerMessage);
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
styleEmitter.on(compiler.BuildWatchEvent.compilering, () => {
|
|
155
|
+
compilerMessage.styles = `[${assetsPath}] Compiling...`;
|
|
156
|
+
logCompilerMessage(compilerMessage);
|
|
157
|
+
});
|
|
158
|
+
styleEmitter.on(
|
|
159
|
+
compiler.BuildWatchEvent.watchingCompiler,
|
|
160
|
+
(styleResult: ICompilerResult) => {
|
|
161
|
+
compilerMessage.styles = generatorFileAndReturnLog(
|
|
162
|
+
styleResult,
|
|
163
|
+
`[Style Compiler] Successfully for 'styles' dir`,
|
|
164
|
+
);
|
|
165
|
+
logCompilerMessage(compilerMessage);
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
// await styleEmitter.watch();
|
|
169
|
+
}
|
|
170
|
+
// 编译 src 内的样式代码
|
|
171
|
+
const srcDir = path.resolve(appDirectory, SRC_STYLE_DIRS);
|
|
172
|
+
const outputDirToSrc = path.join(
|
|
173
|
+
appDirectory,
|
|
174
|
+
outputPath,
|
|
175
|
+
jsPath,
|
|
176
|
+
assetsPath,
|
|
177
|
+
);
|
|
178
|
+
if (importStyle === 'compiled-code') {
|
|
179
|
+
compilerMessage.src = `[src] Compiling`;
|
|
180
|
+
const srcStyleEmitter = compiler.styleCompiler({
|
|
181
|
+
watch: true,
|
|
182
|
+
projectDir: appDirectory,
|
|
183
|
+
stylesDir: srcDir,
|
|
184
|
+
outDir: outputDirToSrc,
|
|
185
|
+
enableVirtualDist: true,
|
|
186
|
+
compilerOption: {
|
|
187
|
+
less: lessOption,
|
|
188
|
+
sass: sassOption,
|
|
189
|
+
postcss: postcssOption,
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
srcStyleEmitter.on(
|
|
193
|
+
compiler.BuildWatchEvent.firstCompiler,
|
|
194
|
+
(srcStyleResult: ICompilerResult) => {
|
|
195
|
+
compilerMessage.src = generatorFileAndReturnLog(
|
|
196
|
+
srcStyleResult,
|
|
197
|
+
`[Style Compiler] Successfully for 'src' dir`,
|
|
198
|
+
);
|
|
199
|
+
logCompilerMessage(compilerMessage);
|
|
200
|
+
},
|
|
201
|
+
);
|
|
202
|
+
srcStyleEmitter.on(compiler.BuildWatchEvent.compilering, () => {
|
|
203
|
+
compilerMessage.src = `[src] Compiling`;
|
|
204
|
+
logCompilerMessage(compilerMessage);
|
|
205
|
+
});
|
|
206
|
+
srcStyleEmitter.on(
|
|
207
|
+
compiler.BuildWatchEvent.watchingCompiler,
|
|
208
|
+
(srcStyleResult: ICompilerResult) => {
|
|
209
|
+
compilerMessage.src = generatorFileAndReturnLog(
|
|
210
|
+
srcStyleResult,
|
|
211
|
+
`[Style Compiler] Successfully for 'src' dir`,
|
|
212
|
+
);
|
|
213
|
+
logCompilerMessage(compilerMessage);
|
|
214
|
+
},
|
|
215
|
+
);
|
|
216
|
+
styleEmitter && (await styleEmitter.watch());
|
|
217
|
+
await srcStyleEmitter.watch();
|
|
218
|
+
} else {
|
|
219
|
+
compilerMessage.src = `['src' dir] Copying in progress`;
|
|
220
|
+
styleEmitter && (await styleEmitter.watch());
|
|
221
|
+
logCompilerMessage(compilerMessage);
|
|
222
|
+
copyOriginStyleFiles({ targetDir: srcDir, outputDir: outputDirToSrc });
|
|
223
|
+
compilerMessage.src = `[Style Compiler] Successfully for 'src' dir`;
|
|
224
|
+
logCompilerMessage(compilerMessage);
|
|
225
|
+
watch(
|
|
226
|
+
`${srcDir}/**/*.{css,less,sass,scss}`,
|
|
227
|
+
({ changeType, changedFilePath }) => {
|
|
228
|
+
compilerMessage.src = `['src' dir] Copying in progress`;
|
|
229
|
+
logCompilerMessage(compilerMessage);
|
|
230
|
+
if (changeType === WatchChangeType.UNLINK) {
|
|
231
|
+
const removeFile = path.normalize(
|
|
232
|
+
`${outputDirToSrc}/${path.relative(srcDir, changedFilePath)}`,
|
|
233
|
+
);
|
|
234
|
+
fs.removeSync(removeFile);
|
|
235
|
+
} else {
|
|
236
|
+
copyOriginStyleFiles({
|
|
237
|
+
targetDir: srcDir,
|
|
238
|
+
outputDir: outputDirToSrc,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
compilerMessage.src = `[Style Compiler] Successfully for 'src' dir`;
|
|
242
|
+
logCompilerMessage(compilerMessage);
|
|
243
|
+
},
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
(async () => {
|
|
249
|
+
pluginNode.enable();
|
|
250
|
+
hooks.buildLifeCycle();
|
|
251
|
+
const { resolved: modernConfig, appContext } = await core.cli.init();
|
|
252
|
+
await core.manager.run(async () => {
|
|
253
|
+
try {
|
|
254
|
+
await taskMain({ modernConfig, appContext });
|
|
255
|
+
} catch (e: any) {
|
|
256
|
+
console.error(e.message);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
})();
|
|
260
|
+
/* eslint-enable max-statements */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const tempTsconfigName = './tsconfig.temp.json';
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { fs, watch, WatchChangeType, Import } from '@modern-js/utils';
|
|
3
|
+
import type { NormalizedConfig, IAppContext } from '@modern-js/core';
|
|
4
|
+
import type { ModuleToolsOutput } from '../types';
|
|
5
|
+
|
|
6
|
+
const argv: typeof import('process.argv').default = Import.lazy(
|
|
7
|
+
'process.argv',
|
|
8
|
+
require,
|
|
9
|
+
);
|
|
10
|
+
const glob: typeof import('glob') = Import.lazy('glob', require);
|
|
11
|
+
const core: typeof import('@modern-js/core') = Import.lazy(
|
|
12
|
+
'@modern-js/core',
|
|
13
|
+
require,
|
|
14
|
+
);
|
|
15
|
+
const pluginNode: typeof import('@modern-js/plugin/node') = Import.lazy(
|
|
16
|
+
'@modern-js/plugin/node',
|
|
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
|
+
pluginNode.enable();
|
|
109
|
+
const { resolved: modernConfig, appContext } = await core.cli.init();
|
|
110
|
+
core.manager.run(() => {
|
|
111
|
+
try {
|
|
112
|
+
taskMain({ modernConfig, appContext });
|
|
113
|
+
} catch (e: any) {
|
|
114
|
+
console.error(e.message);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
})();
|