webpack.config.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. const fs = require('fs')
  2. const path = require('path')
  3. const webpack = require('webpack')
  4. const CopyPlugin = require('copy-webpack-plugin')
  5. const ManifestPlugin = require('webpack-manifest-plugin')
  6. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  7. const PackageVersions = require('./app/src/infrastructure/PackageVersions')
  8. const MODULES_PATH = path.join(__dirname, '/modules')
  9. // Generate a hash of entry points, including modules
  10. const entryPoints = {
  11. main: './frontend/js/main.js',
  12. ide: './frontend/js/ide.js',
  13. style: './frontend/stylesheets/style.less',
  14. 'ieee-style': './frontend/stylesheets/ieee-style.less',
  15. 'light-style': './frontend/stylesheets/light-style.less'
  16. }
  17. // Attempt to load frontend entry-points from modules, if they exist
  18. if (fs.existsSync(MODULES_PATH)) {
  19. fs.readdirSync(MODULES_PATH).reduce((acc, module) => {
  20. const entryPath = path.join(MODULES_PATH, module, '/frontend/js/index.js')
  21. if (fs.existsSync(entryPath)) {
  22. acc[module] = entryPath
  23. }
  24. return acc
  25. }, entryPoints)
  26. }
  27. module.exports = {
  28. // Defines the "entry point(s)" for the application - i.e. the file which
  29. // bootstraps the application
  30. entry: entryPoints,
  31. // Define where and how the bundle will be output to disk
  32. // Note: webpack-dev-server does not write the bundle to disk, instead it is
  33. // kept in memory for speed
  34. output: {
  35. path: path.join(__dirname, '/public'),
  36. // By default write into js directory
  37. filename: 'js/[name].js',
  38. // Output as UMD bundle (allows main JS to import with CJS, AMD or global
  39. // style code bundles
  40. libraryTarget: 'umd',
  41. // Name the exported variable from output bundle
  42. library: ['Frontend', '[name]']
  43. },
  44. // Define how file types are handled by webpack
  45. module: {
  46. rules: [
  47. {
  48. // Pass application JS files through babel-loader, compiling to ES5
  49. test: /\.js$/,
  50. // Only compile application files (npm and vendored dependencies are in
  51. // ES5 already)
  52. exclude: [
  53. /node_modules\/(?!react-dnd\/)/,
  54. path.resolve(__dirname, 'frontend/js/vendor')
  55. ],
  56. use: [
  57. {
  58. loader: 'babel-loader',
  59. options: {
  60. // Configure babel-loader to cache compiled output so that
  61. // subsequent compile runs are much faster
  62. cacheDirectory: true
  63. }
  64. }
  65. ]
  66. },
  67. {
  68. // Wrap PDF.js worker in a Web Worker
  69. test: /pdf\.worker\.js$/,
  70. use: [
  71. {
  72. loader: 'worker-loader',
  73. options: {
  74. // Write into js directory (note: customising this is not possible
  75. // with pdfjs-dist/webpack auto loader)
  76. name: 'js/pdfjs-worker.[hash].js',
  77. // Override dynamically-set publicPath to explicitly use root.
  78. // This prevents a security problem where the Worker - normally
  79. // loaded from a CDN - has cross-origin issues, by forcing it to not
  80. // be loaded from the CDN
  81. publicPath: '/'
  82. }
  83. }
  84. ]
  85. },
  86. {
  87. // Pass Less files through less-loader/css-loader/mini-css-extract-
  88. // plugin (note: run in reverse order)
  89. test: /\.less$/,
  90. use: [
  91. // Allows the CSS to be extracted to a separate .css file
  92. { loader: MiniCssExtractPlugin.loader },
  93. // Resolves any CSS dependencies (e.g. url())
  94. { loader: 'css-loader' },
  95. {
  96. // Runs autoprefixer on CSS via postcss
  97. loader: 'postcss-loader',
  98. options: {
  99. // Uniquely identifies the postcss plugin (required by webpack)
  100. ident: 'postcss',
  101. plugins: [require('autoprefixer')]
  102. }
  103. },
  104. // Compiles the Less syntax to CSS
  105. { loader: 'less-loader' }
  106. ]
  107. },
  108. {
  109. // Pass CSS files through css-loader & mini-css-extract-plugin (note: run in reverse order)
  110. test: /\.css$/i,
  111. use: [MiniCssExtractPlugin.loader, 'css-loader']
  112. },
  113. {
  114. // Load fonts
  115. test: /\.(woff|woff2)$/,
  116. use: [
  117. {
  118. loader: 'file-loader',
  119. options: {
  120. // Output to public/font
  121. outputPath: 'fonts',
  122. publicPath: '/fonts/',
  123. name: '[name].[ext]'
  124. }
  125. }
  126. ]
  127. },
  128. {
  129. // These options are necessary for handlebars to have access to helper
  130. // methods
  131. test: /\.handlebars$/,
  132. loader: 'handlebars-loader',
  133. options: {
  134. compat: true,
  135. knownHelpersOnly: false,
  136. runtimePath: 'handlebars/runtime'
  137. }
  138. },
  139. {
  140. // Load translations files with custom loader, to extract and apply
  141. // fallbacks
  142. test: /locales\/(\w{2}(-\w{2})?)\.json$/,
  143. use: [
  144. {
  145. loader: path.resolve('frontend/translations-loader.js')
  146. }
  147. ]
  148. },
  149. // Allow for injection of modules dependencies by reading contents of
  150. // modules directory and adding necessary dependencies
  151. {
  152. test: path.join(__dirname, 'modules/modules-main.js'),
  153. use: [
  154. {
  155. loader: 'val-loader'
  156. }
  157. ]
  158. },
  159. {
  160. test: path.join(__dirname, 'modules/modules-ide.js'),
  161. use: [
  162. {
  163. loader: 'val-loader'
  164. }
  165. ]
  166. },
  167. {
  168. // Expose jQuery and $ global variables
  169. test: require.resolve('jquery'),
  170. use: [
  171. {
  172. loader: 'expose-loader',
  173. options: 'jQuery'
  174. },
  175. {
  176. loader: 'expose-loader',
  177. options: '$'
  178. }
  179. ]
  180. },
  181. {
  182. // Expose angular global variable
  183. test: require.resolve('angular'),
  184. use: [
  185. {
  186. loader: 'expose-loader',
  187. options: 'angular'
  188. }
  189. ]
  190. }
  191. ]
  192. },
  193. resolve: {
  194. alias: {
  195. // Aliases for AMD modules
  196. // Shortcut to vendored dependencies in frontend/js/vendor/libs
  197. libs: path.join(__dirname, 'frontend/js/vendor/libs'),
  198. // Enables ace/ace shortcut
  199. ace: 'ace-builds/src-noconflict',
  200. // fineupload vendored dependency (which we're aliasing to fineuploadER
  201. // for some reason)
  202. fineuploader: path.join(
  203. __dirname,
  204. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  205. )
  206. }
  207. },
  208. // Split out vendored dependencies that are shared between 2 or more "real
  209. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  210. // that they are de-duplicated from the other bundles. This allows the
  211. // libraries bundle to be independently cached (as it likely will change less
  212. // than the other bundles)
  213. optimization: {
  214. splitChunks: {
  215. cacheGroups: {
  216. libraries: {
  217. test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
  218. name: 'libraries',
  219. chunks: 'initial',
  220. minChunks: 2
  221. }
  222. }
  223. }
  224. },
  225. plugins: [
  226. // Generate a manifest.json file which is used by the backend to map the
  227. // base filenames to the generated output filenames
  228. new ManifestPlugin({
  229. // Always write the manifest file to disk (even if in dev mode, where
  230. // files are held in memory). This is needed because the server will read
  231. // this file (from disk) when building the script's url
  232. writeToFileEmit: true
  233. }),
  234. // Silence react messages in the dev-tools console
  235. new webpack.DefinePlugin({
  236. __REACT_DEVTOOLS_GLOBAL_HOOK__: '({ isDisabled: true })'
  237. }),
  238. // Prevent moment from loading (very large) locale files that aren't used
  239. new webpack.IgnorePlugin({
  240. resourceRegExp: /^\.\/locale$/,
  241. contextRegExp: /moment$/
  242. }),
  243. // Copy the required files for loading MathJax from MathJax NPM package
  244. new CopyPlugin(
  245. [
  246. { from: 'MathJax.js', to: 'js/libs/mathjax' },
  247. { from: 'config/**/*', to: 'js/libs/mathjax' },
  248. { from: 'extensions/**/*', to: 'js/libs/mathjax' },
  249. { from: 'localization/en/**/*', to: 'js/libs/mathjax' },
  250. { from: 'jax/output/HTML-CSS/fonts/TeX/**/*', to: 'js/libs/mathjax' },
  251. { from: 'jax/output/HTML-CSS/**/*.js', to: 'js/libs/mathjax' },
  252. { from: 'jax/element/**/*', to: 'js/libs/mathjax' },
  253. { from: 'jax/input/**/*', to: 'js/libs/mathjax' },
  254. { from: 'fonts/HTML-CSS/TeX/woff/*', to: 'js/libs/mathjax' }
  255. ],
  256. {
  257. context: 'node_modules/mathjax'
  258. }
  259. ),
  260. new CopyPlugin([
  261. {
  262. from: 'frontend/js/vendor/libs/sigma-master',
  263. to: 'js/libs/sigma-master'
  264. },
  265. {
  266. from: 'node_modules/ace-builds/src-min-noconflict',
  267. to: `js/ace-${PackageVersions.version.ace}/`
  268. },
  269. // Copy CMap files from pdfjs-dist package to build output. These are used
  270. // to provide support for non-Latin characters
  271. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' }
  272. ])
  273. ]
  274. }