Skip to content

Commit

Permalink
rename things internally (path -> id, etc) - #30
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jun 28, 2015
1 parent 0b92d52 commit b8894dd
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 30 deletions.
26 changes: 12 additions & 14 deletions src/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ExternalModule from './ExternalModule';
import finalisers from './finalisers/index';
import makeLegalIdentifier from './utils/makeLegalIdentifier';
import ensureArray from './utils/ensureArray';
import { defaultResolver, defaultExternalResolver } from './utils/resolvePath';
import { defaultResolver, defaultExternalResolver } from './utils/resolveId';
import { defaultLoader } from './utils/load';
import getExportMode from './utils/getExportMode';
import getIndentString from './utils/getIndentString';
Expand All @@ -18,12 +18,10 @@ export default class Bundle {
this.entry = options.entry;
this.entryModule = null;

// TODO resolvePath is incorrect - it may not be a filesystem path, but
// something more abstract
this.resolvePath = options.resolvePath || defaultResolver;
this.resolveId = options.resolveId || defaultResolver;
this.load = options.load || defaultLoader;

this.resolvePathOptions = {
this.resolveOptions = {
external: ensureArray( options.external ),
resolveExternal: options.resolveExternal || defaultExternalResolver
};
Expand All @@ -43,9 +41,9 @@ export default class Bundle {
}

fetchModule ( importee, importer ) {
return Promise.resolve( this.resolvePath( importee, importer, this.resolvePathOptions ) )
.then( path => {
if ( !path ) {
return Promise.resolve( this.resolveId( importee, importer, this.resolveOptions ) )
.then( id => {
if ( !id ) {
// external module
if ( !this.modulePromises[ importee ] ) {
const module = new ExternalModule( importee );
Expand All @@ -56,11 +54,11 @@ export default class Bundle {
return this.modulePromises[ importee ];
}

if ( !this.modulePromises[ path ] ) {
this.modulePromises[ path ] = Promise.resolve( this.load( path, this.loadOptions ) )
if ( !this.modulePromises[ id ] ) {
this.modulePromises[ id ] = Promise.resolve( this.load( id, this.loadOptions ) )
.then( source => {
const module = new Module({
path,
id,
source,
bundle: this
});
Expand All @@ -69,7 +67,7 @@ export default class Bundle {
});
}

return this.modulePromises[ path ];
return this.modulePromises[ id ];
});
}

Expand All @@ -89,9 +87,9 @@ export default class Bundle {
}

// `export default a + b` - generate an export name
// based on the filename of the entry module
// based on the id of the entry module
else {
let defaultExportName = makeLegalIdentifier( basename( this.entryModule.path ).slice( 0, -extname( this.entryModule.path ).length ) );
let defaultExportName = makeLegalIdentifier( basename( this.entryModule.id ).slice( 0, -extname( this.entryModule.id ).length ) );

// deconflict
let topLevelNames = [];
Expand Down
20 changes: 11 additions & 9 deletions src/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ function deconflict ( name, names ) {
}

export default class Module {
constructor ({ path, source, bundle }) {
constructor ({ id, source, bundle }) {
this.source = source;

this.bundle = bundle;
this.path = path;
this.id = id;

// By default, `id` is the filename. Custom resolvers and loaders
// can change that, but it makes sense to use it for the source filename
this.magicString = new MagicString( source, {
filename: path
filename: id
});

this.suggestedNames = blank();
Expand All @@ -47,7 +49,7 @@ export default class Module {
});
} catch ( err ) {
err.code = 'PARSE_ERROR';
err.file = path;
err.file = id; // see above - not necessarily true, but true enough
throw err;
}

Expand Down Expand Up @@ -114,7 +116,7 @@ export default class Module {

if ( this.imports[ localName ] ) {
const err = new Error( `Duplicated import '${localName}'` );
err.file = this.path;
err.file = this.id;
err.loc = getLocation( this.source, specifier.start );
throw err;
}
Expand Down Expand Up @@ -260,7 +262,7 @@ export default class Module {
getCanonicalName ( localName ) {
// Special case
if ( localName === 'default' && ( this.exports.default.isModified || !this.suggestedNames.default ) ) {
let canonicalName = makeLegalIdentifier( this.path.replace( dirname( this.bundle.entryModule.path ) + '/', '' ).replace( /\.js$/, '' ) );
let canonicalName = makeLegalIdentifier( this.id.replace( dirname( this.bundle.entryModule.id ) + '/', '' ).replace( /\.js$/, '' ) );
return deconflict( canonicalName, this.definitions );
}

Expand Down Expand Up @@ -313,7 +315,7 @@ export default class Module {
if ( this.imports[ name ] ) {
const importDeclaration = this.imports[ name ];

promise = this.bundle.fetchModule( importDeclaration.source, this.path )
promise = this.bundle.fetchModule( importDeclaration.source, this.id )
.then( module => {
importDeclaration.module = module;

Expand Down Expand Up @@ -359,7 +361,7 @@ export default class Module {
const exportDeclaration = module.exports[ importDeclaration.name ];

if ( !exportDeclaration ) {
throw new Error( `Module ${module.path} does not export ${importDeclaration.name} (imported by ${this.path})` );
throw new Error( `Module ${module.id} does not export ${importDeclaration.name} (imported by ${this.id})` );
}

return module.define( exportDeclaration.localName );
Expand Down Expand Up @@ -434,7 +436,7 @@ export default class Module {
// ...unless they're empty, in which case assume we're importing them for the side-effects
// THIS IS NOT FOOLPROOF. Probably need /*rollup: include */ or similar
if ( !statement.node.specifiers.length ) {
return this.bundle.fetchModule( statement.node.source.value, this.path )
return this.bundle.fetchModule( statement.node.source.value, this.id )
.then( module => {
statement.module = module;
return module.expandAllStatements();
Expand Down
2 changes: 1 addition & 1 deletion src/Statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class Statement {

if ( depth < minDepth ) {
const err = new Error( `Illegal reassignment to import '${node.name}'` );
err.file = this.module.path;
err.file = this.module.id;
err.loc = getLocation( this.module.magicString.toString(), node.start );
throw err;
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/load.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { readFileSync } from 'sander';

export function defaultLoader ( path, options ) {
export function defaultLoader ( id, options ) {
// TODO support plugins e.g. !css and !json?
const source = readFileSync( path, { encoding: 'utf-8' });
const source = readFileSync( id, { encoding: 'utf-8' });

return options.transform.reduce( ( source, transformer ) => {
return transformer( source, path );
return transformer( source, id );
}, source );
}
File renamed without changes.
2 changes: 1 addition & 1 deletion test/function/custom-path-resolver-async/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var assert = require( 'assert' );
module.exports = {
description: 'uses a custom path resolver (asynchronous)',
options: {
resolvePath: function ( importee, importer ) {
resolveId: function ( importee, importer ) {
var Promise = require( 'sander' ).Promise;
var resolved;

Expand Down
2 changes: 1 addition & 1 deletion test/function/custom-path-resolver-on-entry/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
description: 'applies custom resolver to entry point',
//solo: true,
options: {
resolvePath: function ( importee, importer ) {
resolveId: function ( importee, importer ) {
if ( importer === undefined ) {
return '@' + path.relative( __dirname, importee );
}
Expand Down
2 changes: 1 addition & 1 deletion test/function/custom-path-resolver-sync/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var assert = require( 'assert' );
module.exports = {
description: 'uses a custom path resolver (synchronous)',
options: {
resolvePath: function ( importee, importer ) {
resolveId: function ( importee, importer ) {
if ( importee === path.resolve( __dirname, 'main.js' ) ) return importee;
if ( importee === 'foo' ) return path.resolve( __dirname, 'bar.js' );

Expand Down

0 comments on commit b8894dd

Please sign in to comment.