Skip to content
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ npm install cjs-module-lexer
For use in CommonJS:

```js
const parse = require('cjs-module-lexer');
const { parse, init } = require('cjs-module-lexer');

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we should require it to be called or not. E.g. what happens if I use import('cjs-module-lexer')? Possibly confusing. Dunno 🙂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're changing the API regardless it might be worth it. happy to make the change if you want 👍

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, the way the exports are defined in https://github.com/guybedford/cjs-module-lexer/blob/master/package.json#L6, import() should always get the ES module form by definition of the resolution rules and even in bundlers (which RollupJS and Webpack support now!)


// `init` return a promise for parity with the ESM API, but you do not have to call it

const { exports, reexports } = parse(`
// named exports detection
Expand Down
15 changes: 10 additions & 5 deletions lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ExportStar = 2;

const strictReserved = new Set(['implements', 'interface', 'let', 'package', 'private', 'protected', 'public', 'static', 'yield', 'enum']);

module.exports = function parseCJS (source, name = '@') {
function parseCJS (source, name = '@') {
resetState();
try {
parseSource(source);
Expand Down Expand Up @@ -899,7 +899,7 @@ function tryParseLiteralExports () {

// --- Extracted from AcornJS ---
//(https://github.com/acornjs/acorn/blob/master/acorn/src/identifier.js#L23
//
//
// MIT License

// Copyright (C) 2012-2018 by various contributors (see AUTHORS)
Expand Down Expand Up @@ -1034,7 +1034,7 @@ function throwIfImportStatement () {
case 46/*.*/:
throw new Error('Unexpected import.meta in CJS module.');
return;

default:
// no space after "import" -> not an import keyword
if (pos === startPos + 6)
Expand Down Expand Up @@ -1203,7 +1203,7 @@ function readPrecedingKeyword (pos, match) {
}

function readPrecedingKeyword1 (pos, ch) {
return source.charCodeAt(pos) === ch && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1)));
return source.charCodeAt(pos) === ch && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1)));
}

// Detects one of case, debugger, delete, do, else, in, instanceof, new,
Expand Down Expand Up @@ -1274,7 +1274,7 @@ function isExpressionKeyword (pos) {
// throw
return readPrecedingKeyword(pos - 2, 'thr');
default:
return false;
return false;
}
}
return false;
Expand Down Expand Up @@ -1320,3 +1320,8 @@ function isExpressionTerminator (curPos) {
}
return false;
}

const initPromise = Promise.resolve();

module.exports.init = () => initPromise;
module.exports.parse = parseCJS;
Comment on lines +1326 to +1327

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this could be done without breaking changes:

module.exports = (source, name) => parseCJS(source, name);
module.exports.init = () => initPromise;
module.exports.parse = parseCJS;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. @guybedford thoughts?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it simple as a major change actually. Saves on type differences / leaving deprecation for subsequent work.