webpack.config.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. test: /latex-linter.worker.js$/,
  111. use: [
  112. {
  113. loader: 'worker-loader',
  114. options: {
  115. name: 'js/latex-linter.worker.js',
  116. publicPath: '/',
  117. },
  118. },
  119. ],
  120. },
  121. {
  122. // Pass Less files through less-loader/css-loader/mini-css-extract-
  123. // plugin (note: run in reverse order)
  124. test: /\.less$/,
  125. use: [
  126. // Allows the CSS to be extracted to a separate .css file
  127. { loader: MiniCssExtractPlugin.loader },
  128. // Resolves any CSS dependencies (e.g. url())
  129. { loader: 'css-loader' },
  130. {
  131. // Runs autoprefixer on CSS via postcss
  132. loader: 'postcss-loader',
  133. options: {
  134. // Uniquely identifies the postcss plugin (required by webpack)
  135. ident: 'postcss',
  136. plugins: [require('autoprefixer')],
  137. },
  138. },
  139. // Compiles the Less syntax to CSS
  140. { loader: 'less-loader' },
  141. ],
  142. },
  143. {
  144. // Pass CSS files through css-loader & mini-css-extract-plugin (note: run in reverse order)
  145. test: /\.css$/i,
  146. use: [MiniCssExtractPlugin.loader, 'css-loader'],
  147. },
  148. {
  149. // Load fonts
  150. test: /\.(woff|woff2)$/,
  151. use: [
  152. {
  153. loader: 'file-loader',
  154. options: {
  155. // Output to public/font
  156. outputPath: 'fonts',
  157. publicPath: '/fonts/',
  158. name: '[name].[ext]',
  159. },
  160. },
  161. ],
  162. },
  163. {
  164. // Load images (static files)
  165. test: /\.(svg|gif|png|jpg|pdf)$/,
  166. use: [
  167. {
  168. loader: 'file-loader',
  169. options: {
  170. // Output to public/images
  171. outputPath: 'images',
  172. publicPath: '/images/',
  173. name: '[name].[ext]',
  174. },
  175. },
  176. ],
  177. },
  178. {
  179. // These options are necessary for handlebars to have access to helper
  180. // methods
  181. test: /\.handlebars$/,
  182. loader: 'handlebars-loader',
  183. options: {
  184. compat: true,
  185. knownHelpersOnly: false,
  186. runtimePath: 'handlebars/runtime',
  187. },
  188. },
  189. {
  190. // Load translations files with custom loader, to extract and apply
  191. // fallbacks
  192. test: /locales\/(\w{2}(-\w{2})?)\.json$/,
  193. use: [
  194. {
  195. loader: path.resolve('frontend/translations-loader.js'),
  196. },
  197. ],
  198. },
  199. // Allow for injection of modules dependencies by reading contents of
  200. // modules directory and adding necessary dependencies
  201. {
  202. test: path.join(__dirname, 'modules/modules-main.js'),
  203. use: [
  204. {
  205. loader: 'val-loader',
  206. },
  207. ],
  208. },
  209. {
  210. test: path.join(__dirname, 'modules/modules-ide.js'),
  211. use: [
  212. {
  213. loader: 'val-loader',
  214. },
  215. ],
  216. },
  217. {
  218. // Expose jQuery and $ global variables
  219. test: require.resolve('jquery'),
  220. use: [
  221. {
  222. loader: 'expose-loader',
  223. options: 'jQuery',
  224. },
  225. {
  226. loader: 'expose-loader',
  227. options: '$',
  228. },
  229. ],
  230. },
  231. {
  232. // Expose angular global variable
  233. test: require.resolve('angular'),
  234. use: [
  235. {
  236. loader: 'expose-loader',
  237. options: 'angular',
  238. },
  239. ],
  240. },
  241. ],
  242. },
  243. resolve: {
  244. alias: {
  245. // Aliases for AMD modules
  246. // Enables ace/ace shortcut
  247. ace: 'ace-builds/src-noconflict',
  248. // fineupload vendored dependency (which we're aliasing to fineuploadER
  249. // for some reason)
  250. fineuploader: path.join(
  251. __dirname,
  252. `frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
  253. ),
  254. },
  255. },
  256. // Split out files into separate (derived) bundles if they are shared between
  257. // multiple (explicit) bundles, according to some webpack heuristics
  258. optimization: {
  259. splitChunks: {
  260. chunks: 'all',
  261. },
  262. },
  263. plugins: [
  264. // Generate a manifest.json file which is used by the backend to map the
  265. // base filenames to the generated output filenames
  266. new WebpackAssetsManifest({
  267. entrypoints: true,
  268. publicPath: true,
  269. }),
  270. // Prevent moment from loading (very large) locale files that aren't used
  271. new webpack.IgnorePlugin({
  272. resourceRegExp: /^\.\/locale$/,
  273. contextRegExp: /moment$/,
  274. }),
  275. // Copy the required files for loading MathJax from MathJax NPM package
  276. new CopyPlugin(
  277. [
  278. { from: 'MathJax.js', to: 'js/libs/mathjax' },
  279. { from: 'config/**/*', to: 'js/libs/mathjax' },
  280. { from: 'extensions/**/*', to: 'js/libs/mathjax' },
  281. { from: 'localization/en/**/*', to: 'js/libs/mathjax' },
  282. { from: 'jax/output/HTML-CSS/fonts/TeX/**/*', to: 'js/libs/mathjax' },
  283. { from: 'jax/output/HTML-CSS/**/*.js', to: 'js/libs/mathjax' },
  284. { from: 'jax/element/**/*', to: 'js/libs/mathjax' },
  285. { from: 'jax/input/**/*', to: 'js/libs/mathjax' },
  286. { from: 'fonts/HTML-CSS/TeX/woff/*', to: 'js/libs/mathjax' },
  287. ],
  288. {
  289. context: 'node_modules/mathjax',
  290. }
  291. ),
  292. new CopyPlugin([
  293. {
  294. from: 'frontend/js/vendor/libs/sigma-master',
  295. to: 'js/libs/sigma-master',
  296. },
  297. {
  298. from: 'node_modules/ace-builds/src-min-noconflict',
  299. to: `js/ace-${PackageVersions.version.ace}/`,
  300. },
  301. // Copy CMap files (used to provide support for non-Latin characters)
  302. // and static images from pdfjs-dist package to build output.
  303. { from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' },
  304. {
  305. from: 'node_modules/pdfjs-dist/legacy/web/images',
  306. to: 'images',
  307. },
  308. ]),
  309. ],
  310. }