Skip to content

Commit 75360e6

Browse files
authored
feat: support for ESM (#465)
1 parent 8d2ca07 commit 75360e6

File tree

8 files changed

+17
-46
lines changed

8 files changed

+17
-46
lines changed

packages/micro/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"micro": "./dist/src/bin/micro.js"
1515
},
1616
"engines": {
17-
"node": ">= 14.5.0"
17+
"node": ">= 16.0.0"
1818
},
1919
"scripts": {
2020
"build": "tsc",
@@ -34,8 +34,7 @@
3434
"dependencies": {
3535
"arg": "4.1.0",
3636
"content-type": "1.0.4",
37-
"raw-body": "2.4.1",
38-
"tsimportlib": "0.0.3"
37+
"raw-body": "2.4.1"
3938
},
4039
"devDependencies": {
4140
"@types/content-type": "1.1.5",

packages/micro/src/lib/handler.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ export const handle = async (file: string) => {
77
try {
88
mod = await import(file);
99

10-
if (mod && typeof mod === 'object') {
11-
mod = await (mod as { default: unknown }).default; // Await to support es6 module's default export
12-
}
10+
mod = await (mod as { default: unknown }).default; // use ES6 module's default export
1311
} catch (err: unknown) {
1412
if (isErrorObject(err) && err.stack) {
1513
logError(`Error when importing ${file}: ${err.stack}`, 'invalid-entry');

packages/micro/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"extends": "@vercel/style-guide/typescript",
33
"compilerOptions": {
4-
"target": "ES2020",
4+
"target": "ES2021",
55
"module": "CommonJS",
6-
"moduleResolution": "node",
6+
"moduleResolution": "Node16",
77
"esModuleInterop": true,
88
"resolveJsonModule": true,
99
"outDir": "dist",

packages/micro/types/src/lib/handler.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "test",
33
"version": "1.0.0",
4+
"engines": {
5+
"node": ">= 16.0.0"
6+
},
47
"scripts": {
58
"eslint-check": "eslint --max-warnings=0 .",
69
"prettier-check": "prettier --check .",

test/suite/handler.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,21 @@ void test('handle async function', async (t) => {
2020
t.type(result, 'function');
2121
});
2222

23-
void test(`handle Babel's non-async function`, async (t) => {
23+
void test(`handle ESM's non-async function`, async (t) => {
2424
const dir = t.testdir({
25-
'babel-function-export.js': `"use strict";
26-
27-
Object.defineProperty(exports, "__esModule", {
28-
value: true
29-
});
30-
exports.default = void 0;
31-
32-
var _default = (req, res) => {
33-
res.end("Test");
34-
};
35-
36-
exports.default = _default;
37-
`,
25+
'esm-function-export.mjs': `export default () => 'Hello ESM';`,
3826
});
3927

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

44-
void test(`handle Babel's async function`, async (t) => {
32+
void test(`handle ESM's async function`, async (t) => {
4533
const dir = t.testdir({
46-
'babel-async-export.js': `"use strict";
47-
48-
Object.defineProperty(exports, "__esModule", {
49-
value: true
50-
});
51-
exports.default = void 0;
52-
53-
var _default = async (req, res) => {
54-
res.end("Test");
55-
};
56-
57-
exports.default = _default;
58-
`,
34+
'esm-async-export.mjs': `export default async () => 'Hello ESM';`,
5935
});
6036

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

test/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"extends": "@vercel/style-guide/typescript",
33
"compilerOptions": {
4-
"target": "ES2020",
4+
"target": "ES2021",
55
"module": "CommonJS",
6-
"moduleResolution": "node",
6+
"moduleResolution": "Node16",
77
"esModuleInterop": true,
88
"noEmit": true
99
}

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7379,11 +7379,6 @@ tsconfig-paths@^3.12.0, tsconfig-paths@^3.14.1:
73797379
minimist "^1.2.6"
73807380
strip-bom "^3.0.0"
73817381

7382-
tsimportlib@0.0.3:
7383-
version "0.0.3"
7384-
resolved "https://registry.yarnpkg.com/tsimportlib/-/tsimportlib-0.0.3.tgz#008453c0f0eea6f736f2a4431171de240b21fc0d"
7385-
integrity sha512-U9sW2/3D0P4IVRnhH2RCqjCP0sG66qvb4ahB0aQln5xGMphDjntz5rdk0rFZ6Fg+lW3L+i+gRnIl4VvNBvxiQw==
7386-
73877382
tslib@^1.8.1:
73887383
version "1.14.1"
73897384
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"

0 commit comments

Comments
 (0)