Unverified Commit 30d6d8f4 authored by Jimmy Miller's avatar Jimmy Miller Committed by GitHub

Adds this.mode for webpack loaders (#98532)

Pretty straightforward. But a number of loaders depend on it including

- postcss-loader
- sass-loader
- stylus-loader
- vue-loader
- nunjucks-loader
- thread-loader
parent 83cb4fb7
......@@ -157,6 +157,11 @@ pub async fn webpack_loader_options(
rcstr!("web")
},
);
let mode = if builtin_conditions.contains(&WebpackLoaderBuiltinCondition::Development) {
rcstr!("development")
} else {
rcstr!("production")
};
Ok(Vc::cell(Some(
WebpackLoadersOptions {
......@@ -166,6 +171,7 @@ pub async fn webpack_loader_options(
.to_resolved()
.await?,
target,
mode,
}
.resolved_cell(),
)))
......
......@@ -79,11 +79,6 @@ Turbopack uses the [`loader-runner`](https://github.com/webpack/loader-runner) l
- [`fs`](https://webpack.js.org/api/loaders/#thisfs) - Partial support: only `fs.readFile` is currently implemented.
- [`emitFile`](https://webpack.js.org/api/loaders/#thisemitfile) - No support
**Context properties:**
- [`version`](https://webpack.js.org/api/loaders/#thisversion) - No support
- [`mode`](https://webpack.js.org/api/loaders/#thismode) - No support
**Utilities:**
- [`utils`](https://webpack.js.org/api/loaders/#thisutils) - No support
......
......@@ -38,9 +38,14 @@ describe('app dir - css', () => {
Pseudo-elements like '::before' or '::after' can't be followed by selectors like 'Ident("path")'
Generated code of PostCSS transform of loaders [next/dist/build/webpack/loaders/resolve-url-loader/index, next/dist/compiled/sass-loader] transform of file content of app/global.scss:
./app/global.scss.css:1:884
> 1 | ...ate(-50%, 0px)}input.defaultCheckbox::before path{fill:currentColor}input:checked.defaul...
| ^
./app/global.scss.css:46:30
44 | }
45 |
> 46 | input.defaultCheckbox::before path {
| ^
47 | fill: currentColor;
48 | }
49 |
Import trace:
Client Component Browser:
......
import ClientTarget from './client.target-test'
import serverTarget from './server.target-test'
import ClientTarget from './client.misc-test'
import { mode, target } from './server.misc-test'
export default function Page() {
return (
<main>
<p id="server-target">{JSON.stringify(serverTarget)}</p>
<p id="server-target">{JSON.stringify(target)}</p>
<p id="mode">{mode}</p>
<ClientTarget />
</main>
)
......
export const target = '__TARGET__'
export const mode = '__MODE__'
export default target
module.exports = function (source) {
return source
.replaceAll("'__TARGET__'", JSON.stringify(this.target))
.replaceAll("'__MODE__'", JSON.stringify(this.mode))
}
import type { NextConfig } from 'next'
const loader = require.resolve('./target-loader.js')
const loader = require.resolve('./misc-loader.js')
const nextConfig: NextConfig = {
turbopack: {
rules: {
'*.target-test.js': {
'*.misc-test.js': {
loaders: [loader],
},
},
},
webpack(config) {
config.module.rules.push({
test: /\.target-test\.js$/,
test: /\.misc-test\.js$/,
use: loader,
})
return config
......
import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-target', () => {
const { next } = nextTestSetup({
describe('webpack-loader-misc', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
})
it('exposes the server compilation target to loaders', async () => {
it('exposes the server compilation target and mode to loaders', async () => {
const $ = await next.render$('/')
expect($('#server-target').text()).toBe('"node"')
expect($('#mode').text()).toBe(isNextDev ? 'development' : 'production')
})
it('exposes the browser compilation target to loaders', async () => {
......
module.exports = function (source) {
return source.replaceAll("'__TARGET__'", JSON.stringify(this.target))
}
......@@ -163,6 +163,7 @@ const transform = (
query: string,
loaders: LoaderConfig[],
target: string,
mode: 'development' | 'production',
sourceMap: boolean
) => {
return new Promise((resolve, reject) => {
......@@ -193,6 +194,7 @@ const transform = (
currentTraceSpan: new DummySpan(),
rootContext: contextDir,
target,
mode,
sourceMap,
getOptions() {
const entry = this.loaders[this.loaderIndex]
......
......@@ -98,6 +98,7 @@ pub struct WebpackLoaders {
execution_context: ResolvedVc<ExecutionContext>,
loaders: ResolvedVc<WebpackLoaderItems>,
target: ResolvedVc<RcStr>,
mode: RcStr,
rename_as: Option<RcStr>,
resolve_options_context: ResolvedVc<ResolveOptionsContext>,
source_maps: bool,
......@@ -111,6 +112,7 @@ impl WebpackLoaders {
execution_context: ResolvedVc<ExecutionContext>,
loaders: ResolvedVc<WebpackLoaderItems>,
target: ResolvedVc<RcStr>,
mode: RcStr,
rename_as: Option<RcStr>,
resolve_options_context: ResolvedVc<ResolveOptionsContext>,
source_maps: bool,
......@@ -120,6 +122,7 @@ impl WebpackLoaders {
execution_context,
loaders,
target,
mode,
rename_as,
resolve_options_context,
source_maps,
......@@ -323,6 +326,7 @@ impl WebpackLoadersProcessedAsset {
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.mode.to_string().into()),
ResolvedVc::cell(transform.source_maps.into()),
],
additional_invalidation: Completion::immutable().to_resolved().await?,
......
......@@ -684,6 +684,7 @@ async fn process_default_internal(
*execution_context,
loaders_vc,
*webpack_loaders_options.target,
webpack_loaders_options.mode.clone(),
rename_as.clone(),
*resolve_options_context,
source_maps,
......
......@@ -503,6 +503,7 @@ impl ModuleOptions {
*execution_context,
*rule.loaders,
*webpack_loaders_options.target,
webpack_loaders_options.mode.clone(),
rule.rename_as.clone(),
resolve_options_context,
matches!(ecmascript_source_maps, SourceMapsType::Full),
......
......@@ -84,6 +84,7 @@ pub struct WebpackLoadersOptions {
pub builtin_conditions: ResolvedVc<Box<dyn WebpackLoaderBuiltinConditionSet>>,
pub loader_runner_package: Option<ResolvedVc<ImportMapping>>,
pub target: ResolvedVc<RcStr>,
pub mode: 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