Unverified Commit 9d9e1d5b authored by Jimmy Miller's avatar Jimmy Miller Committed by GitHub

Fix turbopacks webpack loader setup to support options being optional (#98270)

If you look at the types for webpacks loaders, you can see that options
is optional.
https://github.com/webpack/webpack/blob/c17742f631eaeaf1ad880a70b105eedd96dd21b9/declarations/LoaderContext.d.ts#L83

The implementation between the two is slightly different so webpacks
impementation doens't jsut set it to {}. But with the way turbopack
works, this is the equivalent.

I saw a bunch of failures all related to this in real loaders, so figure
it was an easy quick fix.

(also extensionOptions is optional in webpack and needed to be here for
the the types to all checkout
https://github.com/webpack/webpack/blob/c17742f631eaeaf1ad880a70b105eedd96dd21b9/declarations/WebpackOptions.d.ts#L2175)
parent 665d3458
import resolved from './file.no-options'
export default function Page() {
return <p id="no-options">{resolved}</p>
}
const fs = require('fs')
module.exports = async function () {
const resolve = this.getResolve()
const result = await resolve(__dirname, './data2')
this.addDependency(result)
return fs.readFileSync(result, 'utf8')
}
...@@ -5,6 +5,9 @@ const nextConfig = { ...@@ -5,6 +5,9 @@ const nextConfig = {
turbopack: { turbopack: {
rules: { rules: {
'*.test-file.ts': [require.resolve('./test-file-loader.js')], '*.test-file.ts': [require.resolve('./test-file-loader.js')],
'*.no-options.ts': [
require.resolve('./get-resolve-no-options-loader.js'),
],
}, },
}, },
webpack(config) { webpack(config) {
...@@ -12,6 +15,10 @@ const nextConfig = { ...@@ -12,6 +15,10 @@ const nextConfig = {
test: /\.test-file\.ts/, test: /\.test-file\.ts/,
use: require.resolve('./test-file-loader.js'), use: require.resolve('./test-file-loader.js'),
}) })
config.module.rules.push({
test: /\.no-options\.ts/,
use: require.resolve('./get-resolve-no-options-loader.js'),
})
return config return config
}, },
} }
......
...@@ -16,4 +16,9 @@ describe('webpack-loader-resolve', () => { ...@@ -16,4 +16,9 @@ describe('webpack-loader-resolve', () => {
expect($('#absolute').text()).toBe('abc') expect($('#absolute').text()).toBe('abc')
expect($('#relative').text()).toBe('xyz') expect($('#relative').text()).toBe('xyz')
}) })
it('should support loader getResolve without options', async () => {
const $ = await next.render$('/no-options')
expect($('#no-options').text()).toBe('xyz')
})
}) })
...@@ -138,7 +138,7 @@ type ResolveOptions = { ...@@ -138,7 +138,7 @@ type ResolveOptions = {
conditionNames?: string[] conditionNames?: string[]
descriptionFiles?: string[] descriptionFiles?: string[]
enforceExtension?: boolean enforceExtension?: boolean
extensionAlias: Record<string, string[]> extensionAlias?: Record<string, string[]>
extensions?: string[] extensions?: string[]
fallback?: Record<string, string[]> fallback?: Record<string, string[]>
mainFields?: string[] mainFields?: string[]
...@@ -215,7 +215,7 @@ const transform = ( ...@@ -215,7 +215,7 @@ const transform = (
) )
}, },
}, },
getResolve: (options: ResolveOptions) => { getResolve: (options: ResolveOptions = {}) => {
const rustOptions = { const rustOptions = {
aliasFields: undefined as undefined | string[], aliasFields: undefined as undefined | string[],
conditionNames: undefined as undefined | string[], conditionNames: undefined as undefined | string[],
......
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