Skip to content

Commit

Permalink
Merge pull request #1365 from rollup/options.amd
Browse files Browse the repository at this point in the history
options.amd
  • Loading branch information
Rich-Harris committed Jun 3, 2017
2 parents ffcc885 + 4d78bba commit b661e7e
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/finalisers/amd.js
Expand Up @@ -14,12 +14,15 @@ export default function amd ( bundle, magicString, { exportMode, indentString, i
deps.unshift( `'exports'` );
}

const amdOptions = options.amd || {};

const params =
( options.moduleId ? `'${options.moduleId}', ` : `` ) +
( amdOptions.id ? `'${amdOptions.id}', ` : `` ) +
( deps.length ? `[${deps.join( ', ' )}], ` : `` );

const useStrict = options.useStrict !== false ? ` 'use strict';` : ``;
const wrapperStart = `define(${params}function (${args.join( ', ' )}) {${useStrict}\n\n`;
const define = amdOptions.define || 'define';
const wrapperStart = `${define}(${params}function (${args.join( ', ' )}) {${useStrict}\n\n`;

// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock( bundle, options );
Expand Down
8 changes: 6 additions & 2 deletions src/finalisers/umd.js
Expand Up @@ -54,10 +54,14 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i
args.unshift( 'exports' );
}

const amdOptions = options.amd || {};

const amdParams =
( options.moduleId ? `'${options.moduleId}', ` : `` ) +
( amdOptions.id ? `'${amdOptions.id}', ` : `` ) +
( amdDeps.length ? `[${amdDeps.join( ', ' )}], ` : `` );

const define = amdOptions.define || 'define';

const cjsExport = exportMode === 'default' ? `module.exports = ` : ``;
const defaultExport = exportMode === 'default' ? `${setupNamespace(options.moduleName)} = ` : '';

Expand All @@ -74,7 +78,7 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i
const wrapperIntro =
`(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? ${cjsExport}factory(${cjsDeps.join( ', ' )}) :
typeof define === 'function' && define.amd ? define(${amdParams}factory) :
typeof ${define} === 'function' && ${define}.amd ? ${define}(${amdParams}factory) :
${globalExport};
}(this, (function (${args}) {${useStrict}
Expand Down
16 changes: 15 additions & 1 deletion src/rollup.js
Expand Up @@ -12,6 +12,7 @@ export const VERSION = '<@VERSION@>';

const ALLOWED_KEYS = [
'acorn',
'amd',
'banner',
'cache',
'context',
Expand All @@ -27,7 +28,6 @@ const ALLOWED_KEYS = [
'intro',
'legacy',
'moduleContext',
'moduleId',
'moduleName',
'noConflict',
'onwarn',
Expand All @@ -51,6 +51,20 @@ function checkOptions ( options ) {
return new Error( 'The `transform`, `load`, `resolveId` and `resolveExternal` options are deprecated in favour of a unified plugin API. See https://github.com/rollup/rollup/wiki/Plugins for details' );
}

if ( options.moduleId ) {
if ( options.amd ) throw new Error( 'Cannot have both options.amd and options.moduleId' );

options.amd = { id: options.moduleId };
delete options.moduleId;

const msg = `options.moduleId is deprecated in favour of options.amd = { id: moduleId }`;
if ( options.onwarn ) {
options.onwarn( msg );
} else {
console.warn( msg ); // eslint-disable-line no-console
}
}

const err = validateKeys( keys(options), ALLOWED_KEYS );
if ( err ) return err;

Expand Down
8 changes: 8 additions & 0 deletions test/form/define-replacement/_config.js
@@ -0,0 +1,8 @@
var path = require('path');

module.exports = {
description: 'amd.define',
options: {
amd: { define: 'enifed' }
}
};
10 changes: 10 additions & 0 deletions test/form/define-replacement/_expected/amd.js
@@ -0,0 +1,10 @@
enifed(function () { 'use strict';

var a = () => {
console.log('props');
};

a();
a();

});
8 changes: 8 additions & 0 deletions test/form/define-replacement/_expected/cjs.js
@@ -0,0 +1,8 @@
'use strict';

var a = () => {
console.log('props');
};

a();
a();
6 changes: 6 additions & 0 deletions test/form/define-replacement/_expected/es.js
@@ -0,0 +1,6 @@
var a = () => {
console.log('props');
};

a();
a();
11 changes: 11 additions & 0 deletions test/form/define-replacement/_expected/iife.js
@@ -0,0 +1,11 @@
(function () {
'use strict';

var a = () => {
console.log('props');
};

a();
a();

}());
14 changes: 14 additions & 0 deletions test/form/define-replacement/_expected/umd.js
@@ -0,0 +1,14 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof enifed === 'function' && enifed.amd ? enifed(factory) :
(factory());
}(this, (function () { 'use strict';

var a = () => {
console.log('props');
};

a();
a();

})));
3 changes: 3 additions & 0 deletions test/form/define-replacement/a.js
@@ -0,0 +1,3 @@
export var a = () => {
console.log('props');
};
4 changes: 4 additions & 0 deletions test/form/define-replacement/main.js
@@ -0,0 +1,4 @@
import { a } from "./a.js";
import { a as b } from "./a.js";
a();
b();
2 changes: 1 addition & 1 deletion test/test.js
Expand Up @@ -132,7 +132,7 @@ describe( 'rollup', function () {
return rollup.rollup({ entry: 'x', plUgins: [] }).then( () => {
throw new Error( 'Missing expected error' );
}, err => {
assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, banner, cache, context, dest, entry, exports, external, footer, format, globals, indent, interop, intro, legacy, moduleContext, moduleId, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, sourceMap, sourceMapFile, targets, treeshake, useStrict' );
assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, amd, banner, cache, context, dest, entry, exports, external, footer, format, globals, indent, interop, intro, legacy, moduleContext, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, sourceMap, sourceMapFile, targets, treeshake, useStrict' );
});
});

Expand Down

0 comments on commit b661e7e

Please sign in to comment.