Skip to content

[Feature Request]: experimental.strictExecutionOrder makes code using esm-env to difficult to eliminate dead code #3410

@sapphi-red

Description

@sapphi-red

What problem does this feature solve?

For this input

import { BROWSER } from 'esm-env';

if (!BROWSER) {
  console.log('is not browser');
}

when experimental.strictExecutionOrder is not enabled, it outputs:

//#region node_modules/esm-env/true.js
var true_default = true;

//#endregion
//#region node_modules/esm-env/dev-fallback.js
const node_env = globalThis.process?.env?.NODE_ENV;
var dev_fallback_default = node_env && !node_env.toLowerCase().startsWith("prod");

//#endregion
//#region src/index.mjs
if (!true_default) console.log("is not browser");

//#endregion

this can be analyzed relatively easily to find out that true_default is always true and probably possible to minimize to

//#region node_modules/esm-env/dev-fallback.js
const node_env = globalThis.process?.env?.NODE_ENV;
node_env && !node_env.toLowerCase().startsWith("prod");

//#endregion
//#region node_modules/esm-env/false.js
//#endregion
//#region src/index.mjs
//#endregion

when OXC minifier supports these cases.

But when experimental.strictExecutionOrder is enabled, the output becomes:

//#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 node_modules/esm-env/true.js
var true_default;
var init_true = __esm({ "node_modules/esm-env/true.js"() {
	true_default = true;
} });

//#endregion
//#region node_modules/esm-env/dev-fallback.js
var node_env, dev_fallback_default;
var init_dev_fallback = __esm({ "node_modules/esm-env/dev-fallback.js"() {
	node_env = globalThis.process?.env?.NODE_ENV;
	dev_fallback_default = node_env && !node_env.toLowerCase().startsWith("prod");
} });

//#endregion
//#region node_modules/esm-env/false.js
var init_false = __esm({ "node_modules/esm-env/false.js"() {} });

//#endregion
//#region node_modules/esm-env/index.js
var init_esm_env = __esm({ "node_modules/esm-env/index.js"() {
	init_true();
	init_dev_fallback();
	init_false();
} });

//#endregion
//#region src/index.mjs
var init_src = __esm({ "src/index.mjs"() {
	init_esm_env();
	if (!true_default) console.log("is not browser");
} });

//#endregion
init_src();

To know that true_default is always true when accesses, it requires tracking the execution, which is quite difficult. Without tracking the execution, we can know that true_default is true or undefined, but that means !true_default might be true and cannot eliminate that console.log.

Reproduction

I found this while testing rolldown-vite in ecosystem-ci. SvelteKit uses manualChunks to support bundleStrategy: "single" (sveltejs/kit#13173). I replaced that manualChunks option with advancedChunks and found this behavior.

What does the proposed API look like?

N/A

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions