This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
index.js
91 lines (71 loc) · 3.01 KB
/
index.js
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
import { dirname } from 'path';
import { buildExternalHelpers, transform } from 'babel-core';
import { createFilter } from 'rollup-pluginutils';
import preflightCheck from './preflightCheck.js';
import { assign, warnOnce } from './utils.js';
import { RUNTIME, BUNDLED, HELPERS } from './constants.js';
const keywordHelpers = [ 'typeof', 'extends', 'instanceof' ];
export default function babel ( options ) {
options = assign( {}, options || {} );
let inlineHelpers = {};
const filter = createFilter( options.include, options.exclude );
delete options.include;
delete options.exclude;
if ( options.sourceMap !== false ) options.sourceMaps = true;
if ( options.sourceMaps !== false ) options.sourceMaps = true;
delete options.sourceMap;
const runtimeHelpers = options.runtimeHelpers;
delete options.runtimeHelpers;
let externalHelpers;
if ( options.externalHelpers ) externalHelpers = true;
delete options.externalHelpers;
let externalHelpersWhitelist = null;
if ( options.externalHelpersWhitelist ) externalHelpersWhitelist = options.externalHelpersWhitelist;
delete options.externalHelpersWhitelist;
let warn = msg => console.warn(msg); // eslint-disable-line no-console
return {
name: 'babel',
options ( options ) {
warn = options.onwarn || warn;
},
resolveId ( id ) {
if ( id === HELPERS ) return id;
},
load ( id ) {
if ( id === HELPERS ) {
const pattern = new RegExp( `babelHelpers\\.(${keywordHelpers.join('|')})`, 'g' );
const helpers = buildExternalHelpers( externalHelpersWhitelist, 'var' )
.replace( pattern, 'var _$1' )
.replace( /^babelHelpers\./gm, 'export var ' ) +
`\n\nexport { ${keywordHelpers.map( word => `_${word} as ${word}`).join( ', ')} }`;
return helpers;
}
},
transform ( code, id ) {
if ( !filter( id ) ) return null;
if ( id === HELPERS ) return null;
const helpers = preflightCheck( options, dirname( id ) );
const localOpts = assign({ filename: id }, options );
const transformed = transform( code, localOpts );
const { usedHelpers } = transformed.metadata;
if ( usedHelpers.length ) {
if ( helpers === BUNDLED ) {
if ( !externalHelpers ) transformed.code += `\n\nimport * as babelHelpers from '${HELPERS}';`;
} else if ( helpers === RUNTIME && !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( 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
};
}
};
}