diff --git a/bin/src/index.js b/bin/src/index.js index 1b8f1e6..cbc8123 100644 --- a/bin/src/index.js +++ b/bin/src/index.js @@ -16,6 +16,7 @@ const command = minimist( process.argv.slice( 2 ), { g: 'globals', h: 'help', i: 'input', + l: 'legacy', m: 'sourcemap', n: 'name', o: 'output', diff --git a/bin/src/runRollup.js b/bin/src/runRollup.js index 53a6231..84cd3da 100644 --- a/bin/src/runRollup.js +++ b/bin/src/runRollup.js @@ -105,6 +105,7 @@ const equivalents = { indent: 'indent', input: 'entry', intro: 'intro', + legacy: 'legacy', name: 'moduleName', output: 'dest', outro: 'outro', diff --git a/src/finalisers/amd.js b/src/finalisers/amd.js index 96626e2..64adffd 100644 --- a/src/finalisers/amd.js +++ b/src/finalisers/amd.js @@ -27,7 +27,7 @@ export default function amd ( bundle, magicString, { exportMode, indentString, i const exportBlock = getExportBlock( bundle.entryModule, exportMode ); if ( exportBlock ) magicString.append( '\n\n' + exportBlock ); - if ( exportMode === 'named' ) magicString.append( `\n\n${esModuleExport}` ); + if ( exportMode === 'named' && options.legacy !== true ) magicString.append( `\n\n${esModuleExport}` ); if ( options.outro ) magicString.append( `\n${options.outro}` ); return magicString diff --git a/src/finalisers/cjs.js b/src/finalisers/cjs.js index 5af4df7..e89ef32 100644 --- a/src/finalisers/cjs.js +++ b/src/finalisers/cjs.js @@ -3,7 +3,7 @@ import esModuleExport from './shared/esModuleExport.js'; export default function cjs ( bundle, magicString, { exportMode, intro }, options ) { intro = ( options.useStrict === false ? intro : `'use strict';\n\n${intro}` ) + - ( exportMode === 'named' ? `${esModuleExport}\n\n` : '' ); + ( exportMode === 'named' && options.legacy !== true ? `${esModuleExport}\n\n` : '' ); let needsInterop = false; diff --git a/src/finalisers/umd.js b/src/finalisers/umd.js index f2be2a2..67b9860 100644 --- a/src/finalisers/umd.js +++ b/src/finalisers/umd.js @@ -73,7 +73,7 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i const exportBlock = getExportBlock( bundle.entryModule, exportMode ); if ( exportBlock ) magicString.append( '\n\n' + exportBlock ); - if ( exportMode === 'named' ) magicString.append( `\n\n${esModuleExport}` ); + if ( exportMode === 'named' && options.legacy !== true ) magicString.append( `\n\n${esModuleExport}` ); if ( options.outro ) magicString.append( `\n${options.outro}` ); return magicString diff --git a/test/form/legacy/_config.js b/test/form/legacy/_config.js index 2bdf641..8ccc71d 100644 --- a/test/form/legacy/_config.js +++ b/test/form/legacy/_config.js @@ -1,6 +1,7 @@ module.exports = { - description: 'supports environments without Object.freeze', + description: 'supports environments without Object.freeze, Object.defined', options: { + moduleName: 'myBundle', legacy: true } }; diff --git a/test/form/legacy/_expected/amd.js b/test/form/legacy/_expected/amd.js index 3ba37d0..c1b004a 100644 --- a/test/form/legacy/_expected/amd.js +++ b/test/form/legacy/_expected/amd.js @@ -1,13 +1,20 @@ -define(function () { 'use strict'; +define(['exports'], function (exports) { 'use strict'; - const foo = 42; + const foo = 1; + const bar = 2; var namespace = (Object.freeze || Object)({ - foo: foo + foo: foo, + bar: bar }); - const x = 'foo'; - assert.equal( namespace[x], 42 ); + console.log( Object.keys( namespace ) ); + + const a = 1; + const b = 2; + + exports.a = a; + exports.b = b; }); diff --git a/test/form/legacy/_expected/cjs.js b/test/form/legacy/_expected/cjs.js index 33c3402..f904af3 100644 --- a/test/form/legacy/_expected/cjs.js +++ b/test/form/legacy/_expected/cjs.js @@ -1,11 +1,18 @@ 'use strict'; -const foo = 42; +const foo = 1; +const bar = 2; var namespace = (Object.freeze || Object)({ - foo: foo + foo: foo, + bar: bar }); -const x = 'foo'; -assert.equal( namespace[x], 42 ); +console.log( Object.keys( namespace ) ); + +const a = 1; +const b = 2; + +exports.a = a; +exports.b = b; diff --git a/test/form/legacy/_expected/es.js b/test/form/legacy/_expected/es.js index be10260..dab29e5 100644 --- a/test/form/legacy/_expected/es.js +++ b/test/form/legacy/_expected/es.js @@ -1,9 +1,15 @@ -const foo = 42; +const foo = 1; +const bar = 2; var namespace = (Object.freeze || Object)({ - foo: foo + foo: foo, + bar: bar }); -const x = 'foo'; -assert.equal( namespace[x], 42 ); +console.log( Object.keys( namespace ) ); + +const a = 1; +const b = 2; + +export { a, b }; diff --git a/test/form/legacy/_expected/iife.js b/test/form/legacy/_expected/iife.js index 095ddd0..19f8c91 100644 --- a/test/form/legacy/_expected/iife.js +++ b/test/form/legacy/_expected/iife.js @@ -1,14 +1,21 @@ -(function () { +(function (exports) { 'use strict'; - const foo = 42; + const foo = 1; + const bar = 2; var namespace = (Object.freeze || Object)({ - foo: foo + foo: foo, + bar: bar }); - const x = 'foo'; - assert.equal( namespace[x], 42 ); + console.log( Object.keys( namespace ) ); -}()); + const a = 1; + const b = 2; + + exports.a = a; + exports.b = b; + +}((this.myBundle = this.myBundle || {}))); diff --git a/test/form/legacy/_expected/umd.js b/test/form/legacy/_expected/umd.js index f3d0fa9..7c98779 100644 --- a/test/form/legacy/_expected/umd.js +++ b/test/form/legacy/_expected/umd.js @@ -1,17 +1,24 @@ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? factory() : - typeof define === 'function' && define.amd ? define(factory) : - (factory()); -}(this, (function () { 'use strict'; + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (factory((global.myBundle = global.myBundle || {}))); +}(this, (function (exports) { 'use strict'; - const foo = 42; + const foo = 1; + const bar = 2; var namespace = (Object.freeze || Object)({ - foo: foo + foo: foo, + bar: bar }); - const x = 'foo'; - assert.equal( namespace[x], 42 ); + console.log( Object.keys( namespace ) ); + + const a = 1; + const b = 2; + + exports.a = a; + exports.b = b; }))); diff --git a/test/form/legacy/main.js b/test/form/legacy/main.js index 321f9b3..d434187 100644 --- a/test/form/legacy/main.js +++ b/test/form/legacy/main.js @@ -1,4 +1,6 @@ import * as namespace from './namespace.js'; -const x = 'foo'; -assert.equal( namespace[x], 42 ); +console.log( Object.keys( namespace ) ); + +export const a = 1; +export const b = 2; diff --git a/test/form/legacy/namespace.js b/test/form/legacy/namespace.js index 9d7381d..e3a53f2 100644 --- a/test/form/legacy/namespace.js +++ b/test/form/legacy/namespace.js @@ -1 +1,2 @@ -export const foo = 42; +export const foo = 1; +export const bar = 2;