UNPKG

3.14 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.maybeJs = exports.updateTsConfigsToJs = exports.toJS = void 0;
4const schematics_1 = require("@angular-devkit/schematics");
5const core_1 = require("@angular-devkit/core");
6const ast_utils_1 = require("../ast-utils");
7const typescript_1 = require("../../utilities/typescript");
8let tsModule;
9function toJS() {
10 if (!tsModule) {
11 tsModule = (0, typescript_1.ensureTypescript)();
12 }
13 const { transpile, JsxEmit, ScriptTarget } = tsModule;
14 return (0, schematics_1.chain)([
15 (0, schematics_1.forEach)((0, schematics_1.when)((path) => path.endsWith('.ts') || path.endsWith('.tsx'), (entry) => {
16 const original = entry.content.toString('utf-8');
17 const result = transpile(original, {
18 allowJs: true,
19 jsx: JsxEmit.Preserve,
20 target: ScriptTarget.ESNext,
21 });
22 return {
23 content: Buffer.from(result, 'utf-8'),
24 path: (0, core_1.normalize)(entry.path.replace(/\.tsx?$/, '.js')),
25 };
26 })),
27 ]);
28}
29exports.toJS = toJS;
30function updateTsConfigsToJs(options) {
31 const paths = {
32 tsConfig: (0, core_1.normalize)(`${options.projectRoot}/tsconfig.json`),
33 tsConfigLib: (0, core_1.normalize)(`${options.projectRoot}/tsconfig.lib.json`),
34 tsConfigApp: (0, core_1.normalize)(`${options.projectRoot}/tsconfig.app.json`),
35 };
36 const getProjectType = (tree) => {
37 if (tree.exists(paths.tsConfigApp)) {
38 return 'application';
39 }
40 if (tree.exists(paths.tsConfigLib)) {
41 return 'library';
42 }
43 throw new schematics_1.SchematicsException(`project is missing tsconfig.lib.json or tsconfig.app.json`);
44 };
45 const getConfigFileForUpdate = (tree) => {
46 const projectType = getProjectType(tree);
47 if (projectType === 'library') {
48 return paths.tsConfigLib;
49 }
50 if (projectType === 'application') {
51 return paths.tsConfigApp;
52 }
53 };
54 return (0, schematics_1.chain)([
55 (0, ast_utils_1.updateJsonInTree)(paths.tsConfig, (json) => {
56 if (json.compilerOptions) {
57 json.compilerOptions.allowJs = true;
58 }
59 else {
60 json.compilerOptions = { allowJs: true };
61 }
62 return json;
63 }),
64 (tree) => {
65 const updateConfigPath = getConfigFileForUpdate(tree);
66 return (0, ast_utils_1.updateJsonInTree)(updateConfigPath, (json) => {
67 json.include = uniq([...json.include, '**/*.js']);
68 json.exclude = uniq([...json.exclude, '**/*.spec.js']);
69 return json;
70 });
71 },
72 ]);
73}
74exports.updateTsConfigsToJs = updateTsConfigsToJs;
75const uniq = (value) => [...new Set(value)];
76function maybeJs(options, path) {
77 return options.js && (path.endsWith('.ts') || path.endsWith('.tsx'))
78 ? path.replace(/\.tsx?$/, '.js')
79 : path;
80}
81exports.maybeJs = maybeJs;
82//# sourceMappingURL=to-js.js.map
\No newline at end of file