plugins
plugins copied to clipboard
[@rollup/plugin-esm-shim] Import regex fails if the the code contains an "import" string followed by more content
trafficstars
- Rollup Plugin Name: @rollup/plugin-esm-shim
- Rollup Plugin Version: 0.1.5
- Rollup Version: 4.8.0
- Operating System (or Browser): Mac
- Node Version: v21.2.0
- Link to reproduction: live https://stackblitz.com/edit/rollup-repro-pba1zx?file=dist%2Fmain.js or as zip rollup-repro-pba1zx.zip
Expected Behavior
The shim to be inserted in the right place with a simple config:
const resolve = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const esmShim = require('@rollup/plugin-esm-shim');
module.exports = {
input: 'src/main.js',
output: {
file: 'dist/main.js',
format: 'es',
},
plugins: [resolve(), commonjs(), esmShim()],
};
from a simple source:
const dn = __dirname;
module.exports = {
keyword: ' import',
dn: '',
};
Actual Behavior
The shims get inserted in the middle of the common js helper.
function getDefa
// -- Shims --
import cjsUrl from 'node:url';
import cjsPath from 'node:path';
import cjsModule from 'node:module';
const __filename = cjsUrl.fileURLToPath(import.meta.url);
const __dirname = cjsPath.dirname(__filename);
const require = cjsModule.createRequire(import.meta.url);
ultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
__dirname;
var main = {
keyword: ' import',
dn: '',
};
var main$1 = /*@__PURE__*/getDefaultExportFromCjs(main);
export { main$1 as default };
Additional Information
- The issue happens with or without any real esm/require "import"s at the top of the file.
- The issue seems to only happens if there is a space before the import string and if there's more "content" in the file after that import string
- The behavior can be reproduced without rollup (using ESMStaticImportRegex and MagicString from utils provideCJSSyntax)
- The above is a reduced test case that comes from a real world usage of highlight.js qml language (see source with import string here https://github.com/highlightjs/highlight.js/blob/6317acd780bfe448f75393ea42d53c0149013274/src/languages/qml.js#L17)
Still happening in: "rollup": "^4.9.6" with Node.js v21.6.1