Unverified Commit 3e4af55d authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix `Unknown module type` error for imports in webpack loaders (#99088)

Fixes an error like
```
./node_modules/.pnpm/@esbuild+linux-x64@0.28.1/node_modules/@esbuild/linux-x64/README.md
Error: Unknown module type
This module doesn't have an associated type. Use a known file extension, or register a loader for it.
```

Regression from https://github.com/vercel/next.js/pull/95732
parent 7f79552e
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <main>Webpack loader traced imports</main>
}
function requireSub(name) {
return require(`./sub/${name}`)
}
module.exports = requireSub
module.exports = function loader(source) {
require.resolve('./dependency')
return source
}
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const dirname = path.dirname(fileURLToPath(import.meta.url))
export default {
turbopack: {
rules: {
'*.tsx': {
loaders: [path.join(dirname, 'loader.js')],
},
},
},
}
Unrelated file in a directory matched by a dynamic require.
import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-traced-imports', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('builds when a loader resolves a module with a dynamic require', async () => {
const $ = await next.render$('/')
expect($('main').text()).toBe('Webpack loader traced imports')
})
})
......@@ -4,25 +4,30 @@ use turbo_tasks::{
graph::{AdjacencyMap, GraphTraversal},
};
use crate::{asset::Asset, module::Module, reference::primary_referenced_modules};
use crate::{asset::Asset, module::Module, reference::referenced_modules_and_affecting_sources};
pub async fn get_referenced_modules(
async fn get_referenced_modules_and_affecting_sources(
parent: ResolvedVc<Box<dyn Module>>,
) -> Result<impl Iterator<Item = ResolvedVc<Box<dyn Module>>> + Send> {
Ok(primary_referenced_modules(*parent)
.owned()
// This graph is only used for change detection. Traverse resolved modules and affecting
// sources without emitting module-type issues for unrelated files matched by dynamic requests.
let modules = referenced_modules_and_affecting_sources(*parent, false)
.await?
.into_iter())
.iter()
.flat_map(|(_, resolved)| resolved.modules.iter().copied())
.collect::<Vec<_>>();
Ok(modules.into_iter())
}
/// Returns a completion that changes when any content of any asset in the whole
/// asset graph changes.
/// Also changes when the resolution of any referenced module changes (= affecting sources).
#[turbo_tasks::function]
pub async fn any_source_content_changed_of_module(
root: ResolvedVc<Box<dyn Module>>,
) -> Result<Vc<Completion>> {
let completions = AdjacencyMap::new()
.visit([root], get_referenced_modules)
.visit([root], get_referenced_modules_and_affecting_sources)
.await
.completed()?
.into_postorder_topological()
......
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