Enforce node: schema for builtins, import extensions

This commit is contained in:
Fedor Indutny 2025-09-16 17:39:03 -07:00 committed by GitHub
commit c02565eaa8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2096 changed files with 14955 additions and 14023 deletions

View file

@ -2,11 +2,13 @@
// SPDX-License-Identifier: AGPL-3.0-only
const esbuild = require('esbuild');
const path = require('path');
const path = require('node:path');
const fs = require('node:fs');
const fastGlob = require('fast-glob');
const ROOT_DIR = path.join(__dirname, '..');
const BUNDLES_DIR = 'bundles';
const NODE_MODULES_DIR = path.join(ROOT_DIR, 'node_modules');
const watch = process.argv.some(argv => argv === '-w' || argv === '--watch');
const isProd = process.argv.some(argv => argv === '-prod' || argv === '--prod');
@ -22,6 +24,33 @@ const nodeDefaults = {
// See: https://github.com/evanw/esbuild/issues/1147
keepNames: true,
logLevel: 'info',
plugins: [
{
name: 'resolve-ts',
setup(b) {
b.onResolve({ filter: /\.js$/ }, args => {
if (!args.path.startsWith('.')) {
return undefined;
}
const targetPath = path.join(args.resolveDir, args.path);
if (targetPath.startsWith(NODE_MODULES_DIR)) {
return undefined;
}
const tsPath = targetPath.replace(/\.js$/, '.ts');
const tsxPath = targetPath.replace(/\.js$/, '.tsx');
if (fs.existsSync(tsPath)) {
return { path: tsPath };
}
if (fs.existsSync(tsxPath)) {
return { path: tsxPath };
}
return undefined;
});
},
},
],
};
const bundleDefaults = {
@ -61,8 +90,11 @@ const bundleDefaults = {
'moment',
'quill',
// Imported, but not used in production builds
'mocha',
// Uses fast-glob and dynamic requires
'./preload_test',
'./preload_test.js',
],
};
@ -119,7 +151,7 @@ async function main() {
entryPoints: [
'preload.wrapper.ts',
...fastGlob
.sync('{app,ts}/**/*.{ts,tsx}', {
.sync('{app,ts,build}/**/*.{ts,tsx}', {
onlyFiles: true,
cwd: ROOT_DIR,
})