Skip to content

feat(rust): remove unused import(..) if importee doesn't have side-effects#3598

Closed
shulaoda wants to merge 3 commits into
rolldown:mainfrom
shulaoda:feat/remove-unused-import
Closed

feat(rust): remove unused import(..) if importee doesn't have side-effects#3598
shulaoda wants to merge 3 commits into
rolldown:mainfrom
shulaoda:feat/remove-unused-import

Conversation

@shulaoda

@shulaoda shulaoda commented Feb 16, 2025

Copy link
Copy Markdown
Member

Description

closes #2827

The core idea is that if a dynamic import module has no included statements and is not referenced, it‘s module.is_included() should be false.

I’m facing an issue with how to implement the polyfill. There are two options: one is to declare var init_foo = Object.freeze(..) at the importer side, and the other is to introduce a global empty dynamic module (runtime).

I'm constantly unable to correctly link the symbol reference for init_foo or __dynamicEmpty. I’m unsure how to proceed correctly, so I’m just sharing my thoughts and seeking help on this.

@shulaoda shulaoda changed the title feat(rust): remove unused import(..) if importee doesn't have side-effects [Need Help] feat(rust): remove unused import(..) if importee doesn't have side-effects Feb 16, 2025
@netlify

netlify Bot commented Feb 16, 2025

Copy link
Copy Markdown

Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 1521a76
🔍 Latest deploy log https://app.netlify.com/sites/rolldown-rs/deploys/67b679df573d270008dcff17

@shulaoda shulaoda changed the title [Need Help] feat(rust): remove unused import(..) if importee doesn't have side-effects feat(rust): remove unused import(..) if importee doesn't have side-effects Feb 16, 2025
@shulaoda
shulaoda force-pushed the feat/remove-unused-import branch 4 times, most recently from c3c25eb to 96bf7bf Compare February 16, 2025 22:37
@shulaoda
shulaoda marked this pull request as draft February 16, 2025 22:53
@shulaoda
shulaoda marked this pull request as ready for review February 16, 2025 22:57
@shulaoda
shulaoda marked this pull request as draft February 16, 2025 23:57
@shulaoda
shulaoda marked this pull request as draft February 16, 2025 23:57
@IWANABETHATGUY IWANABETHATGUY self-assigned this Feb 17, 2025
Comment on lines +43 to +46
var __dynamicEmptyModule = Object.freeze({
__proto__: null,
default: {}
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__DynamicEmptyModule may not be good because there may be the following situations:

let a = await('./a.js')
let b = await('./b.js')

console.log(a == b)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should align with rollup and let me continue to improve.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is difficult to achieve functions similar to rollup.

// entry.js
import('./abc.js')
import('./dynamic.js').then(() => console.log("1"))

// abc.js
import('./dynamic.js').then(() => console.log("2"))

// dynamic.js

The current implementation result is:

// entry.js
import "./entry--jpnM2Ll.js";

// entry--jpnM2Ll.js
import("./abc-BIQEauab.js");
Promise.resolve().then(function() {
	return dynamic;
}).then(() => console.log("1"));
var dynamic = Object.freeze({
	__proto__: null,
	default: {}
});

export { dynamic };

// abc-BIQEauab.js
import { dynamic } from "./entry--jpnM2Ll.js";
Promise.resolve().then(function() {
	return dynamic;
}).then(() => console.log("2"));

Rollup:

// entry.js
import('./abc-qNCBTG9J.js');

Promise.resolve().then(function () { return dynamic; }).then(() => console.log("1"));

var dynamic = /*#__PURE__*/Object.freeze({
	__proto__: null
});

export { dynamic as d };

// abc-qNCBTG9J.js
import('./main.js').then(function (n) { return n.d; });

@shulaoda
shulaoda force-pushed the feat/remove-unused-import branch from 96bf7bf to 1521a76 Compare February 20, 2025 00:39
github-merge-queue Bot pushed a commit that referenced this pull request Mar 20, 2025
…#3911)

<!-- Thank you for contributing! -->

### Description
1. Closed #2827, closed #3598
2. I need to add `console.log` or some module in tests will be broken
since the dynamic module is side effects free.

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: remove unused import(...) if importee doesn't have side-effects

2 participants