locale load can't be bundled anymore
retorquere opened this issue · 5 comments
This snippet
const load = (locale) =>
JSON.parse(fs.readFileSync(new URL(`../locale-data/${locale}.json`, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)))));
can't be bundled. I can work around it by using a compile-time patch, but what is the benefit of loading this way over a require('../locale-data/en.json')
?
What prompted the change was actually that you were confused by the mixed use of CommonJS and ES modules -- we used CJS to load the locale data as a convenience because ES modules didn't support loading JSON. It looks like import assertions now work in Node.js v16 so I think we could switch to those instead.
Switched to import assertions in 4.3.0
It turns out the support in Rollup isn't great; there's a plugin, but there are complications if you (or a dependency) also requires JSON files. I'll revert back to using CommonJS for the locale data until Rollup has built-in support for import assertions.
esbuild has so far been able to compile everything I could throw at it (with the addition of some plugins).
This esbuild file compiles the import assertions without further plugins, and tests seem to pass:
import esbuild from 'esbuild'
esbuild.buildSync({
entryPoints: ['index.js'],
bundle: true,
sourcemap: true,
outfile: 'dist/index.cjs',
external: [
'assert',
'fs',
'nearley',
'randexp'
],
format: 'cjs',
})