Unverified Commit 32acdc03 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: absolute requests in webpack loader (#84575)

Recent sass-loader versions use this heavily: `this.getResolver()(..., "/Users/foo/..../bar.scss")`
parent adb2829e
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-ts-transform', () => { describe('webpack-loader-ts-transform', () => {
const { next } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
skipDeployment: true, skipDeployment: true,
}) })
if (skipped) return
it('should allow passing binary assets to and from a Webpack loader', async () => { it('should allow passing binary assets to and from a Webpack loader', async () => {
const $ = await next.render$('/') const $ = await next.render$('/')
expect($('#text').text()).toBe('Got a buffer of 18 bytes') expect($('#text').text()).toBe('Got a buffer of 18 bytes')
......
...@@ -4,11 +4,13 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -4,11 +4,13 @@ import { nextTestSetup } from 'e2e-utils'
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)( ;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'webpack-loader-conditions', 'webpack-loader-conditions',
() => { () => {
const { next } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
skipDeployment: true, skipDeployment: true,
}) })
if (skipped) return
it('should render correctly on server site', async () => { it('should render correctly on server site', async () => {
const res = await next.fetch('/') const res = await next.fetch('/')
const html = (await res.text()).replaceAll(/<!-- -->/g, '') const html = (await res.text()).replaceAll(/<!-- -->/g, '')
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-fs', () => { describe('webpack-loader-fs', () => {
const { next } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
skipDeployment: true, skipDeployment: true,
}) })
if (skipped) return
it('should allow reading the input FS', async () => { it('should allow reading the input FS', async () => {
const $ = await next.render$('/') const $ = await next.render$('/')
expect($('#test').text()).toBe( expect($('#test').text()).toBe(
......
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import absolute from './file.test-file?absolute=data1'
import relative from './file.test-file?relative=data2'
export default function Page() {
return (
<div>
<p id="absolute">{absolute}</p>
<p id="relative">{relative}</p>
</div>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
turbopack: {
rules: {
'*.test-file.ts': [require.resolve('./test-file-loader.js')],
},
},
webpack(config) {
config.module.rules.push({
test: /\.test-file\.ts/,
use: require.resolve('./test-file-loader.js'),
})
return config
},
}
module.exports = nextConfig
const fs = require('fs')
const path = require('path')
module.exports = async function () {
let params = new URLSearchParams(this.resourceQuery.slice(1))
let file
if (params.has('absolute')) {
file = path.join(__dirname, params.get('absolute'))
} else if (params.has('relative')) {
file = './' + params.get('relative')
} else {
this.callback(null, "throw new Error('no file specified')")
return
}
const resolve = this.getResolve({})
const result = await resolve(__dirname, file)
this.addDependency(result)
return fs.readFileSync(result, 'utf8')
}
import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-resolve', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
// This test is skipped because it's only expected to run in turbopack, which isn't enabled for builds
skipDeployment: true,
})
if (skipped) {
return
}
it('should support resolving absolute path via loader getResolve', async () => {
const $ = await next.render$('/')
expect($('#absolute').text()).toBe('abc')
expect($('#relative').text()).toBe('xyz')
})
})
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-resource-query', () => { describe('webpack-loader-resource-query', () => {
const { next } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
skipDeployment: true, skipDeployment: true,
}) })
// Recommended for tests that check HTML. Cheerio is a HTML parser that has a jQuery like API. if (skipped) return
it('should pass query to loader', async () => { it('should pass query to loader', async () => {
await next.render$('/') await next.render$('/')
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-ts-transform', () => { describe('webpack-loader-ts-transform', () => {
const { next, isTurbopack, skipped } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
// This test is skipped because it's only expected to run in turbopack, which isn't enabled for builds // This test is skipped because it's only expected to run in turbopack, which isn't enabled for builds
skipDeployment: true, skipDeployment: true,
}) })
if (!isTurbopack || skipped) { if (skipped) return
it('should only run the test in turbopack', () => {})
return
}
it('should accept Typescript returned from Webpack loaders', async () => { it('should accept Typescript returned from Webpack loaders', async () => {
const $ = await next.render$('/') const $ = await next.render$('/')
......
...@@ -16,6 +16,7 @@ import { ...@@ -16,6 +16,7 @@ import {
type TransformIpc, type TransformIpc,
} from './transforms' } from './transforms'
import fs from 'fs' import fs from 'fs'
import path from 'path'
export type IpcInfoMessage = export type IpcInfoMessage =
| { | {
...@@ -301,6 +302,22 @@ const transform = ( ...@@ -301,6 +302,22 @@ const transform = (
request: string, request: string,
callback?: (err?: Error, result?: string) => void callback?: (err?: Error, result?: string) => void
) => { ) => {
if (path.isAbsolute(request)) {
// Relativize absolute requests. Turbopack disallow them in JS code, but here it's
// generated programatically and there is a smaller problem of
// non-cacheable/non-portable builds.
request = path.relative(lookupPath, request)
// On Windows, the path might be still absolute if it's on a different drive. Just
// let the resolver throw the error in that case.
if (
!path.isAbsolute(request) &&
request.split(path.sep)[0] !== '..'
) {
request = './' + request
}
}
const promise = ipc const promise = ipc
.sendRequest({ .sendRequest({
type: 'resolve', type: 'resolve',
......
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