suyiScreen/node_modules/rollup-plugin-babel/dist/rollup-plugin-babel.cjs.js

178 lines
5.5 KiB
JavaScript

'use strict';
var path = require('path');
var babelCore = require('babel-core');
var rollupPluginutils = require('rollup-pluginutils');
var INLINE = {};
var RUNTIME = {};
var BUNDLED = {};
var HELPERS = '\0babelHelpers';
function importHelperPlugin (ref) {
var t = ref.types;
/**
* This function is needed because of a bug in Babel 6.x, which prevents the
* declaration of an ExportDefaultDeclaration to be replaced with an
* expression.
* That bug has been fixed in Babel 7.
*/
function replaceWith (path$$1, replacement) {
if (
path$$1.parentPath.isExportDefaultDeclaration() &&
t.isExpression(replacement)
) {
path$$1.parentPath.replaceWith(t.exportDefaultDeclaration(replacement));
} else {
path$$1.replaceWith(replacement);
}
}
return {
visitor: {
ClassDeclaration: function ClassDeclaration (path$$1, state) {
replaceWith(path$$1, state.file.addHelper('classCallCheck'));
}
}
};
}
function createPreflightCheck () {
var preflightCheckResults = {};
return function ( options, dir ) {
if ( !preflightCheckResults[ dir ] ) {
var helpers;
options = Object.assign( {}, options );
delete options.only;
delete options.ignore;
options.filename = path.join( dir, 'x.js' );
options.plugins = [ importHelperPlugin ].concat(options.plugins || []);
var check = babelCore.transform( 'export default class Foo {}', options ).code;
if ( !~check.indexOf( 'export default' ) && !~check.indexOf( 'export { Foo as default }' ) ) { throw new Error( 'It looks like your Babel configuration specifies a module transformer. Please disable it. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information' ); }
if ( ~check.indexOf( 'import _classCallCheck from' ) ) { helpers = RUNTIME; }
else if ( ~check.indexOf( 'function _classCallCheck' ) ) { helpers = INLINE; }
else if ( ~check.indexOf( 'babelHelpers' ) ) { helpers = BUNDLED; }
else {
throw new Error( 'An unexpected situation arose. Please raise an issue at https://github.com/rollup/rollup-plugin-babel/issues. Thanks!' );
}
preflightCheckResults[ dir ] = helpers;
}
return preflightCheckResults[ dir ];
};
}
var warned = {};
function warnOnce ( warn, msg ) {
if ( warned[ msg ] ) { return; }
warned[ msg ] = true;
warn( msg );
}
var keywordHelpers = [ 'typeof', 'extends', 'instanceof' ];
function babel ( options ) {
options = Object.assign( {}, options || {} );
var inlineHelpers = {};
var filter = rollupPluginutils.createFilter( options.include, options.exclude );
var preflightCheck = createPreflightCheck();
delete options.include;
delete options.exclude;
if ( options.sourceMap !== false ) { options.sourceMaps = true; }
if ( options.sourceMaps !== false ) { options.sourceMaps = true; }
delete options.sourceMap;
var runtimeHelpers = options.runtimeHelpers;
delete options.runtimeHelpers;
var externalHelpers;
if ( options.externalHelpers ) { externalHelpers = true; }
delete options.externalHelpers;
var externalHelpersWhitelist = null;
if ( options.externalHelpersWhitelist ) { externalHelpersWhitelist = options.externalHelpersWhitelist; }
delete options.externalHelpersWhitelist;
var warn = function (msg) { return console.warn(msg); }; // eslint-disable-line no-console
return {
name: 'babel',
options: function options$1 ( options ) {
warn = options.onwarn || warn;
},
resolveId: function resolveId ( id ) {
if ( id === HELPERS ) { return id; }
},
load: function load ( id ) {
if ( id === HELPERS ) {
var pattern = new RegExp( ("babelHelpers\\.(" + (keywordHelpers.join('|')) + ")"), 'g' );
var helpers = babelCore.buildExternalHelpers( externalHelpersWhitelist, 'var' )
.replace(/^var babelHelpers = \{\};\n/gm, '')
.replace(/\nbabelHelpers;$/gm, '')
.replace( pattern, 'var _$1' )
.replace( /^babelHelpers\./gm, 'export var ' ) +
"\n\nexport { " + (keywordHelpers.map( function (word) { return ("_" + word + " as " + word); }).join( ', ')) + " }";
return helpers;
}
},
transform: function transform$1 ( code, id ) {
if ( !filter( id ) ) { return null; }
if ( id === HELPERS ) { return null; }
var helpers = preflightCheck( options, path.dirname( id ) );
var localOpts = Object.assign({ filename: id }, options );
var transformed = babelCore.transform( code, localOpts );
var ref = transformed.metadata;
var usedHelpers = ref.usedHelpers;
if ( usedHelpers.length ) {
if ( helpers === BUNDLED ) {
if ( !externalHelpers ) {
transformed.code += "\n\nimport * as babelHelpers from '" + HELPERS + "';";
}
} else if ( helpers === RUNTIME ) {
if ( !runtimeHelpers ) {
throw new Error( 'Runtime helpers are not enabled. Either exclude the transform-runtime Babel plugin or pass the `runtimeHelpers: true` option. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information' );
}
} else {
usedHelpers.forEach( function (helper) {
if ( inlineHelpers[ helper ] ) {
warnOnce( warn, ("The '" + helper + "' 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") );
}
inlineHelpers[ helper ] = true;
});
}
}
return {
code: transformed.code,
map: transformed.map
};
}
};
}
module.exports = babel;
//# sourceMappingURL=rollup-plugin-babel.cjs.js.map