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
105 lines (90 loc) · 2.93 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { dirname } from 'path';
import { buildExternalHelpers, DEFAULT_EXTENSIONS, loadPartialConfig, transform } from '@babel/core';
import { createFilter } from 'rollup-pluginutils';
import createPreflightCheck from './preflightCheck.js';
import helperPlugin from './helperPlugin.js';
import { escapeRegExpCharacters, warnOnce } from './utils.js';
import { RUNTIME, EXTERNAL, HELPERS } from './constants.js';
const unpackOptions = ({
extensions = DEFAULT_EXTENSIONS,
// rollup uses sourcemap, babel uses sourceMaps
// just normalize them here so people don't have to worry about it
sourcemap = true,
sourcemaps = true,
sourceMap = true,
sourceMaps = true,
...rest
} = {}) => ({
extensions,
plugins: [],
sourceMaps: sourcemap && sourcemaps && sourceMap && sourceMaps,
...rest,
caller: {
name: 'rollup-plugin-babel',
supportsStaticESM: true,
supportsDynamicImport: true,
...rest.caller,
}
});
export default function babel ( options ) {
// TODO: remove it later, just provide a helpful warning to people for now
try {
loadPartialConfig({
caller: undefined,
babelrc: false,
configFile: false,
});
} catch (err) {
throw new Error('You should be using @babel/core@^7.0.0-rc.2. Please upgrade or pin rollup-plugin-babel to 4.0.0-beta.8');
}
const {
exclude,
extensions,
externalHelpers,
externalHelpersWhitelist,
include,
runtimeHelpers,
...babelOptions
} = unpackOptions(options);
const extensionRegExp = new RegExp(`(${extensions.map(escapeRegExpCharacters).join('|')})$`);
const includeExcludeFilter = createFilter( include, exclude );
const filter = id => extensionRegExp.test(id) && includeExcludeFilter(id);
const preflightCheck = createPreflightCheck();
return {
name: 'babel',
resolveId ( id ) {
if ( id === HELPERS ) return id;
},
load ( id ) {
if ( id !== HELPERS ) {
return;
}
return buildExternalHelpers( externalHelpersWhitelist, 'module' );
},
transform ( code, id ) {
if ( !filter( id ) ) return null;
if ( id === HELPERS ) return null;
const helpers = preflightCheck( this, babelOptions, dirname( id ) );
if ( helpers === EXTERNAL && !externalHelpers ) {
warnOnce( this, 'Using "external-helpers" plugin with rollup-plugin-babel is deprecated, as it now automatically deduplicates your Babel helpers.' );
} else if ( helpers === RUNTIME && !runtimeHelpers ) {
this.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' );
}
const localOpts = {
filename: id,
...babelOptions,
plugins: helpers !== RUNTIME
? [...babelOptions.plugins, helperPlugin]
: babelOptions.plugins,
};
const transformed = transform( code, localOpts );
if (!transformed) {
return { code };
}
return {
code: transformed.code,
map: transformed.map
};
}
};
}