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

Add support for ESM #465

Merged
merged 1 commit into from
Aug 22, 2022
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: 2 additions & 3 deletions packages/micro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"micro": "./dist/src/bin/micro.js"
},
"engines": {
"node": ">= 14.5.0"
"node": ">= 16.0.0"
},
"scripts": {
"build": "tsc",
Expand All @@ -34,8 +34,7 @@
"dependencies": {
"arg": "4.1.0",
"content-type": "1.0.4",
"raw-body": "2.4.1",
"tsimportlib": "0.0.3"
"raw-body": "2.4.1"
},
"devDependencies": {
"@types/content-type": "1.1.5",
Expand Down
4 changes: 1 addition & 3 deletions packages/micro/src/lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export const handle = async (file: string) => {
try {
mod = await import(file);

if (mod && typeof mod === 'object') {
mod = await (mod as { default: unknown }).default; // Await to support es6 module's default export
}
mod = await (mod as { default: unknown }).default; // use ES6 module's default export
} catch (err: unknown) {
if (isErrorObject(err) && err.stack) {
logError(`Error when importing ${file}: ${err.stack}`, 'invalid-entry');
Expand Down
4 changes: 2 additions & 2 deletions packages/micro/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"extends": "@vercel/style-guide/typescript",
"compilerOptions": {
"target": "ES2020",
"target": "ES2021",
"module": "CommonJS",
"moduleResolution": "node",
"moduleResolution": "Node16",
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/micro/types/src/lib/handler.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "test",
"version": "1.0.0",
"engines": {
"node": ">= 16.0.0"
},
"scripts": {
"eslint-check": "eslint --max-warnings=0 .",
"prettier-check": "prettier --check .",
Expand Down
36 changes: 6 additions & 30 deletions test/suite/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,21 @@ void test('handle async function', async (t) => {
t.type(result, 'function');
});

void test(`handle Babel's non-async function`, async (t) => {
void test(`handle ESM's non-async function`, async (t) => {
const dir = t.testdir({
'babel-function-export.js': `"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _default = (req, res) => {
res.end("Test");
};

exports.default = _default;
`,
'esm-function-export.mjs': `export default () => 'Hello ESM';`,
});

const result = await handle(`${dir}/babel-function-export.js`);
const result = await handle(`${dir}/esm-function-export.mjs`);
t.type(result, 'function');
});

void test(`handle Babel's async function`, async (t) => {
void test(`handle ESM's async function`, async (t) => {
const dir = t.testdir({
'babel-async-export.js': `"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _default = async (req, res) => {
res.end("Test");
};

exports.default = _default;
`,
'esm-async-export.mjs': `export default async () => 'Hello ESM';`,
});

const result = await handle(`${dir}/babel-async-export.js`);
const result = await handle(`${dir}/esm-async-export.mjs`);
t.type(result, 'function');
});

Expand Down
4 changes: 2 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"extends": "@vercel/style-guide/typescript",
"compilerOptions": {
"target": "ES2020",
"target": "ES2021",
"module": "CommonJS",
"moduleResolution": "node",
"moduleResolution": "Node16",
"esModuleInterop": true,
"noEmit": true
}
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7379,11 +7379,6 @@ tsconfig-paths@^3.12.0, tsconfig-paths@^3.14.1:
minimist "^1.2.6"
strip-bom "^3.0.0"

tsimportlib@0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/tsimportlib/-/tsimportlib-0.0.3.tgz#008453c0f0eea6f736f2a4431171de240b21fc0d"
integrity sha512-U9sW2/3D0P4IVRnhH2RCqjCP0sG66qvb4ahB0aQln5xGMphDjntz5rdk0rFZ6Fg+lW3L+i+gRnIl4VvNBvxiQw==

tslib@^1.8.1:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
Expand Down