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. 4
      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(); this.onwarn = options.onwarn || makeOnwarn();
// TODO strictly speaking, this only applies with non-ES6, non-default-only bundles // 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 () { 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); const hasDefaultImport = bundle.externalModules.some( mod => mod.declarations.default);
if (hasDefaultImport) { 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 // TODO handle empty imports, once they're supported
const importBlock = bundle.externalModules const importBlock = bundle.externalModules
.map( module => { .map( module => {
if ( module.declarations.default ) { if ( module.declarations.default ) {
let importStatement = `var ${module.name} = _interopRequire('${module.id}');`;
if (module.exportsNames) { 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 { } else {
return `var ${module.name} = require('${module.id}');`; return `var ${module.name} = require('${module.id}');`;
} }

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

@ -1,8 +1,8 @@
'use strict'; '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 () { $( function () {
$( 'body' ).html( '<h1>hello world!</h1>' ); $( 'body' ).html( '<h1>hello world!</h1>' );

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

@ -1,12 +1,12 @@
'use strict'; '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 baz = require('baz');
var containers = require('shipping-port'); var containers = require('shipping-port');
var alphabet = _interopRequire('alphabet'); var alphabet = require('alphabet');
var alphabet__default = alphabet; var alphabet__default = _interopDefault(alphabet);
factory( null ); factory( null );
baz.foo( baz.bar, containers.port ); baz.foo( baz.bar, containers.port );

Loading…
Cancel
Save