Browse Source

add _interopDefault to globals, fix behavior or module__default, make cjs work with browserify

gh-669
Alexander Early 9 years ago
parent
commit
1f3488dcfd
  1. 2
      src/Bundle.js
  2. 9
      src/finalisers/cjs.js
  3. 6
      test/form/external-imports-custom-names/_expected/cjs.js
  4. 8
      test/form/external-imports/_expected/cjs.js

2
src/Bundle.js

@ -63,7 +63,7 @@ export default class Bundle {
this.onwarn = options.onwarn || makeOnwarn();
// TODO strictly speaking, this only applies with non-ES6, non-default-only bundles
[ 'module', 'exports' ].forEach( global => this.assumedGlobals[ global ] = true );
[ 'module', 'exports', '_interopDefault' ].forEach( global => this.assumedGlobals[ global ] = true );
}
build () {

9
src/finalisers/cjs.js

@ -6,18 +6,19 @@ export default function cjs ( bundle, magicString, { exportMode }, options ) {
const hasDefaultImport = bundle.externalModules.some( mod => mod.declarations.default);
if (hasDefaultImport) {
intro += `function _interopRequire (id) { var ex = require(id); return 'default' in ex ? ex['default'] : ex; }\n\n`;
intro += `function _interopDefault (ex) { return 'default' in ex ? ex['default'] : ex; }\n\n`;
}
// TODO handle empty imports, once they're supported
const importBlock = bundle.externalModules
.map( module => {
if ( module.declarations.default ) {
let importStatement = `var ${module.name} = _interopRequire('${module.id}');`;
if (module.exportsNames) {
importStatement += `\nvar ${module.name}__default = ${module.name};`;
return `var ${module.name} = require('${module.id}');` +
`\nvar ${module.name}__default = _interopDefault(${module.name});`;
} else {
return `var ${module.name} = _interopDefault(require('${module.id}'));`;
}
return importStatement;
} else {
return `var ${module.name} = require('${module.id}');`;
}

6
test/form/external-imports-custom-names/_expected/cjs.js

@ -1,9 +1,9 @@
'use strict';
function _interopRequire (id) { var ex = require(id); return 'default' in ex ? ex['default'] : ex; }
function _interopDefault (ex) { return 'default' in ex ? ex['default'] : ex; }
var $ = _interopRequire('jquery');
var $ = _interopDefault(require('jquery'));
$( function () {
$( 'body' ).html( '<h1>hello world!</h1>' );
});
});

8
test/form/external-imports/_expected/cjs.js

@ -1,12 +1,12 @@
'use strict';
function _interopRequire (id) { var ex = require(id); return 'default' in ex ? ex['default'] : ex; }
function _interopDefault (ex) { return 'default' in ex ? ex['default'] : ex; }
var factory = _interopRequire('factory');
var factory = _interopDefault(require('factory'));
var baz = require('baz');
var containers = require('shipping-port');
var alphabet = _interopRequire('alphabet');
var alphabet__default = alphabet;
var alphabet = require('alphabet');
var alphabet__default = _interopDefault(alphabet);
factory( null );
baz.foo( baz.bar, containers.port );

Loading…
Cancel
Save