webpack.config.js 8.3 KB

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