Skip to content

Commit

Permalink
adapter-vercel: Use CJS entrypoint to load ESM files (#483)
Browse files Browse the repository at this point in the history
* adapter-vercel fixes

Replace require() with await import()
Replace url.parse with new URL()

* adapter-vercel changeset

* adapter-vercel: actually use existing headers

* adapter-vercel: Use single CJS entrypoint

* adapter-vercel: rename app.js to app.mjs

* adapter-vercel: Don't fail on mkdirSync

* adapter-vercel: fix deconstruct
  • Loading branch information
GrygrFlzr authored Mar 12, 2021
1 parent 512b8c9 commit d742029
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
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);
};

0 comments on commit d742029

Please sign in to comment.