rollup.config.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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(), sourcemap },
  37. external: (id) => id.includes("@babel/runtime"),
  38. plugins: [
  39. replace({
  40. "process.browser": true,
  41. "process.env.NODE_ENV": JSON.stringify(mode),
  42. }),
  43. svelte({
  44. dev,
  45. hydratable: true,
  46. emitCss: true,
  47. preprocess,
  48. }),
  49. resolve({
  50. browser: true,
  51. dedupe: ["svelte"],
  52. }),
  53. alias({
  54. entries: [
  55. { find: "@shared", replacement: path.join(__dirname, "../shared/lib/src") },
  56. ],
  57. }),
  58. commonjs(),
  59. typescript(),
  60. json(),
  61. legacy && babel({
  62. extensions: [".js", ".mjs", ".html", ".svelte"],
  63. babelHelpers: "runtime",
  64. exclude: ["node_modules/**"],
  65. presets: ["@babel/preset-env"],
  66. plugins: [
  67. "@babel/plugin-syntax-dynamic-import",
  68. ["@babel/plugin-transform-runtime", {
  69. useESModules: true,
  70. }],
  71. ],
  72. }),
  73. !dev && terser({
  74. module: true,
  75. }),
  76. ],
  77. preserveEntrySignatures: false,
  78. onwarn,
  79. },
  80. server: {
  81. input: { server: config.server.input().server.replace(/\.js$/, ".ts") },
  82. output: {
  83. ...config.server.output(),
  84. sourcemap,
  85. interop: "default",
  86. },
  87. plugins: [
  88. replace({
  89. "process.browser": false,
  90. "process.env.NODE_ENV": JSON.stringify(mode),
  91. "module.require": "require",
  92. }),
  93. svelte({
  94. generate: "ssr",
  95. dev,
  96. preprocess,
  97. }),
  98. resolve({
  99. dedupe: ["svelte"],
  100. }),
  101. alias({
  102. entries: [
  103. { find: "@shared", replacement: path.join(__dirname, "../shared/lib/src") },
  104. ],
  105. }),
  106. typescript(),
  107. commonjs({
  108. include: ["../shared/lib/**"],
  109. }),
  110. json(),
  111. ],
  112. external: Object.keys(pkg.dependencies).concat(
  113. require("module").builtinModules || Object.keys(process.binding("natives")), // eslint-disable-line global-require
  114. ).concat(/rpc_ts/),
  115. preserveEntrySignatures: "strict",
  116. onwarn,
  117. },
  118. };