webpack.config.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 pdfjsVersions = ['pdfjs-dist210', 'pdfjs-dist213']
  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 worker scripts in a Web Worker
  93. test: /\.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/[name].[contenthash].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. // 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. symlinks: false,
  256. extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
  257. },
  258. // Split out files into separate (derived) bundles if they are shared between
  259. // multiple (explicit) bundles, according to some webpack heuristics
  260. optimization: {
  261. splitChunks: {
  262. chunks: 'all',
  263. },
  264. },
  265. plugins: [
  266. // Generate a manifest.json file which is used by the backend to map the
  267. // base filenames to the generated output filenames
  268. new WebpackAssetsManifest({
  269. entrypoints: true,
  270. publicPath: true,
  271. }),
  272. // Prevent moment from loading (very large) locale files that aren't used
  273. new webpack.IgnorePlugin({
  274. resourceRegExp: /^\.\/locale$/,
  275. contextRegExp: /moment$/,
  276. }),
  277. // Copy the required files for loading MathJax from MathJax NPM package
  278. new CopyPlugin(
  279. [
  280. { from: 'MathJax.js', to: 'js/libs/mathjax' },
  281. { from: 'config/**/*', to: 'js/libs/mathjax' },
  282. { from: 'extensions/**/*', to: 'js/libs/mathjax' },
  283. { from: 'localization/en/**/*', to: 'js/libs/mathjax' },
  284. { from: 'jax/output/HTML-CSS/fonts/TeX/**/*', to: 'js/libs/mathjax' },
  285. { from: 'jax/output/HTML-CSS/**/*.js', to: 'js/libs/mathjax' },
  286. { from: 'jax/element/**/*', to: 'js/libs/mathjax' },
  287. { from: 'jax/input/**/*', to: 'js/libs/mathjax' },
  288. { from: 'fonts/HTML-CSS/TeX/woff/*', to: 'js/libs/mathjax' },
  289. ],
  290. {
  291. context: mathjaxDir,
  292. }
  293. ),
  294. new CopyPlugin([
  295. {
  296. from: 'frontend/js/vendor/libs/sigma-master',
  297. to: 'js/libs/sigma-master',
  298. },
  299. {
  300. from: `${aceDir}/src-min-noconflict`,
  301. to: `js/ace-${PackageVersions.version.ace}/`,
  302. },
  303. ...pdfjsVersions.flatMap(version => {
  304. const dir = getModuleDirectory(version)
  305. // Copy CMap files (used to provide support for non-Latin characters)
  306. // and static images from pdfjs-dist package to build output.
  307. return [
  308. { from: `${dir}/cmaps`, to: `js/${version}/cmaps` },
  309. {
  310. from: `${dir}/legacy/web/images`,
  311. to: `images/${version}`,
  312. },
  313. ]
  314. }),
  315. ]),
  316. ],
  317. }