Skip to content

[Bug]: incorrect execution order of static external import #3143

Description

@hi-ogawa

Reproduction link or steps

reproduction: https://stackblitz.com/github/hi-ogawa/reproductions/tree/main/rolldown-external-import?file=README.md

input

  • entry.js
import "./dep.js";
import "@rolldown/test-dep-external";  // this is externalized
  • dep.js
console.log("[importing dep.js]");
  • @rolldown/test-dep-external
console.log("[importing test-dep-external]");

output

  • with strictExecutionOrder: false
import "@rolldown/test-dep-external";

//#region src/dep.js
console.log("[importing dep.js]");
  • with strictExecutionOrder: true
import "@rolldown/test-dep-external";

//#region rolldown:runtime
var __getOwnPropNames = Object.getOwnPropertyNames;
var __esm = (fn, res) => function() {
	return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};

//#endregion
//#region src/dep.js
var init_dep = __esm({ "src/dep.js"() {
	console.log("[importing dep.js]");
} });

//#endregion
//#region src/entry.js
var init_entry = __esm({ "src/entry.js"() {
	init_dep();
} });

//#endregion
init_entry();

What is expected?

Executed like original esm

$ node src/entry.js
[importing dep.js]
[importing test-dep-external]

What is actually happening?

$ pnpm build
$ node dist/entry.js
[importing test-dep-external]
[importing dep.js]

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    rolldown: 0.15.0-snapshot-3cea4f5-20241211003613 => 0.15.0-snapshot-3cea4f5-20241211003613

Any additional comments?

I wrote a comparison with other bundlers and their output in https://github.com/hi-ogawa/reproductions/tree/main/rolldown-external-import
What webpack (and thus rspack, turbopack, next.js) are doing is to effectively rewrite static external import into a dynamic import and handle it as async module and top-level-await.
Vite SSR does something similar, so this example turns out to work (though we know Vite SSR's evaluation is not so robust in general).

As a side note, I had experimented with next.js/turbopack initially and their external handling might have one caveat (thought probably reasonable). I wrote an example here about what happens when indirect dependency is externalized https://github.com/hi-ogawa/reproductions/tree/main/next-external-import

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions