Skip to content
Merged
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
22 changes: 21 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,21 @@ const cjsExportPlugin = {
},
};

/**
* Compiles `import.meta.url` to `undefined` in the CJS build.
* Rollup's CJS output for it calls `require('u' + 'rl')` (Node's `url` module),
* which webpack, Rspack, esbuild and Parcel cannot resolve when they bundle for the browser.
* Code that reads `import.meta.url` must handle `undefined`, as it must in an esbuild IIFE bundle.
*
* @type {Plugin}
*/
const cjsImportMetaPlugin = {
name: 'cjs-import-meta',
resolveImportMeta (property) {
return property === 'url' ? 'undefined' : null;
},
};

/**
* @param {MagicString} s
* @returns {{ code: string; map: SourceMapInput }}
Expand Down Expand Up @@ -441,7 +456,12 @@ async function buildJS () {
cjs: {
rollupOptions: {
...defaultRollupOptions,
plugins: [...defaultRollupOptions.plugins, commonjs(), cjsExportPlugin],
plugins: [
...defaultRollupOptions.plugins,
commonjs(),
cjsExportPlugin,
cjsImportMetaPlugin,
],
},
outputOptions: {
...defaultOutputOptions,
Expand Down
Loading