mirror of https://github.com/lukechilds/rollup.git
44 changed files with 330 additions and 86 deletions
@ -0,0 +1,12 @@ |
|||||
|
export default function getExportBlock ( bundle, exportMode, mechanism = 'return' ) { |
||||
|
if ( exportMode === 'default' ) { |
||||
|
return `${mechanism} ${bundle.entryModule.getCanonicalName('default')};`; |
||||
|
} |
||||
|
|
||||
|
return bundle.toExport |
||||
|
.map( name => { |
||||
|
const prop = name === 'default' ? `['default']` : `.${name}`; |
||||
|
return `exports${prop} = ${bundle.entryModule.getCanonicalName(name)};`; |
||||
|
}) |
||||
|
.join( '\n' ); |
||||
|
} |
@ -1,6 +1,12 @@ |
|||||
export default function getInteropBlock ( bundle ) { |
export default function getInteropBlock ( bundle ) { |
||||
return bundle.externalModules |
return bundle.externalModules |
||||
.filter( module => module.needsDefault && module.needsNamed ) |
.map( module => { |
||||
.map( module => `var ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` ) |
return module.needsDefault ? |
||||
|
( module.needsNamed ? |
||||
|
`var ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` : |
||||
|
`${module.name} = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` ) : |
||||
|
null; |
||||
|
}) |
||||
|
.filter( Boolean ) |
||||
.join( '\n' ); |
.join( '\n' ); |
||||
} |
} |
||||
|
@ -0,0 +1,7 @@ |
|||||
|
module.exports = { |
||||
|
description: 'adds a banner/footer', |
||||
|
options: { |
||||
|
banner: '/* this is a banner */', |
||||
|
footer: '/* this is a footer */' |
||||
|
} |
||||
|
}; |
@ -0,0 +1,7 @@ |
|||||
|
/* this is a banner */ |
||||
|
define(function () { 'use strict'; |
||||
|
|
||||
|
console.log( 'hello world' ); |
||||
|
|
||||
|
}); |
||||
|
/* this is a footer */ |
@ -0,0 +1,5 @@ |
|||||
|
/* this is a banner */ |
||||
|
'use strict'; |
||||
|
|
||||
|
console.log( 'hello world' ); |
||||
|
/* this is a footer */ |
@ -0,0 +1,3 @@ |
|||||
|
/* this is a banner */ |
||||
|
console.log( 'hello world' ); |
||||
|
/* this is a footer */ |
@ -0,0 +1,7 @@ |
|||||
|
/* this is a banner */ |
||||
|
(function () { 'use strict'; |
||||
|
|
||||
|
console.log( 'hello world' ); |
||||
|
|
||||
|
})(); |
||||
|
/* this is a footer */ |
@ -0,0 +1,11 @@ |
|||||
|
/* this is a banner */ |
||||
|
(function (global, factory) { |
||||
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
||||
|
typeof define === 'function' && define.amd ? define(factory) : |
||||
|
factory(); |
||||
|
}(this, function () { 'use strict'; |
||||
|
|
||||
|
console.log( 'hello world' ); |
||||
|
|
||||
|
})); |
||||
|
/* this is a footer */ |
@ -0,0 +1 @@ |
|||||
|
console.log( 'hello world' ); |
@ -0,0 +1,3 @@ |
|||||
|
module.exports = { |
||||
|
description: 'removes existing sourcemap comments' |
||||
|
}; |
@ -0,0 +1,9 @@ |
|||||
|
define(function () { 'use strict'; |
||||
|
|
||||
|
function foo () { |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
console.log( foo() ); |
||||
|
|
||||
|
}); |
@ -0,0 +1,7 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
function foo () { |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
console.log( foo() ); |
@ -0,0 +1,5 @@ |
|||||
|
function foo () { |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
console.log( foo() ); |
@ -0,0 +1,9 @@ |
|||||
|
(function () { 'use strict'; |
||||
|
|
||||
|
function foo () { |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
console.log( foo() ); |
||||
|
|
||||
|
})(); |
@ -0,0 +1,13 @@ |
|||||
|
(function (global, factory) { |
||||
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
||||
|
typeof define === 'function' && define.amd ? define(factory) : |
||||
|
factory(); |
||||
|
}(this, function () { 'use strict'; |
||||
|
|
||||
|
function foo () { |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
console.log( foo() ); |
||||
|
|
||||
|
})); |
@ -0,0 +1,5 @@ |
|||||
|
export default function () { |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
//# sourceMappingURL=foo.js.map
|
@ -0,0 +1,5 @@ |
|||||
|
import foo from './foo'; |
||||
|
|
||||
|
console.log( foo() ); |
||||
|
|
||||
|
//# sourceMappingURL=main.js.map
|
@ -0,0 +1,10 @@ |
|||||
|
var assert = require( 'assert' ); |
||||
|
|
||||
|
module.exports = { |
||||
|
description: 'renames variables named `exports` if necessary', |
||||
|
exports: function ( exports ) { |
||||
|
assert.deepEqual( Object.keys( exports ), [ 'a', 'b' ] ); |
||||
|
assert.equal( exports.a, 'A' ); |
||||
|
assert.equal( exports.b, 42 ); |
||||
|
} |
||||
|
}; |
@ -0,0 +1,8 @@ |
|||||
|
var exports = { |
||||
|
number: 21 |
||||
|
}; |
||||
|
|
||||
|
export var a = 'A'; |
||||
|
export var b = exports.number * 2; |
||||
|
|
||||
|
assert.deepEqual( Object.keys( exports ), [ 'number' ]); |
@ -0,0 +1,13 @@ |
|||||
|
module.exports = { |
||||
|
description: 'deconflicts external imports', |
||||
|
context: { |
||||
|
require: function ( id ) { |
||||
|
return function () { |
||||
|
return id; |
||||
|
}; |
||||
|
} |
||||
|
}, |
||||
|
options: { |
||||
|
external: [ 'foo', 'bar' ] |
||||
|
} |
||||
|
}; |
@ -0,0 +1,5 @@ |
|||||
|
import foo from 'foo'; |
||||
|
|
||||
|
export default function () { |
||||
|
assert.equal( foo(), 'foo' ); |
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
import foo from 'bar'; |
||||
|
|
||||
|
export default function () { |
||||
|
assert.equal( foo(), 'bar' ); |
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
import a from './a'; |
||||
|
import b from './b'; |
||||
|
|
||||
|
a(); |
||||
|
b(); |
@ -0,0 +1,10 @@ |
|||||
|
var assert = require( 'assert' ); |
||||
|
|
||||
|
module.exports = { |
||||
|
description: 'allows export and import reference to share name', |
||||
|
exports: function ( exports ) { |
||||
|
assert.equal( exports.b, 9 ); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
// adapted from es6-module-transpiler
|
@ -0,0 +1,2 @@ |
|||||
|
export var a = 1; |
||||
|
assert.equal(a, 1); |
@ -0,0 +1,13 @@ |
|||||
|
import { a } from './foo'; |
||||
|
|
||||
|
// This variable declaration is going to be altered because `b` needs to be
|
||||
|
// re-written. We need to make sure that the `a` re-writing and the unaffected
|
||||
|
// `c` declarator are not being clobbered by that alteration.
|
||||
|
var a_ = a, b = 9, c = 'c'; |
||||
|
|
||||
|
assert.equal(a, 1); |
||||
|
assert.equal(a_, 1); |
||||
|
assert.equal(b, 9); |
||||
|
assert.equal(c, 'c'); |
||||
|
|
||||
|
export { b }; |
@ -0,0 +1,3 @@ |
|||||
|
module.exports = { |
||||
|
description: 'correctly exports x as y inside a bundle' |
||||
|
}; |
@ -0,0 +1,3 @@ |
|||||
|
var x = 42; |
||||
|
|
||||
|
export { x as y }; |
@ -0,0 +1,3 @@ |
|||||
|
import * as foo from './foo'; |
||||
|
|
||||
|
assert.equal( foo.y, 42 ); |
@ -0,0 +1,34 @@ |
|||||
|
var acorn = require( 'acorn' ); |
||||
|
|
||||
|
var modules = { |
||||
|
'main': 'import foo from \'foo\';\nfoo();', |
||||
|
|
||||
|
// the code points to './bar' but the AST points to './baz', so we
|
||||
|
// can check the AST is being used
|
||||
|
'foo': { |
||||
|
code: 'import bar from \'bar\';\nexport default function foo () {\n\tconsole.log( bar );\n}', |
||||
|
ast: acorn.parse( 'import bar from \'baz\';\nexport default function foo () {\n\tconsole.log( bar );\n}', { |
||||
|
ecmaVersion: 6, |
||||
|
sourceType: 'module' |
||||
|
}) |
||||
|
}, |
||||
|
|
||||
|
'baz': 'export default 42;' |
||||
|
}; |
||||
|
|
||||
|
module.exports = { |
||||
|
description: 'uses supplied AST', |
||||
|
options: { |
||||
|
resolveId: function ( importee, importer ) { |
||||
|
if ( !importer ) return 'main'; |
||||
|
return importee; |
||||
|
}, |
||||
|
load: function ( id ) { |
||||
|
if ( id === 'bar' ) { |
||||
|
throw new Error( 'loaded incorrect module' ); |
||||
|
} |
||||
|
|
||||
|
return modules[ id ]; |
||||
|
} |
||||
|
} |
||||
|
}; |
Loading…
Reference in new issue