diff --git a/package.json b/package.json index 4273c6f9..bf65cf00 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,6 @@ "node-gyp": "^8.4.1", "npm": "^6.13.4", "oracledb": "^4.2.0", - "param-case": "^3.0.4", "passport": "^0.5.2", "passport-google-oauth": "^2.0.0", "path-platform": "^0.11.15", @@ -101,6 +100,7 @@ "the-answer": "^1.0.0", "tiny-json-http": "^7.0.2", "ts-loader": "^8.3.0", + "tsconfck": "^1.2.2", "tsconfig-paths": "^3.7.0", "tsconfig-paths-webpack-plugin": "^3.2.0", "twilio": "^3.23.2", diff --git a/src/cli.js b/src/cli.js index 3c335377..9bbbf7de 100755 --- a/src/cli.js +++ b/src/cli.js @@ -346,8 +346,9 @@ async function runCmd (argv, stdout, stderr) { } } if (args["--watch"]) { - ncc.handler(handler); - ncc.rebuild(() => { + const nccWithWatchOption = await ncc + nccWithWatchOption.handler(handler); + nccWithWatchOption.rebuild(() => { if (ps) ps.kill(); startTime = Date.now(); diff --git a/src/index.js b/src/index.js index 5d05a54d..c3bf8af7 100644 --- a/src/index.js +++ b/src/index.js @@ -32,7 +32,7 @@ const defaultPermissions = 0o666; const relocateLoader = eval('require(__dirname + "/loaders/relocate-loader.js")'); module.exports = ncc; -function ncc ( +async function ncc ( entry, { cache, @@ -109,7 +109,7 @@ function ncc ( existingAssetNames.push(`${filename}.cache`); existingAssetNames.push(`${filename}.cache${ext}`); } - const compilerOptions = loadTsconfigOptions(tsconfigPath, { + const fullTsconfig = await loadTsconfigOptions(tsconfigPath, { base: process.cwd(), start: dirname(entry), filename: 'tsconfig.json' @@ -120,7 +120,7 @@ function ncc ( // error if there's no tsconfig in the working directory try { const tsconfigPathsOptions = { silent: true } - if (compilerOptions.allowJs) { + if (fullTsconfig.compilerOptions.allowJs) { tsconfigPathsOptions.extensions = SUPPORTED_EXTENSIONS } resolvePlugins.push(new TsconfigPathsPlugin(tsconfigPathsOptions)); @@ -357,7 +357,7 @@ function ncc ( compilerOptions: { module: 'esnext', target: 'esnext', - ...compilerOptions, + ...fullTsconfig.compilerOptions, allowSyntheticDefaultImports: true, noEmit: false, outDir: '//' @@ -448,7 +448,7 @@ function ncc ( async function finalizeHandler (stats) { const assets = Object.create(null); - getFlatFiles(mfs.data, assets, relocateLoader.getAssetMeta, compilerOptions); + getFlatFiles(mfs.data, assets, relocateLoader.getAssetMeta, fullTsconfig); // filter symlinks to existing assets const symlinks = Object.create(null); for (const [key, value] of Object.entries(relocateLoader.getSymlinks())) { @@ -642,17 +642,17 @@ function ncc ( } // this could be rewritten with actual FS apis / globs, but this is simpler -function getFlatFiles(mfsData, output, getAssetMeta, tsconfigCompilerOptions, curBase = "") { +function getFlatFiles(mfsData, output, getAssetMeta, tsconfig, curBase = "") { for (const path of Object.keys(mfsData)) { const item = mfsData[path]; let curPath = `${curBase}/${path}`; // directory - if (item[""] === true) getFlatFiles(item, output, getAssetMeta, tsconfigCompilerOptions, curPath); + if (item[""] === true) getFlatFiles(item, output, getAssetMeta, tsconfig, curPath); // file else if (!curPath.endsWith("/")) { const meta = getAssetMeta(curPath.slice(1)) || {}; if(curPath.endsWith(".d.ts")) { - const outDir = tsconfigCompilerOptions.outDir ? pathResolve(tsconfigCompilerOptions.outDir) : pathResolve('dist'); + const outDir = tsconfig.compilerOptions.outDir ? pathResolve(tsconfig.compilerOptions.outDir) : pathResolve('dist'); curPath = curPath .replace(outDir, "") .replace(process.cwd(), "") diff --git a/src/utils/load-tsconfig-options.js b/src/utils/load-tsconfig-options.js index 3d1b43f9..e7a997b2 100644 --- a/src/utils/load-tsconfig-options.js +++ b/src/utils/load-tsconfig-options.js @@ -1,7 +1,10 @@ -const ts = require('typescript'); const { join, dirname, resolve } = require('path'); const fs = require('fs'); -const { paramCase } = require('param-case'); +const { parse } = require('tsconfck'); + +const DEFAULT_TSCONFIG_OPTIONS = { + compilerOptions: {} +}; /** * @typedef {object} LoadTsconfigInit @@ -31,107 +34,22 @@ function walkParentDirs({ base, start, filename }) { return null; } -/** - * @param {ts.CompilerOptions} options - * @param {string | undefined} key - * @param {(value: string) => string} [callback] - * @returns {string | undefined} - */ -function convertEnumCompilerOptions(enumCompilerOptions, key, callback) { - if (key == null) { - return undefined; - } - const value = enumCompilerOptions[key]; - return typeof callback === 'function' ? callback(value) : value; -} - -/** - * @param {string} value - * @returns {string} - */ -function toLowerCase(value) { - return value.toLowerCase(); -} - -/** - * @param {ts.NewLineKind} newLine - * @returns {string | undefined} - */ -function normalizeNewLineOption(newLine) { - switch (newLine) { - case ts.NewLineKind.CarriageReturnLineFeed: - return 'crlf'; - case ts.NewLineKind.LineFeed: - return 'lf'; - default: - return undefined; - } -} - -/** - * @param {ts.ModuleResolutionKind} moduleResolution - * @returns {string | undefined} - */ -function normalizeModuleResolutionOption(moduleResolution) { - switch (moduleResolution) { - case ts.ModuleResolutionKind.Classic: - return 'classic'; - case ts.ModuleResolutionKind.NodeJs: - return 'node'; - case ts.ModuleResolutionKind.Node12: - return 'node12'; - case ts.ModuleResolutionKind.NodeNext: - return 'nodenext'; - default: - return undefined; - } -} - -/** - * @param {ts.CompilerOptions} options - * @returns {ts.CompilerOptions} - */ -function normalizeCompilerOptions(options) { - if (options.importsNotUsedAsValues != null) { - options.importsNotUsedAsValues = convertEnumCompilerOptions( - ts.ImportsNotUsedAsValues, - options.importsNotUsedAsValues, - toLowerCase, - ); - } - if (options.jsx != null) { - options.jsx = convertEnumCompilerOptions(ts.JsxEmit, options.jsx, paramCase); - } - if (options.module != null) { - options.module = convertEnumCompilerOptions(ts.ModuleKind, options.module, toLowerCase); - } - if (options.moduleResolution != null) { - options.moduleResolution = normalizeModuleResolutionOption(options.moduleResolution); - } - if (options.newLine != null) { - options.newLine = normalizeNewLineOption(options.newLine); - } - if (options.target != null) { - options.target = convertEnumCompilerOptions(ts.ScriptTarget, options.target, toLowerCase); - } - return options; -} - /** * @param {string | undefined} configPath * @param {LoadTsconfigInit} - * @returns {ts.CompilerOptions} + * @returns {Promise} */ -exports.loadTsconfigOptions = function (configPath, { base, start, filename }) { +exports.loadTsconfigOptions = async function (configPath, { base, start, filename }) { // throw error if `configPath` does not exist const tsconfig = configPath != null ? resolve(configPath) : walkParentDirs({ base, start, filename }); if (tsconfig == null) { - return {}; + return DEFAULT_TSCONFIG_OPTIONS; } - const content = ts.readConfigFile(tsconfig, ts.sys.readFile); - if (content.error != null || content.config == null) { - return {}; + try { + const result = await parse(tsconfig); + return result.tsconfig; + } catch (error) { + console.error(error); + throw error; } - const { options } = ts.parseJsonConfigFileContent(content.config, ts.sys, dirname(tsconfig)); - return normalizeCompilerOptions(options); }; diff --git a/yarn.lock b/yarn.lock index 89c8f6c9..337e5ad7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5512,14 +5512,6 @@ dot-case@^1.1.0: dependencies: sentence-case "^1.1.2" -dot-case@^3.0.4: - version "3.0.4" - resolved "https://registry.yarnpkg.com/dot-case/-/dot-case-3.0.4.tgz#9b2b670d00a431667a8a75ba29cd1b98809ce751" - integrity sha512-Kv5nKlh6yRrdrGvxeJ2e5y2eRUpkUosIW4A2AS38zwSz27zu7ufDwQPi5Jhs3XAlGNetl3bmnGhQsMtkKJnj3w== - dependencies: - no-case "^3.0.4" - tslib "^2.0.3" - dot-prop@^4.2.1: version "4.2.1" 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: resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-1.1.4.tgz#9a2cabd1b9e8e0ae993a4bf7d5875c39c42e8eac" integrity sha1-miyr0bno4K6ZOkv31YdcOcQujqw= -lower-case@^2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-2.0.2.tgz#6fa237c63dbdc4a82ca0fd882e4722dc5e634e28" - integrity sha512-7fm3l3NAF9WfN6W3JOmf5drwpVqX78JtoGJ3A6W0a6ZnldM41w2fV5D490psKFTpMds8TJse/eHLFFsNHHjHgg== - dependencies: - tslib "^2.0.3" - lowercase-keys@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-1.0.0.tgz#4e3366b39e7f5457e35f1324bdf6f88d0bfc7306" @@ -10762,14 +10747,6 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== -no-case@^3.0.4: - version "3.0.4" - resolved "https://registry.yarnpkg.com/no-case/-/no-case-3.0.4.tgz#d361fd5c9800f558551a8369fc0dcd4662b6124d" - integrity sha512-fgAN3jGAh+RoxUGZHTSOLJIqUc2wmoBwGR4tbpNAKmmovFoWq0OdRkb0VkldReO2a2iBT/OEulG9XSUc10r3zg== - dependencies: - lower-case "^2.0.2" - tslib "^2.0.3" - node-abi@^2.21.0: version "2.30.0" resolved "https://registry.yarnpkg.com/node-abi/-/node-abi-2.30.0.tgz#8be53bf3e7945a34eea10e0fc9a5982776cf550b" @@ -11734,14 +11711,6 @@ param-case@^1.1.0: dependencies: sentence-case "^1.1.2" -param-case@^3.0.4: - version "3.0.4" - resolved "https://registry.yarnpkg.com/param-case/-/param-case-3.0.4.tgz#7d17fe4aa12bde34d4a77d91acfb6219caad01c5" - integrity sha512-RXlj7zCYokReqWpOPH9oYivUzLYZ5vAPIfEmCTNViosC78F8F0H9y7T7gG2M39ymgutxF5gcFEsyZQSph9Bp3A== - dependencies: - dot-case "^3.0.4" - tslib "^2.0.3" - parents@^1.0.0, parents@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/parents/-/parents-1.0.1.tgz#fedd4d2bf193a77745fe71e371d73c3307d9c751" @@ -14858,6 +14827,11 @@ ts-loader@^8.3.0: micromatch "^4.0.0" semver "^7.3.4" +tsconfck@^1.2.2: + version "1.2.2" + resolved "https://registry.yarnpkg.com/tsconfck/-/tsconfck-1.2.2.tgz#3f7ac55bcd16b8d89ff05ba8e27057f3375828c8" + integrity sha512-x5YpjOqjJnMs1EsJvQBQbrysrY32eGoZRRr5YvbN1hwlrXKc7jiphCOUrT7xbFdOWk8sh+EtMYbGPbTO8rDmcw== + tsconfig-paths-webpack-plugin@^3.2.0: version "3.5.1" 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: resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.0.tgz#803b8cdab3e12ba581a4ca41c8839bbb0dacb09e" integrity sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg== -tslib@^2.0.3: - version "2.3.1" - resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01" - integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw== - tslib@~2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a"