webpack.config.js 10 KB

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