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

major: replace ts-loader with swc #926

Draft
wants to merge 7 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 @@ -27,6 +27,7 @@
"@google-cloud/firestore": "^4.14.0",
"@sentry/node": "^6.10.0",
"@slack/web-api": "^6.3.0",
"@swc/wasm": "^1.2.224",
"@tensorflow/tfjs-node": "^3.12.0",
"@vercel/webpack-asset-relocator-loader": "1.7.2",
"analytics-node": "^6.0.0",
Expand Down Expand Up @@ -62,6 +63,7 @@
"isomorphic-unfetch": "^3.0.0",
"jest": "^27.5.1",
"jimp": "^0.16.1",
"json5": "^2.2.1",
"jugglingdb": "2.0.1",
"koa": "^2.6.2",
"leveldown": "^6.0.0",
Expand Down Expand Up @@ -96,15 +98,13 @@
"socket.io": "^4.1.3",
"source-map-support": "^0.5.9",
"stripe": "^8.167.0",
"swc-loader": "^0.2.3",
"swig": "^1.4.2",
"terser": "^5.6.1",
"the-answer": "^1.0.0",
"tiny-json-http": "^7.0.2",
"ts-loader": "^9.3.0",
"tsconfig-paths": "^3.7.0",
"tsconfig-paths-webpack-plugin": "^3.2.0",
"twilio": "^3.23.2",
"typescript": "^4.4.2",
"typescript": "^4.7.4",
"vm2": "^3.6.6",
"vue": "^2.5.17",
"vue-server-renderer": "^2.5.17",
Expand Down
16 changes: 8 additions & 8 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ async function main() {
);
checkUnknownAssets('shebang-loader', Object.keys(shebangLoaderAssets));

