Browse Source

Normalize relative external paths.

gh-669
fatfisz 9 years ago
parent
commit
c1978fb9e6
  1. 33
      src/Bundle.js
  2. 18
      test/function/relative-external-include-once-nested/_config.js
  3. 0
      test/function/relative-external-include-once-nested/first/foo.js
  4. 4
      test/function/relative-external-include-once-nested/first/module.js
  5. 3
      test/function/relative-external-include-once-nested/first/second/module.js
  6. 4
      test/function/relative-external-include-once-nested/main.js
  7. 19
      test/function/relative-external-include-once-up/_config.js
  8. 4
      test/function/relative-external-include-once-up/first/main.js
  9. 4
      test/function/relative-external-include-once-up/first/module.js
  10. 3
      test/function/relative-external-include-once-up/first/second/module.js
  11. 0
      test/function/relative-external-include-once-up/foo.js
  12. 18
      test/function/relative-external-include-once/_config.js
  13. 4
      test/function/relative-external-include-once/first/module.js
  14. 3
      test/function/relative-external-include-once/first/second/module.js
  15. 0
      test/function/relative-external-include-once/foo.js
  16. 4
      test/function/relative-external-include-once/main.js
  17. 4
      test/test.js

33
src/Bundle.js

@ -15,7 +15,7 @@ import transformBundle from './utils/transformBundle.js';
import collapseSourcemaps from './utils/collapseSourcemaps.js';
import SOURCEMAPPING_URL from './utils/sourceMappingURL.js';
import callIfFunction from './utils/callIfFunction.js';
import { isRelative, resolve } from './utils/path.js';
import { dirname, isRelative, relative, resolve } from './utils/path.js';
export default class Bundle {
constructor ( options ) {
@ -28,6 +28,7 @@ export default class Bundle {
});
this.entry = unixizePath( options.entry );
this.entryId = null;
this.entryModule = null;
this.resolveId = first(
@ -71,7 +72,10 @@ export default class Bundle {
// modules it imports, and import those, until we have all
// of the entry module's dependencies
return this.resolveId( this.entry, undefined )
.then( id => this.fetchModule( id, undefined ) )
.then( id => {
this.entryId = id;
return this.fetchModule( id, undefined );
})
.then( entryModule => {
this.entryModule = entryModule;
@ -189,16 +193,20 @@ export default class Bundle {
const forcedExternal = externalName && ~this.external.indexOf( externalName );
if ( !resolvedId || forcedExternal ) {
let normalizedExternal = source;
if ( !forcedExternal ) {
if ( isRelative( source ) ) throw new Error( `Could not resolve ${source} from ${module.id}` );
if ( !~this.external.indexOf( source ) ) this.onwarn( `Treating '${source}' as external dependency` );
} else if ( resolvedId ) {
normalizedExternal = this.getPathRelativeToEntryDirname( resolvedId );
}
module.resolvedIds[ source ] = source;
module.resolvedIds[ source ] = normalizedExternal;
if ( !this.moduleById[ source ] ) {
const module = new ExternalModule( source );
if ( !this.moduleById[ normalizedExternal ] ) {
const module = new ExternalModule( normalizedExternal );
this.externalModules.push( module );
this.moduleById[ source ] = module;
this.moduleById[ normalizedExternal ] = module;
}
}
@ -214,6 +222,19 @@ export default class Bundle {
});
}
getPathRelativeToEntryDirname ( resolvedId ) {
// Get a path relative to the resolved entry directory
const entryDirname = dirname( this.entryId );
const relativeToEntry = relative( entryDirname, resolvedId );
if ( isRelative( relativeToEntry )) {
return relativeToEntry;
}
// The path is missing the `./` prefix
return `./${relativeToEntry}`;
}
render ( options = {} ) {
const format = options.format || 'es6';

18
test/function/relative-external-include-once-nested/_config.js

@ -0,0 +1,18 @@
var assert = require( 'assert' );
var path = require( 'path' );
module.exports = {
description: 'includes a relative external module only once (nested version)',
options: {
external: path.join( __dirname, './first/foo.js' )
},
context: {
require: function ( required ) {
assert.equal( required, './first/foo.js' );
return 1;
}
},
exports: function ( exports ) {
assert.equal( exports, 3 );
}
};

0
test/function/relative-external-include-once-nested/first/foo.js

4
test/function/relative-external-include-once-nested/first/module.js

@ -0,0 +1,4 @@
import foo from './foo';
import second from './second/module';
export default foo + second;

3
test/function/relative-external-include-once-nested/first/second/module.js

@ -0,0 +1,3 @@
import foo from '../foo';
export default foo;

4
test/function/relative-external-include-once-nested/main.js

@ -0,0 +1,4 @@
import foo from './first/foo';
import first from './first/module';
export default foo + first;

19
test/function/relative-external-include-once-up/_config.js

@ -0,0 +1,19 @@
var assert = require( 'assert' );
var path = require( 'path' );
module.exports = {
description: 'includes a relative external module only once (from upper directory too)',
options: {
entry: path.join( __dirname, 'first', 'main.js' ),
external: path.join( __dirname, './foo.js' )
},
context: {
require: function ( required ) {
assert.equal( required, '../foo.js' );
return 1;
}
},
exports: function ( exports ) {
assert.equal( exports, 3 );
}
};

4
test/function/relative-external-include-once-up/first/main.js

@ -0,0 +1,4 @@
import foo from '../foo';
import first from './module';
export default foo + first;

4
test/function/relative-external-include-once-up/first/module.js

@ -0,0 +1,4 @@
import foo from '../foo';
import second from './second/module';
export default foo + second;

3
test/function/relative-external-include-once-up/first/second/module.js

@ -0,0 +1,3 @@
import foo from '../../foo';
export default foo;

0
test/function/relative-external-include-once-up/foo.js

18
test/function/relative-external-include-once/_config.js

@ -0,0 +1,18 @@
var assert = require( 'assert' );
var path = require( 'path' );
module.exports = {
description: 'includes a relative external module only once',
options: {
external: path.join( __dirname, './foo.js' )
},
context: {
require: function ( required ) {
assert.equal( required, './foo.js' );
return 1;
}
},
exports: function ( exports ) {
assert.equal( exports, 3 );
}
};

4
test/function/relative-external-include-once/first/module.js

@ -0,0 +1,4 @@
import foo from '../foo';
import second from './second/module';
export default foo + second;

3
test/function/relative-external-include-once/first/second/module.js

@ -0,0 +1,3 @@
import foo from '../../foo';
export default foo;

0
test/function/relative-external-include-once/foo.js

4
test/function/relative-external-include-once/main.js

@ -0,0 +1,4 @@
import foo from './foo';
import first from './first/module';
export default foo + first;

4
test/test.js

@ -133,9 +133,9 @@ describe( 'rollup', function () {
var config = loadConfig( FUNCTION + '/' + dir + '/_config.js' );
( config.skip ? it.skip : config.solo ? it.only : it )( dir, function () {
var options = extend( {}, config.options, {
var options = extend( {
entry: FUNCTION + '/' + dir + '/main.js'
});
}, config.options );
if ( config.solo ) console.group( dir );

Loading…
Cancel
Save