Browse Source

move Module and Bundle into top-level files

contingency-plan
Rich-Harris 10 years ago
parent
commit
eaa93ae9c0
  1. 31
      src/Bundle.js
  2. 15
      src/Module.js
  3. 10
      src/finalisers/cjs.js

31
src/Bundle/index.js → src/Bundle.js

@ -1,11 +1,11 @@
import { resolve } from 'path';
import { readFile } from 'sander';
import MagicString from 'magic-string';
import { keys, has } from '../utils/object';
import { sequence } from '../utils/promise';
import Module from '../Module/index';
import finalisers from '../finalisers/index';
import replaceIdentifiers from '../utils/replaceIdentifiers';
import { keys, has } from './utils/object';
import { sequence } from './utils/promise';
import Module from './Module';
import finalisers from './finalisers/index';
import replaceIdentifiers from './utils/replaceIdentifiers';
export default class Bundle {
constructor ( options ) {
@ -26,7 +26,7 @@ export default class Bundle {
this.externalModules = [];
}
fetchModule ( path ) {
fetchModule ( path, id ) {
if ( !has( this.modulePromises, path ) ) {
this.modulePromises[ path ] = readFile( path, { encoding: 'utf-8' })
.then( code => {
@ -38,6 +38,25 @@ export default class Bundle {
this.modules[ path ] = module;
return module;
}, err => {
if ( err.code === 'ENOENT' ) {
if ( id[0] === '.' ) {
// external modules can't have relative paths
throw err;
}
// most likely an external module
// TODO fire an event, or otherwise allow some way for
// users to control external modules better?
const module = {
id,
isExternal: true,
specifiers: []
};
this.externalModules.push( module );
return module;
}
});
}

15
src/Module/index.js → src/Module.js

@ -2,9 +2,9 @@ import { dirname, relative, resolve } from 'path';
import { Promise } from 'sander';
import { parse } from 'acorn';
import MagicString from 'magic-string';
import analyse from '../ast/analyse';
import { has } from '../utils/object';
import { sequence } from '../utils/promise';
import analyse from './ast/analyse';
import { has } from './utils/object';
import { sequence } from './utils/promise';
const emptyArrayPromise = Promise.resolve([]);
@ -127,6 +127,8 @@ export default class Module {
if ( has( this.imports, name ) ) {
const importDeclaration = this.imports[ name ];
const module = importDeclaration.module;
// TODO handle external modules
const exportDeclaration = module.exports[ importDeclaration.name ];
return module.getCanonicalName( exportDeclaration.localName );
@ -152,8 +154,13 @@ export default class Module {
const importDeclaration = this.imports[ name ];
const path = resolve( dirname( this.path ), importDeclaration.source ) + '.js';
promise = this.bundle.fetchModule( path )
promise = this.bundle.fetchModule( path, importDeclaration.source )
.then( module => {
if ( module.isExternal ) {
module.specifiers.push( importDeclaration );
return emptyArrayPromise;
}
importDeclaration.module = module;
const exportDeclaration = module.exports[ importDeclaration.name ];

10
src/finalisers/cjs.js

@ -1,3 +1,11 @@
export default function cjs ( bundle, magicString, options ) {
return magicString;
const intro = `'use strict';\n\n`;
// TODO group modules more intelligently
bundle.externalModules.forEach( module => {
console.log( 'module', module );
//intro += `var
});
return magicString.prepend( intro );
}
Loading…
Cancel
Save