rollup.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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: {
  80. ...config.server.output(),
  81. sourcemap,
  82. interop: "default",
  83. },
  84. plugins: [
  85. replace({
  86. "process.browser": false,
  87. "process.env.NODE_ENV": JSON.stringify(mode),
  88. "module.require": "require",
  89. }),
  90. svelte({
  91. generate: "ssr",
  92. dev,
  93. preprocess,
  94. }),
  95. resolve({
  96. dedupe: ["svelte"],
  97. }),
  98. alias({
  99. entries: [
  100. { find: "@shared", replacement: path.join(__dirname, "../shared/lib/src") },
  101. ],
  102. }),
  103. typescript(),
  104. commonjs({
  105. include: ["../shared/lib/**"],
  106. }),
  107. json(),
  108. ],
  109. external: Object.keys(pkg.dependencies).concat(
  110. require("module").builtinModules || Object.keys(process.binding("natives")), // eslint-disable-line global-require
  111. ).concat(/rpc_ts/),
  112. preserveEntrySignatures: "strict",
  113. onwarn,
  114. },
  115. serviceworker: {
  116. input: config.serviceworker.input().replace(/\.js$/, ".ts"),
  117. output: config.serviceworker.output(),
  118. plugins: [
  119. resolve(),
  120. replace({
  121. "process.browser": true,
  122. "process.env.NODE_ENV": JSON.stringify(mode),
  123. }),
  124. commonjs(),
  125. typescript(),
  126. !dev && terser(),
  127. ],
  128. preserveEntrySignatures: false,
  129. onwarn,
  130. },
  131. };