Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
use parse method from transform context
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Feb 5, 2018
1 parent d195bab commit 4131fb3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -21,13 +21,13 @@
"README.md"
],
"dependencies": {
"acorn": "^5.2.1",
"estree-walker": "^0.5.0",
"magic-string": "^0.22.4",
"resolve": "^1.4.0",
"rollup-pluginutils": "^2.0.1"
},
"devDependencies": {
"acorn": "^5.2.1",
"eslint": "^4.8.0",
"locate-character": "^2.0.1",
"mocha": "^4.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -169,7 +169,7 @@ export default function commonjs ( options = {} ) {
if ( extensions.indexOf( extname( id ) ) === -1 ) return null;

return entryModuleIdsPromise.then( (entryModuleIds) => {
const {isEsModule, hasDefaultExport, ast} = checkEsModule( code, id );
const {isEsModule, hasDefaultExport, ast} = checkEsModule( this.parse, code, id );
if ( isEsModule ) {
if ( !hasDefaultExport )
esModulesWithoutDefaultExport.push( id );
Expand All @@ -182,7 +182,7 @@ export default function commonjs ( options = {} ) {
return;
}

const transformed = transformCommonjs( code, id, entryModuleIds.indexOf(id) !== -1, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire, ast );
const transformed = transformCommonjs( this.parse, code, id, entryModuleIds.indexOf(id) !== -1, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire, ast );
if ( !transformed ) return;

commonjsModules.set( id, true );
Expand Down
13 changes: 6 additions & 7 deletions src/transform.js
@@ -1,4 +1,3 @@
import acorn from 'acorn';
import { walk } from 'estree-walker';
import MagicString from 'magic-string';
import { attachScopes, makeLegalIdentifier } from 'rollup-pluginutils';
Expand Down Expand Up @@ -26,9 +25,9 @@ function deconflict ( scope, globals, identifier ) {
return deconflicted;
}

function tryParse ( code, id ) {
function tryParse ( parse, code, id ) {
try {
return acorn.parse( code, {
return parse( code, {
ecmaVersion: 8,
sourceType: 'module',
allowReturnOutsideFunction: true
Expand All @@ -44,8 +43,8 @@ export function checkFirstpass (code, ignoreGlobal) {
return firstpass.test(code);
}

export function checkEsModule (code, id) {
const ast = tryParse(code, id);
export function checkEsModule ( parse, code, id ) {
const ast = tryParse( parse, code, id );

// if there are top-level import/export declarations, this is ES not CommonJS
let hasDefaultExport = false;
Expand All @@ -60,8 +59,8 @@ export function checkEsModule (code, id) {
return { isEsModule, hasDefaultExport, ast };
}

export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire, astCache ) {
const ast = astCache || tryParse( code, id );
export function transformCommonjs ( parse, code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire, astCache ) {
const ast = astCache || tryParse( parse, code, id );

const magicString = new MagicString( code );

Expand Down
7 changes: 6 additions & 1 deletion test/test.js
@@ -1,3 +1,4 @@
const acorn = require( 'acorn' );
const path = require( 'path' );
const fs = require( 'fs' );
const assert = require( 'assert' );
Expand Down Expand Up @@ -55,6 +56,10 @@ async function executeBundle ( bundle, { context, exports } = {} ) {
return execute( code, context );
}

const transformContext = {
parse: acorn.parse
};

describe( 'rollup-plugin-commonjs', () => {
describe( 'form', () => {
fs.readdirSync( 'form' ).forEach( dir => {
Expand All @@ -73,7 +78,7 @@ describe( 'rollup-plugin-commonjs', () => {
const input = fs.readFileSync( `form/${dir}/input.js`, 'utf-8' );
const expected = fs.readFileSync( `form/${dir}/output.js`, 'utf-8' ).trim();

return transform( input, 'input.js' ).then( transformed => {
return transform.call( transformContext, input, 'input.js' ).then( transformed => {
const actual = ( transformed ? transformed.code : input ).trim().replace( /\0/g, '' );
assert.equal( actual, expected );
});
Expand Down

0 comments on commit 4131fb3

Please sign in to comment.