const { code: tsLoader, assets: tsLoaderAssets } = await ncc(
__dirname + "/../src/loaders/ts-loader",
const { code: swcLoader, assets: swcLoaderAssets } = await ncc(
__dirname + "/../src/loaders/swc-loader",
{
filename: "ts-loader.js",
filename: "swc-loader.js",
minify,
cache,
v8cache,
noAssetBuilds: true
},
);
checkUnknownAssets('ts-loader', Object.keys(tsLoaderAssets).filter(asset => !asset.startsWith('lib/') && !asset.startsWith('typescript/lib')));
checkUnknownAssets('swc-loader', Object.keys(swcLoaderAssets).filter(asset => asset !== 'wasm_bg.wasm'));

const { code: stringifyLoader, assets: stringifyLoaderAssets } = await ncc(
__dirname + "/../src/loaders/stringify-loader",
Expand Down Expand Up @@ -92,24 +92,24 @@ async function main() {
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache", sourcemapAssets["sourcemap-register.js.cache"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache", relocateLoaderAssets["relocate-loader.js.cache"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache", shebangLoaderAssets["shebang-loader.js.cache"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache", tsLoaderAssets["ts-loader.js.cache"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/swc-loader.js.cache", swcLoaderAssets["swc-loader.js.cache"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/wasm_bg.wasm", swcLoaderAssets["wasm_bg.wasm"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/stringify-loader.js.cache", stringifyLoaderAssets["stringify-loader.js.cache"].source);

writeFileSync(__dirname + "/../dist/ncc/cli.js.cache.js", cliAssets["cli.js.cache.js"].source);
writeFileSync(__dirname + "/../dist/ncc/index.js.cache.js", indexAssets["index.js.cache.js"].source);
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache.js", sourcemapAssets["sourcemap-register.js.cache.js"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache.js", relocateLoaderAssets["relocate-loader.js.cache.js"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache.js", shebangLoaderAssets["shebang-loader.js.cache.js"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache.js", tsLoaderAssets["ts-loader.js.cache.js"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/swc-loader.js.cache.js", swcLoaderAssets["swc-loader.js.cache.js"].source);
writeFileSync(__dirname + "/../dist/ncc/loaders/stringify-loader.js.cache.js", stringifyLoaderAssets["stringify-loader.js.cache.js"].source);

writeFileSync(__dirname + "/../dist/ncc/cli.js", cli, { mode: 0o777 });
writeFileSync(__dirname + "/../dist/ncc/index.js", index);
writeFileSync(__dirname + "/../dist/ncc/typescript.js", readFileSync(__dirname + "/../src/typescript.js"));
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js", sourcemapSupport);
writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js", relocateLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js", shebangLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js", tsLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/swc-loader.js", swcLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/stringify-loader.js", stringifyLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/uncacheable.js", readFileSync(__dirname + "/../src/loaders/uncacheable.js"));
writeFileSync(__dirname + "/../dist/ncc/loaders/empty-loader.js", readFileSync(__dirname + "/../src/loaders/empty-loader.js"));
Expand Down
126 changes: 64 additions & 62 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ 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 shebangRegEx = require('./utils/shebang');
const nccCacheDir = require("./utils/ncc-cache-dir");
const LicenseWebpackPlugin = require('license-webpack-plugin').LicenseWebpackPlugin;
const JSON5 = require("json5");
const { LicenseWebpackPlugin } = require('license-webpack-plugin');
const { version: nccVersion } = require('../package.json');
const { hasTypeModule } = require('./utils/has-type-module');

Expand All @@ -21,18 +19,18 @@ const SUPPORTED_EXTENSIONS = [".js", ".json", ".node", ".mjs", ".ts", ".tsx"];

const hashOf = name => {
return crypto
.createHash("sha256")
.update(name)
.digest("hex")
.slice(0, 10);
.createHash("sha256")
.update(name)
.digest("hex")
.slice(0, 10);
}

const defaultPermissions = 0o666;

const relocateLoader = eval('require(__dirname + "/loaders/relocate-loader.js")');

module.exports = ncc;
function ncc (
function ncc(
entry,
{
cache,
Expand All @@ -57,7 +55,7 @@ function ncc (
production = true,
// webpack defaults to `module` and `main`, but that's
// not really what node.js supports, so we reset it
mainFields = ['main']
mainFields = ['main'],
} = {}
) {
// v8 cache not supported for ES modules
Expand All @@ -76,7 +74,7 @@ function ncc (
extensions: SUPPORTED_EXTENSIONS,
exportsFields: ["exports"],
importsFields: ["imports"],
conditionNames: ["import", "node", production ? "production": "development"]
conditionNames: ["import", "node", production ? "production" : "development"]
});

const ext = extname(filename);
Expand Down Expand Up @@ -108,45 +106,36 @@ function ncc (
existingAssetNames.push(`${filename}.cache`);
existingAssetNames.push(`${filename}.cache${ext}`);
}

let tsconfig = {};
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 = {};
const resolveModules = [];
try {
const configFileAbsolutePath = walkParentDirs({
const configPath = walkParentDirs({
base: process.cwd(),
start: dirname(entry),
filename: 'tsconfig.json',
});
fullTsconfig = loadTsconfig(configFileAbsolutePath) || {
compilerOptions: {}
};
const contents = fs.readFileSync(configPath, 'utf8')
tsconfig = JSON5.parse(contents);
const baseUrl = tsconfig.compilerOptions.baseUrl;
resolveModules.push(pathResolve(dirname(configPath), baseUrl));
} catch (e) { }
Copy link

Choose a reason for hiding this comment

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

Should we show a warning message about missing/malformed tsconfig.json here, since compilerOptions would be an empty object and the default swc option would be used?

Copy link
Author

Choose a reason for hiding this comment

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

We didn't warn anything in any case https://github.com/vercel/ncc/pull/926/files#diff-bfe9874d239014961b1ae4e89875a6155667db834a410aaaa2ebe3cf89820556L136 , I'd consider this is failsafe same as before.


const tsconfigPathsOptions = { silent: true }
if (fullTsconfig.compilerOptions.allowJs) {
tsconfigPathsOptions.extensions = SUPPORTED_EXTENSIONS
}
resolvePlugins.push(new TsconfigPathsPlugin(tsconfigPathsOptions));

const tsconfig = tsconfigPaths.loadConfig();
if (tsconfig.resultType === "success") {
tsconfigMatchPath = tsconfigPaths.createMatchPath(tsconfig.absoluteBaseUrl, tsconfig.paths);
}
} catch (e) {}
const compilerOptions = tsconfig.compilerOptions || {};

resolvePlugins.push({
apply(resolver) {
const resolve = resolver.resolve;
resolver.resolve = function (context, path, request, resolveContext, callback) {
resolver.resolve = function(context, path, request, resolveContext, callback) {
const self = this;
resolve.call(self, context, path, request, resolveContext, function (err, innerPath, result) {
resolve.call(self, context, path, request, resolveContext, function(err, innerPath, result) {
if (result) return callback(null, innerPath, result);
if (err && !err.message.startsWith('Can\'t resolve'))
return callback(err);
// Allow .js resolutions to .tsx? from .tsx?
if (request.endsWith('.js') && context.issuer && (context.issuer.endsWith('.ts') || context.issuer.endsWith('.tsx'))) {
return resolve.call(self, context, path, request.slice(0, -3), resolveContext, function (err, innerPath, result) {
return resolve.call(self, context, path, request.slice(0, -3), resolveContext, function(err, innerPath, result) {
if (result) return callback(null, innerPath, result);
if (err && !err.message.startsWith('Can\'t resolve'))
return callback(err);
Expand Down Expand Up @@ -227,7 +216,7 @@ function ncc (
if (tapInfo.name !== "CommonJsPlugin") {
return tapInfo;
}
tapInfo.fn = () => {};
tapInfo.fn = () => { };
return tapInfo;
}
});
Expand All @@ -245,8 +234,7 @@ function ncc (
}
]

if (typeof license === 'string' && license.length > 0)
{
if (typeof license === 'string' && license.length > 0) {
plugins.push(new LicenseWebpackPlugin({
outputFilename: license
}));
Expand Down Expand Up @@ -315,11 +303,12 @@ function ncc (
undefined: cjsDeps()
},
mainFields,
plugins: resolvePlugins
plugins: resolvePlugins,
modules: resolveModules.length > 0 ? resolveModules : undefined,
},
// https://github.com/vercel/ncc/pull/29#pullrequestreview-177152175
node: false,
externals ({ context, request, dependencyType }, callback) {
externals({ context, request, dependencyType }, callback) {
const external = externalMap.get(request);
if (external) return callback(null, `${dependencyType === 'esm' && esm ? 'module' : 'node-commonjs'} ${external}`);
return callback();
Expand Down Expand Up @@ -354,17 +343,30 @@ function ncc (
loader: eval('__dirname + "/loaders/uncacheable.js"')
},
{
loader: eval('__dirname + "/loaders/ts-loader.js"'),
loader: eval('__dirname + "/loaders/swc-loader.js"'),
options: {
transpileOnly,
compiler: eval('__dirname + "/typescript.js"'),
compilerOptions: {
module: 'esnext',
target: 'esnext',
...fullTsconfig.compilerOptions,
allowSyntheticDefaultImports: true,
noEmit: false,
outDir: '//'
minify: false,
exclude: tsconfig.exclude,
sourceMaps: compilerOptions.sourceMap || false,
module: {
type: compilerOptions.module && compilerOptions.module.toLowerCase() === 'commonjs' ? 'commonjs' : 'es6',
strict: false,
strictMode: true,
lazy: false,
noInterop: !compilerOptions.esModuleInterop
},
jsc: {
externalHelpers: false,
keepClassNames: true,
target: compilerOptions.target && compilerOptions.target.toLowerCase() || 'es2021',
paths: compilerOptions.paths,
baseUrl: compilerOptions.baseUrl,
parser: {
syntax: 'typescript',
tsx: true, // TODO: use tsconfig.compilerOptions.jsx ???
decorators: compilerOptions.experimentalDecorators || false,
dynamicImport: true, // TODO: use module ???
}
}
}
}]
Expand Down Expand Up @@ -395,10 +397,10 @@ function ncc (
});
});
})
.then(finalizeHandler, function (err) {
compilationStack.pop();
throw err;
});
.then(finalizeHandler, function(err) {
compilationStack.pop();
throw err;
});
}
else {
if (typeof watch === 'object') {
Expand All @@ -425,15 +427,15 @@ function ncc (
});
let closed = false;
return {
close () {
close() {
if (!watcher)
throw new Error('No watcher to close.');
if (closed)
throw new Error('Watcher already closed.');
closed = true;
watcher.close();
},
handler (handler) {
handler(handler) {
if (watchHandler)
throw new Error('Watcher handler already provided.');
watchHandler = handler;
Expand All @@ -442,17 +444,17 @@ function ncc (
cachedResult = null;
}
},
rebuild (handler) {
rebuild(handler) {
if (rebuildHandler)
throw new Error('Rebuild handler already provided.');
rebuildHandler = handler;
}
};
}

async function finalizeHandler (stats) {
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 @@ -593,7 +595,7 @@ function ncc (
const subbuildAssets = [];
for (const asset of Object.keys(assets)) {
if (!asset.endsWith('.js') && !asset.endsWith('.cjs') && !asset.endsWith('.ts') && !asset.endsWith('.mjs') ||
asset.endsWith('.cache.js') || asset.endsWith('.cache.cjs') || asset.endsWith('.cache.ts') || asset.endsWith('.cache.mjs') || asset.endsWith('.d.ts')) {
asset.endsWith('.cache.js') || asset.endsWith('.cache.cjs') || asset.endsWith('.cache.ts') || asset.endsWith('.cache.mjs') || asset.endsWith('.d.ts')) {
existingAssetNames.push(asset);
continue;
}
Expand Down Expand Up @@ -646,17 +648,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.slice(1)) || {};
if(curPath.endsWith(".d.ts")) {
const outDir = tsconfig.compilerOptions.outDir ? pathResolve(tsconfig.compilerOptions.outDir) : pathResolve('dist');
if (curPath.endsWith(".d.ts")) {
const outDir = compilerOptions?.outDir ? pathResolve(compilerOptions.outDir) : pathResolve('dist');
curPath = curPath
.replace(outDir, "")
.replace(process.cwd(), "")
Expand Down
Loading