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

fix esm externals edge case with "module" condition (#28612)

prefer commonjs externals even if "module" condition is used in package.json



## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
parent 71771df6
...@@ -173,20 +173,6 @@ export function attachReactRefresh( ...@@ -173,20 +173,6 @@ export function attachReactRefresh(
} }
} }
const WEBPACK_RESOLVE_OPTIONS = {
// This always uses commonjs resolving, assuming API is identical
// between ESM and CJS in a package
// Otherwise combined ESM+CJS packages will never be external
// as resolving mismatch would lead to opt-out from being external.
dependencyType: 'commonjs',
symlinks: true,
}
const WEBPACK_ESM_RESOLVE_OPTIONS = {
dependencyType: 'esm',
symlinks: true,
}
const NODE_RESOLVE_OPTIONS = { const NODE_RESOLVE_OPTIONS = {
dependencyType: 'commonjs', dependencyType: 'commonjs',
modules: ['node_modules'], modules: ['node_modules'],
...@@ -194,7 +180,7 @@ const NODE_RESOLVE_OPTIONS = { ...@@ -194,7 +180,7 @@ const NODE_RESOLVE_OPTIONS = {
fallback: false, fallback: false,
exportsFields: ['exports'], exportsFields: ['exports'],
importsFields: ['imports'], importsFields: ['imports'],
conditionNames: ['node', 'require', 'module'], conditionNames: ['node', 'require'],
descriptionFiles: ['package.json'], descriptionFiles: ['package.json'],
extensions: ['.js', '.json', '.node'], extensions: ['.js', '.json', '.node'],
enforceExtensions: false, enforceExtensions: false,
...@@ -211,7 +197,7 @@ const NODE_RESOLVE_OPTIONS = { ...@@ -211,7 +197,7 @@ const NODE_RESOLVE_OPTIONS = {
const NODE_ESM_RESOLVE_OPTIONS = { const NODE_ESM_RESOLVE_OPTIONS = {
...NODE_RESOLVE_OPTIONS, ...NODE_RESOLVE_OPTIONS,
dependencyType: 'esm', dependencyType: 'esm',
conditionNames: ['node', 'import', 'module'], conditionNames: ['node', 'import'],
fullySpecified: true, fullySpecified: true,
} }
...@@ -754,7 +740,7 @@ export default async function getBaseWebpackConfig( ...@@ -754,7 +740,7 @@ export default async function getBaseWebpackConfig(
const preferEsm = esmExternals && isEsmRequested const preferEsm = esmExternals && isEsmRequested
const resolve = getResolve( const resolve = getResolve(
preferEsm ? WEBPACK_ESM_RESOLVE_OPTIONS : WEBPACK_RESOLVE_OPTIONS preferEsm ? NODE_ESM_RESOLVE_OPTIONS : NODE_RESOLVE_OPTIONS
) )
// Resolve the import with the webpack provided context, this // Resolve the import with the webpack provided context, this
...@@ -772,7 +758,7 @@ export default async function getBaseWebpackConfig( ...@@ -772,7 +758,7 @@ export default async function getBaseWebpackConfig(
// try the alternative resolving options. // try the alternative resolving options.
if (!res && (isEsmRequested || looseEsmExternals)) { if (!res && (isEsmRequested || looseEsmExternals)) {
const resolveAlternative = getResolve( const resolveAlternative = getResolve(
preferEsm ? WEBPACK_RESOLVE_OPTIONS : WEBPACK_ESM_RESOLVE_OPTIONS preferEsm ? NODE_RESOLVE_OPTIONS : NODE_ESM_RESOLVE_OPTIONS
) )
try { try {
;[res, isEsm] = await resolveAlternative(context, request) ;[res, isEsm] = await resolveAlternative(context, request)
......
...@@ -15,7 +15,7 @@ const appDir = join(__dirname, '../') ...@@ -15,7 +15,7 @@ const appDir = join(__dirname, '../')
let appPort let appPort
let app let app
describe('Valid resolve alias', () => { describe("Handle ESM externals with esmExternals: 'loose'", () => {
beforeAll(async () => { beforeAll(async () => {
await fs.remove(join(appDir, '.next')) await fs.remove(join(appDir, '.next'))
await nextBuild(appDir) await nextBuild(appDir)
......
...@@ -15,7 +15,7 @@ const appDir = join(__dirname, '../') ...@@ -15,7 +15,7 @@ const appDir = join(__dirname, '../')
let appPort let appPort
let app let app
describe('Valid resolve alias', () => { describe('Handle ESM externals with esmExternals: true', () => {
beforeAll(async () => { beforeAll(async () => {
await fs.remove(join(appDir, '.next')) await fs.remove(join(appDir, '.next'))
await nextBuild(appDir) await nextBuild(appDir)
......
{
"name": "module-package",
"exports": {
".": {
"module": "./wrong.mjs",
"import": "./wrong.mjs",
"require": "./correct.js"
}
}
}
if(typeof window === "undefined")
throw new Error("This shouldn't be used on server-side")
export default "World";
import React from 'preact/compat' import React from 'preact/compat'
import World from 'esm-package/entry' import World1 from 'esm-package/entry'
import World2 from 'module-package'
export async function getStaticProps() { export async function getStaticProps() {
return { return {
...@@ -8,5 +9,9 @@ export async function getStaticProps() { ...@@ -8,5 +9,9 @@ export async function getStaticProps() {
} }
export default function Index(props) { export default function Index(props) {
return <div>Hello {World}</div> return (
<div>
Hello {World1}+{World2}
</div>
)
} }
import React from 'preact/compat' import React from 'preact/compat'
import World from 'esm-package/entry' import World1 from 'esm-package/entry'
import World2 from 'module-package'
export function getServerSideProps() { export function getServerSideProps() {
return {} return {}
} }
export default function Index(props) { export default function Index(props) {
return <div>Hello {World}</div> return (
<div>
Hello {World1}+{World2}
</div>
)
} }
import React from 'preact/compat' import React from 'preact/compat'
import World from 'esm-package/entry' import World1 from 'esm-package/entry'
import World2 from 'module-package'
export default function Index(props) { export default function Index(props) {
return <div>Hello {World}</div> return (
<div>
Hello {World1}+{World2}
</div>
)
} }
...@@ -15,7 +15,7 @@ const appDir = join(__dirname, '../') ...@@ -15,7 +15,7 @@ const appDir = join(__dirname, '../')
let appPort let appPort
let app let app
describe('Valid resolve alias', () => { describe('Handle ESM externals', () => {
beforeAll(async () => { beforeAll(async () => {
await fs.remove(join(appDir, '.next')) await fs.remove(join(appDir, '.next'))
await nextBuild(appDir) await nextBuild(appDir)
...@@ -24,18 +24,20 @@ describe('Valid resolve alias', () => { ...@@ -24,18 +24,20 @@ describe('Valid resolve alias', () => {
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
const expected = /Hello <!-- -->World<!-- -->\+<!-- -->World/
it('should render the static page', async () => { it('should render the static page', async () => {
const html = await renderViaHTTP(appPort, '/static') const html = await renderViaHTTP(appPort, '/static')
expect(html).toMatch(/Hello <!-- -->World/) expect(html).toMatch(expected)
}) })
it('should render the ssr page', async () => { it('should render the ssr page', async () => {
const html = await renderViaHTTP(appPort, '/ssr') const html = await renderViaHTTP(appPort, '/ssr')
expect(html).toMatch(/Hello <!-- -->World/) expect(html).toMatch(expected)
}) })
it('should render the ssg page', async () => { it('should render the ssg page', async () => {
const html = await renderViaHTTP(appPort, '/ssg') const html = await renderViaHTTP(appPort, '/ssg')
expect(html).toMatch(/Hello <!-- -->World/) expect(html).toMatch(expected)
}) })
}) })
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