rollup.config.js 4.7 KB

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