webpack.config.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // These options are necessary for handlebars to have access to helper
  109. // methods
  110. test: /\.handlebars$/,
  111. loader: 'handlebars-loader',
  112. options: {
  113. compat: true,
  114. knownHelpersOnly: false,
  115. runtimePath: 'handlebars/runtime'
  116. }
  117. },
  118. // Allow for injection of modules dependencies by reading contents of
  119. // modules directory and adding necessary dependencies
  120. {
  121. test: path.join(__dirname, 'modules/modules-main.js'),
  122. use: [
  123. {
  124. loader: 'val-loader'
  125. }
  126. ]
  127. },
  128. {
  129. test: path.join(__dirname, 'modules/modules-ide.js'),
  130. use: [
  131. {
  132. loader: 'val-loader'
  133. }
  134. ]
  135. },
  136. {
  137. // Expose jQuery and $ global variables
  138. test: require.resolve('jquery'),
  139. use: [
  140. {
  141. loader: 'expose-loader',
  142. options: 'jQuery'
  143. },
  144. {
  145. loader: 'expose-loader',
  146. options: '$'
  147. }
  148. ]
  149. },
  150. {
  151. // Expose angular global variable
  152. test: require.resolve('angular'),
  153. use: [
  154. {
  155. loader: 'expose-loader',
  156. options: 'angular'
  157. }
  158. ]
  159. },
  160. {
  161. // Expose lodash global variable
  162. test: require.resolve('lodash'),
  163. use: [
  164. {
  165. loader: 'expose-loader',
  166. options: '_'
  167. }
  168. ]
  169. }
  170. ]
  171. },
  172. resolve: {
  173. alias: {
  174. // Aliases for AMD modules
  175. // Shortcut to vendored dependencies in frontend/js/vendor/libs
  176. libs: path.join(__dirname, 'frontend/js/vendor/libs'),
  177. // Enables ace/ace shortcut
  178. ace: path.join(
  179. __dirname,
  180. `frontend/js/vendor/${PackageVersions.lib('ace')}`
  181. ),
  182. // fineupload vendored dependency (which we're aliasing to fineuploadER
  183. // for some reason)
  184. fineuploader: path.join(
  185. __dirname,
  186. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  187. )
  188. },
  189. // Define what can be imported with out an absolute or relative path. This
  190. // is because we need to override the default (which is just node_modules)
  191. // to get AMD modules in public/src to work as they do not use relative/
  192. // absolute paths for dependencies
  193. modules: ['frontend/js', 'node_modules']
  194. },
  195. // Split out vendored dependencies that are shared between 2 or more "real
  196. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  197. // that they are de-duplicated from the other bundles. This allows the
  198. // libraries bundle to be independently cached (as it likely will change less
  199. // than the other bundles)
  200. optimization: {
  201. splitChunks: {
  202. cacheGroups: {
  203. libraries: {
  204. test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
  205. name: 'libraries',
  206. chunks: 'initial',
  207. minChunks: 2
  208. }
  209. }
  210. }
  211. },
  212. plugins: [
  213. // Generate a manifest.json file which is used by the backend to map the
  214. // base filenames to the generated output filenames
  215. new ManifestPlugin({
  216. // Always write the manifest file to disk (even if in dev mode, where
  217. // files are held in memory). This is needed because the server will read
  218. // this file (from disk) when building the script's url
  219. writeToFileEmit: true
  220. }),
  221. // Prevent moment from loading (very large) locale files that aren't used
  222. new webpack.IgnorePlugin({
  223. resourceRegExp: /^\.\/locale$/,
  224. contextRegExp: /moment$/
  225. }),
  226. new CopyPlugin([
  227. {
  228. from: 'frontend/js/vendor/libs/mathjax',
  229. to: 'js/libs/mathjax'
  230. },
  231. {
  232. from: 'frontend/js/vendor/libs/sigma-master',
  233. to: 'js/libs/sigma-master'
  234. },
  235. {
  236. from: `frontend/js/vendor/ace-${PackageVersions.version.ace}/`,
  237. to: `js/ace-${PackageVersions.version.ace}/`
  238. },
  239. // Copy CMap files from pdfjs-dist package to build output. These are used
  240. // to provide support for non-Latin characters
  241. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' }
  242. ])
  243. ]
  244. }