index.js 16 KB

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