maishu-scripts 1.2.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/index.d.ts +9 -0
- package/out/index.js +12 -4
- package/out/modules/actions/babel-compile.d.ts +3 -0
- package/out/modules/actions/babel-compile.js +185 -0
- package/out/modules/actions/copy-file.js +18 -8
- package/out/modules/actions/less-compile.d.ts +8 -0
- package/out/modules/actions/less-compile.js +33 -0
- package/out/modules/actions/scss-compile.d.ts +9 -0
- package/out/modules/actions/scss-compile.js +65 -0
- package/out/modules/actions/ts-compile.d.ts +2 -7
- package/out/modules/actions/ts-compile.js +35 -13
- package/out/modules/compile.d.ts +16 -3
- package/out/modules/compile.js +103 -51
- package/out/modules/configs.d.ts +7 -0
- package/out/modules/configs.js +27 -0
- package/out/modules/import-path-rewrite.js +17 -7
- package/out/modules/project-compiler.d.ts +0 -22
- package/out/modules/project-compiler.js +17 -165
- package/out/types.d.ts +1 -0
- package/out/types.js +2 -0
- package/package.json +6 -2
- package/src/index.ts +7 -3
- package/src/modules/actions/{biel-compile.ts → babel-compile.ts} +39 -11
- package/src/modules/actions/copy-file.ts +1 -1
- package/src/modules/actions/less-compile.test.ts +34 -0
- package/src/modules/actions/less-compile.ts +35 -0
- package/src/modules/actions/scss-compile.test.ts +38 -0
- package/src/modules/actions/scss-compile.ts +35 -0
- package/src/modules/actions/ts-compile.test.ts +30 -2
- package/src/modules/actions/ts-compile.ts +22 -7
- package/src/modules/compile.test.ts +154 -14
- package/src/modules/compile.ts +112 -50
- package/src/modules/configs.test.ts +32 -0
- package/src/modules/configs.ts +29 -0
- package/src/modules/project-compiler.ts +1 -170
- /package/src/{types.d.ts → types.ts} +0 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { assert } from 'console';
|
|
2
|
+
import configs from './configs';
|
|
3
|
+
import { create as createTsCompiler } from "./actions/ts-compile";
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
|
|
6
|
+
describe('configs', () => {
|
|
7
|
+
it('引入的 configs 不为空', () => {
|
|
8
|
+
expect(configs).not.toBeNull();
|
|
9
|
+
expect(configs).not.toBeUndefined();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("configs fileActions 应该可以修改", () => {
|
|
13
|
+
|
|
14
|
+
const action1 = configs.fileActions['.ts'];
|
|
15
|
+
expect(action1).not.toBeUndefined();
|
|
16
|
+
expect(action1).not.toBeNull();
|
|
17
|
+
|
|
18
|
+
configs.fileActions['.ts'] = createTsCompiler({
|
|
19
|
+
target: ts.ScriptTarget.ES2015,
|
|
20
|
+
module: ts.ModuleKind.CommonJS,
|
|
21
|
+
strict: true,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const action2 = configs.fileActions['.ts'];
|
|
25
|
+
expect(action2).not.toBeUndefined();
|
|
26
|
+
expect(action2).not.toBeNull();
|
|
27
|
+
expect(action2).not.toBe(action1);
|
|
28
|
+
|
|
29
|
+
expect(action1.toString()).not.toBe(action2.toString());
|
|
30
|
+
|
|
31
|
+
})
|
|
32
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FileAction } from "../types";
|
|
2
|
+
import scssCompile from "./actions/scss-compile";
|
|
3
|
+
import lessCompile from "./actions/less-compile";
|
|
4
|
+
import { create as createBielCompiler } from "./actions/babel-compile";
|
|
5
|
+
import { create as createTsCompiler } from "./actions/ts-compile";
|
|
6
|
+
import ts from "typescript";
|
|
7
|
+
|
|
8
|
+
// const skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"]
|
|
9
|
+
|
|
10
|
+
const fileActions: Record<string, FileAction> = {
|
|
11
|
+
".ts": createBielCompiler(),
|
|
12
|
+
".tsx": createBielCompiler(),
|
|
13
|
+
".scss": scssCompile,
|
|
14
|
+
".less": lessCompile,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class Options {
|
|
18
|
+
fileActions: Record<string, FileAction> = fileActions;
|
|
19
|
+
skipFiles: Record<string, RegExp[]> = {
|
|
20
|
+
".tsx": [/\.d\.tsx?$/, /\.test\.tsx?$/, /\.spec\.tsx?$/],
|
|
21
|
+
".ts": [/\.d\.ts$/, /\.test\.ts$/, /\.spec\.ts$/],
|
|
22
|
+
".scss": [],
|
|
23
|
+
".less": [],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const NAME = "maishu-scripts-options";
|
|
28
|
+
(global as any)[NAME] = (global as any)[NAME] || new Options();
|
|
29
|
+
export default (global as any)[NAME] as Options;
|
|
@@ -12,96 +12,10 @@ type FileAction = (filePath: string, outDir: string) => void;
|
|
|
12
12
|
|
|
13
13
|
export class ProjectCompiler {
|
|
14
14
|
|
|
15
|
-
private fileActions: { [ext: string]: FileAction };
|
|
16
15
|
private skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"];
|
|
17
16
|
|
|
18
17
|
static tsOutExt = tsOutExt;
|
|
19
18
|
|
|
20
|
-
constructor(private projectPath: string, private sourceDirectoryNanme: string, private outputDirectoryName: string) {
|
|
21
|
-
if (!projectPath) throw errors.argumentNull("projectPath");
|
|
22
|
-
if (!sourceDirectoryNanme) throw errors.argumentNull("sourceDirectoryNanme");
|
|
23
|
-
if (!outputDirectoryName) throw errors.argumentNull("outputDirectoryName");
|
|
24
|
-
|
|
25
|
-
if (!path.isAbsolute(projectPath))
|
|
26
|
-
throw errors.notAbsolutePath(projectPath);
|
|
27
|
-
|
|
28
|
-
let sourcePath = path.join(projectPath, sourceDirectoryNanme);
|
|
29
|
-
if (!fs.existsSync(sourcePath))
|
|
30
|
-
throw errors.pathNotExists(sourcePath);
|
|
31
|
-
|
|
32
|
-
this.fileActions = {
|
|
33
|
-
".ts": this.compileFile.bind(this),
|
|
34
|
-
".tsx": this.compileFile.bind(this),
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 编译特定文件,并生成到制定目录
|
|
41
|
-
* @param sourcePath 源文件路径
|
|
42
|
-
* @param outDir 输出目录
|
|
43
|
-
* */
|
|
44
|
-
async compileFile(filePath: string, outDir: string) {
|
|
45
|
-
this.compileTypeScriptFileByBabel(filePath, outDir);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private async compileTypeScriptFileByBabel(sourcePath: string, outDir: string) {
|
|
49
|
-
if (!sourcePath) throw errors.argumentNull("sourcePath");
|
|
50
|
-
if (!outDir) throw errors.argumentNull("outDir");
|
|
51
|
-
if (!fs.existsSync(sourcePath)) throw errors.pathNotExists(sourcePath);
|
|
52
|
-
|
|
53
|
-
let sourceDir = path.dirname(sourcePath);
|
|
54
|
-
let babelConfig = ProjectCompiler.getBabelConfig(this.projectPath, sourceDir);
|
|
55
|
-
let babelOptions = babelConfig.options;
|
|
56
|
-
let babelPath = babelConfig.path;
|
|
57
|
-
babelOptions.filename = sourcePath;
|
|
58
|
-
babelOptions.code = false;
|
|
59
|
-
babelOptions.ast = true;
|
|
60
|
-
|
|
61
|
-
let fileResult = babel.transformFileSync(sourcePath, babelOptions);
|
|
62
|
-
if (!fileResult)
|
|
63
|
-
throw errors.compileError(sourcePath);
|
|
64
|
-
|
|
65
|
-
let ast = fileResult.ast;
|
|
66
|
-
if (!ast)
|
|
67
|
-
throw errors.compileError(sourcePath);
|
|
68
|
-
|
|
69
|
-
new ImportPathRewrite(sourcePath, ast, tsOutExt);
|
|
70
|
-
let r = babel.transformFromAstSync(ast, undefined, {
|
|
71
|
-
filename: sourcePath, plugins: babelOptions.plugins,
|
|
72
|
-
presets: babelOptions.presets, sourceMaps: true
|
|
73
|
-
});
|
|
74
|
-
if (!r || r.code == null)
|
|
75
|
-
throw errors.compileError(sourcePath);
|
|
76
|
-
|
|
77
|
-
let ext = path.extname(sourcePath);
|
|
78
|
-
let outExt = this.fileOutExt(sourcePath);
|
|
79
|
-
let targetPath = path.join(outDir, path.basename(sourcePath).replace(ext, outExt));
|
|
80
|
-
let outDirPath = path.resolve(targetPath, "..");
|
|
81
|
-
|
|
82
|
-
if (r.map) {
|
|
83
|
-
r.map.file = path.basename(targetPath);
|
|
84
|
-
let sources = r.map.sources || [];
|
|
85
|
-
let sourceDir = path.dirname(sourcePath);
|
|
86
|
-
sources.forEach((s, i) => {
|
|
87
|
-
sources[i] = path.relative(outDirPath, path.join(sourceDir, s));
|
|
88
|
-
});
|
|
89
|
-
r.map.sources = sources;
|
|
90
|
-
let mapPath = targetPath + ".map";
|
|
91
|
-
if (!fs.existsSync(outDirPath))
|
|
92
|
-
fs.mkdirSync(outDirPath, { recursive: true });
|
|
93
|
-
|
|
94
|
-
fs.writeFileSync(mapPath, JSON.stringify(r.map));
|
|
95
|
-
r.code += `\n//babelPath:${babelPath}`;
|
|
96
|
-
r.code += "\n//# sourceMappingURL=" + path.basename(sourcePath);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (!fs.existsSync(outDirPath))
|
|
100
|
-
fs.mkdirSync(outDirPath, { recursive: true });
|
|
101
|
-
|
|
102
|
-
fs.writeFileSync(targetPath, r.code);
|
|
103
|
-
|
|
104
|
-
}
|
|
105
19
|
|
|
106
20
|
static loadBabelConfig(configPath: string): babel.TransformOptions {
|
|
107
21
|
if (!configPath) throw errors.argumentNull("configPath");
|
|
@@ -179,90 +93,7 @@ export class ProjectCompiler {
|
|
|
179
93
|
return babelOptions;
|
|
180
94
|
}
|
|
181
95
|
|
|
182
|
-
/**
|
|
183
|
-
* 获取源文件所对应生成文件的扩展名
|
|
184
|
-
* @param file 源文件名
|
|
185
|
-
* */
|
|
186
|
-
private fileOutExt(file: string) {
|
|
187
|
-
let ext = path.extname(file);
|
|
188
|
-
if (ext === ".ts")
|
|
189
|
-
return tsOutExt;
|
|
190
|
-
|
|
191
|
-
if (ext === ".tsx")
|
|
192
|
-
return tsOutExt;
|
|
193
|
-
|
|
194
|
-
return ext;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
private isIgnoredFile(filePath: string) {
|
|
198
|
-
if (!filePath) throw errors.argumentNull("filePath");
|
|
199
|
-
|
|
200
|
-
let isSkip = this.skipFiles.some(pattern => new RegExp(pattern).test(filePath));
|
|
201
|
-
return isSkip;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
generateCode(): void;
|
|
205
|
-
generateCode(sourceDir: string, outDir: string): void;
|
|
206
|
-
generateCode(sourceDir?: string, outDir?: string): void {
|
|
207
|
-
|
|
208
|
-
// if (!sourceDir) throw errors.argumentNull("sourceDir");
|
|
209
|
-
// if (!outDir) throw errors.argumentNull("outDir");
|
|
210
|
-
if (!sourceDir)
|
|
211
|
-
sourceDir = path.join(this.projectPath, this.sourceDirectoryNanme);
|
|
212
|
-
|
|
213
|
-
if (!outDir)
|
|
214
|
-
outDir = path.join(this.projectPath, this.outputDirectoryName);
|
|
215
|
-
|
|
216
|
-
let fileActions = this.fileActions;
|
|
217
|
-
|
|
218
|
-
if (!fs.existsSync(sourceDir))
|
|
219
|
-
throw errors.pathNotExists(sourceDir);
|
|
220
|
-
|
|
221
|
-
let files = fs.readdirSync(sourceDir);
|
|
222
|
-
for (let file of files) {
|
|
223
|
-
let filePath = path.join(sourceDir, file);
|
|
224
|
-
if (!fs.statSync(filePath).isFile()) {
|
|
225
|
-
continue;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
let isSkip = this.isIgnoredFile(filePath);//skipFiles.some(pattern => new RegExp(pattern).test(filePath));
|
|
229
|
-
if (isSkip) {
|
|
230
|
-
console.log(`Skip ${filePath}`);
|
|
231
|
-
continue;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
let ext = path.extname(file);
|
|
235
|
-
let action = fileActions[ext];
|
|
236
|
-
if (action) {
|
|
237
|
-
action(filePath, outDir);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
let dirs = fs.readdirSync(sourceDir);
|
|
242
|
-
for (let dir of dirs) {
|
|
243
|
-
let fullPath = path.join(sourceDir, dir);
|
|
244
|
-
let outDirPath = path.join(outDir, dir);
|
|
245
|
-
if (fs.statSync(fullPath).isDirectory()) {
|
|
246
|
-
this.generateCode(fullPath, outDirPath);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
watchDirectory() {//sourceRoot: string, outRoot: string
|
|
252
|
-
watch(this.sourceDirectoryNanme, { recursive: true }, async (evt, name) => {
|
|
253
|
-
let action = this.fileActions[path.extname(name)];
|
|
254
|
-
let outPath = path.dirname(name).replace(this.sourceDirectoryNanme, this.outputDirectoryName);
|
|
255
|
-
if (action) {
|
|
256
|
-
action(name, outPath);
|
|
257
|
-
}
|
|
258
|
-
})
|
|
259
|
-
}
|
|
260
96
|
|
|
261
97
|
|
|
262
|
-
|
|
263
|
-
nodemon({
|
|
264
|
-
script: `./${this.outputDirectoryName}/main.js`,
|
|
265
|
-
watch: [`./${this.outputDirectoryName}/`],
|
|
266
|
-
})
|
|
267
|
-
}
|
|
98
|
+
|
|
268
99
|
}
|
|
File without changes
|