index.es.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. import * as babel from '@babel/core';
  2. import { createFilter } from '@rollup/pluginutils';
  3. import { addNamed } from '@babel/helper-module-imports';
  4. function _defineProperty(obj, key, value) {
  5. if (key in obj) {
  6. Object.defineProperty(obj, key, {
  7. value: value,
  8. enumerable: true,
  9. configurable: true,
  10. writable: true
  11. });
  12. } else {
  13. obj[key] = value;
  14. }
  15. return obj;
  16. }
  17. function ownKeys(object, enumerableOnly) {
  18. var keys = Object.keys(object);
  19. if (Object.getOwnPropertySymbols) {
  20. var symbols = Object.getOwnPropertySymbols(object);
  21. if (enumerableOnly) symbols = symbols.filter(function (sym) {
  22. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  23. });
  24. keys.push.apply(keys, symbols);
  25. }
  26. return keys;
  27. }
  28. function _objectSpread2(target) {
  29. for (var i = 1; i < arguments.length; i++) {
  30. var source = arguments[i] != null ? arguments[i] : {};
  31. if (i % 2) {
  32. ownKeys(Object(source), true).forEach(function (key) {
  33. _defineProperty(target, key, source[key]);
  34. });
  35. } else if (Object.getOwnPropertyDescriptors) {
  36. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  37. } else {
  38. ownKeys(Object(source)).forEach(function (key) {
  39. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  40. });
  41. }
  42. }
  43. return target;
  44. }
  45. function _objectWithoutPropertiesLoose(source, excluded) {
  46. if (source == null) return {};
  47. var target = {};
  48. var sourceKeys = Object.keys(source);
  49. var key, i;
  50. for (i = 0; i < sourceKeys.length; i++) {
  51. key = sourceKeys[i];
  52. if (excluded.indexOf(key) >= 0) continue;
  53. target[key] = source[key];
  54. }
  55. return target;
  56. }
  57. function _objectWithoutProperties(source, excluded) {
  58. if (source == null) return {};
  59. var target = _objectWithoutPropertiesLoose(source, excluded);
  60. var key, i;
  61. if (Object.getOwnPropertySymbols) {
  62. var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
  63. for (i = 0; i < sourceSymbolKeys.length; i++) {
  64. key = sourceSymbolKeys[i];
  65. if (excluded.indexOf(key) >= 0) continue;
  66. if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
  67. target[key] = source[key];
  68. }
  69. }
  70. return target;
  71. }
  72. const BUNDLED = 'bundled';
  73. const INLINE = 'inline';
  74. const RUNTIME = 'runtime';
  75. const EXTERNAL = 'external'; // NOTE: DO NOT REMOVE the null character `\0` as it may be used by other plugins
  76. // e.g. https://github.com/rollup/rollup-plugin-node-resolve/blob/313a3e32f432f9eb18cc4c231cc7aac6df317a51/src/index.js#L74
  77. const HELPERS = '\0rollupPluginBabelHelpers.js';
  78. function importHelperPlugin({
  79. types: t
  80. }) {
  81. return {
  82. pre(file) {
  83. const cachedHelpers = {};
  84. file.set('helperGenerator', name => {
  85. if (!file.availableHelper(name)) {
  86. return null;
  87. }
  88. if (cachedHelpers[name]) {
  89. return t.cloneNode(cachedHelpers[name]);
  90. }
  91. return cachedHelpers[name] = addNamed(file.path, name, HELPERS);
  92. });
  93. }
  94. };
  95. }
  96. const addBabelPlugin = (options, plugin) => {
  97. return _objectSpread2(_objectSpread2({}, options), {}, {
  98. plugins: options.plugins.concat(plugin)
  99. });
  100. };
  101. const warned = {};
  102. function warnOnce(ctx, msg) {
  103. if (warned[msg]) return;
  104. warned[msg] = true;
  105. ctx.warn(msg);
  106. }
  107. const regExpCharactersRegExp = /[\\^$.*+?()[\]{}|]/g;
  108. const escapeRegExpCharacters = str => str.replace(regExpCharactersRegExp, '\\$&');
  109. function stripQuery(id) {
  110. // strip query params from import
  111. const [bareId, query] = id.split('?');
  112. const suffix = `${query ? `?${query}` : ''}`;
  113. return {
  114. bareId,
  115. query,
  116. suffix
  117. };
  118. }
  119. const MODULE_ERROR = 'Rollup requires that your Babel configuration keeps ES6 module syntax intact. ' + 'Unfortunately it looks like your configuration specifies a module transformer ' + 'to replace ES6 modules with another module format. To continue you have to disable it.' + '\n\n' + "Most commonly it's a CommonJS transform added by @babel/preset-env - " + 'in such case you should disable it by adding `modules: false` option to that preset ' + '(described in more detail here - https://github.com/rollup/plugins/tree/master/packages/babel#modules ).';
  120. const UNEXPECTED_ERROR = 'An unexpected situation arose. Please raise an issue at ' + 'https://github.com/rollup/plugins/issues. Thanks!';
  121. const PREFLIGHT_TEST_STRING = '__ROLLUP__PREFLIGHT_CHECK_DO_NOT_TOUCH__';
  122. const PREFLIGHT_INPUT = `export default "${PREFLIGHT_TEST_STRING}";`;
  123. function helpersTestTransform() {
  124. return {
  125. visitor: {
  126. StringLiteral(path, state) {
  127. if (path.node.value === PREFLIGHT_TEST_STRING) {
  128. path.replaceWith(state.file.addHelper('inherits'));
  129. }
  130. }
  131. }
  132. };
  133. }
  134. const mismatchError = (actual, expected, filename) => `You have declared using "${expected}" babelHelpers, but transforming ${filename} resulted in "${actual}". Please check your configuration.`; // Revert to /\/helpers\/(esm\/)?inherits/ when Babel 8 gets released, this was fixed in https://github.com/babel/babel/issues/14185
  135. const inheritsHelperRe = /[\\/]+helpers[\\/]+(esm[\\/]+)?inherits/;
  136. async function preflightCheck(ctx, babelHelpers, transformOptions) {
  137. const finalOptions = addBabelPlugin(transformOptions, helpersTestTransform);
  138. const check = (await babel.transformAsync(PREFLIGHT_INPUT, finalOptions)).code; // Babel sometimes splits ExportDefaultDeclaration into 2 statements, so we also check for ExportNamedDeclaration
  139. if (!/export (d|{)/.test(check)) {
  140. ctx.error(MODULE_ERROR);
  141. }
  142. if (inheritsHelperRe.test(check)) {
  143. if (babelHelpers === RUNTIME) {
  144. return;
  145. }
  146. ctx.error(mismatchError(RUNTIME, babelHelpers, transformOptions.filename));
  147. }
  148. if (check.includes('babelHelpers.inherits')) {
  149. if (babelHelpers === EXTERNAL) {
  150. return;
  151. }
  152. ctx.error(mismatchError(EXTERNAL, babelHelpers, transformOptions.filename));
  153. } // test unminifiable string content
  154. if (check.includes('Super expression must either be null or a function')) {
  155. if (babelHelpers === INLINE || babelHelpers === BUNDLED) {
  156. return;
  157. }
  158. if (babelHelpers === RUNTIME && !transformOptions.plugins.length) {
  159. ctx.error(`You must use the \`@babel/plugin-transform-runtime\` plugin when \`babelHelpers\` is "${RUNTIME}".\n`);
  160. }
  161. ctx.error(mismatchError(INLINE, babelHelpers, transformOptions.filename));
  162. }
  163. ctx.error(UNEXPECTED_ERROR);
  164. }
  165. async function transformCode(inputCode, babelOptions, overrides, customOptions, ctx, finalizeOptions) {
  166. // loadPartialConfigAsync has become available in @babel/core@7.8.0
  167. const config = await (babel.loadPartialConfigAsync || babel.loadPartialConfig)(babelOptions); // file is ignored by babel
  168. if (!config) {
  169. return null;
  170. }
  171. let transformOptions = !overrides.config ? config.options : await overrides.config.call(ctx, config, {
  172. code: inputCode,
  173. customOptions
  174. });
  175. if (finalizeOptions) {
  176. transformOptions = await finalizeOptions(transformOptions);
  177. }
  178. if (!overrides.result) {
  179. const {
  180. code,
  181. map
  182. } = await babel.transformAsync(inputCode, transformOptions);
  183. return {
  184. code,
  185. map
  186. };
  187. }
  188. const result = await babel.transformAsync(inputCode, transformOptions);
  189. const {
  190. code,
  191. map
  192. } = await overrides.result.call(ctx, result, {
  193. code: inputCode,
  194. customOptions,
  195. config,
  196. transformOptions
  197. });
  198. return {
  199. code,
  200. map
  201. };
  202. }
  203. const unpackOptions = (_ref = {}) => {
  204. let {
  205. extensions = babel.DEFAULT_EXTENSIONS,
  206. // rollup uses sourcemap, babel uses sourceMaps
  207. // just normalize them here so people don't have to worry about it
  208. sourcemap = true,
  209. sourcemaps = true,
  210. sourceMap = true,
  211. sourceMaps = true
  212. } = _ref,
  213. rest = _objectWithoutProperties(_ref, ["extensions", "sourcemap", "sourcemaps", "sourceMap", "sourceMaps"]);
  214. return _objectSpread2(_objectSpread2({
  215. extensions,
  216. plugins: [],
  217. sourceMaps: sourcemap && sourcemaps && sourceMap && sourceMaps
  218. }, rest), {}, {
  219. caller: _objectSpread2({
  220. name: '@rollup/plugin-babel'
  221. }, rest.caller)
  222. });
  223. };
  224. const warnAboutDeprecatedHelpersOption = ({
  225. deprecatedOption,
  226. suggestion
  227. }) => {
  228. // eslint-disable-next-line no-console
  229. console.warn(`\`${deprecatedOption}\` has been removed in favor a \`babelHelpers\` option. Try changing your configuration to \`${suggestion}\`. ` + `Refer to the documentation to learn more: https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers`);
  230. };
  231. const unpackInputPluginOptions = (_ref2, rollupVersion) => {
  232. let {
  233. skipPreflightCheck = false
  234. } = _ref2,
  235. rest = _objectWithoutProperties(_ref2, ["skipPreflightCheck"]);
  236. if ('runtimeHelpers' in rest) {
  237. warnAboutDeprecatedHelpersOption({
  238. deprecatedOption: 'runtimeHelpers',
  239. suggestion: `babelHelpers: 'runtime'`
  240. });
  241. } else if ('externalHelpers' in rest) {
  242. warnAboutDeprecatedHelpersOption({
  243. deprecatedOption: 'externalHelpers',
  244. suggestion: `babelHelpers: 'external'`
  245. });
  246. } else if (!rest.babelHelpers) {
  247. // eslint-disable-next-line no-console
  248. console.warn("babelHelpers: 'bundled' option was used by default. It is recommended to configure this option explicitly, read more here: " + 'https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers');
  249. }
  250. return unpackOptions(_objectSpread2(_objectSpread2({}, rest), {}, {
  251. skipPreflightCheck,
  252. babelHelpers: rest.babelHelpers || BUNDLED,
  253. caller: _objectSpread2({
  254. supportsStaticESM: true,
  255. supportsDynamicImport: true,
  256. supportsTopLevelAwait: true,
  257. // todo: remove version checks for 1.20 - 1.25 when we bump peer deps
  258. supportsExportNamespaceFrom: !rollupVersion.match(/^1\.2[0-5]\./)
  259. }, rest.caller)
  260. }));
  261. };
  262. const unpackOutputPluginOptions = (options, {
  263. format
  264. }) => unpackOptions(_objectSpread2(_objectSpread2({
  265. configFile: false,
  266. sourceType: format === 'es' ? 'module' : 'script'
  267. }, options), {}, {
  268. caller: _objectSpread2({
  269. supportsStaticESM: format === 'es'
  270. }, options.caller)
  271. }));
  272. function getOptionsWithOverrides(pluginOptions = {}, overrides = {}) {
  273. if (!overrides.options) return {
  274. customOptions: null,
  275. pluginOptionsWithOverrides: pluginOptions
  276. };
  277. const overridden = overrides.options(pluginOptions);
  278. if (typeof overridden.then === 'function') {
  279. throw new Error(".options hook can't be asynchronous. It should return `{ customOptions, pluginsOptions }` synchronously.");
  280. }
  281. return {
  282. customOptions: overridden.customOptions || null,
  283. pluginOptionsWithOverrides: overridden.pluginOptions || pluginOptions
  284. };
  285. }
  286. const returnObject = () => {
  287. return {};
  288. };
  289. function createBabelInputPluginFactory(customCallback = returnObject) {
  290. const overrides = customCallback(babel);
  291. return pluginOptions => {
  292. const {
  293. customOptions,
  294. pluginOptionsWithOverrides
  295. } = getOptionsWithOverrides(pluginOptions, overrides);
  296. let babelHelpers;
  297. let babelOptions;
  298. let filter;
  299. let skipPreflightCheck;
  300. return {
  301. name: 'babel',
  302. options() {
  303. // todo: remove options hook and hoist declarations when version checks are removed
  304. let exclude;
  305. let include;
  306. let extensions;
  307. let customFilter;
  308. var _unpackInputPluginOpt = unpackInputPluginOptions(pluginOptionsWithOverrides, this.meta.rollupVersion);
  309. ({
  310. exclude,
  311. extensions,
  312. babelHelpers,
  313. include,
  314. filter: customFilter,
  315. skipPreflightCheck
  316. } = _unpackInputPluginOpt);
  317. babelOptions = _objectWithoutProperties(_unpackInputPluginOpt, ["exclude", "extensions", "babelHelpers", "include", "filter", "skipPreflightCheck"]);
  318. const extensionRegExp = new RegExp(`(${extensions.map(escapeRegExpCharacters).join('|')})$`);
  319. if (customFilter && (include || exclude)) {
  320. throw new Error('Could not handle include or exclude with custom filter together');
  321. }
  322. const userDefinedFilter = typeof customFilter === 'function' ? customFilter : createFilter(include, exclude);
  323. filter = id => extensionRegExp.test(stripQuery(id).bareId) && userDefinedFilter(id);
  324. return null;
  325. },
  326. resolveId(id) {
  327. if (id !== HELPERS) {
  328. return null;
  329. }
  330. return id;
  331. },
  332. load(id) {
  333. if (id !== HELPERS) {
  334. return null;
  335. }
  336. return babel.buildExternalHelpers(null, 'module');
  337. },
  338. transform(code, filename) {
  339. if (!filter(filename)) return null;
  340. if (filename === HELPERS) return null;
  341. return transformCode(code, _objectSpread2(_objectSpread2({}, babelOptions), {}, {
  342. filename
  343. }), overrides, customOptions, this, async transformOptions => {
  344. if (!skipPreflightCheck) {
  345. await preflightCheck(this, babelHelpers, transformOptions);
  346. }
  347. return babelHelpers === BUNDLED ? addBabelPlugin(transformOptions, importHelperPlugin) : transformOptions;
  348. });
  349. }
  350. };
  351. };
  352. }
  353. function getRecommendedFormat(rollupFormat) {
  354. switch (rollupFormat) {
  355. case 'amd':
  356. return 'amd';
  357. case 'iife':
  358. case 'umd':
  359. return 'umd';
  360. case 'system':
  361. return 'systemjs';
  362. default:
  363. return '<module format>';
  364. }
  365. }
  366. function createBabelOutputPluginFactory(customCallback = returnObject) {
  367. const overrides = customCallback(babel);
  368. return pluginOptions => {
  369. const {
  370. customOptions,
  371. pluginOptionsWithOverrides
  372. } = getOptionsWithOverrides(pluginOptions, overrides);
  373. return {
  374. name: 'babel',
  375. renderStart(outputOptions) {
  376. const {
  377. extensions,
  378. include,
  379. exclude,
  380. allowAllFormats
  381. } = pluginOptionsWithOverrides;
  382. if (extensions || include || exclude) {
  383. warnOnce(this, 'The "include", "exclude" and "extensions" options are ignored when transforming the output.');
  384. }
  385. if (!allowAllFormats && outputOptions.format !== 'es' && outputOptions.format !== 'cjs') {
  386. this.error(`Using Babel on the generated chunks is strongly discouraged for formats other than "esm" or "cjs" as it can easily break wrapper code and lead to accidentally created global variables. Instead, you should set "output.format" to "esm" and use Babel to transform to another format, e.g. by adding "presets: [['@babel/env', { modules: '${getRecommendedFormat(outputOptions.format)}' }]]" to your Babel options. If you still want to proceed, add "allowAllFormats: true" to your plugin options.`);
  387. }
  388. },
  389. renderChunk(code, chunk, outputOptions) {
  390. /* eslint-disable no-unused-vars */
  391. const _unpackOutputPluginOp = unpackOutputPluginOptions(pluginOptionsWithOverrides, outputOptions),
  392. babelOptions = _objectWithoutProperties(_unpackOutputPluginOp, ["allowAllFormats", "exclude", "extensions", "externalHelpers", "externalHelpersWhitelist", "include", "runtimeHelpers"]);
  393. /* eslint-enable no-unused-vars */
  394. return transformCode(code, babelOptions, overrides, customOptions, this);
  395. }
  396. };
  397. };
  398. } // export this for symmetry with output-related exports
  399. const getBabelInputPlugin = createBabelInputPluginFactory();
  400. const getBabelOutputPlugin = createBabelOutputPluginFactory();
  401. export { getBabelInputPlugin as babel, createBabelInputPluginFactory, createBabelOutputPluginFactory, getBabelInputPlugin as default, getBabelInputPlugin, getBabelOutputPlugin };