Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { utilFn } from '../utils/file-to-transform'

export default function Page() {
return <p>{utilFn()}</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('node:path')

const loader = async function (content) {
this.async()

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)

this.callback(
null,
`export const utilFn = () => 'Generated at ${new Date().toISOString()}';`
)
}

module.exports = loader
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const path = require('node:path')

/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
turbopack: {
rules: {
'*.ts': {
loaders: [path.resolve(__dirname, './loader.js')],
},
},
},
webpack: (config) => {
config.module.rules.push({
test: /\.ts$/,
use: [path.resolve(__dirname, './loader.js')],
})
return config
},
}

module.exports = nextConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { nextTestSetup } from 'e2e-utils'
import { waitFor } from 'next-test-utils'

describe('turbopack-loader-file-dependencies', () => {
const { next } = nextTestSetup({
files: __dirname,
})

it('should update when the dependency file changes', async () => {
const $ = await next.render$('/')
const initialText = await $('p').text()
expect(initialText).toBeTruthy()

await next.patchFile(
'utils/file-dependency.ts',
'export const magicValue = "magic-value-2";'
)

await waitFor(1000)

const $2 = await next.render$('/')
const newText = await $2('p').text()
expect(newText).not.toBe(initialText)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const magicValue = 'magic-value'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { magicValue } from './file-dependency'

export const utilFn = () => {
return magicValue
}
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-node/src/transforms/webpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ pub enum InfoMessage {
// Sent to inform Turbopack about the dependencies of the task.
// All fields are `default` since it is ok for the client to
// simply omit instead of sending empty arrays.
#[serde(rename_all = "camelCase")]
Dependencies {
#[serde(default)]
env_variables: Vec<RcStr>,
Expand Down