rollup.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import resolve from "@rollup/plugin-node-resolve";
  2. import replace from "@rollup/plugin-replace";
  3. import commonjs from "@rollup/plugin-commonjs";
  4. import json from "@rollup/plugin-json";
  5. import typescript from "@rollup/plugin-typescript";
  6. import svelte from "rollup-plugin-svelte";
  7. import babel from "@rollup/plugin-babel";
  8. import { terser } from "rollup-plugin-terser";
  9. import config from "sapper/config/rollup";
  10. import alias from "@rollup/plugin-alias";
  11. import * as path from "path";
  12. import pkg from "./package.json";
  13. const preprocess = [
  14. // eslint-disable-next-line global-require
  15. require("./svelte.config").preprocess,
  16. // You could have more preprocessors, like MDsveX
  17. ];
  18. const mode = process.env.NODE_ENV;
  19. const dev = mode === "development";
  20. const sourcemap = dev ? "inline" : false;
  21. const legacy = !!process.env.SAPPER_LEGACY_BUILD;
  22. const warningIsIgnored = (warning) => warning.message.includes(
  23. "Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification",
  24. ) || warning.message.includes("Circular dependency: node_modules");
  25. // Workaround for https://github.com/sveltejs/sapper/issues/1221
  26. const onwarn = (warning, _onwarn) => (warning.code === "CIRCULAR_DEPENDENCY" && /[/\\]@sapper[/\\]/.test(warning.message)) || warningIsIgnored(warning) || console.warn(warning.toString());
  27. export default {
  28. client: {
  29. input: config.client.input().replace(/\.js$/, ".ts"),
  30. output: { ...config.client.output() },
  31. plugins: [
  32. replace({
  33. "process.browser": true,
  34. "process.env.NODE_ENV": JSON.stringify(mode),
  35. }),
  36. svelte({
  37. dev,
  38. hydratable: true,
  39. emitCss: true,
  40. preprocess,
  41. }),
  42. resolve({
  43. browser: true,
  44. dedupe: ["svelte"],
  45. }),
  46. alias({
  47. entries: [
  48. { find: "@shared", replacement: path.join(__dirname, "../shared/lib/src") },
  49. ],
  50. }),
  51. commonjs(),
  52. typescript(),
  53. json(),
  54. legacy && babel({
  55. extensions: [".js", ".mjs", ".html", ".svelte"],
  56. babelHelpers: "runtime",
  57. exclude: ["node_modules/@babel/**"],
  58. presets: [
  59. ["@babel/preset-env", {
  60. targets: "> 0.25%, not dead",
  61. }],
  62. ],
  63. plugins: [
  64. "@babel/plugin-syntax-dynamic-import",
  65. ["@babel/plugin-transform-runtime", {
  66. useESModules: true,
  67. }],
  68. ],
  69. }),
  70. !dev && terser({
  71. module: true,
  72. }),
  73. ],
  74. preserveEntrySignatures: false,
  75. onwarn,
  76. },
  77. server: {
  78. input: { server: config.server.input().server.replace(/\.js$/, ".ts") },
  79. output: { ...config.server.output(), sourcemap, interop: "auto" },
  80. plugins: [
  81. replace({
  82. "process.browser": false,
  83. "process.env.NODE_ENV": JSON.stringify(mode),
  84. "module.require": "require",
  85. }),
  86. svelte({
  87. generate: "ssr",
  88. dev,
  89. preprocess,
  90. }),
  91. resolve({
  92. dedupe: ["svelte"],
  93. }),
  94. alias({
  95. entries: [
  96. { find: "@shared", replacement: path.join(__dirname, "../shared/lib/src") },
  97. ],
  98. }),
  99. typescript(),
  100. commonjs({
  101. include: ["../shared/lib/**"],
  102. }),
  103. json(),
  104. ],
  105. external: Object.keys(pkg.dependencies).concat(
  106. require("module").builtinModules || Object.keys(process.binding("natives")), // eslint-disable-line global-require
  107. ).concat(/rpc_ts/),
  108. preserveEntrySignatures: "strict",
  109. onwarn,
  110. },
  111. serviceworker: {
  112. input: config.serviceworker.input().replace(/\.js$/, ".ts"),
  113. output: config.serviceworker.output(),
  114. plugins: [
  115. resolve(),
  116. replace({
  117. "process.browser": true,
  118. "process.env.NODE_ENV": JSON.stringify(mode),
  119. }),
  120. commonjs(),
  121. typescript(),
  122. !dev && terser(),
  123. ],
  124. preserveEntrySignatures: false,
  125. onwarn,
  126. },
  127. };