rollup-plugin-babel.esm.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import * as babel from '@babel/core';
  2. import { transformSync, buildExternalHelpers, loadPartialConfig, DEFAULT_EXTENSIONS } from '@babel/core';
  3. import { createFilter } from 'rollup-pluginutils';
  4. import { addNamed } from '@babel/helper-module-imports';
  5. var INLINE = {};
  6. var RUNTIME = {};
  7. var EXTERNAL = {};
  8. // NOTE: DO NOT REMOVE the null character `\0` as it may be used by other plugins
  9. // e.g. https://github.com/rollup/rollup-plugin-node-resolve/blob/313a3e32f432f9eb18cc4c231cc7aac6df317a51/src/index.js#L74
  10. var HELPERS = '\0rollupPluginBabelHelpers.js';
  11. var addBabelPlugin = function (options, plugin) { return (Object.assign({}, options, {plugins: options.plugins.concat(plugin)})); };
  12. var warned = {};
  13. function warnOnce(ctx, msg) {
  14. if (warned[msg]) { return; }
  15. warned[msg] = true;
  16. ctx.warn(msg);
  17. }
  18. var regExpCharactersRegExp = /[\\^$.*+?()[\]{}|]/g;
  19. var escapeRegExpCharacters = function (str) { return str.replace(regExpCharactersRegExp, '\\$&'); };
  20. var MODULE_ERROR =
  21. 'Rollup requires that your Babel configuration keeps ES6 module syntax intact. ' +
  22. 'Unfortunately it looks like your configuration specifies a module transformer ' +
  23. 'to replace ES6 modules with another module format. To continue you have to disable it.' +
  24. '\n\n' +
  25. "Most commonly it's a CommonJS transform added by @babel/preset-env - " +
  26. 'in such case you should disable it by adding `modules: false` option to that preset ' +
  27. '(described in more detail here - https://github.com/rollup/rollup-plugin-babel#modules ).';
  28. var UNEXPECTED_ERROR =
  29. 'An unexpected situation arose. Please raise an issue at ' +
  30. 'https://github.com/rollup/rollup-plugin-babel/issues. Thanks!';
  31. function fallbackClassTransform() {
  32. return {
  33. visitor: {
  34. ClassDeclaration: function ClassDeclaration(path, state) {
  35. path.replaceWith(state.file.addHelper('inherits'));
  36. },
  37. },
  38. };
  39. }
  40. function createPreflightCheck() {
  41. var preflightCheckResults = {};
  42. return function (ctx, options) {
  43. var key = options.filename;
  44. if (preflightCheckResults[key] === undefined) {
  45. var helpers;
  46. var inputCode = 'class Foo extends Bar {};\nexport default Foo;';
  47. var transformed = transformSync(inputCode, options);
  48. var check = transformed.code;
  49. if (~check.indexOf('class ')) {
  50. check = transformSync(inputCode, addBabelPlugin(options, fallbackClassTransform)).code;
  51. }
  52. if (
  53. !~check.indexOf('export default') &&
  54. !~check.indexOf('export default Foo') &&
  55. !~check.indexOf('export { Foo as default }')
  56. ) {
  57. ctx.error(MODULE_ERROR);
  58. }
  59. if (check.match(/\/helpers\/(esm\/)?inherits/)) { helpers = RUNTIME; }
  60. else if (~check.indexOf('function _inherits')) { helpers = INLINE; }
  61. else if (~check.indexOf('babelHelpers')) { helpers = EXTERNAL; }
  62. else {
  63. ctx.error(UNEXPECTED_ERROR);
  64. }
  65. preflightCheckResults[key] = helpers;
  66. }
  67. return preflightCheckResults[key];
  68. };
  69. }
  70. function importHelperPlugin() {
  71. return {
  72. pre: function pre(file) {
  73. var cachedHelpers = {};
  74. file.set('helperGenerator', function (name) {
  75. if (!file.availableHelper(name)) {
  76. return;
  77. }
  78. if (cachedHelpers[name]) {
  79. return cachedHelpers[name];
  80. }
  81. return (cachedHelpers[name] = addNamed(file.path, name, HELPERS));
  82. });
  83. },
  84. };
  85. }
  86. function objectWithoutProperties (obj, exclude) { var target = {}; for (var k in obj) if (Object.prototype.hasOwnProperty.call(obj, k) && exclude.indexOf(k) === -1) target[k] = obj[k]; return target; }
  87. var unpackOptions = function (ref) {
  88. if ( ref === void 0 ) ref = {};
  89. var extensions = ref.extensions; if ( extensions === void 0 ) extensions = DEFAULT_EXTENSIONS;
  90. var sourcemap = ref.sourcemap; if ( sourcemap === void 0 ) sourcemap = true;
  91. var sourcemaps = ref.sourcemaps; if ( sourcemaps === void 0 ) sourcemaps = true;
  92. var sourceMap = ref.sourceMap; if ( sourceMap === void 0 ) sourceMap = true;
  93. var sourceMaps = ref.sourceMaps; if ( sourceMaps === void 0 ) sourceMaps = true;
  94. var rest$1 = objectWithoutProperties( ref, ["extensions", "sourcemap", "sourcemaps", "sourceMap", "sourceMaps"] );
  95. var rest = rest$1;
  96. return (Object.assign({}, {extensions: extensions,
  97. plugins: [],
  98. sourceMaps: sourcemap && sourcemaps && sourceMap && sourceMaps},
  99. rest,
  100. {caller: Object.assign({}, {name: 'rollup-plugin-babel',
  101. supportsStaticESM: true,
  102. supportsDynamicImport: true},
  103. rest.caller)}));
  104. };
  105. var returnObject = function () { return ({}); };
  106. function createBabelPluginFactory(customCallback) {
  107. if ( customCallback === void 0 ) customCallback = returnObject;
  108. var overrides = customCallback(babel);
  109. return function (pluginOptions) {
  110. var assign;
  111. var customOptions = null;
  112. if (overrides.options) {
  113. var overridden = overrides.options(pluginOptions);
  114. if (typeof overridden.then === 'function') {
  115. throw new Error(
  116. ".options hook can't be asynchronous. It should return `{ customOptions, pluginsOptions }` synchronously."
  117. );
  118. }
  119. ((assign = overridden, customOptions = assign.customOptions, customOptions = customOptions === void 0 ? null : customOptions, pluginOptions = assign.pluginOptions));
  120. }
  121. var ref = unpackOptions(pluginOptions);
  122. var exclude = ref.exclude;
  123. var extensions = ref.extensions;
  124. var externalHelpers = ref.externalHelpers;
  125. var externalHelpersWhitelist = ref.externalHelpersWhitelist;
  126. var include = ref.include;
  127. var runtimeHelpers = ref.runtimeHelpers;
  128. var rest = objectWithoutProperties( ref, ["exclude", "extensions", "externalHelpers", "externalHelpersWhitelist", "include", "runtimeHelpers"] );
  129. var babelOptions = rest;
  130. var extensionRegExp = new RegExp(("(" + (extensions.map(escapeRegExpCharacters).join('|')) + ")$"));
  131. var includeExcludeFilter = createFilter(include, exclude);
  132. var filter = function (id) { return extensionRegExp.test(id) && includeExcludeFilter(id); };
  133. var preflightCheck = createPreflightCheck();
  134. return {
  135. name: 'babel',
  136. resolveId: function resolveId(id) {
  137. if (id === HELPERS) { return id; }
  138. },
  139. load: function load(id) {
  140. if (id !== HELPERS) {
  141. return;
  142. }
  143. return buildExternalHelpers(externalHelpersWhitelist, 'module');
  144. },
  145. transform: function transform(code, filename) {
  146. var this$1 = this;
  147. if (!filter(filename)) { return Promise.resolve(null); }
  148. if (filename === HELPERS) { return Promise.resolve(null); }
  149. var config = loadPartialConfig(Object.assign({}, babelOptions, {filename: filename}));
  150. // file is ignored
  151. if (!config) {
  152. return Promise.resolve(null);
  153. }
  154. return Promise.resolve(
  155. !overrides.config
  156. ? config.options
  157. : overrides.config.call(this, config, {
  158. code: code,
  159. customOptions: customOptions,
  160. })
  161. ).then(function (transformOptions) {
  162. var helpers = preflightCheck(this$1, transformOptions);
  163. if (helpers === EXTERNAL && !externalHelpers) {
  164. warnOnce(
  165. this$1,
  166. 'Using "external-helpers" plugin with rollup-plugin-babel is deprecated, as it now automatically deduplicates your Babel helpers.'
  167. );
  168. } else if (helpers === RUNTIME && !runtimeHelpers) {
  169. this$1.error(
  170. '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'
  171. );
  172. }
  173. if (helpers !== RUNTIME && !externalHelpers) {
  174. transformOptions = addBabelPlugin(transformOptions, importHelperPlugin);
  175. }
  176. var result = transformSync(code, transformOptions);
  177. return Promise.resolve(
  178. !overrides.result
  179. ? result
  180. : overrides.result.call(this$1, result, {
  181. code: code,
  182. customOptions: customOptions,
  183. config: config,
  184. transformOptions: transformOptions,
  185. })
  186. ).then(function (ref) {
  187. var code = ref.code;
  188. var map = ref.map;
  189. return ({ code: code, map: map });
  190. });
  191. });
  192. },
  193. };
  194. };
  195. }
  196. var babelPluginFactory = createBabelPluginFactory();
  197. babelPluginFactory.custom = createBabelPluginFactory;
  198. export default babelPluginFactory;
  199. //# sourceMappingURL=rollup-plugin-babel.esm.js.map