Skip to content

Commit

Permalink
fix: apply type: module only to runtime modules
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jul 19, 2024
1 parent a54b0d0 commit e58d640
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/build/content/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export const copyNextDependencies = async (ctx: PluginContext): Promise<void> =>
const promises: Promise<void>[] = entries.map(async (entry) => {
// copy all except the package.json and distDir (.next) folder as this is handled in a separate function
// this will include the node_modules folder as well
if (entry === 'package.json' || entry === ctx.nextDistDir) {
if (entry === ctx.nextDistDir) {
return
}
const src = join(ctx.standaloneDir, entry)
Expand Down
21 changes: 11 additions & 10 deletions src/build/functions/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ const copyHandlerDependencies = async (ctx: PluginContext) => {
)
}

// We need to create a package.json file with type: module to make sure that the runtime modules
// are handled correctly as ESM modules
promises.push(
writeFile(
join(ctx.serverHandlerRuntimeModulesDir, 'package.json'),
JSON.stringify({ type: 'module' }),
),
)

const fileList = await glob('dist/**/*', { cwd: ctx.pluginDir })

for (const filePath of fileList) {
promises.push(
cp(join(ctx.pluginDir, filePath), join(ctx.serverHandlerDir, '.netlify', filePath), {
cp(join(ctx.pluginDir, filePath), join(ctx.serverHandlerRuntimeModulesDir, filePath), {
recursive: true,
force: true,
}),
Expand Down Expand Up @@ -85,13 +94,6 @@ const writeHandlerManifest = async (ctx: PluginContext) => {
)
}

const writePackageMetadata = async (ctx: PluginContext) => {
await writeFile(
join(ctx.serverHandlerRootDir, 'package.json'),
JSON.stringify({ type: 'module' }),
)
}

const applyTemplateVariables = (template: string, variables: Record<string, string>) => {
return Object.entries(variables).reduce((acc, [key, value]) => {
return acc.replaceAll(key, value)
Expand Down Expand Up @@ -136,13 +138,12 @@ export const clearStaleServerHandlers = async (ctx: PluginContext) => {
*/
export const createServerHandler = async (ctx: PluginContext) => {
await tracer.withActiveSpan('createServerHandler', async () => {
await mkdir(join(ctx.serverHandlerDir, '.netlify'), { recursive: true })
await mkdir(join(ctx.serverHandlerRuntimeModulesDir), { recursive: true })

await copyNextServerCode(ctx)
await copyNextDependencies(ctx)
await copyHandlerDependencies(ctx)
await writeHandlerManifest(ctx)
await writePackageMetadata(ctx)
await writeHandlerFile(ctx)

await verifyHandlerDirStructure(ctx)
Expand Down
4 changes: 4 additions & 0 deletions src/build/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ export class PluginContext {
return join(this.serverHandlerRootDir, this.distDirParent)
}

get serverHandlerRuntimeModulesDir(): string {
return join(this.serverHandlerDir, '.netlify')
}

get nextServerHandler(): string {
if (this.relativeAppDir.length !== 0) {
return join(this.lambdaWorkingDirectory, '.netlify/dist/run/handlers/server.js')
Expand Down

0 comments on commit e58d640

Please sign in to comment.