webpack.config.js 8.9 KB

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