webpack.config.js 10 KB

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