Browse Source

iife finaliser

contingency-plan
Rich-Harris 10 years ago
parent
commit
68f69faadc
  1. 2
      README.md
  2. 11
      src/finalisers/iife.js
  3. 3
      src/finalisers/index.js
  4. 5
      test/form/no-imports-or-exports/_expected/iife.js
  5. 41
      test/test.js

2
README.md

@ -102,7 +102,7 @@ rollup.rollup( 'app.js', {
}).then( function ( bundle ) { }).then( function ( bundle ) {
// generate code and a sourcemap // generate code and a sourcemap
const { code, map } = bundle.generate({ const { code, map } = bundle.generate({
// output format - 'amd', 'cjs', 'es6', 'umd' // output format - 'amd', 'cjs', 'es6', 'iife', 'umd'
format: 'amd', format: 'amd',
// exports - 'auto', 'none', 'default', 'named' // exports - 'auto', 'none', 'default', 'named'

11
src/finalisers/iife.js

@ -0,0 +1,11 @@
export default function es6 ( bundle, magicString, exportMode, options ) {
const intro = `(function () { 'use strict';\n\n`;
const outro = `\n\n})();`;
return magicString
.trim()
.indent()
.prepend( intro )
.append( outro );
}

3
src/finalisers/index.js

@ -1,6 +1,7 @@
import amd from './amd'; import amd from './amd';
import cjs from './cjs'; import cjs from './cjs';
import es6 from './es6'; import es6 from './es6';
import iife from './iife';
import umd from './umd'; import umd from './umd';
export default { amd, cjs, es6, umd }; export default { amd, cjs, es6, iife, umd };

5
test/form/no-imports-or-exports/_expected/iife.js

@ -0,0 +1,5 @@
(function () { 'use strict';
console.log( 'this is it' );
})();

41
test/test.js

@ -125,36 +125,39 @@ describe( 'rollup', function () {
{ format: 'amd' }, { format: 'amd' },
{ format: 'cjs' }, { format: 'cjs' },
{ format: 'es6' }, { format: 'es6' },
{ format: 'iife' },
{ format: 'umd' } { format: 'umd' }
]; ];
sander.readdirSync( FORM ).sort().forEach( function ( dir ) { sander.readdirSync( FORM ).sort().forEach( function ( dir ) {
if ( dir[0] === '.' ) return; // .DS_Store... if ( dir[0] === '.' ) return; // .DS_Store...
var config; describe( dir, function () {
var config;
try { try {
config = require( FORM + '/' + dir + '/_config' ); config = require( FORM + '/' + dir + '/_config' );
} catch ( err ) { } catch ( err ) {
config = { description: dir }; config = { description: dir };
} }
var bundlePromise = rollup.rollup( FORM + '/' + dir + '/main.js', extend( {}, config.options ) ); var bundlePromise = rollup.rollup( FORM + '/' + dir + '/main.js', extend( {}, config.options ) );
profiles.forEach( function ( profile ) { profiles.forEach( function ( profile ) {
( config.skip ? it.skip : config.solo ? it.only : it )( dir + ' (' + profile.format + ')', function () { ( config.skip ? it.skip : config.solo ? it.only : it )( 'generates ' + profile.format, function () {
return bundlePromise.then( function ( bundle ) { return bundlePromise.then( function ( bundle ) {
var actual = bundle.generate({ var actual = bundle.generate({
format: profile.format format: profile.format
}).code.trim(); }).code.trim();
try { try {
var expected = sander.readFileSync( FORM, dir, '_expected', profile.format + '.js' ).toString().trim(); var expected = sander.readFileSync( FORM, dir, '_expected', profile.format + '.js' ).toString().trim();
} catch ( err ) { } catch ( err ) {
assert.equal( actual, 'missing file' ); assert.equal( actual, 'missing file' );
} }
assert.equal( actual, expected ); assert.equal( actual, expected );
});
}); });
}); });
}); });

Loading…
Cancel
Save