rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.57k stars 568 forks source link

[@rollup/plugin-esm-shim] Import regex fails if the the code contains an "import" string followed by more content #1649

Closed dmnsgn closed 3 months ago

dmnsgn commented 6 months ago

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

dmnsgn commented 5 months ago

Still happening in: "rollup": "^4.9.6" with Node.js v21.6.1