webpack.config.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. }
  110. },
  111. module: {
  112. rules: [
  113. {
  114. test: /\.m?js$/,
  115. exclude: /node_modules\/(?!crypto-api|bootstrap)/,
  116. options: {
  117. configFile: path.resolve(__dirname, "babel.config.js"),
  118. cacheDirectory: true,
  119. compact: false
  120. },
  121. type: "javascript/auto",
  122. loader: "babel-loader"
  123. },
  124. {
  125. test: /node-forge/,
  126. loader: "imports-loader",
  127. options: {
  128. additionalCode: "var jQuery = false;"
  129. }
  130. },
  131. {
  132. test: /prime.worker.min.js$/,
  133. type: "asset/source"
  134. },
  135. {
  136. test: /bootstrap-material-design/,
  137. loader: "imports-loader",
  138. options: {
  139. imports: "default popper.js/dist/umd/popper.js Popper"
  140. }
  141. },
  142. {
  143. test: /blueimp-load-image/,
  144. loader: "imports-loader",
  145. options: {
  146. type: "commonjs",
  147. imports: "single min-document document"
  148. }
  149. },
  150. {
  151. test: /\.css$/,
  152. use: [
  153. {
  154. loader: MiniCssExtractPlugin.loader,
  155. options: {
  156. publicPath: "../"
  157. }
  158. },
  159. "css-loader",
  160. "postcss-loader",
  161. ]
  162. },
  163. {
  164. test: /\.(ico|eot|ttf|woff|woff2)$/,
  165. type: "asset/resource",
  166. },
  167. {
  168. test: /\.svg$/,
  169. type: "asset/inline",
  170. },
  171. { // Store font .fnt and .png files in a separate fonts folder
  172. test: /(\.fnt$|bmfonts\/.+\.png$)/,
  173. type: "asset/resource",
  174. generator: {
  175. filename: "assets/fonts/[name][ext]"
  176. }
  177. },
  178. { // First party images are saved as files to be cached
  179. test: /\.(png|jpg|gif)$/,
  180. exclude: /(node_modules|bmfonts)/,
  181. type: "asset/resource",
  182. generator: {
  183. filename: "images/[name][ext]"
  184. }
  185. },
  186. { // Third party images are inlined
  187. test: /\.(png|jpg|gif)$/,
  188. exclude: /web\/static/,
  189. type: "asset/inline",
  190. },
  191. ]
  192. },
  193. stats: {
  194. children: false,
  195. chunks: false,
  196. modules: false,
  197. entrypoints: false
  198. },
  199. ignoreWarnings: [
  200. /source-map/,
  201. /source map/,
  202. /dependency is an expression/,
  203. /export 'default'/,
  204. /Can't resolve 'sodium'/
  205. ],
  206. performance: {
  207. hints: false
  208. }
  209. };