webpack.config.js 9.3 KB

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