Skip to content
Closed
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
8 changes: 8 additions & 0 deletions deps/cjs-module-lexer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ Forked from https://github.com/guybedford/es-module-lexer.

_Comprehensively handles the JS language grammar while remaining small and fast. - ~90ms per MB of JS cold and ~15ms per MB of JS warm, [see benchmarks](#benchmarks) for more info._

### Project Status

This project is used in Node.js core for detecting the named exports available when importing a CJS module into ESM, and is maintained for this purpose.

PRs will be accepted and upstreamed for parser bugs, performance improvements or new syntax support only.

_Detection patterns for this project are **frozen**_. This is because adding any new export detection patterns would result in fragmented backwards-compatibility. Specifically, it would be very difficult to figure out why an ES module named export for CommonJS might work in newer Node.js versions but not older versions. This problem would only be discovered downstream of module authors, with the fix for module authors being to then have to understand which patterns in this project provide full backwards-compatibily. Rather, by fully freezing the detected patterns, if it works in any Node.js version it will work in any other. Build tools can also reliably treat the supported syntax for this project as a part of their output target for ensuring syntax support.

### Usage

```
Expand Down
5 changes: 4 additions & 1 deletion deps/cjs-module-lexer/dist/lexer.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions deps/cjs-module-lexer/dist/lexer.mjs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions deps/cjs-module-lexer/lexer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Exports {
exports: string[];
reexports: string[];
}

export declare function parse(source: string, name?: string): Exports;
export declare function init(): Promise<void>;
10 changes: 7 additions & 3 deletions deps/cjs-module-lexer/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,10 @@ function codePointAtLast (bPos) {
return ch;
}

function esmSyntaxErr (msg) {
return Object.assign(new Error(msg), { code: 'ERR_LEXER_ESM_SYNTAX' });
}

function throwIfImportStatement () {
const startPos = pos;
pos += 6;
Expand All @@ -1160,7 +1164,7 @@ function throwIfImportStatement () {
return;
// import.meta
case 46/*.*/:
throw new Error('Unexpected import.meta in CJS module.');
throw esmSyntaxErr('Unexpected import.meta in CJS module.');

default:
// no space after "import" -> not an import keyword
Expand All @@ -1176,7 +1180,7 @@ function throwIfImportStatement () {
return;
}
// import statements are a syntax error in CommonJS
throw new Error('Unexpected import statement in CJS module.');
throw esmSyntaxErr('Unexpected import statement in CJS module.');
}
}

Expand All @@ -1186,7 +1190,7 @@ function throwIfExportStatement () {
const ch = commentWhitespace();
if (pos === curPos && !isPunctuator(ch))
return;
throw new Error('Unexpected export statement in CJS module.');
throw esmSyntaxErr('Unexpected export statement in CJS module.');
}

function commentWhitespace () {
Expand Down
25 changes: 13 additions & 12 deletions deps/cjs-module-lexer/package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
{
"name": "cjs-module-lexer",
"version": "1.2.2",
"version": "1.3.1",
"description": "Lexes CommonJS modules, returning their named exports metadata",
"main": "lexer.js",
"exports": {
"import": "./dist/lexer.mjs",
"import": {
"types": "./lexer.d.mts",
"default": "./dist/lexer.mjs"
},
"default": "./lexer.js"
},
"types": "lexer.d.ts",
"scripts": {
"test-js": "mocha -b -u tdd test/*.js",
"test-wasm": "WASM=1 mocha -b -u tdd test/*.js",
"test-wasm": "cross-env WASM=1 mocha -b -u tdd test/*.js",
"test": "npm run test-wasm && npm run test-js",
"bench": "node --expose-gc bench/index.mjs",
"build": "node build.js && babel dist/lexer.mjs | terser -o dist/lexer.js",
"build": "node build.js",
"build-wasm": "make lib/lexer.wasm && node build.js",
"prepublishOnly": "make && npm run build",
"footprint": "npm run build && cat dist/lexer.js | gzip -9f | wc -c"
},
"author": "Guy Bedford",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
"cross-env": "^7.0.3",
"esbuild": "^0.19.12",
"kleur": "^2.0.2",
"mocha": "^5.2.0",
"terser": "^4.1.4"
"mocha": "^9.1.3"
},
"files": [
"dist",
"lexer.d.ts"
],
"repository": {
"type": "git",
"url": "git+https://github.com/guybedford/cjs-module-lexer.git"
"url": "git+https://github.com/nodejs/cjs-module-lexer.git"
},
"bugs": {
"url": "https://github.com/guybedford/cjs-module-lexer/issues"
"url": "https://github.com/nodejs/cjs-module-lexer/issues"
},
"homepage": "https://github.com/guybedford/cjs-module-lexer#readme"
"homepage": "https://github.com/nodejs/cjs-module-lexer#readme"
}