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 = { ...@@ -5,7 +5,7 @@ const nextConfig = {
turbopack: { turbopack: {
rules: { rules: {
'*.txt': { '*.txt': {
loaders: [require.resolve('./test-file-loader.js')], loaders: ['./test-file-loader.js'],
as: '*.js', as: '*.js',
}, },
'*.mp4': { '*.mp4': {
...@@ -17,7 +17,7 @@ const nextConfig = { ...@@ -17,7 +17,7 @@ const nextConfig = {
webpack(config) { webpack(config) {
config.module.rules.push({ config.module.rules.push({
test: /\.txt/, test: /\.txt/,
use: require.resolve('./test-file-loader.js'), use: './test-file-loader.js',
}) })
config.module.rules.push({ config.module.rules.push({
test: /\.mp4/, test: /\.mp4/,
......
...@@ -428,7 +428,7 @@ const transform = ( ...@@ -428,7 +428,7 @@ const transform = (
loaders: loadersWithOptions.map((loader) => ({ loaders: loadersWithOptions.map((loader) => ({
loader: __turbopack_external_require__.resolve(loader.loader, { loader: __turbopack_external_require__.resolve(loader.loader, {
paths: [resourceDir], paths: [contextDir, resourceDir],
}), }),
options: loader.options, 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