Skip to content

Commit 30d6d8f

Browse files
authored
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
1 parent 83cb4fb commit 30d6d8f

18 files changed

Lines changed: 51 additions & 31 deletions

File tree

‎crates/next-core/src/next_shared/webpack_rules/mod.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ pub async fn webpack_loader_options(
157157
rcstr!("web")
158158
},
159159
);
160+
let mode = if builtin_conditions.contains(&WebpackLoaderBuiltinCondition::Development) {
161+
rcstr!("development")
162+
} else {
163+
rcstr!("production")
164+
};
160165

161166
Ok(Vc::cell(Some(
162167
WebpackLoadersOptions {
@@ -166,6 +171,7 @@ pub async fn webpack_loader_options(
166171
.to_resolved()
167172
.await?,
168173
target,
174+
mode,
169175
}
170176
.resolved_cell(),
171177
)))

‎docs/01-app/03-api-reference/05-config/01-next-config-js/turbopack.mdx‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ Turbopack uses the [`loader-runner`](https://github.com/webpack/loader-runner) l
7979
- [`fs`](https://webpack.js.org/api/loaders/#thisfs) - Partial support: only `fs.readFile` is currently implemented.
8080
- [`emitFile`](https://webpack.js.org/api/loaders/#thisemitfile) - No support
8181

82-
**Context properties:**
83-
84-
- [`version`](https://webpack.js.org/api/loaders/#thisversion) - No support
85-
- [`mode`](https://webpack.js.org/api/loaders/#thismode) - No support
86-
8782
**Utilities:**
8883

8984
- [`utils`](https://webpack.js.org/api/loaders/#thisutils) - No support

‎test/development/sass-error/index.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ describe('app dir - css', () => {
3838
Pseudo-elements like '::before' or '::after' can't be followed by selectors like 'Ident("path")'
3939
4040
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:
41-
./app/global.scss.css:1:884
42-
> 1 | ...ate(-50%, 0px)}input.defaultCheckbox::before path{fill:currentColor}input:checked.defaul...
43-
| ^
41+
./app/global.scss.css:46:30
42+
44 | }
43+
45 |
44+
> 46 | input.defaultCheckbox::before path {
45+
| ^
46+
47 | fill: currentColor;
47+
48 | }
48+
49 |
4449
4550
Import trace:
4651
Client Component Browser:

test/e2e/app-dir/webpack-loader-target/app/client.target-test.js renamed to test/e2e/app-dir/webpack-loader-misc/app/client.misc-test.js

File renamed without changes.
File renamed without changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import ClientTarget from './client.misc-test'
2+
import { mode, target } from './server.misc-test'
3+
4+
export default function Page() {
5+
return (
6+
<main>
7+
<p id="server-target">{JSON.stringify(target)}</p>
8+
<p id="mode">{mode}</p>
9+
<ClientTarget />
10+
</main>
11+
)
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const target = '__TARGET__'
2+
export const mode = '__MODE__'
3+
4+
export default target
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = function (source) {
2+
return source
3+
.replaceAll("'__TARGET__'", JSON.stringify(this.target))
4+
.replaceAll("'__MODE__'", JSON.stringify(this.mode))
5+
}

test/e2e/app-dir/webpack-loader-target/next.config.ts renamed to test/e2e/app-dir/webpack-loader-misc/next.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import type { NextConfig } from 'next'
22

3-
const loader = require.resolve('./target-loader.js')
3+
const loader = require.resolve('./misc-loader.js')
44

55
const nextConfig: NextConfig = {
66
turbopack: {
77
rules: {
8-
'*.target-test.js': {
8+
'*.misc-test.js': {
99
loaders: [loader],
1010
},
1111
},
1212
},
1313
webpack(config) {
1414
config.module.rules.push({
15-
test: /\.target-test\.js$/,
15+
test: /\.misc-test\.js$/,
1616
use: loader,
1717
})
1818
return config

test/e2e/app-dir/webpack-loader-target/webpack-loader-target.test.ts renamed to test/e2e/app-dir/webpack-loader-misc/webpack-loader-misc.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { nextTestSetup } from 'e2e-utils'
22

3-
describe('webpack-loader-target', () => {
4-
const { next } = nextTestSetup({
3+
describe('webpack-loader-misc', () => {
4+
const { next, isNextDev } = nextTestSetup({
55
files: __dirname,
66
})
77

8-
it('exposes the server compilation target to loaders', async () => {
8+
it('exposes the server compilation target and mode to loaders', async () => {
99
const $ = await next.render$('/')
1010
expect($('#server-target').text()).toBe('"node"')
11+
expect($('#mode').text()).toBe(isNextDev ? 'development' : 'production')
1112
})
1213

1314
it('exposes the browser compilation target to loaders', async () => {

0 commit comments

Comments
 (0)