Unverified Commit 4797acb8 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix instrumentationClientInject with type:module (#94184)

If the user's `package.json` contains, `type: module`, then the file
generated by Turbopack at `[project]/__next_instrumentation_client.js`
would get treated as ESM. But it always contains CJS, causing
```
Uncaught (in promise) ReferenceError: module is not defined
    at module evaluation (__next_instrumentation_client.js:3:1)
```
parent 1095b9eb
......@@ -1452,7 +1452,10 @@ async fn insert_instrumentation_client_alias(
body.push_str("}};\n");
let virtual_source = VirtualSource::new(
project_path.join("__next_instrumentation_client.js")?,
// Use cjs here in case the user has type:module in the package.json. We do intentionally
// place this file in the user's folder, so that the `require`s inserted above resolve
// as expected.
project_path.join("__next_instrumentation_client.cjs")?,
AssetContent::file(FileContent::Content(body.into()).cell()),
)
.to_resolved()
......
/** @type {import('next').NextConfig} */
module.exports = {
export default {
instrumentationClientInject: ['./inject-a.js', './inject-b.js'],
}
......@@ -3,7 +3,7 @@ import { retry } from 'next-test-utils'
import path from 'path'
describe('Instrumentation Client Hook', () => {
const testCases = [
describe.each([
{
name: 'With src folder',
appDir: 'app-with-src',
......@@ -19,9 +19,7 @@ describe('Instrumentation Client Hook', () => {
appDir: 'pages-router',
shouldLog: false,
},
]
testCases.forEach(({ name, appDir, shouldLog }) => {
])('$name', ({ name, appDir, shouldLog }) => {
describe(name, () => {
const { next, isNextDev } = nextTestSetup({
files: path.join(__dirname, appDir),
......@@ -102,9 +100,19 @@ describe('Instrumentation Client Hook', () => {
})
})
describe('instrumentationClientInject', () => {
describe.each([
{
name: 'default',
packageJson: {},
},
{
name: 'with type:module',
packageJson: { type: 'module' },
},
])('instrumentationClientInject $name', ({ packageJson }) => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'inject'),
packageJson,
})
it('runs each injected entry before the user instrumentation-client and before hydration, in array order', async () => {
......
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