rollup.config.js 4.1 KB

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