bundle.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. const {
  9. rollup
  10. } = require('rollup');
  11. const {
  12. terser
  13. } = require('rollup-plugin-terser');
  14. const {
  15. writeFile
  16. } = require('fs-extra');
  17. const babel = require('rollup-plugin-babel');
  18. const omt = require('@surma/rollup-plugin-off-main-thread');
  19. const upath = require('upath');
  20. const presetEnv = require('@babel/preset-env');
  21. const replace = require('@rollup/plugin-replace');
  22. const resolve = require('@rollup/plugin-node-resolve');
  23. const tempy = require('tempy');
  24. module.exports = async ({
  25. babelPresetEnvTargets,
  26. inlineWorkboxRuntime,
  27. mode,
  28. sourcemap,
  29. swDest,
  30. unbundledCode
  31. }) => {
  32. // We need to write this to the "real" file system, as Rollup won't read from
  33. // a custom file system.
  34. const {
  35. dir,
  36. base
  37. } = upath.parse(swDest);
  38. const temporaryFile = tempy.file({
  39. name: base
  40. });
  41. await writeFile(temporaryFile, unbundledCode);
  42. const plugins = [resolve(), replace({
  43. 'process.env.NODE_ENV': JSON.stringify(mode)
  44. }), babel({
  45. // Disable the logic that checks for local Babel config files:
  46. // https://github.com/GoogleChrome/workbox/issues/2111
  47. babelrc: false,
  48. configFile: false,
  49. presets: [[presetEnv, {
  50. targets: {
  51. browsers: babelPresetEnvTargets
  52. },
  53. loose: true
  54. }]]
  55. })];
  56. if (mode === 'production') {
  57. plugins.push(terser({
  58. mangle: {
  59. toplevel: true,
  60. properties: {
  61. regex: /(^_|_$)/
  62. }
  63. }
  64. }));
  65. }
  66. const rollupConfig = {
  67. plugins,
  68. input: temporaryFile
  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. rollupConfig.plugins.unshift(omt());
  73. rollupConfig.manualChunks = id => {
  74. return id.includes('workbox') ? 'workbox' : undefined;
  75. };
  76. }
  77. const bundle = await rollup(rollupConfig);
  78. const {
  79. output
  80. } = 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.isAsset) {
  88. files.push({
  89. name: chunkOrAsset.fileName,
  90. contents: chunkOrAsset.source
  91. });
  92. } else {
  93. let code = chunkOrAsset.code;
  94. if (chunkOrAsset.map) {
  95. const sourceMapFile = chunkOrAsset.fileName + '.map';
  96. code += `//# sourceMappingURL=${sourceMapFile}\n`;
  97. files.push({
  98. name: sourceMapFile,
  99. contents: chunkOrAsset.map.toString()
  100. });
  101. }
  102. files.push({
  103. name: chunkOrAsset.fileName,
  104. contents: code
  105. });
  106. }
  107. } // Make sure that if there was a directory portion included in swDest, it's
  108. // preprended to all of the generated files.
  109. return files.map(file => {
  110. file.name = upath.format({
  111. dir,
  112. base: file.name
  113. });
  114. return file;
  115. });
  116. };