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

add basic webpack loader support #3284

Merged
merged 2 commits into from
Jan 12, 2023
Merged

add basic webpack loader support #3284

merged 2 commits into from
Jan 12, 2023

Conversation

sokra
Copy link
Member

@sokra sokra commented Jan 12, 2023

add the minimum to run a simple raw-loader (see test case)

  • add experimental.turbopackWebpackLoaders to next.config.js
    • key is extension like ".mdx", value is an array of loaders like ["mdx-loader"]

@sokra sokra requested review from a team as code owners January 12, 2023 11:49
@vercel
Copy link

vercel bot commented Jan 12, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
turbo-site ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Jan 12, 2023 at 4:02PM (UTC)
9 Ignored Deployments
Name Status Preview Comments Updated
examples-basic-web ⬜️ Ignored (Inspect) Jan 12, 2023 at 4:02PM (UTC)
examples-cra-web ⬜️ Ignored (Inspect) Jan 12, 2023 at 4:02PM (UTC)
examples-designsystem-docs ⬜️ Ignored (Inspect) Jan 12, 2023 at 4:02PM (UTC)
examples-kitchensink-blog ⬜️ Ignored (Inspect) Jan 12, 2023 at 4:02PM (UTC)
examples-native-web ⬜️ Ignored (Inspect) Jan 12, 2023 at 4:02PM (UTC)
examples-nonmonorepo ⬜️ Ignored (Inspect) Jan 12, 2023 at 4:02PM (UTC)
examples-svelte-web ⬜️ Ignored (Inspect) Jan 12, 2023 at 4:02PM (UTC)
examples-tailwind-web ⬜️ Ignored (Inspect) Jan 12, 2023 at 4:02PM (UTC)
turbo-vite-web ⬜️ Ignored (Inspect) Jan 12, 2023 at 4:02PM (UTC)

@github-actions
Copy link
Contributor

Benchmark for fd8b959

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 8653.96µs ± 46.17µs 8620.86µs ± 41.99µs -0.38%
bench_hmr_to_commit/Turbopack RCC/1000 modules 8918.28µs ± 65.97µs 8977.85µs ± 42.91µs +0.67%
bench_hmr_to_commit/Turbopack RSC/1000 modules 474.49ms ± 2.83ms 472.36ms ± 1.88ms -0.45%
bench_hmr_to_commit/Turbopack SSR/1000 modules 8720.86µs ± 47.14µs 8694.98µs ± 45.84µs -0.30%
bench_hmr_to_eval/Turbopack CSR/1000 modules 7623.01µs ± 72.45µs 7687.43µs ± 48.99µs +0.85%
bench_hmr_to_eval/Turbopack RCC/1000 modules 7798.03µs ± 53.37µs 7794.52µs ± 50.73µs -0.05%
bench_hmr_to_eval/Turbopack SSR/1000 modules 7653.41µs ± 64.93µs 7745.28µs ± 41.25µs +1.20%
bench_hydration/Turbopack RCC/1000 modules 3339.05ms ± 8.17ms 3353.19ms ± 11.83ms +0.42%
bench_hydration/Turbopack RSC/1000 modules 2842.97ms ± 9.54ms 2858.91ms ± 11.43ms +0.56%
bench_hydration/Turbopack SSR/1000 modules 2654.13ms ± 10.13ms 2649.45ms ± 8.84ms -0.18%
bench_startup/Turbopack CSR/1000 modules 1654.42ms ± 6.13ms 1636.82ms ± 6.45ms -1.06%
bench_startup/Turbopack RCC/1000 modules 2501.11ms ± 8.82ms 2509.27ms ± 7.87ms +0.33%
bench_startup/Turbopack RSC/1000 modules 2381.04ms ± 10.38ms 2398.48ms ± 6.87ms +0.73%
bench_startup/Turbopack SSR/1000 modules 2049.14ms ± 4.71ms 2052.14ms ± 6.02ms +0.15%

@github-actions
Copy link
Contributor

github-actions bot commented Jan 12, 2023

🟢 CI successful 🟢

Thanks

Copy link
Contributor

@alexkirsz alexkirsz left a comment

Choose a reason for hiding this comment

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

A few nits and questions, but otherwise :ship-it:

