Unverified Commit b8797762 authored by Jimmy Miller's avatar Jimmy Miller Committed by GitHub

Adds this.target for webpack loaders (#98436)

There are a bunch of different loaders I found depending on this

- vue-loader
- svelte-loader
- svg-sprite-loader
- nunjucks-loader
- workerize-loader
- worklet-loader
- nativescript-worker-loader
- thread-loader
- svg-sprite-generation-loader

This doesn't by itself fix any of them fully, but does increase
compatibility
parent 4d621240
......@@ -150,6 +150,13 @@ pub async fn webpack_loader_options(
&mut get_babel_loader_rules(&project_path, next_config, &builtin_conditions, &user_rules)
.await?,
);
let target = ResolvedVc::cell(
if builtin_conditions.contains(&WebpackLoaderBuiltinCondition::Node) {
rcstr!("node")
} else {
rcstr!("web")
},
);
Ok(Vc::cell(Some(
WebpackLoadersOptions {
......@@ -158,6 +165,7 @@ pub async fn webpack_loader_options(
builtin_conditions: NextWebpackLoaderBuiltinConditionSet::new(builtin_conditions)
.to_resolved()
.await?,
target,
}
.resolved_cell(),
)))
......
......@@ -83,7 +83,6 @@ Turbopack uses the [`loader-runner`](https://github.com/webpack/loader-runner) l
- [`version`](https://webpack.js.org/api/loaders/#thisversion) - No support
- [`mode`](https://webpack.js.org/api/loaders/#thismode) - No support
- [`target`](https://webpack.js.org/api/loaders/#thistarget) - No support
**Utilities:**
......
'use client'
import { useEffect, useState } from 'react'
const target = '__TARGET__'
export default function ClientTarget() {
const [mounted, setMounted] = useState(false)
useEffect(() => setMounted(true), [])
return <p id="client-target">{mounted ? JSON.stringify(target) : ''}</p>
}
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import ClientTarget from './client.target-test'
import serverTarget from './server.target-test'
export default function Page() {
return (
<main>
<p id="server-target">{JSON.stringify(serverTarget)}</p>
<ClientTarget />
</main>
)
}
const target = '__TARGET__'
export default target
import type { NextConfig } from 'next'
const loader = require.resolve('./target-loader.js')
const nextConfig: NextConfig = {
turbopack: {
rules: {
'*.target-test.js': {
loaders: [loader],
},
},
},
webpack(config) {
config.module.rules.push({
test: /\.target-test\.js$/,
use: loader,
})
return config
},
}
export default nextConfig
module.exports = function (source) {
return source.replaceAll("'__TARGET__'", JSON.stringify(this.target))
}
import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-target', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('exposes the server compilation target to loaders', async () => {
const $ = await next.render$('/')
expect($('#server-target').text()).toBe('"node"')
})
it('exposes the browser compilation target to loaders', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('#client-target').text()).toBe('"web"')
})
})
......@@ -162,6 +162,7 @@ const transform = (
name: string,
query: string,
loaders: LoaderConfig[],
target: string,
sourceMap: boolean
) => {
return new Promise((resolve, reject) => {
......@@ -191,6 +192,7 @@ const transform = (
},
currentTraceSpan: new DummySpan(),
rootContext: contextDir,
target,
sourceMap,
getOptions() {
const entry = this.loaders[this.loaderIndex]
......
......@@ -97,6 +97,7 @@ pub struct WebpackLoaders {
evaluate_context: ResolvedVc<Box<dyn AssetContext>>,
execution_context: ResolvedVc<ExecutionContext>,
loaders: ResolvedVc<WebpackLoaderItems>,
target: ResolvedVc<RcStr>,
rename_as: Option<RcStr>,
resolve_options_context: ResolvedVc<ResolveOptionsContext>,
source_maps: bool,
......@@ -109,6 +110,7 @@ impl WebpackLoaders {
evaluate_context: ResolvedVc<Box<dyn AssetContext>>,
execution_context: ResolvedVc<ExecutionContext>,
loaders: ResolvedVc<WebpackLoaderItems>,
target: ResolvedVc<RcStr>,
rename_as: Option<RcStr>,
resolve_options_context: ResolvedVc<ResolveOptionsContext>,
source_maps: bool,
......@@ -117,6 +119,7 @@ impl WebpackLoaders {
evaluate_context,
execution_context,
loaders,
target,
rename_as,
resolve_options_context,
source_maps,
......@@ -319,6 +322,7 @@ impl WebpackLoadersProcessedAsset {
ResolvedVc::cell(resource_path.to_string().into()),
ResolvedVc::cell(self.source.ident().await?.query.to_string().into()),
ResolvedVc::cell(json!(*loaders)),
ResolvedVc::cell(transform.target.await?.to_string().into()),
ResolvedVc::cell(transform.source_maps.into()),
],
additional_invalidation: Completion::immutable().to_resolved().await?,
......
......@@ -732,6 +732,7 @@ async fn process_default_internal(
*evaluate_context,
*execution_context,
loaders_vc,
*webpack_loaders_options.target,
rename_as.clone(),
*resolve_options_context,
source_maps,
......
......@@ -500,6 +500,7 @@ impl ModuleOptions {
),
*execution_context,
*rule.loaders,
*webpack_loaders_options.target,
rule.rename_as.clone(),
resolve_options_context,
matches!(ecmascript_source_maps, SourceMapsType::Full),
......
......@@ -83,6 +83,7 @@ pub struct WebpackLoadersOptions {
pub rules: ResolvedVc<WebpackRules>,
pub builtin_conditions: ResolvedVc<Box<dyn WebpackLoaderBuiltinConditionSet>>,
pub loader_runner_package: Option<ResolvedVc<ImportMapping>>,
pub target: ResolvedVc<RcStr>,
}
pub enum WebpackLoaderBuiltinConditionSetMatch {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment