Browse Source

treat missing namespace exports as warning, not error

better-aggressive
Rich-Harris 9 years ago
parent
commit
e94de0e8f1
  1. 16
      src/Module.js
  2. 7
      test/function/namespace-missing-export/_config.js
  3. 1
      test/function/namespace-missing-export/empty.js
  4. 2
      test/function/namespace-missing-export/main.js

16
src/Module.js

@ -81,10 +81,9 @@ class SyntheticNamespaceDeclaration {
// throw with an informative error message if the reference doesn't exist.
if ( !original ) {
const err = new Error( `Export '${reference.name}' is not defined by '${this.module.id}'` );
err.code = 'MISSING_EXPORT';
err.file = this.module.id;
throw err;
this.module.bundle.onwarn( `Export '${reference.name}' is not defined by '${this.module.id}'` );
reference.isUndefined = true;
return;
}
original.addReference( reference );
@ -581,15 +580,20 @@ export default class Module {
let toDeshadow = blank();
statement.references.forEach( reference => {
const { start, end } = reference;
if ( reference.isUndefined ) {
magicString.overwrite( start, end, 'undefined', true );
}
const declaration = reference.declaration;
if ( declaration ) {
const { start, end } = reference;
const name = declaration.render( es6 );
// the second part of this check is necessary because of
// namespace optimisation – name of `foo.bar` could be `bar`
if ( reference.name === name && name.length === reference.end - reference.start ) return;
if ( reference.name === name && name.length === end - start ) return;
reference.rewritten = true;

7
test/function/namespace-missing-export/_config.js

@ -2,8 +2,9 @@ var assert = require( 'assert' );
var path = require( 'path' );
module.exports = {
error: function ( err ) {
assert.equal( path.normalize( err.file ), path.resolve( __dirname, 'empty.js' ) );
assert.ok( /Export 'foo' is not defined by/.test( err.message ) );
options: {
onwarn: function ( msg ) {
assert.ok( /Export 'foo' is not defined by/.test( msg ) );
}
}
};

1
test/function/namespace-missing-export/empty.js

@ -0,0 +1 @@
// this space left intentionally blank

2
test/function/namespace-missing-export/main.js

@ -1,3 +1,3 @@
import * as mod from './empty.js';
mod.foo();
assert.equal( typeof mod.foo, 'undefined' );

Loading…
Cancel
Save