Unverified Commit a5ace6aa authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Canonicalize server action self-emits

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 9151d0aa
...@@ -3,8 +3,9 @@ import { retry } from 'next-test-utils' ...@@ -3,8 +3,9 @@ import { retry } from 'next-test-utils'
for (const type of ['with-mdx-rs', 'without-mdx-rs']) { for (const type of ['with-mdx-rs', 'without-mdx-rs']) {
describe(`mdx ${type}`, () => { describe(`mdx ${type}`, () => {
const { next } = nextTestSetup({ const { next, isTurbopack } = nextTestSetup({
files: __dirname, files: __dirname,
skipStart: true,
dependencies: { dependencies: {
'@next/mdx': 'workspace:*', '@next/mdx': 'workspace:*',
'@mdx-js/loader': '^2.2.1', '@mdx-js/loader': '^2.2.1',
...@@ -20,7 +21,16 @@ for (const type of ['with-mdx-rs', 'without-mdx-rs']) { ...@@ -20,7 +21,16 @@ for (const type of ['with-mdx-rs', 'without-mdx-rs']) {
}, },
}) })
beforeAll(async () => {
if (!isTurbopack) {
// Webpack's MDX loader does not apply the server-action transform to this route.
await next.deleteFile('app/actions/page.mdx')
}
await next.start()
})
describe('app directory', () => { describe('app directory', () => {
// @force-gate turbopack
it('should invoke an inline server action from MDX', async () => { it('should invoke an inline server action from MDX', async () => {
const browser = await next.browser('/actions') const browser = await next.browser('/actions')
await browser.elementById('submit-mdx-action').click() await browser.elementById('submit-mdx-action').click()
......
...@@ -15,7 +15,7 @@ use turbopack_core::{ ...@@ -15,7 +15,7 @@ use turbopack_core::{
reference::ModuleReference, reference::ModuleReference,
reference_type::EcmaScriptModulesReferenceSubType, reference_type::EcmaScriptModulesReferenceSubType,
resolve::{ resolve::{
BindingUsage, ExportUsage, ModuleResolveResult, ResolveErrorMode, BindingUsage, ExportUsage, ModulePart, ModuleResolveResult, ResolveErrorMode,
options::ResolveOptions, options::ResolveOptions,
origin::{ResolveOrigin, ResolveOriginExt}, origin::{ResolveOrigin, ResolveOriginExt},
parse::Request, parse::Request,
...@@ -24,11 +24,13 @@ use turbopack_core::{ ...@@ -24,11 +24,13 @@ use turbopack_core::{
use turbopack_resolve::ecmascript::esm_resolve; use turbopack_resolve::ecmascript::esm_resolve;
use crate::{ use crate::{
EcmascriptModuleAsset,
analyzer::imports::ImportAnnotations, analyzer::imports::ImportAnnotations,
ast_path_trie::{AstPathId, AstPathTrie, AstPathTrieBuilder}, ast_path_trie::{AstPathId, AstPathTrie, AstPathTrieBuilder},
code_gen::{CodeGen, CodeGeneration, IntoCodeGenReference}, code_gen::{CodeGen, CodeGeneration, IntoCodeGenReference},
collect_module::{COLLECT_LIST_EXPORT, EcmascriptCollectModule}, collect_module::{COLLECT_LIST_EXPORT, EcmascriptCollectModule},
create_visitor, create_visitor,
module_canonicalization::{EcmascriptModuleCanonicalization, canonicalize_ecmascript_module},
references::{ references::{
esm::{base::ReferencedAsset, mangle::generated_export_key}, esm::{base::ReferencedAsset, mangle::generated_export_key},
pattern_mapping::{PatternMapping, ResolveType}, pattern_mapping::{PatternMapping, ResolveType},
...@@ -110,8 +112,28 @@ impl ModuleReference for EmitReference { ...@@ -110,8 +112,28 @@ impl ModuleReference for EmitReference {
if self.annotations.transition().is_some() { if self.annotations.transition().is_some() {
bail!("Transitions are not supported with a null emit target"); bail!("Transitions are not supported with a null emit target");
} }
// Keep the module identity instead of resolving a processed filename on disk. // Match normal imports' canonical facade/parts without resolving a filename. A raw
return Ok(*ModuleResolveResult::module(self.parent_module)); // asset and its facade can share an ident and must not coexist in the module graph.
let module = if let Some(module) =
ResolvedVc::try_downcast_type::<EcmascriptModuleAsset>(self.parent_module)
{
let options = module.options().await?;
let canonicalization = if options.module_fragments_enabled {
EcmascriptModuleCanonicalization::ModuleFragments(ModulePart::facade())
} else if options.follow_reexports {
EcmascriptModuleCanonicalization::FollowReexports(None)
} else {
EcmascriptModuleCanonicalization::None
};
ResolvedVc::upcast(
canonicalize_ecmascript_module(*module, canonicalization)
.to_resolved()
.await?,
)
} else {
self.parent_module
};
return Ok(*ModuleResolveResult::module(module));
}; };
esm_resolve( esm_resolve(
......
{
"scopeHoisting": false,
"inferModuleSideEffects": false,
"moduleFragmentsEnabled": true
}
...@@ -4,3 +4,5 @@ __turbopack_emit__(null, { ...@@ -4,3 +4,5 @@ __turbopack_emit__(null, {
}) })
export const value = 'self-emitted value' export const value = 'self-emitted value'
export const identity = {}
export { reexported } from './reexported.js'
import { value as firstValue } from './emitter.js?first' import * as firstModule from './emitter.js?first'
import { value as secondValue } from './emitter.js?second' import * as secondModule from './emitter.js?second'
const getList = __turbopack_collect__({ namespace: 'self-test' }) const getList = __turbopack_collect__({ namespace: 'self-test' })
it('should emit the originating module and preserve its query', async () => { it('should emit the originating module and preserve its query', async () => {
expect(firstValue).toBe('self-emitted value') for (const module of [firstModule, secondModule]) {
expect(secondValue).toBe('self-emitted value') expect(module).toMatchObject({
value: 'self-emitted value',
reexported: 're-exported value',
})
}
expect(firstModule.identity).not.toBe(secondModule.identity)
const list = getList() const list = getList()
expect(list).toHaveLength(2) expect(list).toHaveLength(2)
...@@ -20,6 +25,13 @@ it('should emit the originating module and preserve its query', async () => { ...@@ -20,6 +25,13 @@ it('should emit the originating module and preserve its query', async () => {
expect(entry.data).toBe('self-emitted data') expect(entry.data).toBe('self-emitted data')
const module = await entry.import() const module = await entry.import()
expect(module.value).toBe('self-emitted value') expect(module.value).toBe('self-emitted value')
expect(module.reexported).toBe('re-exported value')
// Module fragments can expose different namespace wrappers, but must share the instance.
expect(module.identity).toBe(
entry.id.includes('?first')
? firstModule.identity
: secondModule.identity
)
return module return module
}) })
) )
......
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