webpack.config.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // Serve from the root of public directory
  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 (dependencies are in ES5 already)
  54. exclude: /node_modules/,
  55. use: [
  56. {
  57. loader: 'babel-loader',
  58. options: {
  59. // Configure babel-loader to cache compiled output so that
  60. // subsequent compile runs are much faster
  61. cacheDirectory: true
  62. }
  63. }
  64. ]
  65. },
  66. {
  67. // Wrap PDF.js worker in a Web Worker
  68. test: /pdf\.worker\.js$/,
  69. use: [
  70. {
  71. loader: 'worker-loader',
  72. options: {
  73. // Write into js directory (note: customising this is not possible
  74. // with pdfjs-dist/webpack auto loader)
  75. name: 'js/pdfjs-worker.[hash].js'
  76. }
  77. }
  78. ]
  79. },
  80. {
  81. // Pass Less files through less-loader/css-loader/mini-css-extract-
  82. // plugin (note: run in reverse order)
  83. test: /\.less$/,
  84. use: [
  85. // Allows the CSS to be extracted to a separate .css file
  86. { loader: MiniCssExtractPlugin.loader },
  87. // Resolves any CSS dependencies (e.g. url())
  88. { loader: 'css-loader' },
  89. {
  90. // Runs autoprefixer on CSS via postcss
  91. loader: 'postcss-loader',
  92. options: {
  93. // Uniquely identifies the postcss plugin (required by webpack)
  94. ident: 'postcss',
  95. plugins: [
  96. require('autoprefixer')({ env: 'last 2 versions, ie >= 10' })
  97. ]
  98. }
  99. },
  100. // Compiles the Less syntax to CSS
  101. { loader: 'less-loader' }
  102. ]
  103. },
  104. {
  105. // These options are necessary for handlebars to have access to helper
  106. // methods
  107. test: /\.handlebars$/,
  108. loader: 'handlebars-loader',
  109. options: {
  110. compat: true,
  111. knownHelpersOnly: false,
  112. runtimePath: 'handlebars/runtime'
  113. }
  114. },
  115. // Allow for injection of modules dependencies by reading contents of
  116. // modules directory and adding necessary dependencies
  117. {
  118. test: path.join(__dirname, 'modules/modules-main.js'),
  119. use: [
  120. {
  121. loader: 'val-loader'
  122. }
  123. ]
  124. },
  125. {
  126. test: path.join(__dirname, 'modules/modules-ide.js'),
  127. use: [
  128. {
  129. loader: 'val-loader'
  130. }
  131. ]
  132. },
  133. {
  134. // Expose underscore global variable
  135. test: path.join(
  136. __dirname,
  137. `frontend/js/vendor/libs/${PackageVersions.lib('underscore')}.js`
  138. ),
  139. use: [
  140. {
  141. loader: 'expose-loader',
  142. options: '_'
  143. }
  144. ]
  145. },
  146. {
  147. // Expose Algolia global variable
  148. test: path.join(
  149. __dirname,
  150. `frontend/js/vendor/libs/${PackageVersions.lib('algolia')}.js`
  151. ),
  152. use: [
  153. {
  154. loader: 'expose-loader',
  155. options: 'AlgoliaSearch'
  156. }
  157. ]
  158. }
  159. ]
  160. },
  161. resolve: {
  162. alias: {
  163. // Aliases for AMD modules
  164. // Vendored dependencies in public/src/vendor/libs (e.g. angular)
  165. libs: path.join(__dirname, 'frontend/js/vendor/libs'),
  166. // Use vendored moment (with correct version)
  167. moment: path.join(
  168. __dirname,
  169. `frontend/js/vendor/libs/${PackageVersions.lib('moment')}`
  170. ),
  171. // Enables ace/ace shortcut
  172. ace: path.join(
  173. __dirname,
  174. `frontend/js/vendor/${PackageVersions.lib('ace')}`
  175. ),
  176. // fineupload vendored dependency (which we're aliasing to fineuploadER
  177. // for some reason)
  178. fineuploader: path.join(
  179. __dirname,
  180. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  181. )
  182. },
  183. // Define what can be imported with out an absolute or relative path. This
  184. // is because we need to override the default (which is just node_modules)
  185. // to get AMD modules in public/src to work as they do not use relative/
  186. // absolute paths for dependencies
  187. modules: ['frontend/js', 'node_modules']
  188. },
  189. // Split out vendored dependencies that are shared between 2 or more "real
  190. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  191. // that they are de-duplicated from the other bundles. This allows the
  192. // libraries bundle to be independently cached (as it likely will change less
  193. // than the other bundles)
  194. optimization: {
  195. splitChunks: {
  196. cacheGroups: {
  197. libraries: {
  198. test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
  199. name: 'libraries',
  200. chunks: 'initial',
  201. minChunks: 2
  202. }
  203. }
  204. }
  205. },
  206. plugins: [
  207. // Generate a manifest.json file which is used by the backend to map the
  208. // base filenames to the generated output filenames
  209. new ManifestPlugin({
  210. // Always write the manifest file to disk (even if in dev mode, where
  211. // files are held in memory). This is needed because the server will read
  212. // this file (from disk) when building the script's url
  213. writeToFileEmit: true
  214. }),
  215. // Silence warning when loading moment from vendored dependencies as it
  216. // attempts to load locales.js file which does not exist (but this is fine
  217. // as we don't want to load the large amount of locale data from moment)
  218. new webpack.IgnorePlugin(/^\.\/locale$/, /frontend\/js\/vendor\/libs/),
  219. new CopyPlugin([
  220. {
  221. from: 'frontend/js/vendor/libs/angular-1.6.4.min.js',
  222. to: 'js/libs/angular-1.6.4.min.js'
  223. },
  224. {
  225. from: 'frontend/js/vendor/libs/angular-1.6.4.min.js.map',
  226. to: 'js/libs/angular-1.6.4.min.js.map'
  227. },
  228. {
  229. from: 'frontend/js/vendor/libs/jquery-1.11.1.min.js',
  230. to: 'js/libs/jquery-1.11.1.min.js'
  231. },
  232. {
  233. from: 'frontend/js/vendor/libs/jquery-1.11.1.min.js.map',
  234. to: 'js/libs/jquery-1.11.1.min.js.map'
  235. },
  236. {
  237. from: 'frontend/js/vendor/libs/mathjax',
  238. to: 'js/libs/mathjax'
  239. },
  240. {
  241. from: 'frontend/js/vendor/libs/sigma-master',
  242. to: 'js/libs/sigma-master'
  243. },
  244. {
  245. from: `frontend/js/vendor/ace-${PackageVersions.version.ace}/`,
  246. to: `js/ace-${PackageVersions.version.ace}/`
  247. },
  248. // Copy CMap files from pdfjs-dist package to build output. These are used
  249. // to provide support for non-Latin characters
  250. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' }
  251. ])
  252. ],
  253. // If jquery or underscore is required by another dependency *don't* include
  254. // in the bundle and use the relevant global variable instead
  255. externals: {
  256. jquery: '$',
  257. underscore: '_'
  258. }
  259. }