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