Skip to content

Commit def448f

Browse files
committed
Make esbuild optional
esbuild is a large dependency, and pinning to a specific version makes it less likely that it can be shared across other dependencies. To make i18next-parser lighter: - Dynamically import esbuild if needed. Change esbuild to an optional peer dependency. - Update the version range. - Try loading .ts files using Node.js support for stripping TypeScript types (enabled by default in current Node.js versions), to potentially remove having to import ESBuild at all. This is a potentially breaking change: if a someone uses a .ts i18next-parser configuration, doesn't have type stripping enabled (or has a configuration file that's incompatible with Node.js's type stripping), and doesn't otherwise have esbuild installed, then they'll have to install it.
1 parent 6c10a2b commit def448f

File tree

4 files changed

+185
-155
lines changed

4 files changed

+185
-155
lines changed

bin/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ import i18nTransform from '../dist/transform.js'
5858
`${pkg.name}.config.mjs`,
5959
`${pkg.name}.config.json`,
6060
`${pkg.name}.config.ts`,
61+
`${pkg.name}.config.mts`,
6162
`${pkg.name}.config.yaml`,
6263
`${pkg.name}.config.yml`,
6364
],
6465
loaders: {
6566
'.js': esConfigLoader,
6667
'.mjs': esConfigLoader,
6768
'.ts': tsConfigLoader,
69+
'.mts': tsConfigLoader,
6870
'.yaml': yamlConfigLoader,
6971
'.yml': yamlConfigLoader,
7072
},

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"colors": "^1.4.0",
3434
"commander": "^12.1.0",
3535
"eol": "^0.9.1",
36-
"esbuild": "^0.25.0",
3736
"fs-extra": "^11.2.0",
3837
"gulp-sort": "^2.0.0",
3938
"i18next": "^23.5.1 || ^24.2.0",
@@ -58,6 +57,7 @@
5857
"broccoli-funnel": "^3.0.8",
5958
"c8": "^10.1.2",
6059
"chai": "^5.1.1",
60+
"esbuild": "^0.27.0",
6161
"execa": "^9.5.2",
6262
"gulp": "^5.0.0",
6363
"husky": "^9.1.6",
@@ -67,6 +67,14 @@
6767
"prettier": "^3.3.3",
6868
"sinon": "^19.0.2"
6969
},
70+
"peerDependencies": {
71+
"esbuild": "^0.25.0 || ^0.26.0 || ^0.27.0"
72+
},
73+
"peerDependenciesMeta": {
74+
"esbuild": {
75+
"optional": true
76+
}
77+
},
7078
"repository": {
7179
"type": "git",
7280
"url": "https://github.com/i18next/i18next-parser"

src/helpers.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { pathToFileURL } from 'url'
2-
import { build } from 'esbuild'
32
import { rmSync } from 'fs'
43
import yaml from 'js-yaml'
54
import { builtinModules } from 'module'
@@ -286,7 +285,22 @@ async function esConfigLoader(filepath) {
286285
}
287286

288287
async function tsConfigLoader(filepath) {
288+
// Newer Node.js has --experimental-strip-types by default. Try that.
289+
try {
290+
return await esConfigLoader(filepath)
291+
} catch (err) {
292+
// Fall through.
293+
}
294+
289295
const outfile = filepath + '.bundle.mjs'
296+
let build
297+
try {
298+
;({ build } = await import('esbuild'))
299+
} catch (err) {
300+
throw new Error(
301+
'esbuild is required to load TypeScript configuration files. Please install it as a peer dependency; e.g.: npm install -D esbuild'
302+
)
303+
}
290304
await build({
291305
absWorkingDir: process.cwd(),
292306
entryPoints: [filepath],

0 commit comments

Comments
 (0)