Skip to content

Commit 5d85cd2

Browse files
committed
refactor: modify way of parsing tsconfig.json
1 parent 6e22085 commit 5d85cd2

File tree

5 files changed

+31
-143
lines changed

5 files changed

+31
-143
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
"node-gyp": "^8.4.1",
7878
"npm": "^6.13.4",
7979
"oracledb": "^4.2.0",
80-
"param-case": "^3.0.4",
8180
"passport": "^0.5.2",
8281
"passport-google-oauth": "^2.0.0",
8382
"path-platform": "^0.11.15",
@@ -101,6 +100,7 @@
101100
"the-answer": "^1.0.0",
102101
"tiny-json-http": "^7.0.2",
103102
"ts-loader": "^8.3.0",
103+
"tsconfck": "^1.2.2",
104104
"tsconfig-paths": "^3.7.0",
105105
"tsconfig-paths-webpack-plugin": "^3.2.0",
106106
"twilio": "^3.23.2",

src/cli.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ async function runCmd (argv, stdout, stderr) {
346346
}
347347
}
348348
if (args["--watch"]) {
349-
ncc.handler(handler);
350-
ncc.rebuild(() => {
349+
const nccWithWatchOption = await ncc
350+
nccWithWatchOption.handler(handler);
351+
nccWithWatchOption.rebuild(() => {
351352
if (ps)
352353
ps.kill();
353354
startTime = Date.now();

src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const defaultPermissions = 0o666;
3232
const relocateLoader = eval('require(__dirname + "/loaders/relocate-loader.js")');
3333

3434
module.exports = ncc;
35-
function ncc (
35+
async function ncc (
3636
entry,
3737
{
3838
cache,
@@ -109,7 +109,7 @@ function ncc (
109109
existingAssetNames.push(`${filename}.cache`);
110110
existingAssetNames.push(`${filename}.cache${ext}`);
111111
}
112-
const compilerOptions = loadTsconfigOptions(tsconfigPath, {
112+
const fullTsconfig = await loadTsconfigOptions(tsconfigPath, {
113113
base: process.cwd(),
114114
start: dirname(entry),
115115
filename: 'tsconfig.json'
@@ -120,7 +120,7 @@ function ncc (
120120
// error if there's no tsconfig in the working directory
121121
try {
122122
const tsconfigPathsOptions = { silent: true }
123-
if (compilerOptions.allowJs) {
123+
if (fullTsconfig.compilerOptions.allowJs) {
124124
tsconfigPathsOptions.extensions = SUPPORTED_EXTENSIONS
125125
}
126126
resolvePlugins.push(new TsconfigPathsPlugin(tsconfigPathsOptions));
@@ -357,7 +357,7 @@ function ncc (
357357
compilerOptions: {
358358
module: 'esnext',
359359
target: 'esnext',
360-
...compilerOptions,
360+
...fullTsconfig.compilerOptions,
361361
allowSyntheticDefaultImports: true,
362362
noEmit: false,
363363
outDir: '//'
@@ -448,7 +448,7 @@ function ncc (
448448

449449
async function finalizeHandler (stats) {
450450
const assets = Object.create(null);
451-
getFlatFiles(mfs.data, assets, relocateLoader.getAssetMeta, compilerOptions);
451+
getFlatFiles(mfs.data, assets, relocateLoader.getAssetMeta, fullTsconfig);
452452
// filter symlinks to existing assets
453453
const symlinks = Object.create(null);
454454
for (const [key, value] of Object.entries(relocateLoader.getSymlinks())) {
@@ -642,17 +642,17 @@ function ncc (
642642
}
643643

644644
// this could be rewritten with actual FS apis / globs, but this is simpler
645-
function getFlatFiles(mfsData, output, getAssetMeta, tsconfigCompilerOptions, curBase = "") {
645+
function getFlatFiles(mfsData, output, getAssetMeta, tsconfig, curBase = "") {
646646
for (const path of Object.keys(mfsData)) {
647647
const item = mfsData[path];
648648
let curPath = `${curBase}/${path}`;
649649
// directory
650-
if (item[""] === true) getFlatFiles(item, output, getAssetMeta, tsconfigCompilerOptions, curPath);
650+
if (item[""] === true) getFlatFiles(item, output, getAssetMeta, tsconfig, curPath);
651651
// file
652652
else if (!curPath.endsWith("/")) {
653653
const meta = getAssetMeta(curPath.slice(1)) || {};
654654
if(curPath.endsWith(".d.ts")) {
655-
const outDir = tsconfigCompilerOptions.outDir ? pathResolve(tsconfigCompilerOptions.outDir) : pathResolve('dist');
655+
const outDir = tsconfig.compilerOptions.outDir ? pathResolve(tsconfig.compilerOptions.outDir) : pathResolve('dist');
656656
curPath = curPath
657657
.replace(outDir, "")
658658
.replace(process.cwd(), "")

src/utils/load-tsconfig-options.js

Lines changed: 14 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const ts = require('typescript');
21
const { join, dirname, resolve } = require('path');
32
const fs = require('fs');
4-
const { paramCase } = require('param-case');
3+
const { parse } = require('tsconfck');
4+
5+
const DEFAULT_TSCONFIG_OPTIONS = {
6+
compilerOptions: {}
7+
};
58

69
/**
710
* @typedef {object} LoadTsconfigInit
@@ -31,107 +34,22 @@ function walkParentDirs({ base, start, filename }) {
3134
return null;
3235
}
3336

34-
/**
35-
* @param {ts.CompilerOptions} options
36-
* @param {string | undefined} key
37-
* @param {(value: string) => string} [callback]
38-
* @returns {string | undefined}
39-
*/
40-
function convertEnumCompilerOptions(enumCompilerOptions, key, callback) {
41-
if (key == null) {
42-
return undefined;
43-
}
44-
const value = enumCompilerOptions[key];
45-
return typeof callback === 'function' ? callback(value) : value;
46-
}
47-
48-
/**
49-
* @param {string} value
50-
* @returns {string}
51-
*/
52-
function toLowerCase(value) {
53-
return value.toLowerCase();
54-
}
55-
56-
/**
57-
* @param {ts.NewLineKind} newLine
58-
* @returns {string | undefined}
59-
*/
60-
function normalizeNewLineOption(newLine) {
61-
switch (newLine) {
62-
case ts.NewLineKind.CarriageReturnLineFeed:
63-
return 'crlf';
64-
case ts.NewLineKind.LineFeed:
65-
return 'lf';
66-
default:
67-
return undefined;
68-
}
69-
}
70-
71-
/**
72-
* @param {ts.ModuleResolutionKind} moduleResolution
73-
* @returns {string | undefined}
74-
*/
75-
function normalizeModuleResolutionOption(moduleResolution) {
76-
switch (moduleResolution) {
77-
case ts.ModuleResolutionKind.Classic:
78-
return 'classic';
79-
case ts.ModuleResolutionKind.NodeJs:
80-
return 'node';
81-
case ts.ModuleResolutionKind.Node12:
82-
return 'node12';
83-
case ts.ModuleResolutionKind.NodeNext:
84-
return 'nodenext';
85-
default:
86-
return undefined;
87-
}
88-
}
89-
90-
/**
91-
* @param {ts.CompilerOptions} options
92-
* @returns {ts.CompilerOptions}
93-
*/
94-
function normalizeCompilerOptions(options) {
95-
if (options.importsNotUsedAsValues != null) {
96-
options.importsNotUsedAsValues = convertEnumCompilerOptions(
97-
ts.ImportsNotUsedAsValues,
98-
options.importsNotUsedAsValues,
99-
toLowerCase,
100-
);
101-
}
102-
if (options.jsx != null) {
103-
options.jsx = convertEnumCompilerOptions(ts.JsxEmit, options.jsx, paramCase);
104-
}
105-
if (options.module != null) {
106-
options.module = convertEnumCompilerOptions(ts.ModuleKind, options.module, toLowerCase);
107-
}
108-
if (options.moduleResolution != null) {
109-
options.moduleResolution = normalizeModuleResolutionOption(options.moduleResolution);
110-
}
111-
if (options.newLine != null) {
112-
options.newLine = normalizeNewLineOption(options.newLine);
113-
}
114-
if (options.target != null) {
115-
options.target = convertEnumCompilerOptions(ts.ScriptTarget, options.target, toLowerCase);
116-
}
117-
return options;
118-
}
119-
12037
/**
12138
* @param {string | undefined} configPath
12239
* @param {LoadTsconfigInit}
123-
* @returns {ts.CompilerOptions}
40+
* @returns {Promise<object>}
12441
*/
125-
exports.loadTsconfigOptions = function (configPath, { base, start, filename }) {
42+
exports.loadTsconfigOptions = async function (configPath, { base, start, filename }) {
12643
// throw error if `configPath` does not exist
12744
const tsconfig = configPath != null ? resolve(configPath) : walkParentDirs({ base, start, filename });
12845
if (tsconfig == null) {
129-
return {};
46+
return DEFAULT_TSCONFIG_OPTIONS;
13047
}
131-
const content = ts.readConfigFile(tsconfig, ts.sys.readFile);
132-
if (content.error != null || content.config == null) {
133-
return {};
48+
try {
49+
const result = await parse(tsconfig);
50+
return result.tsconfig;
51+
} catch (error) {
52+
console.error(error);
53+
throw error;
13454
}
135-
const { options } = ts.parseJsonConfigFileContent(content.config, ts.sys, dirname(tsconfig));
136-
return normalizeCompilerOptions(options);
13755
};

yarn.lock

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5512,14 +5512,6 @@ dot-case@^1.1.0:
55125512
dependencies:
55135513
sentence-case "^1.1.2"
55145514

5515-
dot-case@^3.0.4:
5516-
version "3.0.4"
5517-
resolved "https://registry.yarnpkg.com/dot-case/-/dot-case-3.0.4.tgz#9b2b670d00a431667a8a75ba29cd1b98809ce751"
5518-
integrity sha512-Kv5nKlh6yRrdrGvxeJ2e5y2eRUpkUosIW4A2AS38zwSz27zu7ufDwQPi5Jhs3XAlGNetl3bmnGhQsMtkKJnj3w==
5519-
dependencies:
5520-
no-case "^3.0.4"
5521-
tslib "^2.0.3"
5522-
55235515
dot-prop@^4.2.1:
55245516
version "4.2.1"
55255517
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-4.2.1.tgz#45884194a71fc2cda71cbb4bceb3a4dd2f433ba4"
@@ -9949,13 +9941,6 @@ lower-case@^1.1.0, lower-case@^1.1.1, lower-case@^1.1.2:
99499941
resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-1.1.4.tgz#9a2cabd1b9e8e0ae993a4bf7d5875c39c42e8eac"
99509942
integrity sha1-miyr0bno4K6ZOkv31YdcOcQujqw=
99519943

9952-
lower-case@^2.0.2:
9953-
version "2.0.2"
9954-
resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-2.0.2.tgz#6fa237c63dbdc4a82ca0fd882e4722dc5e634e28"
9955-
integrity sha512-7fm3l3NAF9WfN6W3JOmf5drwpVqX78JtoGJ3A6W0a6ZnldM41w2fV5D490psKFTpMds8TJse/eHLFFsNHHjHgg==
9956-
dependencies:
9957-
tslib "^2.0.3"
9958-
99599944
lowercase-keys@1.0.0:
99609945
version "1.0.0"
99619946
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-1.0.0.tgz#4e3366b39e7f5457e35f1324bdf6f88d0bfc7306"
@@ -10762,14 +10747,6 @@ nice-try@^1.0.4:
1076210747
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
1076310748
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
1076410749

10765-
no-case@^3.0.4:
10766-
version "3.0.4"
10767-
resolved "https://registry.yarnpkg.com/no-case/-/no-case-3.0.4.tgz#d361fd5c9800f558551a8369fc0dcd4662b6124d"
10768-
integrity sha512-fgAN3jGAh+RoxUGZHTSOLJIqUc2wmoBwGR4tbpNAKmmovFoWq0OdRkb0VkldReO2a2iBT/OEulG9XSUc10r3zg==
10769-
dependencies:
10770-
lower-case "^2.0.2"
10771-
tslib "^2.0.3"
10772-
1077310750
node-abi@^2.21.0:
1077410751
version "2.30.0"
1077510752
resolved "https://registry.yarnpkg.com/node-abi/-/node-abi-2.30.0.tgz#8be53bf3e7945a34eea10e0fc9a5982776cf550b"
@@ -11734,14 +11711,6 @@ param-case@^1.1.0:
1173411711
dependencies:
1173511712
sentence-case "^1.1.2"
1173611713

11737-
param-case@^3.0.4:
11738-
version "3.0.4"
11739-
resolved "https://registry.yarnpkg.com/param-case/-/param-case-3.0.4.tgz#7d17fe4aa12bde34d4a77d91acfb6219caad01c5"
11740-
integrity sha512-RXlj7zCYokReqWpOPH9oYivUzLYZ5vAPIfEmCTNViosC78F8F0H9y7T7gG2M39ymgutxF5gcFEsyZQSph9Bp3A==
11741-
dependencies:
11742-
dot-case "^3.0.4"
11743-
tslib "^2.0.3"
11744-
1174511714
parents@^1.0.0, parents@^1.0.1:
1174611715
version "1.0.1"
1174711716
resolved "https://registry.yarnpkg.com/parents/-/parents-1.0.1.tgz#fedd4d2bf193a77745fe71e371d73c3307d9c751"
@@ -14858,6 +14827,11 @@ ts-loader@^8.3.0:
1485814827
micromatch "^4.0.0"
1485914828
semver "^7.3.4"
1486014829

14830+
tsconfck@^1.2.2:
14831+
version "1.2.2"
14832+
resolved "https://registry.yarnpkg.com/tsconfck/-/tsconfck-1.2.2.tgz#3f7ac55bcd16b8d89ff05ba8e27057f3375828c8"
14833+
integrity sha512-x5YpjOqjJnMs1EsJvQBQbrysrY32eGoZRRr5YvbN1hwlrXKc7jiphCOUrT7xbFdOWk8sh+EtMYbGPbTO8rDmcw==
14834+
1486114835
tsconfig-paths-webpack-plugin@^3.2.0:
1486214836
version "3.5.1"
1486314837
resolved "https://registry.yarnpkg.com/tsconfig-paths-webpack-plugin/-/tsconfig-paths-webpack-plugin-3.5.1.tgz#e4dbf492a20dca9caab60086ddacb703afc2b726"
@@ -14892,11 +14866,6 @@ tslib@^2.0.0, tslib@^2.0.1, tslib@^2.1.0, tslib@^2.2.0:
1489214866
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.0.tgz#803b8cdab3e12ba581a4ca41c8839bbb0dacb09e"
1489314867
integrity sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==
1489414868

14895-
tslib@^2.0.3:
14896-
version "2.3.1"
14897-
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
14898-
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==
14899-
1490014869
tslib@~2.1.0:
1490114870
version "2.1.0"
1490214871
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a"

0 commit comments

Comments
 (0)