Files
Jimmy Miller 7c366e61be [turbopack] Track webpack loader build dependency files (#98777)
Track exact existing files and warn when loaders register unsupported
build dependency inputs.

I'm going to take this bit by bit so it is nice and reviewable. Does add
some noise. But the final fully supported thing was just too large. So I
will keep removing error cases on each commit.
2026-09-17 11:53:21 -07:00

39 lines
1.2 KiB
JavaScript

const path = require('node:path')
const fs = require('node:fs')
const loader = async function (content) {
this.async()
if (this.resourcePath.endsWith('unsupported-build-dependency.ts')) {
this.addBuildDependency(path.join(__dirname, 'utils'))
return this.callback(
null,
`export const utilFn = () => 'unsupported build dependency';`
)
}
if (!this.resourcePath.endsWith('file-to-transform.ts')) {
return this.callback(null, content)
}
const dependencyFile = './file-dependency.ts'
const context = path.dirname(this.resourcePath)
const resolve = this.getResolve({})
const result = await resolve(context, dependencyFile)
this.addDependency(result)
const missingDependency = path.join(context, 'missing-dependency.ts')
this.addMissingDependency(missingDependency)
const buildDependency = path.join(context, 'build-dependency.js')
this.addBuildDependency(buildDependency)
const buildDependencyValue = fs
.readFileSync(buildDependency, 'utf8')
.match(/'([^']+)'/)[1]
this.callback(
null,
`export const utilFn = () => 'Generated at ${new Date().toISOString()}, missing dependency: ${fs.existsSync(missingDependency)}, build dependency: ${buildDependencyValue}';`
)
}
module.exports = loader