webpack.config.js 8.6 KB

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