UNPKG

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