webpack.config.js 9.1 KB

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