rollup.config.js 4.3 KB

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