2018-03-21 08:08:18 -04:00
|
|
|
const fs = require('fs')
|
2018-01-12 06:41:02 -05:00
|
|
|
const path = require('path')
|
2019-10-16 06:10:54 -04:00
|
|
|
const webpack = require('webpack')
|
|
|
|
const CopyPlugin = require('copy-webpack-plugin')
|
|
|
|
const ManifestPlugin = require('webpack-manifest-plugin')
|
2019-11-28 05:20:22 -05:00
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
|
2019-10-16 06:10:54 -04:00
|
|
|
|
|
|
|
const PackageVersions = require('./app/src/infrastructure/PackageVersions')
|
2018-01-12 06:41:02 -05:00
|
|
|
|
2018-03-21 08:08:18 -04:00
|
|
|
const MODULES_PATH = path.join(__dirname, '/modules')
|
|
|
|
|
|
|
|
// Generate a hash of entry points, including modules
|
2019-10-16 06:10:54 -04:00
|
|
|
const entryPoints = {
|
2019-11-05 09:00:13 -05:00
|
|
|
main: './frontend/js/main.js',
|
2019-11-28 05:20:22 -05:00
|
|
|
ide: './frontend/js/ide.js',
|
|
|
|
style: './frontend/stylesheets/style.less',
|
|
|
|
'ieee-style': './frontend/stylesheets/ieee-style.less',
|
|
|
|
'light-style': './frontend/stylesheets/light-style.less',
|
|
|
|
'sl-style': './frontend/stylesheets/sl-style.less'
|
2019-10-16 06:10:54 -04:00
|
|
|
}
|
|
|
|
|
2019-11-18 05:57:50 -05:00
|
|
|
// Attempt to load frontend entry-points from modules, if they exist
|
2018-03-21 08:08:18 -04:00
|
|
|
if (fs.existsSync(MODULES_PATH)) {
|
2019-08-06 08:20:02 -04:00
|
|
|
fs.readdirSync(MODULES_PATH).reduce((acc, module) => {
|
2019-11-05 09:00:13 -05:00
|
|
|
const entryPath = path.join(MODULES_PATH, module, '/frontend/js/index.js')
|
2019-08-06 08:20:02 -04:00
|
|
|
if (fs.existsSync(entryPath)) {
|
|
|
|
acc[module] = entryPath
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, entryPoints)
|
2018-03-21 08:08:18 -04:00
|
|
|
}
|
|
|
|
|
2018-01-12 06:41:02 -05:00
|
|
|
module.exports = {
|
2019-08-06 08:20:02 -04:00
|
|
|
// Defines the "entry point(s)" for the application - i.e. the file which
|
|
|
|
// bootstraps the application
|
|
|
|
entry: entryPoints,
|
2018-01-12 06:41:02 -05:00
|
|
|
|
2019-08-06 08:20:02 -04:00
|
|
|
// Define where and how the bundle will be output to disk
|
|
|
|
// Note: webpack-dev-server does not write the bundle to disk, instead it is
|
|
|
|
// kept in memory for speed
|
|
|
|
output: {
|
2019-11-28 05:20:22 -05:00
|
|
|
path: path.join(__dirname, '/public'),
|
2018-01-12 06:41:02 -05:00
|
|
|
|
2019-11-28 05:20:22 -05:00
|
|
|
// By default write into js directory
|
|
|
|
filename: 'js/[name].js',
|
2018-01-12 06:41:02 -05:00
|
|
|
|
2019-08-06 08:20:02 -04:00
|
|
|
// Output as UMD bundle (allows main JS to import with CJS, AMD or global
|
|
|
|
// style code bundles
|
|
|
|
libraryTarget: 'umd',
|
|
|
|
// Name the exported variable from output bundle
|
|
|
|
library: ['Frontend', '[name]']
|
|
|
|
},
|
2018-01-12 06:41:02 -05:00
|
|
|
|
2019-08-06 08:20:02 -04:00
|
|
|
// Define how file types are handled by webpack
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
// Pass application JS files through babel-loader, compiling to ES5
|
|
|
|
test: /\.js$/,
|
|
|
|
// Only compile application files (dependencies are in ES5 already)
|
|
|
|
exclude: /node_modules/,
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'babel-loader',
|
|
|
|
options: {
|
|
|
|
// Configure babel-loader to cache compiled output so that
|
|
|
|
// subsequent compile runs are much faster
|
|
|
|
cacheDirectory: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2019-11-28 05:20:22 -05:00
|
|
|
{
|
|
|
|
// Wrap PDF.js worker in a Web Worker
|
|
|
|
test: /pdf\.worker\.js$/,
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'worker-loader',
|
|
|
|
options: {
|
|
|
|
// Write into js directory (note: customising this is not possible
|
|
|
|
// with pdfjs-dist/webpack auto loader)
|
2019-12-11 04:53:31 -05:00
|
|
|
name: 'js/pdfjs-worker.[hash].js',
|
|
|
|
// Override dynamically-set publicPath to explicitly use root.
|
|
|
|
// This prevents a security problem where the Worker - normally
|
|
|
|
// loaded from a CDN - has cross-origin issues, by forcing it to not
|
|
|
|
// be loaded from the CDN
|
|
|
|
publicPath: '/'
|
2019-11-28 05:20:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Pass Less files through less-loader/css-loader/mini-css-extract-
|
|
|
|
// plugin (note: run in reverse order)
|
|
|
|
test: /\.less$/,
|
|
|
|
use: [
|
|
|
|
// Allows the CSS to be extracted to a separate .css file
|
|
|
|
{ loader: MiniCssExtractPlugin.loader },
|
|
|
|
// Resolves any CSS dependencies (e.g. url())
|
|
|
|
{ loader: 'css-loader' },
|
|
|
|
{
|
|
|
|
// Runs autoprefixer on CSS via postcss
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: {
|
|
|
|
// Uniquely identifies the postcss plugin (required by webpack)
|
|
|
|
ident: 'postcss',
|
|
|
|
plugins: [
|
|
|
|
require('autoprefixer')({ env: 'last 2 versions, ie >= 10' })
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Compiles the Less syntax to CSS
|
|
|
|
{ loader: 'less-loader' }
|
|
|
|
]
|
|
|
|
},
|
2019-08-06 08:20:02 -04:00
|
|
|
{
|
2019-10-16 06:10:54 -04:00
|
|
|
// These options are necessary for handlebars to have access to helper
|
2019-08-06 08:20:02 -04:00
|
|
|
// methods
|
|
|
|
test: /\.handlebars$/,
|
|
|
|
loader: 'handlebars-loader',
|
|
|
|
options: {
|
|
|
|
compat: true,
|
|
|
|
knownHelpersOnly: false,
|
|
|
|
runtimePath: 'handlebars/runtime'
|
|
|
|
}
|
2019-10-16 06:10:54 -04:00
|
|
|
},
|
|
|
|
// Allow for injection of modules dependencies by reading contents of
|
|
|
|
// modules directory and adding necessary dependencies
|
|
|
|
{
|
|
|
|
test: path.join(__dirname, 'modules/modules-main.js'),
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'val-loader'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: path.join(__dirname, 'modules/modules-ide.js'),
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'val-loader'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2019-12-16 10:02:29 -05:00
|
|
|
{
|
|
|
|
// Expose jQuery and $ global variables
|
|
|
|
test: require.resolve('jquery'),
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'expose-loader',
|
|
|
|
options: 'jQuery'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'expose-loader',
|
|
|
|
options: '$'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Expose angular global variable
|
|
|
|
test: require.resolve('angular'),
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'expose-loader',
|
|
|
|
options: 'angular'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2019-10-16 06:10:54 -04:00
|
|
|
{
|
2020-01-16 08:09:47 -05:00
|
|
|
// Expose lodash global variable
|
|
|
|
test: require.resolve('lodash'),
|
2019-10-16 06:10:54 -04:00
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'expose-loader',
|
|
|
|
options: '_'
|
|
|
|
}
|
|
|
|
]
|
2019-08-06 08:20:02 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
alias: {
|
2019-10-16 06:10:54 -04:00
|
|
|
// Aliases for AMD modules
|
|
|
|
|
2020-01-07 10:23:54 -05:00
|
|
|
// Shortcut to vendored dependencies in frontend/js/vendor/libs
|
2019-11-05 09:00:13 -05:00
|
|
|
libs: path.join(__dirname, 'frontend/js/vendor/libs'),
|
2019-10-16 06:10:54 -04:00
|
|
|
// Enables ace/ace shortcut
|
2019-10-30 07:19:44 -04:00
|
|
|
ace: path.join(
|
|
|
|
__dirname,
|
2019-11-05 09:00:13 -05:00
|
|
|
`frontend/js/vendor/${PackageVersions.lib('ace')}`
|
2019-10-30 07:19:44 -04:00
|
|
|
),
|
2019-10-16 06:10:54 -04:00
|
|
|
// fineupload vendored dependency (which we're aliasing to fineuploadER
|
|
|
|
// for some reason)
|
|
|
|
fineuploader: path.join(
|
|
|
|
__dirname,
|
2019-11-05 09:00:13 -05:00
|
|
|
`frontend/js/vendor/libs/${PackageVersions.lib('fineuploader')}`
|
2019-10-16 06:10:54 -04:00
|
|
|
)
|
|
|
|
},
|
|
|
|
// Define what can be imported with out an absolute or relative path. This
|
|
|
|
// is because we need to override the default (which is just node_modules)
|
|
|
|
// to get AMD modules in public/src to work as they do not use relative/
|
|
|
|
// absolute paths for dependencies
|
2019-11-05 09:00:13 -05:00
|
|
|
modules: ['frontend/js', 'node_modules']
|
2019-10-16 06:10:54 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Split out vendored dependencies that are shared between 2 or more "real
|
|
|
|
// bundles" (e.g. ide.js/main.js) as a separate "libraries" bundle and ensure
|
|
|
|
// that they are de-duplicated from the other bundles. This allows the
|
|
|
|
// libraries bundle to be independently cached (as it likely will change less
|
|
|
|
// than the other bundles)
|
|
|
|
optimization: {
|
|
|
|
splitChunks: {
|
|
|
|
cacheGroups: {
|
|
|
|
libraries: {
|
2019-11-05 09:00:13 -05:00
|
|
|
test: /[\\/]node_modules[\\/]|[\\/]frontend[\\/]js[\\/]vendor[\\/]libs[\\/]/,
|
2019-10-16 06:10:54 -04:00
|
|
|
name: 'libraries',
|
|
|
|
chunks: 'initial',
|
|
|
|
minChunks: 2
|
|
|
|
}
|
|
|
|
}
|
2019-08-06 08:20:02 -04:00
|
|
|
}
|
2019-10-16 06:10:54 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
plugins: [
|
|
|
|
// Generate a manifest.json file which is used by the backend to map the
|
|
|
|
// base filenames to the generated output filenames
|
|
|
|
new ManifestPlugin({
|
|
|
|
// Always write the manifest file to disk (even if in dev mode, where
|
|
|
|
// files are held in memory). This is needed because the server will read
|
|
|
|
// this file (from disk) when building the script's url
|
|
|
|
writeToFileEmit: true
|
|
|
|
}),
|
|
|
|
|
2020-01-07 10:23:54 -05:00
|
|
|
// Prevent moment from loading (very large) locale files that aren't used
|
|
|
|
new webpack.IgnorePlugin({
|
|
|
|
resourceRegExp: /^\.\/locale$/,
|
|
|
|
contextRegExp: /moment$/
|
|
|
|
}),
|
2019-10-16 06:10:54 -04:00
|
|
|
|
2019-10-30 07:19:44 -04:00
|
|
|
new CopyPlugin([
|
|
|
|
{
|
2019-11-05 09:00:13 -05:00
|
|
|
from: 'frontend/js/vendor/libs/mathjax',
|
2019-11-28 05:20:22 -05:00
|
|
|
to: 'js/libs/mathjax'
|
2019-10-30 07:19:44 -04:00
|
|
|
},
|
|
|
|
{
|
2019-11-05 09:00:13 -05:00
|
|
|
from: 'frontend/js/vendor/libs/sigma-master',
|
2019-11-28 05:20:22 -05:00
|
|
|
to: 'js/libs/sigma-master'
|
2019-10-30 07:19:44 -04:00
|
|
|
},
|
|
|
|
{
|
2019-11-05 09:00:13 -05:00
|
|
|
from: `frontend/js/vendor/ace-${PackageVersions.version.ace}/`,
|
2019-11-28 05:20:22 -05:00
|
|
|
to: `js/ace-${PackageVersions.version.ace}/`
|
2019-10-30 07:19:44 -04:00
|
|
|
},
|
2019-11-05 09:00:13 -05:00
|
|
|
// Copy CMap files from pdfjs-dist package to build output. These are used
|
|
|
|
// to provide support for non-Latin characters
|
2019-11-28 05:20:22 -05:00
|
|
|
{ from: 'node_modules/pdfjs-dist/cmaps', to: 'js/cmaps' }
|
2019-10-30 07:19:44 -04:00
|
|
|
])
|
2020-01-16 08:09:47 -05:00
|
|
|
]
|
2018-03-29 11:15:53 -04:00
|
|
|
}
|