Skip to content

[Bug]: Incorrect import.meta.url shim for CJS #274

@chenjiahan

Description

@chenjiahan

Version

Rslib 0.0.10

Details

Rslib should inject __rslib_import_meta_url__ before using it. Otherwise there will be an runtime error:

Image

  • Input:
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);

console.log(require.resolve('stylus-loader'));
  • Current output:
"use strict";
var __webpack_exports__ = {};
const external_node_module_namespaceObject = require("node:module");
const src_require = (0, external_node_module_namespaceObject.createRequire)(__rslib_import_meta_url__);
console.log(src_require.resolve('stylus-loader'));
var __webpack_export_target__ = exports;
for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
var __rslib_import_meta_url__ = /*#__PURE__*/ function() {
    return 'undefined' == typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
}();
  • Expected output:
"use strict";
var __webpack_exports__ = {};
var __rslib_import_meta_url__ = /*#__PURE__*/ function() {
    return 'undefined' == typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
}();
const external_node_module_namespaceObject = require("node:module");
const src_require = (0, external_node_module_namespaceObject.createRequire)(__rslib_import_meta_url__);
console.log(src_require.resolve('stylus-loader'));
var __webpack_export_target__ = exports;
for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});

Reproduce link

web-infra-dev/rsbuild#3679

Reproduce Steps

  1. cd packages/plugin-stylus
  2. pnpm build

Metadata

Metadata

Assignees

Labels

🐞 bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions