Fix multiple imports to CJS modules when referencing via module_id hash

This commit is contained in:
Jarred Sumner
2021-09-17 14:47:14 -07:00
parent a188c7d791
commit bc65e40313
7 changed files with 111 additions and 6 deletions

View File

@@ -91,6 +91,7 @@ async function main() {
"/export.js",
"/type-only-imports.ts",
"/global-is-remapped-to-globalThis.js",
"/multiple-imports.js",
];
for (let test of tests) {

View File

@@ -0,0 +1,25 @@
import {
__require as require
} from "http://localhost:8080/__runtime.js";
import * as JSX from "http://localhost:8080/node_modules/react/jsx-dev-runtime.js";
import * as $bbcd215f from "http://localhost:8080/node_modules/react/index.js";
var JSXClassic = require($bbcd215f);
var jsx = require(JSX).jsxDEV, fileName = "multiple-imports.js", JSXFrag = require(JSX, JSXClassic).Fragment;
var { default: React} = require($bbcd215f);
var { default: React2} = require($bbcd215f);
const bacon = React;
const bacon2 = jsx(JSXFrag, {
children: ["hello"]
}, undefined, true, {
fileName,
lineNumber: 92
}, this);
export function test() {
console.assert(bacon === React);
console.assert(bacon === React2);
console.assert(typeof bacon2 !== "undefined");
console.assert(React.isValidElement(bacon2));
return testDone(import.meta.url);
}

View File

@@ -5,9 +5,11 @@ import {
__cJS2eSM
} from "http://localhost:8080/__runtime.js";
import * as _login_b977_0 from "http://localhost:8080/_login.js";
import * as _login_b977_1 from "http://localhost:8080/_login.js";
import _login from "http://localhost:8080/_login.js";
import _auth from "http://localhost:8080/_auth.js";
import * as _loginReally from "http://localhost:8080/_login.js";
import * as _loginReally2 from "http://localhost:8080/_login.js";
import * as _authReally from "http://localhost:8080/_auth.js";
export default __cJS2eSM(function(module, exports) {
@@ -16,9 +18,11 @@ export default __cJS2eSM(function(module, exports) {
;
;
;
;
module.exports.iAmCommonJs = true;
exports.YouAreCommonJS = true;
require(_login_b977_0);
require(_login_b977_1);
Object.defineProperty(module.exports,"login",{get: () => _login, enumerable: true, configurable: true});
var test = function test() {
return testDone(import.meta.url);

View File

@@ -0,0 +1,46 @@
import {
__HMRClient as Bun
} from "http://localhost:8080/__runtime.js";
import {
__require as require
} from "http://localhost:8080/__runtime.js";
import {
__HMRModule as HMR
} from "http://localhost:8080/__runtime.js";
import * as JSX from "http://localhost:8080/node_modules/react/jsx-dev-runtime.js";
import * as $bbcd215f from "http://localhost:8080/node_modules/react/index.js";
var JSXClassic = require($bbcd215f);
var jsx = require(JSX).jsxDEV, fileName = "multiple-imports.js", JSXFrag = require(JSX, JSXClassic).Fragment;
var { default: React} = require($bbcd215f);
var { default: React2} = require($bbcd215f);
Bun.activate(true);
var hmr = new HMR(2165509932, "multiple-imports.js"), exports = hmr.exports;
(hmr._load = function() {
const bacon = React;
const bacon2 = jsx(JSXFrag, {
children: ["hello"]
}, undefined, true, {
fileName,
lineNumber: 92
}, this);
function test() {
console.assert(bacon === React);
console.assert(bacon === React2);
console.assert(typeof bacon2 !== "undefined");
console.assert(React.isValidElement(bacon2));
return testDone(import.meta.url);
}
hmr.exportAll({
test: () => test
});
})();
var $$hmr_test = hmr.exports.test;
hmr._update = function(exports) {
$$hmr_test = exports.test;
};
export {
$$hmr_test as test
};

View File

@@ -1,11 +1,13 @@
import _login from "./_login";
import _auth from "./_auth";
import * as _loginReally from "./_login";
import * as _loginReally2 from "./_login";
import * as _authReally from "./_auth";
module.exports.iAmCommonJs = true;
exports.YouAreCommonJS = true;
require("./_login");
require("./_login");
export { _login as login };
export function test() {

View File

@@ -0,0 +1,13 @@
import React from "react";
import React2 from "react";
const bacon = React;
const bacon2 = <>hello</>;
export function test() {
console.assert(bacon === React);
console.assert(bacon === React2);
console.assert(typeof bacon2 !== "undefined");
console.assert(React.isValidElement(bacon2));
return testDone(import.meta.url);
}

View File

@@ -43,6 +43,9 @@ const first_low_surrogate: u21 = 0xDC00;
const last_low_surrogate: u21 = 0xDFFF;
const assert = std.debug.assert;
threadlocal var imported_module_ids_list: std.ArrayList(u32) = undefined;
threadlocal var imported_module_ids_list_unset: bool = true;
fn notimpl() void {
Global.panic("Not implemented yet!", .{});
}
@@ -234,6 +237,7 @@ pub fn NewPrinter(
writer: Writer,
has_printed_bundled_import_statement: bool = false,
imported_module_ids: std.ArrayList(u32),
renamer: rename.Renamer,
prev_stmt_tag: Stmt.Tag = .s_empty,
@@ -2943,12 +2947,14 @@ pub fn NewPrinter(
const module_id = record.module_id;
p.print("import * as ");
p.printModuleId(module_id);
p.print(" from \"");
p.print(record.path.text);
p.print("\";\n");
if (std.mem.indexOfScalar(u32, p.imported_module_ids.items, module_id) == null) {
p.print("import * as ");
p.printModuleId(module_id);
p.print(" from \"");
p.print(record.path.text);
p.print("\";\n");
try p.imported_module_ids.append(module_id);
}
if (record.contains_import_star) {
p.print("var ");
@@ -3761,12 +3767,20 @@ pub fn NewPrinter(
opts: Options,
linker: ?*Linker,
) !Printer {
if (imported_module_ids_list_unset) {
imported_module_ids_list = std.ArrayList(u32).init(default_allocator);
imported_module_ids_list_unset = false;
}
imported_module_ids_list.clearRetainingCapacity();
return Printer{
.import_records = tree.import_records,
.options = opts,
.symbols = symbols,
.writer = writer,
.linker = linker,
.imported_module_ids = imported_module_ids_list,
.renamer = rename.Renamer.init(symbols, source),
};
}