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-vercel: Use CJS entrypoint to load ESM files #483

Merged
merged 7 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/selfish-owls-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

Fix mixed usage of CJS and ESM
9 changes: 7 additions & 2 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFileSync, mkdirSync } from 'fs';
import { writeFileSync, mkdirSync, renameSync } from 'fs';
import { dirname, resolve, join } from 'path';
import { fileURLToPath } from 'url';
import { copy } from '@sveltejs/app-utils/files';
Expand All @@ -18,6 +18,7 @@ export default async function adapter(builder) {

builder.log.minor('Building lambda...');
builder.copy_server_files(server_directory);
renameSync(join(server_directory, 'app.js'), join(server_directory, 'app.mjs'));

copy(join(__dirname, 'files'), lambda_directory);

Expand All @@ -27,7 +28,11 @@ export default async function adapter(builder) {
});

builder.log.minor('Writing routes...');
mkdirSync(config_directory);
try {
mkdirSync(config_directory);
} catch {
// directory already exists
}
writeFileSync(
join(config_directory, 'routes.json'),
JSON.stringify([
Expand Down
29 changes: 19 additions & 10 deletions packages/adapter-vercel/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
input: 'src/index.js',
output: {
file: 'files/index.js',
format: 'esm',
sourcemap: true,
exports: 'default'
export default [
{
input: 'src/entry.js',
output: {
file: 'files/entry.mjs',
format: 'es',
sourcemap: true,
exports: 'default'
},
plugins: [nodeResolve(), commonjs()],
external: [...require('module').builtinModules, './server/app.mjs']
},
plugins: [nodeResolve(), commonjs()],
external: require('module').builtinModules
};
{
input: 'src/index.cjs',
output: {
file: 'files/index.js'
},
external: './entry.mjs'
}
];
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { parse, URLSearchParams } from 'url';
import { URL, URLSearchParams } from 'url';
import { get_body } from '@sveltejs/app-utils/http';

const app = require('./server/app.js');

export default async (req, res) => {
const { pathname, query = '' } = parse(req.url || '');
const host = `${req.headers['x-forwarded-proto']}://${req.headers.host}`;
const { pathname, query = '' } = new URL(req.url || '', host);

const { render } = await import('./server/app.mjs');

const rendered = await app.render({
const rendered = await render({
method: req.method,
headers: req.headers,
path: pathname,
Expand Down
4 changes: 4 additions & 0 deletions packages/adapter-vercel/src/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = async (res, req) => {
const { default: app } = await import('./entry.mjs');
await app(res, req);
};