Skip to content

Commit

Permalink
Make it possible to parse ES module syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
fossamagna committed Nov 26, 2020
1 parent d920a76 commit 5ffab59
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -204,7 +204,7 @@ function createGlobalAssignmentASTNode(functionName) {
}

exports.generate = function(source, options = { comment: false, autoGlobalExports: false }){
const ast = esprima.parseScript(source, { attachComment: options.comment });
const ast = esprima.parseModule(source, { attachComment: options.comment });
const functions = generateStubs(ast, options);
const globalAssignments = options.autoGlobalExports ? generateGlobalAssignments(ast, options) : undefined;
return {
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/esm-expected.js
@@ -0,0 +1,6 @@
/**
* Leading Block Comment for foo.
*/
// leading line comment for foo
function foo() {
}
1 change: 1 addition & 0 deletions test/fixtures/esm-exports.js
@@ -0,0 +1 @@
export const foo = () => 'foo';
7 changes: 7 additions & 0 deletions test/fixtures/esm-source.js
@@ -0,0 +1,7 @@
import { foo } from './esm-exports';

/**
* Leading Block Comment for foo.
*/
// leading line comment for foo
global.foo = foo;
9 changes: 9 additions & 0 deletions test/index.test.js
Expand Up @@ -20,3 +20,12 @@ test('generate function generate entry functions from global assignments', funct
t.equal(output.globalAssignments, undefined, 'actual output will match expected');
t.end();
});

test('Can parse ES module syntax', function(t) {
const source = fs.readFileSync(__dirname + '/fixtures/esm-source.js', {encoding: 'utf8'});
const expected = fs.readFileSync(__dirname + '/fixtures/esm-expected.js', {encoding: 'utf8'});
const output = generate(source, { comment: true });
t.equal(output.entryPointFunctions, expected, 'actual output will match expected');
t.equal(output.globalAssignments, undefined, 'actual output will match expected');
t.end();
});

0 comments on commit 5ffab59

Please sign in to comment.