bundle.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. /*
  3. Copyright 2019 Google LLC
  4. Use of this source code is governed by an MIT-style
  5. license that can be found in the LICENSE file or at
  6. https://opensource.org/licenses/MIT.
  7. */
  8. var __importDefault = (this && this.__importDefault) || function (mod) {
  9. return (mod && mod.__esModule) ? mod : { "default": mod };
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.bundle = void 0;
  13. const plugin_babel_1 = require("@rollup/plugin-babel");
  14. const plugin_node_resolve_1 = require("@rollup/plugin-node-resolve");
  15. const rollup_1 = require("rollup");
  16. const rollup_plugin_terser_1 = require("rollup-plugin-terser");
  17. const fs_extra_1 = require("fs-extra");
  18. const rollup_plugin_off_main_thread_1 = __importDefault(require("@surma/rollup-plugin-off-main-thread"));
  19. const preset_env_1 = __importDefault(require("@babel/preset-env"));
  20. const plugin_replace_1 = __importDefault(require("@rollup/plugin-replace"));
  21. const tempy_1 = __importDefault(require("tempy"));
  22. const upath_1 = __importDefault(require("upath"));
  23. async function bundle({ babelPresetEnvTargets, inlineWorkboxRuntime, mode, sourcemap, swDest, unbundledCode, }) {
  24. // We need to write this to the "real" file system, as Rollup won't read from
  25. // a custom file system.
  26. const { dir, base } = upath_1.default.parse(swDest);
  27. const temporaryFile = tempy_1.default.file({ name: base });
  28. await (0, fs_extra_1.writeFile)(temporaryFile, unbundledCode);
  29. const plugins = [
  30. (0, plugin_node_resolve_1.nodeResolve)(),
  31. (0, plugin_replace_1.default)({
  32. // See https://github.com/GoogleChrome/workbox/issues/2769
  33. 'preventAssignment': true,
  34. 'process.env.NODE_ENV': JSON.stringify(mode),
  35. }),
  36. (0, plugin_babel_1.babel)({
  37. babelHelpers: 'bundled',
  38. // Disable the logic that checks for local Babel config files:
  39. // https://github.com/GoogleChrome/workbox/issues/2111
  40. babelrc: false,
  41. configFile: false,
  42. presets: [
  43. [
  44. preset_env_1.default,
  45. {
  46. targets: {
  47. browsers: babelPresetEnvTargets,
  48. },
  49. loose: true,
  50. },
  51. ],
  52. ],
  53. }),
  54. ];
  55. if (mode === 'production') {
  56. plugins.push((0, rollup_plugin_terser_1.terser)({
  57. mangle: {
  58. toplevel: true,
  59. properties: {
  60. regex: /(^_|_$)/,
  61. },
  62. },
  63. }));
  64. }
  65. const rollupConfig = {
  66. plugins,
  67. input: temporaryFile,
  68. };
  69. // Rollup will inline the runtime by default. If we don't want that, we need
  70. // to add in some additional config.
  71. if (!inlineWorkboxRuntime) {
  72. // No lint for omt(), library has no types.
  73. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  74. rollupConfig.plugins.unshift((0, rollup_plugin_off_main_thread_1.default)());
  75. rollupConfig.manualChunks = (id) => {
  76. return id.includes('workbox') ? 'workbox' : undefined;
  77. };
  78. }
  79. const bundle = await (0, rollup_1.rollup)(rollupConfig);
  80. const { output } = await bundle.generate({
  81. sourcemap,
  82. // Using an external Workbox runtime requires 'amd'.
  83. format: inlineWorkboxRuntime ? 'es' : 'amd',
  84. });
  85. const files = [];
  86. for (const chunkOrAsset of output) {
  87. if (chunkOrAsset.type === 'asset') {
  88. files.push({
  89. name: chunkOrAsset.fileName,
  90. contents: chunkOrAsset.source,
  91. });
  92. }
  93. else {
  94. let code = chunkOrAsset.code;
  95. if (chunkOrAsset.map) {
  96. const sourceMapFile = chunkOrAsset.fileName + '.map';
  97. code += `//# sourceMappingURL=${sourceMapFile}\n`;
  98. files.push({
  99. name: sourceMapFile,
  100. contents: chunkOrAsset.map.toString(),
  101. });
  102. }
  103. files.push({
  104. name: chunkOrAsset.fileName,
  105. contents: code,
  106. });
  107. }
  108. }
  109. // Make sure that if there was a directory portion included in swDest, it's
  110. // preprended to all of the generated files.
  111. return files.map((file) => {
  112. file.name = upath_1.default.format({
  113. dir,
  114. base: file.name,
  115. ext: '',
  116. name: '',
  117. root: '',
  118. });
  119. return file;
  120. });
  121. }
  122. exports.bundle = bundle;