Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply resolve hook to entry module #31

Merged
merged 2 commits into from
Jul 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Bundle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename, dirname, extname, relative, resolve } from 'path';
import { basename, dirname, extname, relative } from 'path';
import { Promise } from 'sander';
import MagicString from 'magic-string';
import { blank, keys } from './utils/object';
Expand All @@ -15,9 +15,11 @@ import { unixizePath } from './utils/normalizePlatform.js';

export default class Bundle {
constructor ( options ) {
this.entryPath = resolve( options.entry ).replace( /\.js$/, '' ) + '.js';
this.base = dirname( this.entryPath );
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.load = options.load || defaultLoader;

Expand All @@ -30,8 +32,6 @@ export default class Bundle {
transform: ensureArray( options.transform )
};

this.entryModule = null;

this.varExports = blank();
this.toExport = null;

Expand All @@ -43,7 +43,7 @@ export default class Bundle {
}

fetchModule ( importee, importer ) {
return Promise.resolve( importer === null ? importee : this.resolvePath( importee, importer, this.resolvePathOptions ) )
return Promise.resolve( this.resolvePath( importee, importer, this.resolvePathOptions ) )
.then( path => {
if ( !path ) {
// external module
Expand Down Expand Up @@ -75,7 +75,7 @@ export default class Bundle {

build () {
// bring in top-level AST nodes from the entry module
return this.fetchModule( this.entryPath, null )
return this.fetchModule( this.entry, undefined )
.then( entryModule => {
const defaultExport = entryModule.exports.default;

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

// deconflict
let topLevelNames = [];
Expand Down
3 changes: 2 additions & 1 deletion src/Module.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dirname } from 'path';
import { Promise } from 'sander';
import { parse } from 'acorn';
import MagicString from 'magic-string';
Expand Down Expand Up @@ -251,7 +252,7 @@ export default class Module {
getCanonicalName ( localName ) {
// Special case
if ( localName === 'default' && this.exports.default && this.exports.default.isModified ) {
let canonicalName = makeLegalIdentifier( this.path.replace( this.bundle.base + '/', '' ).replace( /\.js$/, '' ) );
let canonicalName = makeLegalIdentifier( this.path.replace( dirname( this.bundle.entryModule.path ) + '/', '' ).replace( /\.js$/, '' ) );
while ( this.definitions[ canonicalName ] ) {
canonicalName = `_${canonicalName}`;
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/resolvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export function defaultResolver ( importee, importer, options ) {
// absolute paths are left untouched
if ( absolutePath.test( importee ) ) return importee;

// if this is the entry point, resolve against cwd
if ( importer === undefined ) return resolve( importee );

// we try to resolve external modules
if ( importee[0] !== '.' ) {
// unless we want to keep it external, that is
Expand Down
5 changes: 4 additions & 1 deletion test/function/custom-path-resolver-async/_config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var path = require( 'path' );
var assert = require( 'assert' );

module.exports = {
Expand All @@ -7,8 +8,10 @@ module.exports = {
var Promise = require( 'sander' ).Promise;
var resolved;

if ( importee === path.resolve( __dirname, 'main.js' ) ) return importee;

if ( importee === 'foo' ) {
resolved = require( 'path' ).resolve( __dirname, 'bar.js' );
resolved = path.resolve( __dirname, 'bar.js' );
} else {
resolved = false;
}
Expand Down
35 changes: 35 additions & 0 deletions test/function/custom-path-resolver-on-entry/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var path = require( 'path' );
var fs = require( 'fs' );
var assert = require( 'assert' );

var cachedModules = {
'@main.js': 'import foo from "./foo"; export default foo();'
};

module.exports = {
description: 'applies custom resolver to entry point',
//solo: true,
options: {
resolvePath: function ( importee, importer ) {
if ( importer === undefined ) {
return '@' + path.relative( __dirname, importee );
}

if ( importer[0] === '@' ) {
return path.resolve( __dirname, importee ) + '.js';
}

return path.resolve( path.dirname( importer ), importee ) + '.js';
},
load: function ( moduleId ) {
if ( moduleId[0] === '@' ) {
return cachedModules[ moduleId ];
}

return fs.readFileSync( moduleId, 'utf-8' );
}
},
exports: function ( exports ) {
assert.equal( exports, 42 );
}
};
3 changes: 3 additions & 0 deletions test/function/custom-path-resolver-on-entry/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function () {
return 21;
}
5 changes: 5 additions & 0 deletions test/function/custom-path-resolver-on-entry/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import bar from './bar';

export default function () {
return bar() * 2;
}
6 changes: 3 additions & 3 deletions test/function/custom-path-resolver-sync/_config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var path = require( 'path' );
var assert = require( 'assert' );

module.exports = {
description: 'uses a custom path resolver (synchronous)',
options: {
resolvePath: function ( importee, importer ) {
if ( importee === 'foo' ) {
return require( 'path' ).resolve( __dirname, 'bar.js' );
}
if ( importee === path.resolve( __dirname, 'main.js' ) ) return importee;
if ( importee === 'foo' ) return path.resolve( __dirname, 'bar.js' );

return false;
}
Expand Down