Browse Source

allow importing external modules for side effects, fixes #55

contingency-plan
Arpad Borsos 9 years ago
parent
commit
759d3938b9
  1. 5
      src/Module.js
  2. 15
      test/function/import-empty-from-external/_config.js
  3. 1
      test/function/import-empty-from-external/foo.js
  4. 3
      test/function/import-empty-from-external/main.js

5
src/Module.js

@ -201,7 +201,7 @@ export default class Module {
let strongDependencies = blank(); let strongDependencies = blank();
this.statements.forEach( statement => { 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 // include module for its side-effects
strongDependencies[ statement.module.id ] = statement.module; // TODO is this right? `statement.module` should be `this`, surely? 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 ) return this.bundle.fetchModule( statement.node.source.value, this.id )
.then( module => { .then( module => {
statement.module = module; statement.module = module;
if ( module.isExternal ) {
return;
}
return module.markAllStatements(); return module.markAllStatements();
}); });
} }

15
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' ]
}
};

1
test/function/import-empty-from-external/foo.js

@ -0,0 +1 @@
global.answer = 42;

3
test/function/import-empty-from-external/main.js

@ -0,0 +1,3 @@
import 'foo';
assert.equal( global.answer, 42 );
Loading…
Cancel
Save