Browse Source

preserve ast to avoid re-parsing

ghi-672
Rich Harris 8 years ago
parent
commit
0a7a391c04
  1. 22
      src/Module.js
  2. 2
      src/rollup.js

22
src/Module.js

@ -53,7 +53,8 @@ export default class Module {
}
this.comments = [];
this.statements = this.parse( ast );
this.ast = ast;
this.statements = this.parse();
this.declarations = blank();
this.analyse();
@ -298,13 +299,13 @@ export default class Module {
return this.declarations['*'];
}
parse ( ast ) {
parse () {
// The ast can be supplied programmatically (but usually won't be)
if ( !ast ) {
if ( !this.ast ) {
// Try to extract a list of top-level statements/declarations. If
// the parse fails, attach file info and abort
try {
ast = parse( this.code, {
this.ast = parse( this.code, {
ecmaVersion: 6,
sourceType: 'module',
onComment: ( block, text, start, end ) => this.comments.push({ block, text, start, end }),
@ -318,7 +319,7 @@ export default class Module {
}
}
walk( ast, {
walk( this.ast, {
enter: node => {
// eliminate dead branches early
if ( node.type === 'IfStatement' ) {
@ -354,7 +355,7 @@ export default class Module {
let lastChar = 0;
let commentIndex = 0;
ast.body.forEach( node => {
this.ast.body.forEach( node => {
if ( node.type === 'EmptyStatement' ) return;
if (
@ -637,6 +638,15 @@ export default class Module {
return marked;
}
toJSON () {
return {
id: this.id,
code: this.code,
originalCode: this.originalCode,
ast: this.ast
};
}
trace ( name ) {
if ( name in this.declarations ) return this.declarations[ name ];
if ( name in this.imports ) {

2
src/rollup.js

@ -53,7 +53,7 @@ export function rollup ( options ) {
return {
imports: bundle.externalModules.map( module => module.id ),
exports: keys( bundle.entryModule.exports ),
modules: bundle.orderedModules.map( ( { id, code, originalCode } ) => ( { id, code, originalCode } ) ),
modules: bundle.orderedModules.map( module => module.toJSON() ),
generate: options => bundle.render( options ),
write: options => {

Loading…
Cancel
Save