webpack.config.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. 'ide-detached': './frontend/js/ide-detached.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. function getModuleDirectory(moduleName) {
  40. const entrypointPath = require.resolve(moduleName)
  41. const suffix = `node_modules/${moduleName}`
  42. const idx = entrypointPath.indexOf(suffix)
  43. if (idx === -1) {
  44. throw new Error(`could not find Node module: ${moduleName}`)
  45. }
  46. return entrypointPath.slice(0, idx + suffix.length)
  47. }
  48. const mathjaxDir = getModuleDirectory('mathjax')
  49. const aceDir = getModuleDirectory('ace-builds')
  50. const pdfjsVersions = ['pdfjs-dist210', 'pdfjs-dist213']
  51. const vendorDir = path.join(__dirname, 'frontend/js/vendor')
  52. module.exports = {
  53. // Defines the "entry point(s)" for the application - i.e. the file which
  54. // bootstraps the application
  55. entry: entryPoints,
  56. // Define where and how the bundle will be output to disk
  57. // Note: webpack-dev-server does not write the bundle to disk, instead it is
  58. // kept in memory for speed
  59. output: {
  60. path: path.join(__dirname, 'public'),
  61. publicPath: '/',
  62. // By default write into js directory
  63. filename: 'js/[name].js',
  64. // Output as UMD bundle (allows main JS to import with CJS, AMD or global
  65. // style code bundles
  66. libraryTarget: 'umd',
  67. // Name the exported variable from output bundle
  68. library: ['Frontend', '[name]'],
  69. },
  70. // Define how file types are handled by webpack
  71. module: {
  72. rules: [
  73. {
  74. // Pass application JS/TS files through babel-loader, compiling to ES5
  75. test: /\.[j|t]sx?$/,
  76. // Only compile application files (npm and vendored dependencies are in
  77. // ES5 already)
  78. exclude: [/node_modules\/(?!react-dnd\/)/, vendorDir],
  79. use: [
  80. {
  81. loader: 'babel-loader',
  82. options: {
  83. // Configure babel-loader to cache compiled output so that
  84. // subsequent compile runs are much faster
  85. cacheDirectory: true,
  86. configFile: path.join(__dirname, './babel.config.json'),
  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.join(__dirname, '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. {
  297. from: 'libs/sigma-master',
  298. to: 'js/libs/sigma-master',
  299. },
  300. ],
  301. {
  302. context: vendorDir,
  303. }
  304. ),
  305. new CopyPlugin(
  306. [
  307. {
  308. from: 'src-min-noconflict',
  309. to: `js/ace-${PackageVersions.version.ace}/`,
  310. },
  311. ],
  312. {
  313. context: aceDir,
  314. }
  315. ),
  316. new CopyPlugin([
  317. ...pdfjsVersions.flatMap(version => {
  318. const dir = getModuleDirectory(version)
  319. // Copy CMap files (used to provide support for non-Latin characters)
  320. // and static images from pdfjs-dist package to build output.
  321. return [
  322. { from: `${dir}/cmaps`, to: `js/${version}/cmaps` },
  323. {
  324. from: `${dir}/legacy/web/images`,
  325. to: `images/${version}`,
  326. },
  327. ]
  328. }),
  329. ]),
  330. ],
  331. }