fix(bundle): support importing directories with a package.json from an es module when bundling (#30273)
Some checks are pending
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions

This commit is contained in:
David Sherret 2025-07-31 18:33:25 -04:00 committed by GitHub
parent 6ae4eda86a
commit efb16e70d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 66 additions and 25 deletions

View file

@ -562,7 +562,9 @@ impl<
let maybe_file_type = self.sys.get_file_type(&path);
match maybe_file_type {
Ok(FileType::Dir) => {
if resolution_mode == ResolutionMode::Import {
if resolution_mode == ResolutionMode::Import
&& !self.resolution_config.bundle_mode
{
let suggested_file_name = ["index.mjs", "index.js", "index.cjs"]
.into_iter()
.find(|e| self.sys.is_file(&path.join(e)));

View file

@ -1,4 +0,0 @@
{
"args": "bundle --quiet main.cjs",
"output": "bundle.out"
}

View file

@ -1,20 +0,0 @@
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
// node_modules/package/constants/sub.js
var require_sub = __commonJS({
"node_modules/package/constants/sub.js"(exports, module) {
module.exports = 5;
}
});
// main.cjs
var require_main = __commonJS({
"main.cjs"() {
var value = require_sub();
console.log(value);
}
});
export default require_main();

View file

@ -0,0 +1,4 @@
{
"args": "bundle --quiet main.mjs",
"output": "bundle.out"
}

View file

@ -0,0 +1,48 @@
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// node_modules/package/constants/sub.js
var require_sub = __commonJS({
"node_modules/package/constants/sub.js"(exports, module) {
module.exports = 5;
}
});
// main.cjs
var require_main = __commonJS({
"main.cjs"() {
var value = require_sub();
console.log(value);
}
});
// node_modules/esm-pkg/index.js
var constants = __toESM(require_sub());
var esm_pkg_default = constants;
// main.mjs
var import_main = __toESM(require_main(), 1);
console.log(esm_pkg_default);

View file

@ -0,0 +1,3 @@
import constants from "esm-pkg";
console.log(constants);
import "./main.cjs";

View file

@ -0,0 +1,5 @@
// this should work when bundling even though it's esm,
// but will not work at runtime due to Node's ESM restrictions
import * as constants from "package/constants";
export default constants;

View file

@ -0,0 +1,3 @@
{
"type": "module"
}