Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use cwd for tsconfig.json #774

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"isomorphic-unfetch": "^3.0.0",
"jest": "^27.0.6",
"jimp": "^0.16.1",
"json5": "^2.2.0",
"jugglingdb": "2.0.1",
"koa": "^2.6.2",
"leveldown": "^6.0.0",
Expand All @@ -72,6 +73,7 @@
"mailgun": "^0.5.0",
"mariadb": "^2.0.1-beta",
"memcached": "^2.2.2",
"memory-fs": "^0.5.0",
"mkdirp": "^1.0.4",
"mongoose": "^5.3.12",
"mysql": "^2.16.0",
Expand Down Expand Up @@ -101,11 +103,10 @@
"terser": "^5.6.1",
"the-answer": "^1.0.0",
"tiny-json-http": "^7.0.2",
"ts-loader": "^8.3.0",
"tsconfig-paths": "^3.7.0",
"tsconfig-paths-webpack-plugin": "^3.2.0",
"ts-loader": "^9.2.5",
"tsconfig-paths-webpack-plugin": "^3.5.1",
"twilio": "^3.23.2",
"typescript": "^4.4.2",
"typescript": "^4.4.3",
"vm2": "^3.6.6",
"vue": "^2.5.17",
"vue-server-renderer": "^2.5.17",
Expand Down
45 changes: 20 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const { join, dirname, extname, relative, resolve: pathResolve } = require("path
const webpack = require("webpack");
const MemoryFS = require("memory-fs");
const terser = require("terser");
const tsconfigPaths = require("tsconfig-paths");
const { loadTsconfig } = require("tsconfig-paths/lib/tsconfig-loader");
const JSON5 = require("json5");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const shebangRegEx = require('./utils/shebang');
const nccCacheDir = require("./utils/ncc-cache-dir");
Expand Down Expand Up @@ -109,26 +108,22 @@ function ncc (
// add TsconfigPathsPlugin to support `paths` resolution in tsconfig
// we need to catch here because the plugin will
// error if there's no tsconfig in the working directory
let fullTsconfig = {};
let compilerOptions = {}
try {
const configFileAbsolutePath = walkParentDirs({
base: process.cwd(),
start: dirname(entry),
filename: 'tsconfig.json',
});
fullTsconfig = loadTsconfig(configFileAbsolutePath) || {
compilerOptions: {}
};

const tsconfigPathsOptions = { silent: true }
if (fullTsconfig.compilerOptions.allowJs) {
tsconfigPathsOptions.extensions = SUPPORTED_EXTENSIONS
const configPath = join(process.cwd(), 'tsconfig.json');
const contents = fs.readFileSync(configPath, 'utf8')
const tsconfig = JSON5.parse(contents);
if (tsconfig && tsconfig.compilerOptions) {
compilerOptions = tsconfig.compilerOptions;
}
resolvePlugins.push(new TsconfigPathsPlugin(tsconfigPathsOptions));

if (tsconfig.resultType === "success") {
tsconfigMatchPath = tsconfigPaths.createMatchPath(tsconfig.absoluteBaseUrl, tsconfig.paths);
const opts = {
silent: true,
configFile: configPath,
extensions: compilerOptions.allowJs
? SUPPORTED_EXTENSIONS
: SUPPORTED_EXTENSIONS.filter(ext => ext !== '.js')
}
resolvePlugins.push(new TsconfigPathsPlugin(opts));
} catch (e) {}

resolvePlugins.push({
Expand Down Expand Up @@ -338,10 +333,10 @@ function ncc (
transpileOnly,
compiler: eval('__dirname + "/typescript.js"'),
compilerOptions: {
allowSyntheticDefaultImports: true,
module: 'esnext',
target: 'esnext',
...fullTsconfig.compilerOptions,
allowSyntheticDefaultImports: true,
...compilerOptions,
noEmit: false,
outDir: '//'
}
Expand Down Expand Up @@ -431,7 +426,7 @@ function ncc (

async function finalizeHandler (stats) {
const assets = Object.create(null);
getFlatFiles(mfs.data, assets, relocateLoader.getAssetMeta, fullTsconfig);
getFlatFiles(mfs.data, assets, relocateLoader.getAssetMeta, compilerOptions);
// filter symlinks to existing assets
const symlinks = Object.create(null);
for (const [key, value] of Object.entries(relocateLoader.getSymlinks())) {
Expand Down Expand Up @@ -625,17 +620,17 @@ function ncc (
}

// this could be rewritten with actual FS apis / globs, but this is simpler
function getFlatFiles(mfsData, output, getAssetMeta, tsconfig, curBase = "") {
function getFlatFiles(mfsData, output, getAssetMeta, compilerOptions, curBase = "") {
for (const path of Object.keys(mfsData)) {
const item = mfsData[path];
let curPath = `${curBase}/${path}`;
// directory
if (item[""] === true) getFlatFiles(item, output, getAssetMeta, tsconfig, curPath);
if (item[""] === true) getFlatFiles(item, output, getAssetMeta, compilerOptions, curPath);
// file
else if (!curPath.endsWith("/")) {
const meta = getAssetMeta(curPath.substr(1)) || {};
if(curPath.endsWith(".d.ts")) {
const outDir = tsconfig.compilerOptions.outDir ? pathResolve(tsconfig.compilerOptions.outDir) : pathResolve('dist');
const outDir = compilerOptions.outDir ? pathResolve(compilerOptions.outDir) : pathResolve('dist');
curPath = curPath
.replace(outDir, "")
.replace(process.cwd(), "")
Expand Down
60 changes: 17 additions & 43 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3326,11 +3326,6 @@ bcryptjs@^2.1.0:
resolved "https://registry.yarnpkg.com/bcryptjs/-/bcryptjs-2.4.3.tgz#9ab5627b93e60621ff7cdac5da9733027df1d0cb"
integrity sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms=

big.js@^5.2.2:
version "5.2.2"
resolved "https://registry.yarnpkg.com/big.js/-/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328"
integrity sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ==

big.js@^6.0.0:
version "6.1.1"
resolved "https://registry.yarnpkg.com/big.js/-/big.js-6.1.1.tgz#63b35b19dc9775c94991ee5db7694880655d5537"
Expand Down Expand Up @@ -5416,11 +5411,6 @@ emoji-regex@^8.0.0:
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
integrity sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==

emojis-list@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-3.0.0.tgz#5570662046ad29e2e916e71aae260abdff4f6a78"
integrity sha512-/kyM18EfinwXZbno9FyUGeFh87KC8HRQBQGildHZbEuRyWFOmv1U10o9BBp8XVZDVNNuQKyIGIu5ZYAAXJ0V2Q==

encodeurl@^1.0.2, encodeurl@~1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
Expand Down Expand Up @@ -5460,14 +5450,13 @@ engine.io@~5.1.1:
engine.io-parser "~4.0.0"
ws "~7.4.2"

enhanced-resolve@^4.0.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-4.5.0.tgz#2f3cfd84dbe3b487f18f2db2ef1e064a571ca5ec"
integrity sha512-Nv9m36S/vxpsI+Hc4/ZGRs0n9mXqSWGGq49zxb/cJfPAQMbUtttJAlNPS4AQzaBdw/pKskw5bMbekT/Y7W/Wlg==
enhanced-resolve@^5.0.0:
version "5.8.2"
resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-5.8.2.tgz#15ddc779345cbb73e97c611cd00c01c1e7bf4d8b"
integrity sha512-F27oB3WuHDzvR2DOGNTaYy0D5o0cnrv8TeI482VM4kYgQd/FT9lUQwuNsJ0oOHtBUq7eiW5ytqzp7nBFknL+GA==
dependencies:
graceful-fs "^4.1.2"
memory-fs "^0.5.0"
tapable "^1.0.0"
graceful-fs "^4.2.4"
tapable "^2.2.0"

enhanced-resolve@^5.7.0:
version "5.7.0"
Expand Down Expand Up @@ -9396,15 +9385,6 @@ loader-runner@^4.2.0:
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-4.2.0.tgz#d7022380d66d14c5fb1d496b89864ebcfd478384"
integrity sha512-92+huvxMvYlMzMt0iIOukcwYBFpkYJdpl2xsZ7LrlayO7E8SOv+JJUEK17B/dJIHAOLMfh2dZZ/Y18WgmGtYNw==

loader-utils@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-2.0.0.tgz#e4cace5b816d425a166b5f097e10cd12b36064b0"
integrity sha512-rP4F0h2RaWSvPEkD7BLDFQnvSf+nK+wr3ESUjNTyAGobqrijmW92zc+SO6d4p4B1wh7+B/Jg1mkQe5NYUEHtHQ==
dependencies:
big.js "^5.2.2"
emojis-list "^3.0.0"
json5 "^2.1.2"

locate-path@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e"
Expand Down Expand Up @@ -14216,11 +14196,6 @@ syntax-error@^1.1.1:
dependencies:
acorn-node "^1.2.0"

tapable@^1.0.0:
version "1.1.3"
resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.3.tgz#a1fccc06b58db61fd7a45da2da44f5f3a3e67ba2"
integrity sha512-4WK/bYZmj8xLr+HUCODHGF1ZFzsYffasLUgEiMBY4fgtltdO6B4WJtlSbPaDTLpYTcGVwM2qLnFTICEcNxs3kA==

tapable@^2.1.1, tapable@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/tapable/-/tapable-2.2.0.tgz#5c373d281d9c672848213d0e037d1c4165ab426b"
Expand Down Expand Up @@ -14578,18 +14553,17 @@ ts-invariant@^0.4.0:
dependencies:
tslib "^1.9.3"

ts-loader@^8.3.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-8.3.0.tgz#83360496d6f8004fab35825279132c93412edf33"
integrity sha512-MgGly4I6cStsJy27ViE32UoqxPTN9Xly4anxxVyaIWR+9BGxboV4EyJBGfR3RePV7Ksjj3rHmPZJeIt+7o4Vag==
ts-loader@^9.2.5:
version "9.2.5"
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-9.2.5.tgz#127733a5e9243bf6dafcb8aa3b8a266d8041dca9"
integrity sha512-al/ATFEffybdRMUIr5zMEWQdVnCGMUA9d3fXJ8dBVvBlzytPvIszoG9kZoR+94k6/i293RnVOXwMaWbXhNy9pQ==
dependencies:
chalk "^4.1.0"
enhanced-resolve "^4.0.0"
loader-utils "^2.0.0"
enhanced-resolve "^5.0.0"
micromatch "^4.0.0"
semver "^7.3.4"

tsconfig-paths-webpack-plugin@^3.2.0:
tsconfig-paths-webpack-plugin@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/tsconfig-paths-webpack-plugin/-/tsconfig-paths-webpack-plugin-3.5.1.tgz#e4dbf492a20dca9caab60086ddacb703afc2b726"
integrity sha512-n5CMlUUj+N5pjBhBACLq4jdr9cPTitySCjIosoQm0zwK99gmrcTGAfY9CwxRFT9+9OleNWXPRUcxsKP4AYExxQ==
Expand All @@ -14598,7 +14572,7 @@ tsconfig-paths-webpack-plugin@^3.2.0:
enhanced-resolve "^5.7.0"
tsconfig-paths "^3.9.0"

tsconfig-paths@^3.7.0, tsconfig-paths@^3.9.0:
tsconfig-paths@^3.9.0:
version "3.10.1"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.10.1.tgz#79ae67a68c15289fdf5c51cb74f397522d795ed7"
integrity sha512-rETidPDgCpltxF7MjBZlAFPUHv5aHH2MymyPvh+vEyWAED4Eb/WeMbsnD/JDr4OKPOA1TssDHgIcpTN5Kh0p6Q==
Expand Down Expand Up @@ -14718,10 +14692,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.2.tgz#6d618640d430e3569a1dfb44f7d7e600ced3ee86"
integrity sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ==
typescript@^4.4.3:
version "4.4.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.3.tgz#bdc5407caa2b109efd4f82fe130656f977a29324"
integrity sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==

uglify-es@^3.3.9:
version "3.3.9"
Expand Down