Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapter-netlify: Use CJS entrypoint to load ESM files #485

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silver-scissors-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-netlify': patch
---

adapter-netlify: Use CJS entrypoint
6 changes: 6 additions & 0 deletions packages/adapter-netlify/files/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
async handler(event) {
const { handler } = await import('./handler.mjs');
return await handler(event);
}
};
2 changes: 1 addition & 1 deletion packages/adapter-netlify/files/render.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { URLSearchParams } from 'url';
import { render } from './app.js'; // eslint-disable-line import/no-unresolved
import { render } from './app.mjs'; // eslint-disable-line import/no-unresolved

export async function handler(event) {
const {
Expand Down
10 changes: 8 additions & 2 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { existsSync, readFileSync, copyFileSync, writeFileSync } from 'fs';
import { existsSync, readFileSync, copyFileSync, writeFileSync, renameSync } from 'fs';
import { dirname, resolve } from 'path';
import toml from 'toml';
import { fileURLToPath } from 'url';
Expand Down Expand Up @@ -37,8 +37,14 @@ export default async function adapter(builder) {
builder.copy_client_files(publish);
builder.copy_server_files(`${functions}/render`);

// rename app to .mjs
renameSync(`${functions}/render/app.js`, `${functions}/render/app.mjs`);

// copy the renderer
copyFileSync(resolve(__dirname, 'files/render.js'), `${functions}/render/index.js`);
copyFileSync(resolve(__dirname, 'files/render.js'), `${functions}/render/handler.mjs`);

// copy the entry point
copyFileSync(resolve(__dirname, 'files/index.cjs'), `${functions}/render/index.js`);

// create _redirects
writeFileSync(`${publish}/_redirects`, '/* /.netlify/functions/render 200');
Expand Down