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

Use custom ts paths resolve plugin #775

Draft
wants to merge 3 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
8 changes: 4 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,9 @@
"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",
"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
60 changes: 28 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ 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 TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const JSON5 = require("json5");
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');

Expand Down Expand Up @@ -105,31 +104,27 @@ 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 fullTsconfig = {};
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
}
resolvePlugins.push(new TsconfigPathsPlugin(tsconfigPathsOptions));
let tsconfig = {};
try {
const configPath = join(process.cwd(), 'tsconfig.json');
const contents = fs.readFileSync(configPath, 'utf8')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using ts api to parse the config? It has type hint

const config = ts.readConfigFile(tsconfigPath, ts.sys.readFile).config;
const compilerOptions = ts.parseJsonConfigFileContent(
        config,
        ts.sys,
        './',
).options;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I'll probably go with the other PR instead #777

tsconfig = JSON5.parse(contents);
} catch (e) {}

if (tsconfig.resultType === "success") {
tsconfigMatchPath = tsconfigPaths.createMatchPath(tsconfig.absoluteBaseUrl, tsconfig.paths);
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)
)
}
} catch (e) {}
}

resolvePlugins.push({
apply(resolver) {
Expand Down Expand Up @@ -294,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
Expand Down Expand Up @@ -338,10 +334,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 +427,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 +621,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
226 changes: 226 additions & 0 deletions src/jsconfig-paths-plugin.js
Original file line number Diff line number Diff line change
@@ -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
}
Loading