Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin-asset): support generator['asset'].importMode for Rslib #8724

Merged
merged 4 commits into from
Jan 20, 2025

Conversation

SoonIter
Copy link
Member

@SoonIter SoonIter commented Dec 16, 2024

Summary

for web-infra-dev/rslib#570 and associated with web-infra-dev/rslib#684

For mid-level artifacts such as libraries in ESM/CJS formats, import statements for assets can be preserved to allow the upper-level applications to handle them.

In Rslib, we provide an option for users to choose, this feature needs to be implemented in Rspack.

Details

this pr does these things below:

  1. add generator['asset/resource'].importMode

type: 'url' | 'preserve' default: 'url'

When this option is turned on, we will keep the require or import statement for asset. (you can see the example below from Rslib or you can see the test-case)

input

└── src
    ├── assets
    │   └── logo.svg   // <--
    └── index.tsx
// index.tsx
import foo from './assets/logo.svg';
console.log(foo);

output

./dist
└── esm
    ├── assets
    │   └── logo.mjs     // <--
    ├── static/svg
    │   └── logo.svg     // <--
    ├── index.d.ts
    └── index.mjs
// dist/esm/index.mjs
import foo from './assets/logo.mjs';
console.log(foo);
// dist/esm/assets/logo.mjs
import url from '../static/svg/logo.svg';
export { url as default };

  1. add CodeGenerationPublicPathAutoReplace to generator_context.data, it can mark the js modules need to do replacements of AUTO_PUBLIC_PATH_PLACEHOLDER, it has the similar logic with experiments.css

    [Feature]: Support publicPath: auto in asset module #8748

    // For performance, mark the js modules contains AUTO_PUBLIC_PATH_PLACEHOLDER, and replace it
    #[derive(Clone, Debug)]
    pub struct CodeGenerationPublicPathAutoReplace(pub bool);
    // logo.mjs
    // before
    import logo_namespaceObject from 'AUTO_PUBLIC_PATH_PLACEHOLDERstatic/svg/logo.svg';
    export { logo_namespaceObject as default };
    // after
    import logo_namespaceObject from '../../static/svg/logo.svg';
    export { logo_namespaceObject as default };

  1. fix webpack asset module bugs(different from webpack)

    • concaten: when using asset as entry, the asset is missing because of being concatenated
    • exports dependency: asset module has default export literally, however, it has no export dependeny in webpack which is an ancient mistake I think. it causes weird behavior when asset module is set as entry, or being concatenated

Related links

close #8711
partially support #8748

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copy link

netlify bot commented Dec 16, 2024

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit 22235e3
🔍 Latest deploy log https://app.netlify.com/sites/rspack/deploys/678e255de146d400087627ba

@github-actions github-actions bot added the release: feature release: feature related release(mr only) label Dec 16, 2024
@SoonIter SoonIter changed the title feat: export asset feat: support export asset Dec 16, 2024
@SoonIter SoonIter force-pushed the export-asset branch 2 times, most recently from 8453ef5 to f481b0a Compare December 17, 2024 10:38
@SoonIter SoonIter changed the title feat: support export asset feat(plugin-asset): support re-export and preserve-export in asset for Rslib Dec 17, 2024
@SoonIter SoonIter changed the title feat(plugin-asset): support re-export and preserve-export in asset for Rslib feat(plugin-asset): support re-export and preserve-import in asset for Rslib Dec 18, 2024
@hardfist
Copy link
Contributor

!pkg.pr.new

@SoonIter SoonIter force-pushed the export-asset branch 3 times, most recently from ad92af5 to 083d2d5 Compare January 13, 2025 10:40
@SoonIter SoonIter changed the title feat(plugin-asset): support re-export and preserve-import in asset for Rslib feat(plugin-asset): support experimentalLibPreserveImport in asset for Rslib Jan 13, 2025
Copy link

codspeed-hq bot commented Jan 13, 2025

CodSpeed Performance Report

Merging #8724 will not alter performance

Comparing export-asset (22235e3) with main (876f50e)

Summary

✅ 3 untouched benchmarks

@SoonIter SoonIter force-pushed the export-asset branch 2 times, most recently from ed51dea to c4e833d Compare January 14, 2025 12:56
@SoonIter SoonIter marked this pull request as ready for review January 14, 2025 12:58
@SoonIter SoonIter changed the title feat(plugin-asset): support experimentalLibPreserveImport in asset for Rslib feat(plugin-asset): support generator['asset'].experimentalLibPreserveImport for Rslib Jan 15, 2025
@SoonIter
Copy link
Member Author

One drawback is that there may be some errors in the sourcemap, I have not tested it at present

@ahabhgk
Copy link
Contributor

ahabhgk commented Jan 16, 2025

We need a more general name for this, this shouldn't be lib specific

@SoonIter SoonIter changed the title feat(plugin-asset): support generator['asset'].experimentalLibPreserveImport for Rslib feat(plugin-asset): support generator['asset'].experimentalPreserveImport for Rslib Jan 17, 2025
@SoonIter
Copy link
Member Author

SoonIter commented Jan 17, 2025

@JSerFeng @ahabhgk has been renamed to generator['asset'].experimentalPreserveImport

@JSerFeng
Copy link
Contributor

LGTM @ahabhgk

@SoonIter SoonIter changed the title feat(plugin-asset): support generator['asset'].experimentalPreserveImport for Rslib feat(plugin-asset): support generator['asset'].importMode for Rslib Jan 20, 2025
@SoonIter SoonIter merged commit 33654c7 into main Jan 20, 2025
30 checks passed
@SoonIter SoonIter deleted the export-asset branch January 20, 2025 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: feature release: feature related release(mr only)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: optimize asset modules concatenation
4 participants