From c1978fb9e6e4ec5d3d78afea11505f588e313e03 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Tue, 5 Apr 2016 01:28:01 +0200 Subject: [PATCH] Normalize relative external paths. --- src/Bundle.js | 33 +++++++++++++++---- .../_config.js | 18 ++++++++++ .../first/foo.js | 0 .../first/module.js | 4 +++ .../first/second/module.js | 3 ++ .../main.js | 4 +++ .../_config.js | 19 +++++++++++ .../first/main.js | 4 +++ .../first/module.js | 4 +++ .../first/second/module.js | 3 ++ .../relative-external-include-once-up/foo.js | 0 .../relative-external-include-once/_config.js | 18 ++++++++++ .../first/module.js | 4 +++ .../first/second/module.js | 3 ++ .../relative-external-include-once/foo.js | 0 .../relative-external-include-once/main.js | 4 +++ test/test.js | 4 +-- 17 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 test/function/relative-external-include-once-nested/_config.js create mode 100644 test/function/relative-external-include-once-nested/first/foo.js create mode 100644 test/function/relative-external-include-once-nested/first/module.js create mode 100644 test/function/relative-external-include-once-nested/first/second/module.js create mode 100644 test/function/relative-external-include-once-nested/main.js create mode 100644 test/function/relative-external-include-once-up/_config.js create mode 100644 test/function/relative-external-include-once-up/first/main.js create mode 100644 test/function/relative-external-include-once-up/first/module.js create mode 100644 test/function/relative-external-include-once-up/first/second/module.js create mode 100644 test/function/relative-external-include-once-up/foo.js create mode 100644 test/function/relative-external-include-once/_config.js create mode 100644 test/function/relative-external-include-once/first/module.js create mode 100644 test/function/relative-external-include-once/first/second/module.js create mode 100644 test/function/relative-external-include-once/foo.js create mode 100644 test/function/relative-external-include-once/main.js diff --git a/src/Bundle.js b/src/Bundle.js index 1c28ce3..07a788c 100644 --- a/src/Bundle.js +++ b/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'; diff --git a/test/function/relative-external-include-once-nested/_config.js b/test/function/relative-external-include-once-nested/_config.js new file mode 100644 index 0000000..277cc61 --- /dev/null +++ b/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 ); + } +}; \ No newline at end of file diff --git a/test/function/relative-external-include-once-nested/first/foo.js b/test/function/relative-external-include-once-nested/first/foo.js new file mode 100644 index 0000000..e69de29 diff --git a/test/function/relative-external-include-once-nested/first/module.js b/test/function/relative-external-include-once-nested/first/module.js new file mode 100644 index 0000000..16ff3cb --- /dev/null +++ b/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; \ No newline at end of file diff --git a/test/function/relative-external-include-once-nested/first/second/module.js b/test/function/relative-external-include-once-nested/first/second/module.js new file mode 100644 index 0000000..50031db --- /dev/null +++ b/test/function/relative-external-include-once-nested/first/second/module.js @@ -0,0 +1,3 @@ +import foo from '../foo'; + +export default foo; \ No newline at end of file diff --git a/test/function/relative-external-include-once-nested/main.js b/test/function/relative-external-include-once-nested/main.js new file mode 100644 index 0000000..5f344ae --- /dev/null +++ b/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; \ No newline at end of file diff --git a/test/function/relative-external-include-once-up/_config.js b/test/function/relative-external-include-once-up/_config.js new file mode 100644 index 0000000..12f56a3 --- /dev/null +++ b/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 ); + } +}; \ No newline at end of file diff --git a/test/function/relative-external-include-once-up/first/main.js b/test/function/relative-external-include-once-up/first/main.js new file mode 100644 index 0000000..6081969 --- /dev/null +++ b/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; \ No newline at end of file diff --git a/test/function/relative-external-include-once-up/first/module.js b/test/function/relative-external-include-once-up/first/module.js new file mode 100644 index 0000000..5c8cdb0 --- /dev/null +++ b/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; \ No newline at end of file diff --git a/test/function/relative-external-include-once-up/first/second/module.js b/test/function/relative-external-include-once-up/first/second/module.js new file mode 100644 index 0000000..abfe393 --- /dev/null +++ b/test/function/relative-external-include-once-up/first/second/module.js @@ -0,0 +1,3 @@ +import foo from '../../foo'; + +export default foo; \ No newline at end of file diff --git a/test/function/relative-external-include-once-up/foo.js b/test/function/relative-external-include-once-up/foo.js new file mode 100644 index 0000000..e69de29 diff --git a/test/function/relative-external-include-once/_config.js b/test/function/relative-external-include-once/_config.js new file mode 100644 index 0000000..a12f8bb --- /dev/null +++ b/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 ); + } +}; \ No newline at end of file diff --git a/test/function/relative-external-include-once/first/module.js b/test/function/relative-external-include-once/first/module.js new file mode 100644 index 0000000..5c8cdb0 --- /dev/null +++ b/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; \ No newline at end of file diff --git a/test/function/relative-external-include-once/first/second/module.js b/test/function/relative-external-include-once/first/second/module.js new file mode 100644 index 0000000..abfe393 --- /dev/null +++ b/test/function/relative-external-include-once/first/second/module.js @@ -0,0 +1,3 @@ +import foo from '../../foo'; + +export default foo; \ No newline at end of file diff --git a/test/function/relative-external-include-once/foo.js b/test/function/relative-external-include-once/foo.js new file mode 100644 index 0000000..e69de29 diff --git a/test/function/relative-external-include-once/main.js b/test/function/relative-external-include-once/main.js new file mode 100644 index 0000000..52efbbe --- /dev/null +++ b/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; \ No newline at end of file diff --git a/test/test.js b/test/test.js index 39a81fc..358e390 100644 --- a/test/test.js +++ b/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 );