From 759d3938b9d2103e76a000fcdedb086faa43b2a3 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Wed, 22 Jul 2015 13:43:27 +0200 Subject: [PATCH] allow importing external modules for side effects, fixes #55 --- src/Module.js | 5 ++++- .../import-empty-from-external/_config.js | 15 +++++++++++++++ test/function/import-empty-from-external/foo.js | 1 + test/function/import-empty-from-external/main.js | 3 +++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/function/import-empty-from-external/_config.js create mode 100644 test/function/import-empty-from-external/foo.js create mode 100644 test/function/import-empty-from-external/main.js diff --git a/src/Module.js b/src/Module.js index 018d2b6..17d35e0 100644 --- a/src/Module.js +++ b/src/Module.js @@ -201,7 +201,7 @@ export default class Module { let strongDependencies = blank(); this.statements.forEach( statement => { - if ( statement.isImportDeclaration && !statement.node.specifiers.length ) { + if ( statement.isImportDeclaration && !statement.node.specifiers.length && !statement.module.isExternal ) { // include module for its side-effects strongDependencies[ statement.module.id ] = statement.module; // TODO is this right? `statement.module` should be `this`, surely? } @@ -475,6 +475,9 @@ export default class Module { return this.bundle.fetchModule( statement.node.source.value, this.id ) .then( module => { statement.module = module; + if ( module.isExternal ) { + return; + } return module.markAllStatements(); }); } diff --git a/test/function/import-empty-from-external/_config.js b/test/function/import-empty-from-external/_config.js new file mode 100644 index 0000000..c2207ae --- /dev/null +++ b/test/function/import-empty-from-external/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'imports external module for side effects', + context: { + // override require here, making "foo" appear as a global module + require: function ( name ) { + if ( name === 'foo' ) { + return require( './foo' ); + } + return require( name ); + } + }, + options: { + external: [ 'foo' ] + } +}; diff --git a/test/function/import-empty-from-external/foo.js b/test/function/import-empty-from-external/foo.js new file mode 100644 index 0000000..fcd98c3 --- /dev/null +++ b/test/function/import-empty-from-external/foo.js @@ -0,0 +1 @@ +global.answer = 42; diff --git a/test/function/import-empty-from-external/main.js b/test/function/import-empty-from-external/main.js new file mode 100644 index 0000000..91e0072 --- /dev/null +++ b/test/function/import-empty-from-external/main.js @@ -0,0 +1,3 @@ +import 'foo'; + +assert.equal( global.answer, 42 );