webpack.config.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. const webpack = require("webpack");
  2. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  3. const CopyWebpackPlugin = require("copy-webpack-plugin");
  4. const { ModifySourcePlugin } = require("modify-source-webpack-plugin");
  5. const path = require("path");
  6. /**
  7. * Webpack configuration details for use with Grunt.
  8. *
  9. * @author n1474335 [n1474335@gmail.com]
  10. * @copyright Crown Copyright 2017
  11. * @license Apache-2.0
  12. */
  13. const banner = `/**
  14. * CyberChef - The Cyber Swiss Army Knife
  15. *
  16. * @copyright Crown Copyright 2016
  17. * @license Apache-2.0
  18. *
  19. * Copyright 2016 Crown Copyright
  20. *
  21. * Licensed under the Apache License, Version 2.0 (the "License");
  22. * you may not use this file except in compliance with the License.
  23. * You may obtain a copy of the License at
  24. *
  25. * http://www.apache.org/licenses/LICENSE-2.0
  26. *
  27. * Unless required by applicable law or agreed to in writing, software
  28. * distributed under the License is distributed on an "AS IS" BASIS,
  29. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  30. * See the License for the specific language governing permissions and
  31. * limitations under the License.
  32. */`;
  33. module.exports = {
  34. output: {
  35. publicPath: "",
  36. globalObject: "this",
  37. assetModuleFilename: "assets/[hash][ext][query]"
  38. },
  39. plugins: [
  40. new webpack.ProvidePlugin({
  41. $: "jquery",
  42. jQuery: "jquery",
  43. log: "loglevel",
  44. // process and Buffer are no longer polyfilled in webpack 5 but
  45. // many of our dependencies expect them, so it is easiest to just
  46. // provide them everywhere as was the case in webpack 4-
  47. process: "process",
  48. Buffer: ["buffer", "Buffer"]
  49. }),
  50. new webpack.BannerPlugin({
  51. banner: banner,
  52. raw: true,
  53. entryOnly: true
  54. }),
  55. new webpack.DefinePlugin({
  56. // Required by Jimp to improve loading speed in browsers
  57. "process.browser": "true"
  58. }),
  59. new MiniCssExtractPlugin({
  60. filename: "assets/[name].css"
  61. }),
  62. new CopyWebpackPlugin({
  63. patterns: [
  64. {
  65. context: "src/core/vendor/",
  66. from: "tesseract/**/*",
  67. to: "assets/"
  68. }, {
  69. context: "node_modules/tesseract.js/",
  70. from: "dist/worker.min.js",
  71. to: "assets/tesseract"
  72. }, {
  73. context: "node_modules/tesseract.js-core/",
  74. from: "tesseract-core.wasm.js",
  75. to: "assets/tesseract"
  76. }, {
  77. context: "node_modules/node-forge/dist",
  78. from: "prime.worker.min.js",
  79. to: "assets/forge/"
  80. }
  81. ]
  82. }),
  83. new ModifySourcePlugin({
  84. rules: [
  85. {
  86. // Fix toSpare(0) bug in Split.js by avoiding gutter accomodation
  87. test: /split\.es\.js$/,
  88. modify: (src, path) =>
  89. src.replace("if (pixelSize < elementMinSize)", "if (false)")
  90. }
  91. ]
  92. })
  93. ],
  94. resolve: {
  95. extensions: [".mjs", ".js", ".json"], // Allows importing files without extensions
  96. alias: {
  97. jquery: "jquery/src/jquery",
  98. },
  99. fallback: {
  100. "fs": false,
  101. "child_process": false,
  102. "net": false,
  103. "tls": false,
  104. "path": require.resolve("path/"),
  105. "buffer": require.resolve("buffer/"),
  106. "crypto": require.resolve("crypto-browserify"),
  107. "stream": require.resolve("stream-browserify"),
  108. "zlib": require.resolve("browserify-zlib"),
  109. "process": false
  110. }
  111. },
  112. module: {
  113. // argon2-browser loads argon2.wasm by itself, so Webpack should not load it
  114. noParse: /argon2\.wasm$/,
  115. rules: [
  116. {
  117. test: /\.m?js$/,
  118. exclude: /node_modules\/(?!crypto-api|bootstrap)/,
  119. options: {
  120. configFile: path.resolve(__dirname, "babel.config.js"),
  121. cacheDirectory: true,
  122. compact: false
  123. },
  124. type: "javascript/auto",
  125. loader: "babel-loader"
  126. },
  127. {
  128. test: /node-forge/,
  129. loader: "imports-loader",
  130. options: {
  131. additionalCode: "var jQuery = false;"
  132. }
  133. },
  134. {
  135. // Load argon2.wasm as base64-encoded binary file expected by argon2-browser
  136. test: /argon2\.wasm$/,
  137. loader: "base64-loader",
  138. type: "javascript/auto"
  139. },
  140. {
  141. test: /prime.worker.min.js$/,
  142. type: "asset/source"
  143. },
  144. {
  145. test: /bootstrap-material-design/,
  146. loader: "imports-loader",
  147. options: {
  148. imports: "default popper.js/dist/umd/popper.js Popper"
  149. }
  150. },
  151. {
  152. test: /blueimp-load-image/,
  153. loader: "imports-loader",
  154. options: {
  155. type: "commonjs",
  156. imports: "single min-document document"
  157. }
  158. },
  159. {
  160. test: /\.css$/,
  161. use: [
  162. {
  163. loader: MiniCssExtractPlugin.loader,
  164. options: {
  165. publicPath: "../"
  166. }
  167. },
  168. "css-loader",
  169. "postcss-loader",
  170. ]
  171. },
  172. {
  173. test: /\.(ico|eot|ttf|woff|woff2)$/,
  174. type: "asset/resource",
  175. },
  176. {
  177. test: /\.svg$/,
  178. type: "asset/inline",
  179. },
  180. { // Store font .fnt and .png files in a separate fonts folder
  181. test: /(\.fnt$|bmfonts\/.+\.png$)/,
  182. type: "asset/resource",
  183. generator: {
  184. filename: "assets/fonts/[name][ext]"
  185. }
  186. },
  187. { // First party images are saved as files to be cached
  188. test: /\.(png|jpg|gif)$/,
  189. exclude: /(node_modules|bmfonts)/,
  190. type: "asset/resource",
  191. generator: {
  192. filename: "images/[name][ext]"
  193. }
  194. },
  195. { // Third party images are inlined
  196. test: /\.(png|jpg|gif)$/,
  197. exclude: /web\/static/,
  198. type: "asset/inline",
  199. },
  200. ]
  201. },
  202. stats: {
  203. children: false,
  204. chunks: false,
  205. modules: false,
  206. entrypoints: false
  207. },
  208. ignoreWarnings: [
  209. /source-map/,
  210. /source map/,
  211. /dependency is an expression/,
  212. /export 'default'/,
  213. /Can't resolve 'sodium'/
  214. ],
  215. performance: {
  216. hints: false
  217. }
  218. };