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( ...@@ -1452,7 +1452,10 @@ async fn insert_instrumentation_client_alias(
body.push_str("}};\n"); body.push_str("}};\n");
let virtual_source = VirtualSource::new( 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()), AssetContent::file(FileContent::Content(body.into()).cell()),
) )
.to_resolved() .to_resolved()
......
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
module.exports = { export default {
instrumentationClientInject: ['./inject-a.js', './inject-b.js'], instrumentationClientInject: ['./inject-a.js', './inject-b.js'],
} }
...@@ -3,7 +3,7 @@ import { retry } from 'next-test-utils' ...@@ -3,7 +3,7 @@ import { retry } from 'next-test-utils'
import path from 'path' import path from 'path'
describe('Instrumentation Client Hook', () => { describe('Instrumentation Client Hook', () => {
const testCases = [ describe.each([
{ {
name: 'With src folder', name: 'With src folder',
appDir: 'app-with-src', appDir: 'app-with-src',
...@@ -19,9 +19,7 @@ describe('Instrumentation Client Hook', () => { ...@@ -19,9 +19,7 @@ describe('Instrumentation Client Hook', () => {
appDir: 'pages-router', appDir: 'pages-router',
shouldLog: false, shouldLog: false,
}, },
] ])('$name', ({ name, appDir, shouldLog }) => {
testCases.forEach(({ name, appDir, shouldLog }) => {
describe(name, () => { describe(name, () => {
const { next, isNextDev } = nextTestSetup({ const { next, isNextDev } = nextTestSetup({
files: path.join(__dirname, appDir), files: path.join(__dirname, appDir),
...@@ -102,9 +100,19 @@ describe('Instrumentation Client Hook', () => { ...@@ -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({ const { next } = nextTestSetup({
files: path.join(__dirname, 'inject'), files: path.join(__dirname, 'inject'),
packageJson,
}) })
it('runs each injected entry before the user instrumentation-client and before hydration, in array order', async () => { 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