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
package/out/index.d.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
+
import copyFile from "./modules/actions/copy-file";
|
|
1
2
|
export { generateCode, watchDirectory } from "./modules/compile";
|
|
2
3
|
export { run } from "./modules/run";
|
|
4
|
+
export declare const options: {
|
|
5
|
+
fileActions: Record<string, import("./types").FileAction>;
|
|
6
|
+
skipFiles: Record<string, RegExp[]>;
|
|
7
|
+
};
|
|
8
|
+
export { create as createTsCompiler } from "./modules/actions/ts-compile";
|
|
9
|
+
export { create as createBielCompiler } from "./modules/actions/babel-compile";
|
|
10
|
+
export { copyFile };
|
|
11
|
+
export { FileAction } from "./types";
|
package/out/index.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.run = exports.watchDirectory = exports.generateCode = void 0;
|
|
6
|
+
exports.copyFile = exports.createBielCompiler = exports.createTsCompiler = exports.options = exports.run = exports.watchDirectory = exports.generateCode = void 0;
|
|
7
|
+
const configs_1 = __importDefault(require("./modules/configs"));
|
|
8
|
+
const copy_file_1 = __importDefault(require("./modules/actions/copy-file"));
|
|
9
|
+
exports.copyFile = copy_file_1.default;
|
|
4
10
|
var compile_1 = require("./modules/compile");
|
|
5
11
|
Object.defineProperty(exports, "generateCode", { enumerable: true, get: function () { return compile_1.generateCode; } });
|
|
6
12
|
Object.defineProperty(exports, "watchDirectory", { enumerable: true, get: function () { return compile_1.watchDirectory; } });
|
|
7
13
|
var run_1 = require("./modules/run");
|
|
8
14
|
Object.defineProperty(exports, "run", { enumerable: true, get: function () { return run_1.run; } });
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
exports.options = configs_1.default;
|
|
16
|
+
var ts_compile_1 = require("./modules/actions/ts-compile");
|
|
17
|
+
Object.defineProperty(exports, "createTsCompiler", { enumerable: true, get: function () { return ts_compile_1.create; } });
|
|
18
|
+
var babel_compile_1 = require("./modules/actions/babel-compile");
|
|
19
|
+
Object.defineProperty(exports, "createBielCompiler", { enumerable: true, get: function () { return babel_compile_1.create; } });
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.create = create;
|
|
37
|
+
const babel = __importStar(require("@babel/core"));
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const errors_1 = require("../errors");
|
|
41
|
+
const project_compiler_1 = require("../project-compiler");
|
|
42
|
+
const outExt = project_compiler_1.ProjectCompiler.tsOutExt;
|
|
43
|
+
function create(babelOptions) {
|
|
44
|
+
const action = (sourcePath, outputPath, projectPath) => {
|
|
45
|
+
if (!babelOptions) {
|
|
46
|
+
let bablePath;
|
|
47
|
+
let sourceDir = path.dirname(sourcePath);
|
|
48
|
+
if (projectPath) {
|
|
49
|
+
let c = project_compiler_1.ProjectCompiler.getBabelConfig(projectPath, sourceDir);
|
|
50
|
+
bablePath = c.path;
|
|
51
|
+
babelOptions = c.options;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
babelOptions = project_compiler_1.ProjectCompiler.getDefaultBabelConfig();
|
|
55
|
+
bablePath = '';
|
|
56
|
+
}
|
|
57
|
+
babelOptions.filename = sourcePath;
|
|
58
|
+
babelOptions.code = false;
|
|
59
|
+
babelOptions.ast = true;
|
|
60
|
+
}
|
|
61
|
+
return compileFile(sourcePath, outputPath, projectPath, babelOptions);
|
|
62
|
+
};
|
|
63
|
+
return action;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 编译特定文件,并生成到制定目录
|
|
67
|
+
* @param sourcePath 源文件路径
|
|
68
|
+
* @param outputPath 输出目录
|
|
69
|
+
* @param projectPath 项目路径
|
|
70
|
+
* */
|
|
71
|
+
async function compileFile(sourcePath, outputPath, projectPath, babelOptions) {
|
|
72
|
+
if (!sourcePath)
|
|
73
|
+
throw errors_1.errors.argumentNull("sourcePath");
|
|
74
|
+
if (!outputPath)
|
|
75
|
+
throw errors_1.errors.argumentNull("outputPath");
|
|
76
|
+
if (!projectPath)
|
|
77
|
+
throw errors_1.errors.argumentNull("projectPath");
|
|
78
|
+
// if (!fs.existsSync(sourcePath)) throw errors.pathNotExists(sourcePath);
|
|
79
|
+
if (!fs.existsSync(sourcePath)) {
|
|
80
|
+
console.warn(`Path not exists: ${sourcePath}`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
let sourceDir = path.dirname(sourcePath);
|
|
84
|
+
// let babelOptions: babel.TransformOptions;
|
|
85
|
+
let bablePath;
|
|
86
|
+
//= projectPath ?
|
|
87
|
+
// ProjectCompiler.getBabelConfig(projectPath, sourceDir) : ProjectCompiler.getDefaultBabelConfig();
|
|
88
|
+
if (projectPath) {
|
|
89
|
+
// let c = ProjectCompiler.getBabelConfig(projectPath, sourceDir);
|
|
90
|
+
// bablePath = c.path;
|
|
91
|
+
// babelOptions = c.options;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
// babelOptions = ProjectCompiler.getDefaultBabelConfig();
|
|
95
|
+
// bablePath = '';
|
|
96
|
+
}
|
|
97
|
+
// babelOptions.filename = sourcePath;
|
|
98
|
+
// babelOptions.code = false;
|
|
99
|
+
// babelOptions.ast = true;
|
|
100
|
+
// let fileResult = babel.transformFileSync(sourcePath, {
|
|
101
|
+
// filename: sourcePath, code: false, ast: true, plugins,
|
|
102
|
+
// presets
|
|
103
|
+
// });
|
|
104
|
+
let fileResult = babel.transformFileSync(sourcePath, babelOptions);
|
|
105
|
+
if (!fileResult)
|
|
106
|
+
throw errors_1.errors.compileError(sourcePath);
|
|
107
|
+
let ast = fileResult.ast;
|
|
108
|
+
if (!ast)
|
|
109
|
+
throw errors_1.errors.compileError(sourcePath);
|
|
110
|
+
new ImportPathRewrite(sourcePath, ast);
|
|
111
|
+
let r = babel.transformFromAstSync(ast, undefined, {
|
|
112
|
+
filename: sourcePath, plugins: babelOptions.plugins,
|
|
113
|
+
presets: babelOptions.presets, sourceMaps: babelOptions.sourceMaps
|
|
114
|
+
});
|
|
115
|
+
if (!r || r.code == null)
|
|
116
|
+
throw errors_1.errors.compileError(sourcePath);
|
|
117
|
+
let ext = extname(sourcePath);
|
|
118
|
+
let outExt = fileOutExt(sourcePath);
|
|
119
|
+
let targetPath = path.join(outputPath, path.basename(sourcePath).replace(ext, outExt)); //sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
|
|
120
|
+
let outDirPath = path.resolve(targetPath, "..");
|
|
121
|
+
let mapPath = targetPath + ".map";
|
|
122
|
+
if (r.map) {
|
|
123
|
+
r.map.file = path.basename(targetPath);
|
|
124
|
+
r.map.sources = [path.relative(outputPath, sourcePath)];
|
|
125
|
+
if (!fs.existsSync(outDirPath))
|
|
126
|
+
fs.mkdirSync(outDirPath, { recursive: true });
|
|
127
|
+
fs.writeFileSync(mapPath, JSON.stringify(r.map));
|
|
128
|
+
// r.code += `\n//babel file path = ${bablePath}`;
|
|
129
|
+
r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
|
|
130
|
+
}
|
|
131
|
+
else if (fs.existsSync(mapPath)) {
|
|
132
|
+
fs.rmSync(targetPath);
|
|
133
|
+
}
|
|
134
|
+
if (!fs.existsSync(outDirPath))
|
|
135
|
+
fs.mkdirSync(outDirPath, { recursive: true });
|
|
136
|
+
fs.writeFileSync(targetPath, r.code);
|
|
137
|
+
}
|
|
138
|
+
function extname(file) {
|
|
139
|
+
// let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
|
|
140
|
+
let ext = path.extname(file);
|
|
141
|
+
return ext;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 获取源文件所对应生成文件的扩展名
|
|
145
|
+
* @param file 源文件名
|
|
146
|
+
* */
|
|
147
|
+
function fileOutExt(file) {
|
|
148
|
+
let ext = extname(file);
|
|
149
|
+
if (ext === ".ts")
|
|
150
|
+
return outExt;
|
|
151
|
+
if (ext === ".tsx")
|
|
152
|
+
return outExt;
|
|
153
|
+
return ext;
|
|
154
|
+
}
|
|
155
|
+
class ImportPathRewrite {
|
|
156
|
+
filePath;
|
|
157
|
+
constructor(filePath, node) {
|
|
158
|
+
this.filePath = filePath;
|
|
159
|
+
this.traverse(node);
|
|
160
|
+
}
|
|
161
|
+
traverse(node) {
|
|
162
|
+
switch (node.type) {
|
|
163
|
+
case "Program":
|
|
164
|
+
node.body.forEach(c => this.traverse(c));
|
|
165
|
+
break;
|
|
166
|
+
case "File":
|
|
167
|
+
this.traverse(node.program);
|
|
168
|
+
break;
|
|
169
|
+
case "ImportDeclaration":
|
|
170
|
+
if (node.source.type === "StringLiteral" && node.source.value.startsWith(".")) {
|
|
171
|
+
let ext = extname(node.source.value);
|
|
172
|
+
if (ext != outExt) {
|
|
173
|
+
let dir = path.dirname(this.filePath);
|
|
174
|
+
let fullPath = path.join(dir, node.source.value);
|
|
175
|
+
console.log(`ImportDeclaration: ${fullPath} -> ${ext}`);
|
|
176
|
+
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
|
|
177
|
+
node.source.value = node.source.value + "/index";
|
|
178
|
+
}
|
|
179
|
+
node.source.value += outExt;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -15,18 +15,28 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
36
|
const errors_1 = require("../errors");
|
|
27
37
|
const fs = __importStar(require("fs"));
|
|
28
38
|
const path = __importStar(require("path"));
|
|
29
|
-
let copyFile = (filePath, outPath
|
|
39
|
+
let copyFile = (filePath, outPath) => {
|
|
30
40
|
if (!filePath)
|
|
31
41
|
throw errors_1.errors.argumentNull("filePath");
|
|
32
42
|
if (!outPath)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const less_1 = __importDefault(require("less"));
|
|
7
|
+
const errors_1 = require("../errors");
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
/**
|
|
11
|
+
* 编译特定Less文件,并生成到指定目录
|
|
12
|
+
* @param filePath 源文件路径,绝对路径
|
|
13
|
+
* @param outDir 输出目录,相对于项目路径
|
|
14
|
+
* @param projectPath 项目路径,绝对路径
|
|
15
|
+
*/
|
|
16
|
+
const compileFile = async (filePath, outDir, projectPath) => {
|
|
17
|
+
if (!filePath)
|
|
18
|
+
throw errors_1.errors.argumentNull('filePath');
|
|
19
|
+
if (!outDir)
|
|
20
|
+
throw errors_1.errors.argumentNull('outDir');
|
|
21
|
+
if (!projectPath)
|
|
22
|
+
throw errors_1.errors.argumentNull('projectPath');
|
|
23
|
+
let content = fs_1.default.readFileSync(filePath, 'utf-8');
|
|
24
|
+
let result = await less_1.default.render(content, { filename: filePath });
|
|
25
|
+
let ext = path_1.default.extname(filePath);
|
|
26
|
+
let outExt = '.css';
|
|
27
|
+
let targetPath = path_1.default.join(projectPath, outDir, path_1.default.basename(filePath).replace(ext, outExt));
|
|
28
|
+
let outDirPath = path_1.default.resolve(targetPath, "..");
|
|
29
|
+
if (!fs_1.default.existsSync(outDirPath))
|
|
30
|
+
fs_1.default.mkdirSync(outDirPath, { recursive: true });
|
|
31
|
+
fs_1.default.writeFileSync(targetPath, result.css);
|
|
32
|
+
};
|
|
33
|
+
exports.default = compileFile;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.compileFile = void 0;
|
|
37
|
+
const sass = __importStar(require("sass"));
|
|
38
|
+
const errors_1 = require("../errors");
|
|
39
|
+
const fs = __importStar(require("fs"));
|
|
40
|
+
const path = __importStar(require("path"));
|
|
41
|
+
/**
|
|
42
|
+
* 编译特定SCSS文件,并生成到指定目录
|
|
43
|
+
* @param filePath 源文件路径,绝对路径
|
|
44
|
+
* @param outDir 输出目录,相对于项目路径
|
|
45
|
+
* @param projectPath 项目路径,绝对路径
|
|
46
|
+
*/
|
|
47
|
+
const compileFile = (filePath, outDir, projectPath) => {
|
|
48
|
+
if (!filePath)
|
|
49
|
+
throw errors_1.errors.argumentNull('filePath');
|
|
50
|
+
if (!outDir)
|
|
51
|
+
throw errors_1.errors.argumentNull('outDir');
|
|
52
|
+
if (!projectPath)
|
|
53
|
+
throw errors_1.errors.argumentNull('projectPath');
|
|
54
|
+
let content = fs.readFileSync(filePath, 'utf-8');
|
|
55
|
+
let result = sass.renderSync({ data: content });
|
|
56
|
+
let ext = path.extname(filePath);
|
|
57
|
+
let outExt = '.css';
|
|
58
|
+
let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
|
|
59
|
+
let outDirPath = path.resolve(targetPath, "..");
|
|
60
|
+
if (!fs.existsSync(outDirPath))
|
|
61
|
+
fs.mkdirSync(outDirPath, { recursive: true });
|
|
62
|
+
fs.writeFileSync(targetPath, result.css.toString());
|
|
63
|
+
};
|
|
64
|
+
exports.compileFile = compileFile;
|
|
65
|
+
exports.default = exports.compileFile;
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @param filePath 源文件路径,绝对路径
|
|
5
|
-
* @param outDir 输出目录,相对于项目路径
|
|
6
|
-
* @param projectPath 项目路径,绝对路径
|
|
7
|
-
* */
|
|
8
|
-
export declare function compileFile(filePath: string, outDir: string, projectPath: string): Promise<void>;
|
|
2
|
+
import { FileAction } from '../../types';
|
|
3
|
+
export declare function create(compilerOptions?: ts.CompilerOptions): FileAction;
|
|
9
4
|
export declare function getTypescriptConfig(projectPath: string, directoryPath: string): ts.CompilerOptions;
|
|
10
5
|
export declare function findTypeScriptConfigPath(projectPath: string, directoryPath: string): string | null;
|
|
@@ -15,15 +15,25 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
36
|
+
exports.create = create;
|
|
27
37
|
exports.getTypescriptConfig = getTypescriptConfig;
|
|
28
38
|
exports.findTypeScriptConfigPath = findTypeScriptConfigPath;
|
|
29
39
|
const ts = __importStar(require("typescript"));
|
|
@@ -32,25 +42,31 @@ const fs = __importStar(require("fs"));
|
|
|
32
42
|
const path = __importStar(require("path"));
|
|
33
43
|
const project_compiler_1 = require("../project-compiler");
|
|
34
44
|
const TS_CONFIG_FILE = 'tsconfig.json';
|
|
45
|
+
function create(compilerOptions) {
|
|
46
|
+
const action = async (filePath, outDir, projectPath) => {
|
|
47
|
+
if (!compilerOptions) {
|
|
48
|
+
compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
49
|
+
}
|
|
50
|
+
return compileFile(filePath, outDir, projectPath, compilerOptions);
|
|
51
|
+
};
|
|
52
|
+
return action;
|
|
53
|
+
}
|
|
35
54
|
/**
|
|
36
55
|
* 编译特定文件,并生成到制定目录
|
|
37
56
|
* @param filePath 源文件路径,绝对路径
|
|
38
57
|
* @param outDir 输出目录,相对于项目路径
|
|
39
58
|
* @param projectPath 项目路径,绝对路径
|
|
40
59
|
* */
|
|
41
|
-
async function compileFile(filePath, outDir, projectPath) {
|
|
60
|
+
async function compileFile(filePath, outDir, projectPath, compilerOptions) {
|
|
42
61
|
if (!filePath)
|
|
43
62
|
throw errors_1.errors.argumentNull('sourcePath');
|
|
44
63
|
if (!outDir)
|
|
45
64
|
throw errors_1.errors.argumentNull('outDir');
|
|
46
65
|
if (!projectPath)
|
|
47
66
|
throw errors_1.errors.argumentNull('projectPath');
|
|
48
|
-
if (!path.isAbsolute(filePath))
|
|
49
|
-
throw errors_1.errors.notAbsolutePath(filePath);
|
|
50
67
|
let content = fs.readFileSync(filePath, 'utf-8');
|
|
51
|
-
let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
68
|
+
// let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
52
69
|
let result = ts.transpileModule(content, { compilerOptions });
|
|
53
|
-
console.log(result.outputText);
|
|
54
70
|
let ext = path.extname(filePath);
|
|
55
71
|
let outExt = project_compiler_1.ProjectCompiler.tsOutExt;
|
|
56
72
|
let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
|
|
@@ -58,6 +74,12 @@ async function compileFile(filePath, outDir, projectPath) {
|
|
|
58
74
|
if (!fs.existsSync(outDirPath))
|
|
59
75
|
fs.mkdirSync(outDirPath, { recursive: true });
|
|
60
76
|
fs.writeFileSync(targetPath, result.outputText);
|
|
77
|
+
// 编译声明文件
|
|
78
|
+
if (compilerOptions.declaration && result.outputText) {
|
|
79
|
+
const d = ts.transpileDeclaration(content, { compilerOptions });
|
|
80
|
+
const declPath = targetPath.replace(outExt, '.d.ts');
|
|
81
|
+
fs.writeFileSync(declPath, d.outputText);
|
|
82
|
+
}
|
|
61
83
|
}
|
|
62
84
|
function getTypescriptConfig(projectPath, directoryPath) {
|
|
63
85
|
let configPath = findTypeScriptConfigPath(projectPath, directoryPath);
|
package/out/modules/compile.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
+
import { FileAction } from "../types";
|
|
2
|
+
type Callback = (filePath: string, outDir: string, projectPath: string) => void;
|
|
1
3
|
/** 将 sourceDir 目录下所有文件生成到 outDir */
|
|
2
|
-
export declare function generateCode(sourceDir:
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
export declare function generateCode(sourceDir: Options): void;
|
|
5
|
+
export declare function generateCode(sourceDir: string, outDir: string, projectPath: string, callback?: Callback): void;
|
|
6
|
+
/** 监听 sourceDir 目录下所有文件变化,并生成到 outDir */
|
|
7
|
+
export declare function watchDirectory(sourceDir: Options): void;
|
|
8
|
+
export declare function watchDirectory(sourceDir: string, outDir: string, projectPath: string, callback?: Callback): void;
|
|
5
9
|
export declare function isIgnoredFile(filePath: string): boolean;
|
|
10
|
+
type Options = {
|
|
11
|
+
sourceDir: string;
|
|
12
|
+
outDir: string;
|
|
13
|
+
projectPath: string;
|
|
14
|
+
skipFiles?: RegExp[];
|
|
15
|
+
fileAction?: (fileName: string) => FileAction | undefined;
|
|
16
|
+
callback?: Callback;
|
|
17
|
+
};
|
|
18
|
+
export {};
|