Comment on lines 221 to 223
let Some(experimental) = this.experimental.as_ref() else {
return Ok(WebpackLoadersOptionsVc::cell(WebpackLoadersOptions::default()));
};
let Some(turbopack_webpack_loaders) = experimental.turbopack_webpack_loaders.as_ref() else {
return Ok(WebpackLoadersOptionsVc::cell(WebpackLoadersOptions::default()));
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let Some(experimental) = this.experimental.as_ref() else {
return Ok(WebpackLoadersOptionsVc::cell(WebpackLoadersOptions::default()));
};
let Some(turbopack_webpack_loaders) = experimental.turbopack_webpack_loaders.as_ref() else {
return Ok(WebpackLoadersOptionsVc::cell(WebpackLoadersOptions::default()));
};
let Some(turbopack_webpack_loaders) = this.experimental.as_ref().and_then(|experimental| experimental.turbopack_webpack_loaders.as_ref()) else {
return Ok(WebpackLoadersOptionsVc::cell(WebpackLoadersOptions::default()));
};

@@ -22,6 +44,7 @@ pub struct ModuleOptionsContext {
pub enable_styled_components: bool,
pub enable_styled_jsx: bool,
pub enable_postcss_transform: Option<PostCssTransformOptions>,
pub enable_webpack_loaders: Option<WebpackLoadersOptions>,
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems weird that this is worded as a boolean (enable_something) instead of just webpack_loaders, like we have for other options. Same comment goes for PostCSS.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah. pub enable_styled_jsx: bool, could also be pub styled_jsx: bool,

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's do that in a follow-up PR

#[turbo_tasks::function]
fn read(&self, _fs_path: FileSystemPathVc) -> Result<FileContentVc> {
bail!("Reading is not possible from the virtual filesystem for the dev server")
bail!("Reading is not possible from the marker filesystem for the server")
Copy link
Contributor

Choose a reason for hiding this comment

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

What is a "marker" file system?

Copy link
Member Author

Choose a reason for hiding this comment

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

We only used it for paths and do not read/write from it. A virtual filesystem would allow to read/write...

"check": "tsc --noEmit"
},
"dependencies": {
"loader-runner": "^4.3.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

How do we ensure this is installed by the user? Is it one of Next.js' transitive dependencies? Or should we compile it and distribute it with turbopack?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sadly it's not accessible from next/dist. We probably should exposed that as compiled packages. It's currently bundled into webpack

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently the user need to install it to use this feature. That's fine for the first iteration...


// environment
"jsx": "react-jsx",
"lib": ["ESNext", "DOM"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to include DOM type definitions for this package?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. I just copied the tsconfig from next-dev^^

}

#[turbo_tasks::value]
struct WebpackLoadersedAsset {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
struct WebpackLoadersedAsset {
struct WebpackLoadersProcessedAsset {

@Brooooooklyn
Copy link
Contributor

It looks like it would be easier to implement this with napi-rs, @sokra do you mind if I make a prototype based on this with napi-rs?

@sokra
Copy link
Member Author

sokra commented Jan 12, 2023

It looks like it would be easier to implement this with napi-rs, @sokra do you mind if I make a prototype based on this with napi-rs?

A few questions?

  • Can we use napi-rs even if you run the standalone binary next-dev.exe without being called from next.js?`
  • Can we create a separate Node.js Isolate with napi-rs that can be restarted separately?
  • Can we provide the JS source files from in-memory without them being written to disk at all?

@Brooooooklyn
Copy link
Contributor

Can we use napi-rs even if you run the standalone binary next-dev.exe without being called from next.js?`

Why do we need to distribute turbopack as a binary? IMO the entry file can be a JavaScript file that calls the napi-rs wrapped Rust function like next-swc.

Can we create a separate Node.js Isolate with napi-rs that can be restarted separately?

We can use worker_threads API to do that, and I think if we can call JavaScript function directly in Rust, we can avoid restarting Node.js process scenario in most cases; just re-call the JavaScript function is fine.

Can we provide the JS source files from in-memory without them being written to disk at all?

Yes, sure; we can keep all the ability in turbopack with napi-rs, including the memory fs, resolve alias, and even dev-server.
Building turbopack with napi-rs just changes the way to distribute turbopack, we will not lose any features and abilities in turbopack.

@github-actions
Copy link
Contributor

Benchmark for e448d45

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 8058.07µs ± 39.91µs 8148.72µs ± 35.72µs +1.12%
bench_hmr_to_commit/Turbopack RCC/1000 modules 8382.36µs ± 51.08µs 8399.79µs ± 47.82µs +0.21%
bench_hmr_to_commit/Turbopack RSC/1000 modules 466.39ms ± 1.27ms 466.01ms ± 1.68ms -0.08%
bench_hmr_to_commit/Turbopack SSR/1000 modules 8257.27µs ± 34.50µs 8143.01µs ± 58.31µs -1.38%
bench_hmr_to_eval/Turbopack CSR/1000 modules 7160.96µs ± 66.89µs 7207.18µs ± 50.13µs +0.65%
bench_hmr_to_eval/Turbopack RCC/1000 modules 7342.99µs ± 44.93µs 7345.22µs ± 55.73µs +0.03%
bench_hmr_to_eval/Turbopack SSR/1000 modules 7228.54µs ± 62.71µs 7368.68µs ± 52.92µs +1.94%
bench_hydration/Turbopack RCC/1000 modules 3300.49ms ± 10.53ms 3278.94ms ± 11.82ms -0.65%
bench_hydration/Turbopack RSC/1000 modules 2806.19ms ± 8.70ms 2802.55ms ± 10.46ms -0.13%
bench_hydration/Turbopack SSR/1000 modules 2596.12ms ± 7.92ms 2613.78ms ± 9.99ms +0.68%
bench_startup/Turbopack CSR/1000 modules 1614.33ms ± 3.05ms 1614.55ms ± 6.48ms +0.01%
bench_startup/Turbopack RCC/1000 modules 2448.58ms ± 8.18ms 2448.79ms ± 5.01ms +0.01%
bench_startup/Turbopack RSC/1000 modules 2344.72ms ± 11.81ms 2371.13ms ± 7.48ms +1.13%
bench_startup/Turbopack SSR/1000 modules 2016.93ms ± 2.42ms 2012.42ms ± 4.87ms -0.22%

@sokra sokra merged commit 160dd1b into main Jan 12, 2023
@sokra sokra deleted the sokra/web-311-webpack-loaders branch January 12, 2023 20:07
.map(|(file, (content, _source_map))| {
// TODO handle SourceMap
VirtualAssetVc::new(
ServerFileSystemVc::new().root().join(&file),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be passing in the file system to use?

fn webpack_loaders_executor(project_root: FileSystemPathVc, context: AssetContextVc) -> AssetVc {
EcmascriptModuleAssetVc::new(
VirtualAssetVc::new(
project_root.join("__turbopack__/webpack-loaders-executor.ts"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't that be [embedded_modules]/@vercel/turbopack-node

jridgewell pushed a commit to vercel/next.js that referenced this pull request Mar 10, 2023
add the minimum to run a simple raw-loader (see test case)

* add `experimental.turbopackWebpackLoaders` to next.config.js
* key is extension like `".mdx"`, value is an array of loaders like
`["mdx-loader"]`
sokra added a commit to vercel/next.js that referenced this pull request Mar 13, 2023
add the minimum to run a simple raw-loader (see test case)

* add `experimental.turbopackWebpackLoaders` to next.config.js
* key is extension like `".mdx"`, value is an array of loaders like
`["mdx-loader"]`
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 25, 2024
add the minimum to run a simple raw-loader (see test case)

* add `experimental.turbopackWebpackLoaders` to next.config.js
* key is extension like `".mdx"`, value is an array of loaders like
`["mdx-loader"]`
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
add the minimum to run a simple raw-loader (see test case)

* add `experimental.turbopackWebpackLoaders` to next.config.js
* key is extension like `".mdx"`, value is an array of loaders like
`["mdx-loader"]`
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Aug 1, 2024
add the minimum to run a simple raw-loader (see test case)

* add `experimental.turbopackWebpackLoaders` to next.config.js
* key is extension like `".mdx"`, value is an array of loaders like
`["mdx-loader"]`
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.

6 participants