Unverified Commit efc5ae42 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: app externals test case improvements (#62871)

### What?

* rename test case
* improve error message and handle edge case for require resolving
* fix test case actually testing externals (webpack was silently
bundling, Turbopack showed error that helped to find the broken test
case)

### Why?

### How?


Closes PACK-2662
parent 82dc672a
...@@ -171,10 +171,10 @@ pub async fn get_server_resolve_options_context( ...@@ -171,10 +171,10 @@ pub async fn get_server_resolve_options_context(
| ServerContextType::PagesApi { .. } => { | ServerContextType::PagesApi { .. } => {
vec![ vec![
Vc::upcast(module_feature_report_resolve_plugin), Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(external_cjs_modules_plugin),
Vc::upcast(unsupported_modules_resolve_plugin), Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_external_plugin),
Vc::upcast(next_node_shared_runtime_plugin), Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(external_cjs_modules_plugin),
Vc::upcast(next_external_plugin),
] ]
} }
ServerContextType::AppSSR { .. } ServerContextType::AppSSR { .. }
...@@ -182,10 +182,10 @@ pub async fn get_server_resolve_options_context( ...@@ -182,10 +182,10 @@ pub async fn get_server_resolve_options_context(
| ServerContextType::AppRoute { .. } => { | ServerContextType::AppRoute { .. } => {
vec![ vec![
Vc::upcast(module_feature_report_resolve_plugin), Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(server_component_externals_plugin),
Vc::upcast(unsupported_modules_resolve_plugin), Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_external_plugin),
Vc::upcast(next_node_shared_runtime_plugin), Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(server_component_externals_plugin),
Vc::upcast(next_external_plugin),
] ]
} }
ServerContextType::Middleware { .. } => { ServerContextType::Middleware { .. } => {
...@@ -199,8 +199,8 @@ pub async fn get_server_resolve_options_context( ...@@ -199,8 +199,8 @@ pub async fn get_server_resolve_options_context(
vec![ vec![
Vc::upcast(module_feature_report_resolve_plugin), Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(unsupported_modules_resolve_plugin), Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_external_plugin),
Vc::upcast(next_node_shared_runtime_plugin), Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(next_external_plugin),
] ]
} }
}; };
......
...@@ -150,7 +150,7 @@ createNextDescribe( ...@@ -150,7 +150,7 @@ createNextDescribe(
).toMatch(/^__myFont_.{6}, __myFont_Fallback_.{6}$/) ).toMatch(/^__myFont_.{6}, __myFont_Fallback_.{6}$/)
}) })
it('should not apply swc optimizer transform for external packages in browser layer', async () => { it('should not apply swc optimizer transform for external packages in browser layer in web worker', async () => {
const browser = await next.browser('/browser') const browser = await next.browser('/browser')
expect(await browser.elementByCss('#worker-state').text()).toBe('default') expect(await browser.elementByCss('#worker-state').text()).toBe('default')
......
...@@ -11,7 +11,8 @@ ...@@ -11,7 +11,8 @@
"default": "./subpath.js" "default": "./subpath.js"
}, },
"./react": { "./react": {
"import": "./react.js" "import": "./react.mjs",
"default": "./react.js"
}, },
"./package.json": "./package.json" "./package.json": "./package.json"
} }
......
import React from 'react' const React = require('react')
export function getReactVersion() { function getReactVersion() {
return React.version return React.version
} }
exports.getReactVersion = getReactVersion
import React from 'react'
export function getReactVersion() {
return React.version
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import World1 from 'app-esm-package1/entry' import World1 from 'app-esm-package1/entry'
import World2 from 'app-esm-package2/entry' import World2 from 'app-esm-package2/entry'
import World3 from 'invalid-esm-package/entry' import World3 from 'app-cjs-esm-package/entry'
export default function Index() { export default function Index() {
return ( return (
......
import World1 from 'app-esm-package1/entry' import World1 from 'app-esm-package1/entry'
import World2 from 'app-esm-package2/entry' import World2 from 'app-esm-package2/entry'
import World3 from 'invalid-esm-package/entry' import World3 from 'app-cjs-esm-package/entry'
export default function Page() { export default function Page() {
return ( return (
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
function normalize(str: string) {
return str.replace(/<!-- -->/g, '')
}
describe('esm-externals', () => { describe('esm-externals', () => {
const { next } = nextTestSetup({ const { next, isTurbopack } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
// Pages // Pages
{ {
const expectedHtml =
/Hello <!-- -->World<!-- -->\+<!-- -->World<!-- -->\+<!-- -->World<!-- -->\+<!-- -->World\+World\+World/
const expectedText = /Hello World\+World\+World\+World\+World\+World/
const urls = ['/static', '/ssr', '/ssg'] const urls = ['/static', '/ssr', '/ssg']
for (const url of urls) { for (const url of urls) {
// TODO fix webpack behavior
const expectedHtml = isTurbopack
? /Hello World\+World\+World\+World\+World\+World/
: url === '/static'
? /Hello World\+World\+Alternative\+World\+World\+World/
: /Hello World\+World\+Alternative\+World\+World\+Alternative/
// TODO fix webpack behavior
const expectedText =
isTurbopack || url === '/static'
? /Hello World\+World\+World\+World\+World\+World/
: /Hello World\+World\+World\+World\+World\+Alternative/
it(`should return the correct SSR HTML for ${url}`, async () => { it(`should return the correct SSR HTML for ${url}`, async () => {
const res = await next.fetch(url) const res = await next.fetch(url)
const html = await res.text() const html = await res.text()
expect(html).toMatch(expectedHtml) expect(normalize(html)).toMatch(expectedHtml)
}) })
it(`should render the correct page for ${url}`, async () => { it(`should render the correct page for ${url}`, async () => {
...@@ -29,22 +42,20 @@ describe('esm-externals', () => { ...@@ -29,22 +42,20 @@ describe('esm-externals', () => {
{ {
// TODO App Dir doesn't use esmExternals: true correctly for webpack and Turbopack // TODO App Dir doesn't use esmExternals: true correctly for webpack and Turbopack
// so we only verify that the page doesn't crash, but ignore the actual content // so we only verify that the page doesn't crash, but ignore the actual content
// const expectedHtml = // const expectedHtml = /Hello World\+World\+World/
// /Hello <!-- -->World<!-- -->\+<!-- -->World<!-- -->\+<!-- -->World/ const expectedHtml = /Hello Wrong\+Wrong\+Alternative/
const expectedHtml =
/Hello <!-- -->Wrong<!-- -->\+<!-- -->Wrong<!-- -->\+<!-- -->Wrong/
// const expectedText = /Hello World\+World\+World/ // const expectedText = /Hello World\+World\+World/
const urls = ['/server', '/client'] const urls = ['/server', '/client']
for (const url of urls) { for (const url of urls) {
const expectedText = const expectedText =
url === '/server' url === '/server'
? /Hello Wrong\+Wrong\+Wrong/ ? /Hello Wrong\+Wrong\+Alternative/
: /Hello World\+World\+World/ : /Hello World\+World\+World/
it(`should return the correct SSR HTML for ${url}`, async () => { it(`should return the correct SSR HTML for ${url}`, async () => {
const res = await next.fetch(url) const res = await next.fetch(url)
const html = await res.text() const html = await res.text()
expect(html).toMatch(expectedHtml) expect(normalize(html)).toMatch(expectedHtml)
}) })
it(`should render the correct page for ${url}`, async () => { it(`should render the correct page for ${url}`, async () => {
......
...@@ -8,7 +8,7 @@ module.exports = { ...@@ -8,7 +8,7 @@ module.exports = {
serverComponentsExternalPackages: [ serverComponentsExternalPackages: [
'app-esm-package1', 'app-esm-package1',
'app-esm-package2', 'app-esm-package2',
'app-invalid-esm-package', 'app-cjs-esm-package',
], ],
}, },
webpack(config, { isServer }) { webpack(config, { isServer }) {
......
{ {
"name": "app-invalid-esm-package", "name": "app-cjs-esm-package",
"exports": { "exports": {
"./entry": { "./entry": {
"browser": "./browser.js", "browser": "./browser.js",
"import": "./wrong.js", "import": "./correct.js",
"require": "./correct.js" "require": "./alternative.js"
} }
} }
} }
module.exports = 'Alternative'
// ensure it's external
if (Math.random() < 0) require('fail')
module.exports = 'World' export default 'World'
// ensure it's external
if (Math.random() < 0) require('fail')
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
"exports": { "exports": {
"./entry": { "./entry": {
"browser": "./browser.js", "browser": "./browser.js",
"import": "./wrong.js", "import": "./correct.js",
"require": "./correct.js" "require": "./alternative.js"
} }
} }
} }
...@@ -2555,6 +2555,7 @@ ...@@ -2555,6 +2555,7 @@
"app dir - external dependency react in external esm packages should use the same react in server app", "app dir - external dependency react in external esm packages should use the same react in server app",
"app dir - external dependency server actions should compile server actions from node_modules in client components", "app dir - external dependency server actions should compile server actions from node_modules in client components",
"app dir - external dependency server actions should not prefer to resolve esm over cjs for bundling optout packages", "app dir - external dependency server actions should not prefer to resolve esm over cjs for bundling optout packages",
"app dir - external dependency should be able to opt-out 3rd party packages being bundled in server components",
"app dir - external dependency should correctly collect global css imports and mark them as side effects", "app dir - external dependency should correctly collect global css imports and mark them as side effects",
"app dir - external dependency should emit cjs helpers for external cjs modules when compiled", "app dir - external dependency should emit cjs helpers for external cjs modules when compiled",
"app dir - external dependency should export client module references in esm", "app dir - external dependency should export client module references in esm",
...@@ -2571,8 +2572,7 @@ ...@@ -2571,8 +2572,7 @@
"app dir - external dependency should use the same export type for packages in both ssr and client" "app dir - external dependency should use the same export type for packages in both ssr and client"
], ],
"failed": [ "failed": [
"app dir - external dependency should be able to opt-out 3rd party packages being bundled in server components", "app dir - external dependency should not apply swc optimizer transform for external packages in browser layer in web worker"
"app dir - external dependency should not apply swc optimizer transform for external packages in browser layer"
], ],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
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