webpack.config.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 underscore global variable
  138. test: path.join(
  139. __dirname,
  140. `frontend/js/vendor/libs/${PackageVersions.lib('underscore')}.js`
  141. ),
  142. use: [
  143. {
  144. loader: 'expose-loader',
  145. options: '_'
  146. }
  147. ]
  148. },
  149. {
  150. // Expose Algolia global variable
  151. test: path.join(
  152. __dirname,
  153. `frontend/js/vendor/libs/${PackageVersions.lib('algolia')}.js`
  154. ),
  155. use: [
  156. {
  157. loader: 'expose-loader',
  158. options: 'AlgoliaSearch'
  159. }
  160. ]
  161. }
  162. ]
  163. },
  164. resolve: {
  165. alias: {
  166. // Aliases for AMD modules
  167. // Vendored dependencies in public/src/vendor/libs (e.g. angular)
  168. libs: path.join(__dirname, 'frontend/js/vendor/libs'),
  169. // Use vendored moment (with correct version)
  170. moment: path.join(
  171. __dirname,
  172. `frontend/js/vendor/libs/${PackageVersions.lib('moment')}`
  173. ),
  174. // Enables ace/ace shortcut
  175. ace: path.join(
  176. __dirname,
  177. `frontend/js/vendor/${PackageVersions.lib('ace')}`
  178. ),
  179. // fineupload vendored dependency (which we're aliasing to fineuploadER
  180. // for some reason)
  181. fineuploader: path.join(
  182. __dirname,
  183. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  184. )
  185. },
  186. // Define what can be imported with out an absolute or relative path. This
  187. // is because we need to override the default (which is just node_modules)
  188. // to get AMD modules in public/src to work as they do not use relative/
  189. // absolute paths for dependencies
  190. modules: ['frontend/js', 'node_modules']
  191. },
  192. // Split out vendored dependencies that are shared between 2 or more "real
  193. // bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
  194. // that they are de-duplicated from the other bundles. This allows the
  195. // libraries bundle to be independently cached (as it likely will change less
  196. // than the other bundles)
  197. optimization: {
  198. splitChunks: {
  199. cacheGroups: {
  200. libraries: {
  201. test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
  202. name: 'libraries',
  203. chunks: 'initial',
  204. minChunks: 2
  205. }
  206. }
  207. }
  208. },
  209. plugins: [
  210. // Generate a manifest.json file which is used by the backend to map the
  211. // base filenames to the generated output filenames
  212. new ManifestPlugin({
  213. // Always write the manifest file to disk (even if in dev mode, where
  214. // files are held in memory). This is needed because the server will read
  215. // this file (from disk) when building the script's url
  216. writeToFileEmit: true
  217. }),
  218. // Silence warning when loading moment from vendored dependencies as it
  219. // attempts to load locales.js file which does not exist (but this is fine
  220. // as we don't want to load the large amount of locale data from moment)
  221. new webpack.IgnorePlugin(/^\.\/locale$/, /frontend\/js\/vendor\/libs/),
  222. new CopyPlugin([
  223. {
  224. from: 'frontend/js/vendor/libs/angular-1.6.4.min.js',
  225. to: 'js/libs/angular-1.6.4.min.js'
  226. },
  227. {
  228. from: 'frontend/js/vendor/libs/angular-1.6.4.min.js.map',
  229. to: 'js/libs/angular-1.6.4.min.js.map'
  230. },
  231. {
  232. from: 'frontend/js/vendor/libs/jquery-1.11.1.min.js',
  233. to: 'js/libs/jquery-1.11.1.min.js'
  234. },
  235. {
  236. from: 'frontend/js/vendor/libs/jquery-1.11.1.min.js.map',
  237. to: 'js/libs/jquery-1.11.1.min.js.map'
  238. },
  239. {
  240. from: 'frontend/js/vendor/libs/mathjax',
  241. to: 'js/libs/mathjax'
  242. },
  243. {
  244. from: 'frontend/js/vendor/libs/sigma-master',
  245. to: 'js/libs/sigma-master'
  246. },
  247. {
  248. from: `frontend/js/vendor/ace-${PackageVersions.version.ace}/`,
  249. to: `js/ace-${PackageVersions.version.ace}/`
  250. },
  251. // Copy CMap files from pdfjs-dist package to build output. These are used
  252. // to provide support for non-Latin characters
  253. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' }
  254. ])
  255. ],
  256. // If jquery or underscore is required by another dependency *don't* include
  257. // in the bundle and use the relevant global variable instead
  258. externals: {
  259. jquery: '$',
  260. underscore: '_'
  261. }
  262. }