Browse Source

apply resolvePath hook to entry module (#30)

contingency-plan
Rich Harris 9 years ago
parent
commit
dc6940de2e
  1. 16
      src/Bundle.js
  2. 3
      src/Module.js
  3. 3
      src/utils/resolvePath.js
  4. 35
      test/function/custom-path-resolver-on-entry/_config.js
  5. 3
      test/function/custom-path-resolver-on-entry/bar.js
  6. 5
      test/function/custom-path-resolver-on-entry/foo.js

16
src/Bundle.js

@ -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';
@ -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;
@ -30,8 +32,6 @@ export default class Bundle {
transform: ensureArray( options.transform )
};
this.entryModule = null;
this.varExports = blank();
this.toExport = null;
@ -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
@ -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;
@ -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 = [];

3
src/Module.js

@ -1,3 +1,4 @@
import { dirname } from 'path';
import { Promise } from 'sander';
import { parse } from 'acorn';
import MagicString from 'magic-string';
@ -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}`;
}

3
src/utils/resolvePath.js

@ -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

35
test/function/custom-path-resolver-on-entry/_config.js

@ -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
test/function/custom-path-resolver-on-entry/bar.js

@ -0,0 +1,3 @@
export default function () {
return 21;
}

5
test/function/custom-path-resolver-on-entry/foo.js

@ -0,0 +1,5 @@
import bar from './bar';
export default function () {
return bar() * 2;
}
Loading…
Cancel
Save