1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
var assert = require( 'assert' );
var path = require( 'path' );
var rollup = require( 'rollup' );
var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;
var babelPlugin = require( '..' );
require( 'source-map-support' ).install();
process.chdir( __dirname );
function getLocation ( source, charIndex ) {
var lines = source.split( '\n' );
var len = lines.length;
var lineStart = 0;
var i;
for ( i = 0; i < len; i += 1 ) {
var line = lines[i];
var lineEnd = lineStart + line.length + 1; // +1 for newline
if ( lineEnd > charIndex ) {
return { line: i + 1, column: charIndex - lineStart };
}
lineStart = lineEnd;
}
throw new Error( 'Could not determine location of character' );
}
const consoleWarn = console.warn;
describe( 'rollup-plugin-babel', function () {
this.timeout( 15000 );
it( 'runs code through babel', function () {
return rollup.rollup({
entry: 'samples/basic/main.js',
plugins: [ babelPlugin() ]
}).then( function ( bundle ) {
var generated = bundle.generate();
var code = generated.code;
assert.ok( code.indexOf( 'const' ) === -1, code );
});
});
it( 'adds helpers', function () {
return rollup.rollup({
entry: 'samples/class/main.js',
plugins: [ babelPlugin() ]
}).then( function ( bundle ) {
var generated = bundle.generate();
var code = generated.code;
// TODO not the greatest test... inline helpers are prefixed with
// an underscore for whatever reason
assert.ok( code.indexOf( 'var classCallCheck =' ) !== -1, generated.code );
assert.ok( code.indexOf( 'var _classCallCheck =' ) === -1, generated.code );
});
});
it( 'does not add helpers unnecessarily', function () {
return rollup.rollup({
entry: 'samples/basic/main.js',
plugins: [ babelPlugin() ]
}).then( function ( bundle ) {
var generated = bundle.generate();
var code = generated.code;
assert.ok( code.indexOf( 'babelHelpers' ) === -1, generated.code );
});
});
it( 'does not add helpers when externalHelpers option is truthy', function () {
return rollup.rollup({
entry: 'samples/class/main.js',
plugins: [ babelPlugin({externalHelpers: true}) ]
}).then( function ( bundle ) {
var generated = bundle.generate();
var code = generated.code;
assert.ok( code.indexOf( 'babelHelpers =' ) === -1, generated.code );
assert.ok( code.indexOf( 'babelHelpers.classCallCheck =' ) === -1, generated.code );
});
});
it( 'does not babelify excluded code', function () {
return rollup.rollup({
entry: 'samples/exclusions/main.js',
plugins: [
babelPlugin({ exclude: '**/foo.js' })
]
}).then( function ( bundle ) {
var generated = bundle.generate();
var code = generated.code;
assert.ok( code.indexOf( '${foo()}' ) === -1, generated.code );
assert.ok( code.indexOf( '=> 42' ) !== -1, generated.code );
});
});
it( 'generates sourcemap by default', function () {
return rollup.rollup({
entry: 'samples/class/main.js',
plugins: [ babelPlugin() ]
}).then( function ( bundle ) {
var target = 'log';
var generated = bundle.generate({ sourceMap: true });
var smc = new SourceMapConsumer( generated.map );
var loc = getLocation( generated.code, generated.code.indexOf( target ) );
var original = smc.originalPositionFor( loc );
assert.deepEqual( original, {
source: path.resolve( 'samples/class/main.js' ).split( path.sep ).join( '/' ),
line: 3,
column: 10,
name: target
});
});
});
it( 'works with transform-decorators (#18)', function () {
return rollup.rollup({
entry: 'samples/transform-decorators/main.js',
plugins: [ babelPlugin() ]
});
});
it( 'checks config per-file', function () {
return rollup.rollup({
entry: 'samples/checks/main.js',
plugins: [ babelPlugin() ]
})
.then( function () {
assert.ok( false, 'promise should not fulfil' );
})
.catch( function ( err ) {
assert.ok( /configuring-babel/.test( err.message ), 'Expected an error about external helpers or module transform, got "' + err.message + '"' );
});
});
it( 'allows transform-runtime to be used instead of bundled helpers', function () {
return rollup.rollup({
entry: 'samples/runtime-helpers/main.js',
plugins: [ babelPlugin({ runtimeHelpers: true }) ],
onwarn: function ( msg ) {
assert.equal( msg, 'Treating \'babel-runtime/helpers/classCallCheck\' as external dependency' );
}
}).then( function ( bundle ) {
var cjs = bundle.generate({ format: 'cjs' }).code;
assert.ok( !~cjs.indexOf( 'babelHelpers' ) );
});
});
it( 'allows transform-runtime to be used with custom moduleName', function () {
return rollup.rollup({
entry: 'samples/runtime-helpers-custom-name/main.js',
plugins: [
babelPlugin({ runtimeHelpers: true })
],
onwarn: function ( msg ) {
assert.equal( msg, 'Treating \'custom-name/helpers/classCallCheck\' as external dependency' );
}
}).then( function ( bundle ) {
var cjs = bundle.generate({ format: 'cjs' }).code;
assert.ok( !~cjs.indexOf( 'babelHelpers' ) );
});
});
it( 'correctly renames helpers (#22)', () => {
return rollup.rollup({
entry: 'samples/named-function-helper/main.js',
plugins: [ babelPlugin() ],
onwarn: function ( msg ) {
assert.equal( msg, 'Treating \'babel-runtime/helpers/classCallCheck\' as external dependency' );
}
}).then( function ( bundle ) {
var cjs = bundle.generate({ format: 'cjs' }).code;
assert.ok( !~cjs.indexOf( 'babelHelpers_get get' ), 'helper was incorrectly renamed' );
});
});
it( 'runs preflight check correctly in absence of class transformer (#23)', () => {
return rollup.rollup({
entry: 'samples/no-class-transformer/main.js',
plugins: [ babelPlugin() ]
});
});
it( 'warns on duplicated helpers', () => {
let messages = [];
return rollup.rollup({
entry: 'samples/duplicated-helpers-warning/main.js',
plugins: [ babelPlugin() ],
onwarn: msg => messages.push( msg )
}).then( () => {
assert.deepEqual( messages, [
'The \'classCallCheck\' Babel helper is used more than once in your code. It\'s strongly recommended that you use the "external-helpers" plugin or the "es2015-rollup" preset. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information'
]);
});
});
it( 'does not warn on duplicated helpers if correctly configured', () => {
let messages = [];
console.warn = msg => messages.push( msg );
return rollup.rollup({
entry: 'samples/duplicated-helpers-no-warning/main.js',
plugins: [ babelPlugin() ]
}).then( () => {
console.warn = consoleWarn;
assert.deepEqual( messages, []);
});
});
it( 'produces valid code with typeof helper', () => {
return rollup.rollup({
entry: 'samples/typeof/main.js',
plugins: [ babelPlugin() ]
}).then( bundle => {
var generated = bundle.generate();
assert.equal( generated.code.indexOf( 'var typeof' ), -1, generated.code );
});
});
it( 'does not warn about duplicated helpers with transform-runtime', () => {
return rollup.rollup({
entry: 'samples/runtime-helpers-duplicated/main.js',
plugins: [
babelPlugin({
runtimeHelpers: true
})
],
onwarn ( msg ) {
if ( /helper is used more than once in your code/.test( msg ) ) {
throw new Error( 'no warning about duplicated helpers should be given' );
}
}
});
});
});
|