keeweb/build/webpack.config.js

242 lines
9.2 KiB
JavaScript
Raw Permalink Normal View History

2019-09-16 18:18:51 +02:00
const path = require('path');
2020-05-09 16:14:20 +02:00
const fs = require('fs');
2019-09-16 18:18:51 +02:00
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
2019-09-18 19:59:39 +02:00
const TerserPlugin = require('terser-webpack-plugin');
2019-09-16 18:18:51 +02:00
const rootDir = path.join(__dirname, '..');
const pkg = require('../package.json');
process.noDeprecation = true; // for css loaders
2019-10-12 13:24:37 +02:00
function config(options) {
const mode = options.mode || 'production';
2019-09-16 18:18:51 +02:00
const devMode = mode === 'development';
2019-10-12 13:24:37 +02:00
const date = options.date;
2019-09-16 18:18:51 +02:00
const dt = date.toISOString().replace(/T.*/, '');
const year = date.getFullYear();
return {
mode,
entry: {
2020-11-19 18:43:50 +01:00
app: ['babel-helpers', 'app', 'main.scss']
2019-09-16 18:18:51 +02:00
},
output: {
path: path.resolve('.', 'tmp'),
filename: 'js/[name].js'
},
target: 'web',
performance: {
hints: false
},
stats: {
colors: false,
modules: true,
reasons: true
},
progress: false,
failOnError: true,
resolve: {
modules: [
path.join(rootDir, 'app/scripts'),
path.join(rootDir, 'app/styles'),
path.join(rootDir, 'node_modules')
],
alias: {
'babel-helpers': path.join(rootDir, 'app/lib/babel-helpers.js'),
2019-09-22 09:56:48 +02:00
jquery: `jquery/dist/jquery${devMode ? '' : '.min'}.js`,
morphdom: `morphdom/dist/morphdom-umd${devMode ? '' : '.min'}.js`,
kdbxweb: `kdbxweb/dist/kdbxweb${devMode ? '' : '.min'}.js`,
baron: `baron/baron${devMode ? '' : '.min'}.js`,
qrcode: `jsqrcode/dist/qrcode${devMode ? '' : '.min'}.js`,
2019-09-16 18:18:51 +02:00
argon2: 'argon2-browser/dist/argon2.js',
marked: devMode ? 'marked/lib/marked.js' : 'marked/marked.min.js',
2019-09-22 09:56:48 +02:00
dompurify: `dompurify/dist/purify${devMode ? '' : '.min'}.js`,
2021-04-06 20:10:42 +02:00
tweetnacl: `tweetnacl/nacl${devMode ? '' : '.min'}.js`,
2019-09-16 18:18:51 +02:00
hbs: 'handlebars/runtime.js',
'argon2-wasm': 'argon2-browser/dist/argon2.wasm',
templates: path.join(rootDir, 'app/templates'),
'public-key.pem': path.join(rootDir, 'app/resources/public-key.pem'),
2019-09-28 14:40:46 +02:00
'public-key-new.pem': path.join(rootDir, 'app/resources/public-key-new.pem'),
2020-03-29 15:01:11 +02:00
'demo.kdbx': path.join(rootDir, 'app/resources/Demo.kdbx'),
2020-11-25 18:20:53 +01:00
'fontawesome.woff2': '@fortawesome/fontawesome-free/webfonts/fa-regular-400.woff2'
2020-11-19 18:43:50 +01:00
},
fallback: {
console: false,
process: false,
crypto: false,
Buffer: false,
__filename: false,
__dirname: false,
fs: false,
setImmediate: false,
path: false,
moment: false
2019-09-16 18:18:51 +02:00
}
},
2020-11-25 18:20:53 +01:00
resolveLoader: {
modules: ['node_modules', path.join(__dirname, 'loaders')]
},
2019-09-16 18:18:51 +02:00
module: {
rules: [
{
test: /\.hbs$/,
2020-05-09 16:14:20 +02:00
use: [
{
loader: 'handlebars-loader',
2020-11-19 18:43:50 +01:00
options: {
2020-05-09 16:14:20 +02:00
knownHelpers: fs
.readdirSync(path.join(rootDir, 'app/scripts/hbs-helpers'))
2020-06-01 16:53:51 +02:00
.map((f) => f.replace('.js', ''))
.filter((f) => f !== 'index'),
2020-05-09 16:14:20 +02:00
partialResolver(partial, callback) {
const location = path.join(
rootDir,
'app/templates/partials',
`${partial}.hbs`
);
callback(null, location);
}
}
},
{
loader: 'string-replace-loader',
options: {
search: /\r?\n\s*/g,
replace: '\n'
}
2020-05-09 16:14:20 +02:00
}
]
2019-09-16 18:18:51 +02:00
},
{
test: /runtime-info\.js$/,
2020-11-19 18:43:50 +01:00
loader: 'string-replace-loader',
options: {
multiple: [
2019-09-16 18:18:51 +02:00
{
2020-11-19 18:43:50 +01:00
search: /@@VERSION/g,
replace: pkg.version + (options.beta ? '-beta' : '')
2019-09-16 18:18:51 +02:00
},
{
2020-11-19 18:43:50 +01:00
search: /@@BETA/g,
replace: options.beta ? '1' : ''
2019-09-16 18:18:51 +02:00
},
2020-11-19 18:43:50 +01:00
{ search: /@@DATE/g, replace: dt },
2019-09-16 18:18:51 +02:00
{
2020-11-19 18:43:50 +01:00
search: /@@COMMIT/g,
replace: options.sha
},
{ search: /@@DEVMODE/g, replace: devMode ? '1' : '' },
{ search: /@@APPLE_TEAM_ID/g, replace: options.appleTeamId }
2019-09-16 18:18:51 +02:00
]
2020-11-19 18:43:50 +01:00
}
2019-09-16 18:18:51 +02:00
},
{
test: /baron(\.min)?\.js$/,
2020-04-29 18:41:16 +02:00
use: [
2020-11-19 18:43:50 +01:00
{
loader: 'string-replace-loader',
options: {
search: /\(1,\s*eval\)\('this'\)/g,
replace: 'window'
}
},
2020-06-13 10:54:10 +02:00
{
loader: 'exports-loader',
options: { type: 'module', exports: 'default baron' }
}
2020-04-29 18:41:16 +02:00
]
2019-09-16 18:18:51 +02:00
},
{
test: /babel-helpers\.js$/,
2020-06-13 10:54:10 +02:00
loader: 'exports-loader',
options: { type: 'module', exports: 'default babelHelpers' }
2019-09-16 18:18:51 +02:00
},
{ test: /handlebars/, loader: 'strip-sourcemap-loader' },
{
test: /\.js$/,
exclude: /(node_modules|babel-helpers\.js)/,
loader: 'babel-loader',
2020-11-19 18:43:50 +01:00
options: { cacheDirectory: true }
2019-09-16 18:18:51 +02:00
},
{ test: /argon2\.wasm/, type: 'javascript/auto', loader: 'base64-loader' },
{ test: /argon2(\.min)?\.js/, loader: 'raw-loader' },
{
test: /\.s?css$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: { sourceMap: devMode } },
2019-09-20 23:37:17 +02:00
{ loader: 'postcss-loader', options: { sourceMap: devMode } },
2020-11-25 18:20:53 +01:00
{ loader: 'sass-loader', options: { sourceMap: devMode } },
{ loader: 'scss-add-icons-loader' }
2019-09-16 18:18:51 +02:00
]
},
2020-11-25 18:20:53 +01:00
{ test: /fontawesome.*\.woff2$/, loader: 'fontawesome-loader' },
2019-09-16 18:18:51 +02:00
{ test: /\.pem$/, loader: 'raw-loader' },
2020-11-25 18:20:53 +01:00
{ test: /\.kdbx$/, loader: 'base64-loader' }
2019-09-16 18:18:51 +02:00
]
},
optimization: {
2020-12-12 10:38:39 +01:00
runtimeChunk: false,
2019-09-18 19:59:39 +02:00
minimize: !devMode,
2019-09-16 18:18:51 +02:00
minimizer: [
2019-09-18 19:59:39 +02:00
new TerserPlugin({
2019-09-28 15:49:04 +02:00
extractComments: 'never-extract-comments',
2019-09-18 19:59:39 +02:00
terserOptions: {
ecma: 6
}
2019-09-16 18:18:51 +02:00
}),
new OptimizeCSSAssetsPlugin({
cssProcessorPluginOptions: {
preset: ['default', { discardComments: { removeAll: true } }]
}
}),
new BundleAnalyzerPlugin({
openAnalyzer: false,
analyzerMode: 'static',
reportFilename: 'stats/analyzer_report.html',
generateStatsFile: true,
statsFilename: 'stats/stats.json'
})
]
},
plugins: [
new webpack.BannerPlugin(
'keeweb v' +
pkg.version +
', (c) ' +
year +
' ' +
pkg.author.name +
', opensource.org/licenses/' +
pkg.license
),
new webpack.ProvidePlugin({
$: 'jquery',
babelHelpers: 'babel-helpers'
}),
new webpack.IgnorePlugin(/^(moment)$/),
new MiniCssExtractPlugin({
filename: 'css/[name].css'
})
],
node: {
__filename: false,
2020-11-19 18:43:50 +01:00
__dirname: false
2019-09-16 18:18:51 +02:00
},
externals: {
xmldom: 'null',
crypto: 'null',
fs: 'null',
path: 'null'
},
devtool: devMode ? 'source-map' : undefined
};
}
module.exports.config = config;