Unverified Commit 4669a153 authored by zoomdong's avatar zoomdong Committed by GitHub

fix(turbopack): webpack-loaders failed to resolve relative path (#82720)

fixed: #82106 

The error was cause by the follow reason, when pass:

```ts
const nextConfig: NextConfig = {
  /* config options here */
  turbopack: {
    rules: {
      '*.txt': {
        loaders: ['./test-file-loader.js'],
        as: '*.js',
      },
    }
  }
};
```

and the project directory as follows:

```bash
src
 |_ app
      |_ index.tsx
      |_ text.txt
```

The loader will deal the `text.txt` under `src/app`, then the loader
will resolve path like:

```ts
require.resolve('./test-file-loader.js', paths: ['/Users/next-app/src/app'])
```

Because the resource dir the `dirname(resoucePath)`, and the
resourcePath is `/Users/next-app/src/app/text.txt`, this will never
resolve the right loader path here.

So I just add a more `path` param(which is `contextDir`) for the
require.resolve function, it will help the loader to find the right path
here.
parent b421c9ff
......@@ -5,7 +5,7 @@ const nextConfig = {
turbopack: {
rules: {
'*.txt': {
loaders: [require.resolve('./test-file-loader.js')],
loaders: ['./test-file-loader.js'],
as: '*.js',
},
'*.mp4': {
......@@ -17,7 +17,7 @@ const nextConfig = {
webpack(config) {
config.module.rules.push({
test: /\.txt/,
use: require.resolve('./test-file-loader.js'),
use: './test-file-loader.js',
})
config.module.rules.push({
test: /\.mp4/,
......
......@@ -428,7 +428,7 @@ const transform = (
loaders: loadersWithOptions.map((loader) => ({
loader: __turbopack_external_require__.resolve(loader.loader, {
paths: [resourceDir],
paths: [contextDir, resourceDir],
}),
options: loader.options,
})),
......
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