rollup.config.js 3.9 KB

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