From 4deed526097ada6eb5506e9adb37190447463dcc Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 13 Sep 2021 19:45:07 -0400 Subject: [PATCH 1/3] Fix missing variable declaration --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index f7dff022..32504586 100644 --- a/src/index.js +++ b/src/index.js @@ -126,6 +126,7 @@ function ncc ( } resolvePlugins.push(new TsconfigPathsPlugin(tsconfigPathsOptions)); + const tsconfig = tsconfigPaths.loadConfig(); if (tsconfig.resultType === "success") { tsconfigMatchPath = tsconfigPaths.createMatchPath(tsconfig.absoluteBaseUrl, tsconfig.paths); } From ef9d66c7fdb4c53312234dac8bb9160da6be51f7 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 16 Sep 2021 18:35:27 -0400 Subject: [PATCH 2/3] Always use cwd for tsconfig.json --- package.json | 9 ++++---- src/index.js | 46 ++++++++++++++++++---------------------- yarn.lock | 60 +++++++++++++++------------------------------------- 3 files changed, 42 insertions(+), 73 deletions(-) diff --git a/package.json b/package.json index d472ba02..8199dad3 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", diff --git a/src/index.js b/src/index.js index 32504586..ab79f509 100644 --- a/src/index.js +++ b/src/index.js @@ -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"); @@ -109,27 +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)); - - const tsconfig = tsconfigPaths.loadConfig(); - 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({ @@ -339,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: '//' } @@ -432,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())) { @@ -626,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(), "") diff --git a/yarn.lock b/yarn.lock index f232deac..59be0292 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" @@ -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" @@ -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== @@ -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== @@ -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" From 85e8d2a9d2f87d1b850e0bd86560c53382eb14e0 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 16 Sep 2021 19:36:18 -0400 Subject: [PATCH 3/3] Use custom ts paths resolve plugin --- package.json | 1 - src/index.js | 39 +++--- src/jsconfig-paths-plugin.js | 226 +++++++++++++++++++++++++++++++++++ yarn.lock | 26 ---- 4 files changed, 246 insertions(+), 46 deletions(-) create mode 100644 src/jsconfig-paths-plugin.js diff --git a/package.json b/package.json index 8199dad3..dcfc1fa7 100644 --- a/package.json +++ b/package.json @@ -104,7 +104,6 @@ "the-answer": "^1.0.0", "tiny-json-http": "^7.0.2", "ts-loader": "^9.2.5", - "tsconfig-paths-webpack-plugin": "^3.5.1", "twilio": "^3.23.2", "typescript": "^4.4.3", "vm2": "^3.6.6", diff --git a/src/index.js b/src/index.js index ab79f509..39f5050c 100644 --- a/src/index.js +++ b/src/index.js @@ -6,10 +6,10 @@ const webpack = require("webpack"); const MemoryFS = require("memory-fs"); const terser = require("terser"); const JSON5 = require("json5"); -const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin"); const shebangRegEx = require('./utils/shebang'); const nccCacheDir = require("./utils/ncc-cache-dir"); -const LicenseWebpackPlugin = require('license-webpack-plugin').LicenseWebpackPlugin; +const { JsConfigPathsPlugin } = require('./jsconfig-paths-plugin'); +const { LicenseWebpackPlugin } = require('license-webpack-plugin'); const { version: nccVersion } = require('../package.json'); const { hasTypeModule } = require('./utils/has-type-module'); @@ -104,28 +104,28 @@ function ncc ( existingAssetNames.push(`${filename}.cache`); existingAssetNames.push(`${filename}.cache${ext}`); } - const resolvePlugins = []; - // 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 compilerOptions = {} + + let tsconfig = {}; try { 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; - } - const opts = { - silent: true, - configFile: configPath, - extensions: compilerOptions.allowJs - ? SUPPORTED_EXTENSIONS - : SUPPORTED_EXTENSIONS.filter(ext => ext !== '.js') - } - resolvePlugins.push(new TsconfigPathsPlugin(opts)); + tsconfig = JSON5.parse(contents); } catch (e) {} + const resolvePlugins = []; + const resolveModules = []; + const compilerOptions = tsconfig.compilerOptions || {}; + + if (compilerOptions.baseUrl) { + const resolvedBaseUrl = pathResolve(process.cwd(), compilerOptions.baseUrl); + resolveModules.push(resolvedBaseUrl); + if (compilerOptions.paths) { + resolvePlugins.push( + new JsConfigPathsPlugin(compilerOptions.paths, resolvedBaseUrl) + ) + } + } + resolvePlugins.push({ apply(resolver) { const resolve = resolver.resolve; @@ -289,6 +289,7 @@ function ncc ( // webpack defaults to `module` and `main`, but that's // not really what node.js supports, so we reset it mainFields: ["main"], + modules: resolveModules, plugins: resolvePlugins }, // https://github.com/vercel/ncc/pull/29#pullrequestreview-177152175 diff --git a/src/jsconfig-paths-plugin.js b/src/jsconfig-paths-plugin.js new file mode 100644 index 00000000..b02b8e55 --- /dev/null +++ b/src/jsconfig-paths-plugin.js @@ -0,0 +1,226 @@ +/** + * This webpack resolver is largely from Next.js + * https://github.com/vercel/next.js/blob/29ab433222adc879e7ccaa23b29bed674e123ec4/packages/next/build/webpack/plugins/jsconfig-paths-plugin.ts#L1 + */ +const path = require('path') + +const asterisk = 0x2a + +function hasZeroOrOneAsteriskCharacter(str) { + let seenAsterisk = false + for (let i = 0; i < str.length; i++) { + if (str.charCodeAt(i) === asterisk) { + if (!seenAsterisk) { + seenAsterisk = true + } else { + // have already seen asterisk + return false + } + } + } + return true +} + +/** + * Determines whether a path starts with a relative path component (i.e. `.` or `..`). + */ +function pathIsRelative(testPath) { + return /^\.\.?($|[\\/])/.test(testPath) +} + +function tryParsePattern(pattern) { + // This should be verified outside of here and a proper error thrown. + const indexOfStar = pattern.indexOf('*') + return indexOfStar === -1 + ? undefined + : { + prefix: pattern.substr(0, indexOfStar), + suffix: pattern.substr(indexOfStar + 1), + } + } + +function isPatternMatch({ prefix, suffix }, candidate) { + return ( + candidate.length >= prefix.length + suffix.length && + candidate.startsWith(prefix) && + candidate.endsWith(suffix) + ) +} + +/** Return the object corresponding to the best pattern to match `candidate`. */ +function findBestPatternMatch( + values, + getPattern, + candidate +) { + let matchedValue + // use length of prefix as betterness criteria + let longestMatchPrefixLength = -1 + + for (const v of values) { + const pattern = getPattern(v) + if ( + isPatternMatch(pattern, candidate) && + pattern.prefix.length > longestMatchPrefixLength + ) { + longestMatchPrefixLength = pattern.prefix.length + matchedValue = v + } + } + + return matchedValue +} + +/** +* patternStrings contains both pattern strings (containing "*") and regular strings. +* Return an exact match if possible, or a pattern match, or undefined. +* (These are verified by verifyCompilerOptions to have 0 or 1 "*" characters.) +*/ +function matchPatternOrExact( + patternStrings, + candidate +) { + const patterns = [] + for (const patternString of patternStrings) { + if (!hasZeroOrOneAsteriskCharacter(patternString)) continue + const pattern = tryParsePattern(patternString) + if (pattern) { + patterns.push(pattern) + } else if (patternString === candidate) { + // pattern was matched as is - no need to search further + return patternString + } + } + + return findBestPatternMatch(patterns, (_) => _, candidate) +} + +/** +* Tests whether a value is string +*/ +function isString(text) { + return typeof text === 'string' +} + +/** +* Given that candidate matches pattern, returns the text matching the '*'. +* E.g.: matchedText(tryParsePattern("foo*baz"), "foobarbaz") === "bar" +*/ +function matchedText(pattern, candidate) { + return candidate.substring( + pattern.prefix.length, + candidate.length - pattern.suffix.length + ) +} + +function patternText({ prefix, suffix }) { + return `${prefix}*${suffix}` +} + +const NODE_MODULES_REGEX = /node_modules/ + +class JsConfigPathsPlugin { + constructor(paths, resolvedBaseUrl) { + this.paths = paths + this.resolvedBaseUrl = resolvedBaseUrl + console.log('tsconfig.json or jsconfig.json paths: %O', paths) + console.log('resolved baseUrl: %s', resolvedBaseUrl) + } + apply(resolver) { + const paths = this.paths + const pathsKeys = Object.keys(paths) + + // If no aliases are added bail out + if (pathsKeys.length === 0) { + console.log('paths are empty, bailing out') + return + } + + const baseDirectory = this.resolvedBaseUrl + const target = resolver.ensureHook('resolve') + resolver + .getHook('described-resolve') + .tapPromise( + 'JsConfigPathsPlugin', + async (request, resolveContext) => { + const moduleName = request.request + + // Exclude node_modules from paths support (speeds up resolving) + if (request.path.match(NODE_MODULES_REGEX)) { + console.log('skipping request as it is inside node_modules %s', moduleName) + return + } + + if ( + path.posix.isAbsolute(moduleName) || + (process.platform === 'win32' && path.win32.isAbsolute(moduleName)) + ) { + console.log('skipping request as it is an absolute path %s', moduleName) + return + } + + if (pathIsRelative(moduleName)) { + console.log('skipping request as it is a relative path %s', moduleName) + return + } + + // console.log('starting to resolve request %s', moduleName) + + // If the module name does not match any of the patterns in `paths` we hand off resolving to webpack + const matchedPattern = matchPatternOrExact(pathsKeys, moduleName) + if (!matchedPattern) { + console.log('moduleName did not match any paths pattern %s', moduleName) + return + } + + const matchedStar = isString(matchedPattern) + ? undefined + : matchedText(matchedPattern, moduleName) + const matchedPatternText = isString(matchedPattern) + ? matchedPattern + : patternText(matchedPattern) + + let triedPaths = [] + + for (const subst of paths[matchedPatternText]) { + const curPath = matchedStar + ? subst.replace('*', matchedStar) + : subst + + // Ensure .d.ts is not matched + if (curPath.endsWith('.d.ts')) { + continue + } + + const candidate = path.join(baseDirectory, curPath) + const [err, result] = await new Promise((resolve) => { + const obj = Object.assign({}, request, { + request: candidate, + }) + resolver.doResolve( + target, + obj, + `Aliased with tsconfig.json or jsconfig.json ${matchedPatternText} to ${candidate}`, + resolveContext, + (resolverErr, resolverResult) => { + resolve([resolverErr, resolverResult]) + } + ) + }) + + // There's multiple paths values possible, so we first have to iterate them all first before throwing an error + if (err || result === undefined) { + triedPaths.push(candidate) + continue + } + + return result + } + } + ) + } +} + +module.exports = { + JsConfigPathsPlugin +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 59be0292..16bbf155 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5458,14 +5458,6 @@ enhanced-resolve@^5.0.0: graceful-fs "^4.2.4" tapable "^2.2.0" -enhanced-resolve@^5.7.0: - version "5.7.0" - resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-5.7.0.tgz#525c5d856680fbd5052de453ac83e32049958b5c" - integrity sha512-6njwt/NsZFUKhM6j9U8hzVyD4E4r0x7NQzhTCbcWOJ0IQjNSAoalWmb0AE51Wn+fwan5qVESWi7t2ToBxs9vrw== - dependencies: - graceful-fs "^4.2.4" - tapable "^2.2.0" - enhanced-resolve@^5.8.0: version "5.8.0" resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-5.8.0.tgz#d9deae58f9d3773b6a111a5a46831da5be5c9ac0" @@ -14563,24 +14555,6 @@ ts-loader@^9.2.5: micromatch "^4.0.0" semver "^7.3.4" -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== - dependencies: - chalk "^4.1.0" - enhanced-resolve "^5.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== - dependencies: - json5 "^2.2.0" - minimist "^1.2.0" - strip-bom "^3.0.0" - tslib@1.10.0: version "1.10.0" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"