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: support loader-runner and pitching #2808

Merged
merged 26 commits into from
Apr 21, 2023
Merged

feat: support loader-runner and pitching #2808

merged 26 commits into from
Apr 21, 2023

Conversation

h-a-n-a
Copy link
Collaborator

@h-a-n-a h-a-n-a commented Apr 18, 2023

Related issue (if exists)

closes #2394
closes #1750
closes #1751

Summary

This PR refactors the loader architecture and adapts webpack's loader-runner to rspack.
Pitching loader is supported for both Rust and JS side from now on.

The old architecture is a quite simple version, which only supports loaders for normal stage.
Pitching loader does not put into consideration. The basic concept of the old version is to
convert the normal loader to a native function which can be called from the Rust side.
Furthermore, for performance reason, Rspack also composes loaders from the JS side to
mitigate the performance issue of Node/Rust communications.

In this new architecture, loaders will not be converted directly into native functions.
Instead, it is almost the same with how webpack's loader-runner resolves its loaders, by
leveraging the identifier. Every time Rspack wants to invoke a JS loader, the identifiers will
be passed to the handler passed by Node side to process. The implementation also keeps
the feature of composing JS loaders
for performance reason.

See #2846 for more details on the architecture.

Tracking PRs:

🤖 Generated by Copilot at 2c6ea7c

This pull request refactors the rspack_core and rspack_loader_runner crates to use a new ResolverFactory abstraction for resolving module dependencies and a new run_loaders function for running loaders. It also adds support for more complex resource URLs, loaders, and modules, and implements hot module replacement (HMR) for modules. It removes unused and outdated code and simplifies the code with the derivative crate.

Walkthrough

🤖 Generated by Copilot at 2c6ea7c

  • Remove the LoaderRunnerRunner type and replace it with the ResolverFactory type and the run_loaders function to simplify the loader running and resolving logic (link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link)
  • Add the CompilerContext type and the BuildContext type to hold the common compiler options and the ResolverFactory instance that are used by different modules and loaders (link, link)
  • Add the LoaderRunnerPluginProcessResource type and use it in the NormalModule struct to support plugin hooks for the loader runner (link, link)
  • Add the resource_fragment, resource_query, and resource_path fields to the ResourceData struct to support more complex resource URLs and parameters (link)
  • Modify the identifier method of the NormalModuleFactory struct to use the identifier method of the Loader trait instead of the name method to support more complex and dynamic loaders and modules (link)
  • Add the derivative crate and the Derivative derive macro to customize the Debug implementation for some structs and enums in the rspack_core crate (link, link, link, link, link, link, link)
  • Rename the rspack_loader_runner2 crate to rspack_loader_runner and update the rspack_core crate to use the new version (link, link)
  • Delete the old version of the rspack_loader_runner crate and its files (link, link, link, link, link, link, link, link)

@changeset-bot
Copy link

changeset-bot bot commented Apr 18, 2023

🦋 Changeset detected

Latest commit: 303b79b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@rspack/binding Patch
@rspack/postcss-loader Patch
@rspack/core Patch
webpack-test Patch
@rspack/cli Patch
@rspack/dev-middleware Patch
@rspack/dev-server Patch
@rspack/plugin-html Patch
benchmarkcase-rspack-react-refresh Patch
@rspack/dev-client Patch
@rspack/plugin-minify Patch
@rspack/plugin-node-polyfill Patch
@rspack/binding-darwin-arm64 Patch
@rspack/binding-darwin-x64 Patch
@rspack/binding-linux-x64-gnu Patch
@rspack/binding-win32-x64-msvc Patch
@rspack/fs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the team The issue/pr is created by the member of Rspack. label Apr 18, 2023
@h-a-n-a h-a-n-a force-pushed the refactor/loader branch 2 times, most recently from 45357ef to ec5b85a Compare April 20, 2023 06:11
@h-a-n-a h-a-n-a changed the title refactor: loader refactor: support loader-runner and pitching Apr 20, 2023
@h-a-n-a h-a-n-a marked this pull request as ready for review April 20, 2023 09:22
@hardfist
Copy link
Contributor

it would be better if we have a example about pitching loader

@hardfist
Copy link
Contributor

!bench

@h-a-n-a
Copy link
Collaborator Author

h-a-n-a commented Apr 20, 2023

it would be better if we have a example about pitching loader

@hardfist Pitching loader is often used with inline loaders to initiate a new loader pipeline like how style-loader did, rspack does not support inline loaders now, I would like to add an example after style-loader or something else is supported.

@github-actions
Copy link
Contributor

Benchmark Results

group                                                 baseline                               pr
-----                                                 --------                               --
criterion_benchmark/ten_copy_of_threejs               1.00       2.3±0.07s        ? ?/sec    1.02       2.3±0.06s        ? ?/sec
high_cost_benchmark/ten_copy_of_threejs_production    1.02       7.5±0.09s        ? ?/sec    1.00       7.4±0.10s        ? ?/sec

@h-a-n-a h-a-n-a changed the title refactor: support loader-runner and pitching feat: support loader-runner and pitching Apr 21, 2023
@h-a-n-a h-a-n-a enabled auto-merge April 21, 2023 05:15
@h-a-n-a h-a-n-a added this pull request to the merge queue Apr 21, 2023
Merged via the queue into main with commit 0fd6d7a Apr 21, 2023
@h-a-n-a h-a-n-a deleted the refactor/loader branch April 21, 2023 05:18
@github-actions github-actions bot mentioned this pull request Apr 25, 2023
siyou pushed a commit to siyou/rspack that referenced this pull request May 14, 2023
* refactor: init

* refactor: replace with new loader

* chore: rename

* fix: execution order

* chore: lint

* chore: wip testing

* feat: make it compile

* feat: apply loader to rust

* feat: js wip

* feat: impl original content for pitching

* fix: fix typo

* feat: it works!

* feat: update message when last loader returns nothing

* fix: fix control flow

* chore: add more comments

* fix: fix resolving

* chore: remove testing

* feat: add some optimizations

* chore: lint

* chore: cleanup

* fix: hmr

* test: add pitching test

* chore: cleanup

* chore: lint

* add cgst

* chore: lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team The issue/pr is created by the member of Rspack.
Projects
None yet
3 participants