webpack.config.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. 'cdn-load-test': './frontend/js/cdn-load-test.js',
  14. marketing: './frontend/js/marketing.js',
  15. style: './frontend/stylesheets/style.less',
  16. 'ieee-style': './frontend/stylesheets/ieee-style.less',
  17. 'light-style': './frontend/stylesheets/light-style.less',
  18. }
  19. // Add entrypoints for each "page"
  20. glob
  21. .sync(path.join(__dirname, 'modules/*/frontend/js/pages/**/*.js'))
  22. .forEach(page => {
  23. // in: /workspace/services/web/modules/foo/frontend/js/pages/bar.js
  24. // out: modules/foo/pages/bar
  25. const name = path
  26. .relative(__dirname, page)
  27. .replace(/frontend[/]js[/]/, '')
  28. .replace(/.js$/, '')
  29. entryPoints[name] = './' + path.relative(__dirname, page)
  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. // Load images (static files)
  154. test: /\.(svg|gif|png|jpg|pdf)$/,
  155. use: [
  156. {
  157. loader: 'file-loader',
  158. options: {
  159. // Output to public/images
  160. outputPath: 'images',
  161. publicPath: '/images/',
  162. name: '[name].[ext]',
  163. },
  164. },
  165. ],
  166. },
  167. {
  168. // These options are necessary for handlebars to have access to helper
  169. // methods
  170. test: /\.handlebars$/,
  171. loader: 'handlebars-loader',
  172. options: {
  173. compat: true,
  174. knownHelpersOnly: false,
  175. runtimePath: 'handlebars/runtime',
  176. },
  177. },
  178. {
  179. // Load translations files with custom loader, to extract and apply
  180. // fallbacks
  181. test: /locales\/(\w{2}(-\w{2})?)\.json$/,
  182. use: [
  183. {
  184. loader: path.resolve('frontend/translations-loader.js'),
  185. },
  186. ],
  187. },
  188. // Allow for injection of modules dependencies by reading contents of
  189. // modules directory and adding necessary dependencies
  190. {
  191. test: path.join(__dirname, 'modules/modules-main.js'),
  192. use: [
  193. {
  194. loader: 'val-loader',
  195. },
  196. ],
  197. },
  198. {
  199. test: path.join(__dirname, 'modules/modules-ide.js'),
  200. use: [
  201. {
  202. loader: 'val-loader',
  203. },
  204. ],
  205. },
  206. {
  207. // Expose jQuery and $ global variables
  208. test: require.resolve('jquery'),
  209. use: [
  210. {
  211. loader: 'expose-loader',
  212. options: 'jQuery',
  213. },
  214. {
  215. loader: 'expose-loader',
  216. options: '$',
  217. },
  218. ],
  219. },
  220. {
  221. // Expose angular global variable
  222. test: require.resolve('angular'),
  223. use: [
  224. {
  225. loader: 'expose-loader',
  226. options: 'angular',
  227. },
  228. ],
  229. },
  230. ],
  231. },
  232. resolve: {
  233. alias: {
  234. // Aliases for AMD modules
  235. // Enables ace/ace shortcut
  236. ace: 'ace-builds/src-noconflict',
  237. // fineupload vendored dependency (which we're aliasing to fineuploadER
  238. // for some reason)
  239. fineuploader: path.join(
  240. __dirname,
  241. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  242. ),
  243. },
  244. },
  245. // Split out files into separate (derived) bundles if they are shared between
  246. // multiple (explicit) bundles, according to some webpack heuristics
  247. optimization: {
  248. splitChunks: {
  249. chunks: 'all',
  250. },
  251. },
  252. plugins: [
  253. // Generate a manifest.json file which is used by the backend to map the
  254. // base filenames to the generated output filenames
  255. new WebpackAssetsManifest({
  256. entrypoints: true,
  257. publicPath: true,
  258. }),
  259. // Prevent moment from loading (very large) locale files that aren't used
  260. new webpack.IgnorePlugin({
  261. resourceRegExp: /^\.\/locale$/,
  262. contextRegExp: /moment$/,
  263. }),
  264. // Copy the required files for loading MathJax from MathJax NPM package
  265. new CopyPlugin(
  266. [
  267. { from: 'MathJax.js', to: 'js/libs/mathjax' },
  268. { from: 'config/**/*', to: 'js/libs/mathjax' },
  269. { from: 'extensions/**/*', to: 'js/libs/mathjax' },
  270. { from: 'localization/en/**/*', to: 'js/libs/mathjax' },
  271. { from: 'jax/output/HTML-CSS/fonts/TeX/**/*', to: 'js/libs/mathjax' },
  272. { from: 'jax/output/HTML-CSS/**/*.js', to: 'js/libs/mathjax' },
  273. { from: 'jax/element/**/*', to: 'js/libs/mathjax' },
  274. { from: 'jax/input/**/*', to: 'js/libs/mathjax' },
  275. { from: 'fonts/HTML-CSS/TeX/woff/*', to: 'js/libs/mathjax' },
  276. ],
  277. {
  278. context: 'node_modules/mathjax',
  279. }
  280. ),
  281. new CopyPlugin([
  282. {
  283. from: 'frontend/js/vendor/libs/sigma-master',
  284. to: 'js/libs/sigma-master',
  285. },
  286. {
  287. from: 'node_modules/ace-builds/src-min-noconflict',
  288. to: `js/ace-${PackageVersions.version.ace}/`,
  289. },
  290. // Copy CMap files (used to provide support for non-Latin characters)
  291. // and static images from pdfjs-dist package to build output.
  292. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' },
  293. {
  294. from: 'node_modules/pdfjs-dist/legacy/web/images',
  295. to: 'images',
  296. },
  297. ]),
  298. ],
  299. }