mirror of https://github.com/lukechilds/rollup.git
Oskar Segersvärd
9 years ago
2 changed files with 151 additions and 0 deletions
@ -0,0 +1,73 @@ |
|||||
|
import Scope from './Scope'; |
||||
|
import { blank, forOwn, keys } from './utils/object'; |
||||
|
|
||||
|
// A JSONModule represents a static JSON object that can be rolled-up.
|
||||
|
export default class JSONModule extends Scope { |
||||
|
constructor ( name, data ) { |
||||
|
// Implement Identifier interface.
|
||||
|
// Doing so allows a JSONModule to be referenced.
|
||||
|
this.originalName = this.name = name; |
||||
|
|
||||
|
this.isIncluded = false; |
||||
|
this.namesToInclude = {}; |
||||
|
|
||||
|
this.data = data; |
||||
|
|
||||
|
forOwn( this.data, ( value, key ) => { |
||||
|
if ( value && typeof value === 'object' ) { |
||||
|
this.bind( key, new JSONModule( key, value ) ); |
||||
|
} else { |
||||
|
this.define( key ); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
// Data has no dependencies
|
||||
|
consolidateDependencies () { |
||||
|
return { |
||||
|
strongDependencies: blank(), |
||||
|
weakDependencies: blank() |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
// Mark a name within the module, or the value of the module itself.
|
||||
|
mark ( name ) { |
||||
|
if ( !name ) { |
||||
|
// `Statement.mark`
|
||||
|
// The names that may be accessed within the module is unknown.
|
||||
|
// We must include all of it from this point.
|
||||
|
keys( this.data ).forEach( key => this.mark( key ) ); |
||||
|
this.isIncluded = true; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
this.namesToInclude[ name ] = true; |
||||
|
} |
||||
|
|
||||
|
render () { |
||||
|
const [ code, data ] = this.renderData(); |
||||
|
|
||||
|
return new MagicString( `const ${this.name} = ${data};` ); |
||||
|
} |
||||
|
|
||||
|
renderData () { |
||||
|
if ( !this.isIncluded ) return []; |
||||
|
|
||||
|
const codes = []; |
||||
|
|
||||
|
let tuples = keys( this.namesToInclude ).map( name => { |
||||
|
if ( ) { |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
const [ code, data ] = sub.renderData(); |
||||
|
|
||||
|
codes.push( code ); |
||||
|
|
||||
|
return `${str(name)} = ${data}`; |
||||
|
}); |
||||
|
|
||||
|
return [ codes, ]; |
||||
|
} |
||||
|
} |
@ -0,0 +1,78 @@ |
|||||
|
import { blank } from './utils/object'; |
||||
|
|
||||
|
class Identifier { |
||||
|
constructor ( name ) { |
||||
|
this.originalName = this.name = name; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Reference { |
||||
|
constructor ( scope, index ) { |
||||
|
this.scope = scope; |
||||
|
this.index = index; |
||||
|
} |
||||
|
|
||||
|
deref () { |
||||
|
return this.scope.ids[ this.index ]; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function isntReference ( id ) { |
||||
|
return !( id instanceof Reference ); |
||||
|
} |
||||
|
|
||||
|
// Prefix the argument with _.
|
||||
|
function underscorePrefix ( x ) { |
||||
|
return '_' + x; |
||||
|
} |
||||
|
|
||||
|
export default class Scope { |
||||
|
constructor () { |
||||
|
this.ids = []; |
||||
|
this.names = {}; |
||||
|
} |
||||
|
|
||||
|
bind ( name, ref ) { |
||||
|
this.ids[ this.index( name ) ] = ref; |
||||
|
} |
||||
|
|
||||
|
deconflict ( rename = underscorePrefix ) { |
||||
|
const names = blank(); |
||||
|
|
||||
|
this.ids.filter( isntReference ).forEach( id => { |
||||
|
let name = id.name; |
||||
|
|
||||
|
while ( name in names && names[ name ] !== id ) { |
||||
|
name = rename( name ); |
||||
|
} |
||||
|
|
||||
|
id.name = name; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
define ( name, id ) { |
||||
|
this.ids[ this.index( name ) ] = id || new Identifier( name ); |
||||
|
} |
||||
|
|
||||
|
index ( name ) { |
||||
|
if ( !( name in this.names ) ) { |
||||
|
return this.names[ name ] = 1 + this.ids.push(); |
||||
|
} |
||||
|
|
||||
|
return this.names[ name ]; |
||||
|
} |
||||
|
|
||||
|
lookup ( name ) { |
||||
|
let id = this.ids[ this.names[ name ] ]; |
||||
|
|
||||
|
while ( id instanceof Reference ) { |
||||
|
id = id.deref(); |
||||
|
} |
||||
|
|
||||
|
return id; |
||||
|
} |
||||
|
|
||||
|
reference ( name ) { |
||||
|
return new Reference( this, this.names[ name ] ); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue