UNPKG

@bn-digital/webpack

Version:

Webpack configuration with decorators

163 lines (162 loc) 6.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPlugins = void 0; const tslib_1 = require("tslib"); const eslint_webpack_plugin_1 = tslib_1.__importDefault(require("eslint-webpack-plugin")); const html_webpack_plugin_1 = tslib_1.__importDefault(require("html-webpack-plugin")); const mini_css_extract_plugin_1 = tslib_1.__importDefault(require("mini-css-extract-plugin")); const clean_webpack_plugin_1 = require("clean-webpack-plugin"); const webpack_1 = require("webpack"); const stylelint_webpack_plugin_1 = tslib_1.__importDefault(require("stylelint-webpack-plugin")); const webpack_bundle_analyzer_1 = require("webpack-bundle-analyzer"); const dotenv_webpack_1 = tslib_1.__importDefault(require("dotenv-webpack")); const terser_webpack_plugin_1 = tslib_1.__importDefault(require("terser-webpack-plugin")); const webpack_plugin_1 = tslib_1.__importDefault(require("@sentry/webpack-plugin")); const node_polyfill_webpack_plugin_1 = tslib_1.__importDefault(require("node-polyfill-webpack-plugin")); const copy_webpack_plugin_1 = tslib_1.__importDefault(require("copy-webpack-plugin")); const path_1 = tslib_1.__importDefault(require("path")); const fs_1 = tslib_1.__importDefault(require("fs")); const css_minimizer_webpack_plugin_1 = tslib_1.__importDefault(require("css-minimizer-webpack-plugin")); const appDir = fs_1.default.realpathSync(process.cwd()); /** * Defines environment variables from process.env * @param envVars * @param mode */ function define(envVars = {}, mode = 'development') { var _a, _b; const appVariables = Object.entries(envVars) .filter(([key]) => key.startsWith('WEBSITE_')) .reduce((all, [key, value]) => (Object.assign(Object.assign({}, all), { [`process.env.${key}`]: value })), {}); return new webpack_1.DefinePlugin(Object.assign(Object.assign({ 'process.env.NODE_ENV': JSON.stringify((_a = process.env.NODE_ENV) !== null && _a !== void 0 ? _a : mode), 'process.env.PUBLIC_URL': JSON.stringify((_b = process.env.PUBLIC_URL) !== null && _b !== void 0 ? _b : '') }, appVariables), envVars)); } /** * @param {CopyPluginOptions} options */ function copy(options = { patterns: [ { from: '**/*', context: path_1.default.join(appDir, 'public'), noErrorOnMissing: true, }, ], }) { return new copy_webpack_plugin_1.default(options); } /** * @param {BundleAnalyzerPlugin.Options} options */ function bundleAnalyzer(options) { return new webpack_bundle_analyzer_1.BundleAnalyzerPlugin(options); } /** * @param {DotenvPlugin.Options} options */ function dotenv(options = {}) { return new dotenv_webpack_1.default(options); } function nodePolyfill(options = {}) { return new node_polyfill_webpack_plugin_1.default(options); } /** * HTML Plugin * @param options * @param mode */ function html(options = { templateParameters: {}, }, mode = 'development') { return new html_webpack_plugin_1.default(Object.assign(Object.assign({}, options), { inject: true, cache: true, hash: mode === 'production', template: 'src/index.html', scriptLoading: 'module', showErrors: mode === 'development', minify: mode === 'production' ? { removeComments: true, collapseWhitespace: true, removeRedundantAttributes: true, useShortDoctype: true, removeEmptyAttributes: true, removeStyleLinkTypeAttributes: true, keepClosingSlash: true, minifyJS: true, minifyCSS: true, minifyURLs: true, } : options.minify })); } /** * ESLint plugin * @param {EslintPluginOptions} options * @param mode * @return {WebpackPlugin} */ function eslint(options = {}, mode = 'development') { return new eslint_webpack_plugin_1.default(Object.assign({ cacheLocation: path_1.default.resolve(appDir, 'node_modules/.cache/.eslintcache'), cache: true, cwd: appDir, resolvePluginsRelativeTo: __dirname, extensions: ['tsx', 'ts', 'graphql'], failOnError: mode === 'production', failOnWarning: false }, options)); } /** * Stylelint plugin * @param {StylelintPluginOptions} options * @param mode * @return {WebpackPlugin} */ function stylelint(options = {}, mode = 'development') { return new stylelint_webpack_plugin_1.default(Object.assign({ extensions: ['less'], cacheLocation: path_1.default.resolve(appDir, 'node_modules/.cache/.stylelintcache'), customSyntax: 'postcss-less', cache: true, failOnError: mode === 'production', failOnWarning: false }, options)); } /** * Cleans up output directory before build * @param options */ function clean(options = {}) { return new clean_webpack_plugin_1.CleanWebpackPlugin(options); } function terser(options = {}) { return new terser_webpack_plugin_1.default({ terserOptions: Object.assign({ parse: { ecma: 2018 }, compress: { ecma: 5 } }, options), }); } function ignore() { return new webpack_1.IgnorePlugin({ contextRegExp: /^\.\/locale$/, resourceRegExp: /moment$/ }); } function automaticPrefetch() { return new webpack_1.AutomaticPrefetchPlugin(); } function contextReplacement() { return new webpack_1.ContextReplacementPlugin(/power-assert-formatter[\\/]lib/, new RegExp('^\\./.*\\.js$')); } function cssMinimizer() { return new css_minimizer_webpack_plugin_1.default(); } /** * @param {SentryCliPluginOptions} options */ function sentry(options = { include: '.' }) { return new webpack_plugin_1.default(options); } /** * Extracts CSS into separate files * @param mode * @param options */ function miniCssExtract(mode, options = {}) { return new mini_css_extract_plugin_1.default(Object.assign({ ignoreOrder: true, runtime: true }, options)); } /** * @param {Mode} mode */ const getPlugins = (mode = 'development') => ({ automaticPrefetch, bundleAnalyzer, clean, cssMinimizer, copy, define: (options) => define(options, mode), dotenv, contextReplacement, eslint: (options = {}) => eslint(options, mode), html: (options = {}) => html(options, mode), ignore, miniCssExtract: (options = {}) => miniCssExtract(mode, options), nodePolyfill, sentry: (options) => sentry(options), stylelint: (options = {}) => stylelint(options, mode), terser, }); exports.getPlugins = getPlugins;