UNPKG

@expo/webpack-config

Version:

The default Webpack configuration used to build Expo apps targeting the web.

84 lines 3.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const clean_webpack_plugin_1 = require("clean-webpack-plugin"); const webpack_bundle_analyzer_1 = require("webpack-bundle-analyzer"); const chalk_1 = __importDefault(require("chalk")); const config_1 = require("../utils/config"); const env_1 = require("../env"); /** * @internal */ exports.DEFAULT_REPORTING_OPTIONS = { analyzerMode: 'static', defaultSizes: 'gzip', generateStatsFile: true, openAnalyzer: false, verbose: false, path: 'web-report', statsFilename: 'stats.json', reportFilename: 'report.html', }; /** * * @param param0 * @internal */ function throwDeprecatedConfig({ web = {} }) { const { build = {} } = web; if (typeof build.report !== 'undefined') { throw new Error('expo.web.build.report is deprecated. Please extend webpack.config.js and use env.report instead.'); } } exports.throwDeprecatedConfig = throwDeprecatedConfig; /** * * @param env * @internal */ function maybeWarnAboutRebuilds(env) { const mode = env_1.getMode(env); if (mode === 'development') { console.log(chalk_1.default.bgYellow.black `Generating a report, this will add noticeably more time to rebuilds.`); } } exports.maybeWarnAboutRebuilds = maybeWarnAboutRebuilds; /** * Generate a bundle analysis and stats.json via the `webpack-bundle-analyzer` plugin. * * @param webpackConfig Existing Webpack config to modify. * @param env Use the `report` prop to enable and configure reporting tools. * @category addons */ function withReporting(webpackConfig, env) { // Force deprecate the report option in the app.json in favor of modifying the Webpack config directly. throwDeprecatedConfig(env_1.getConfig(env)); const reportConfig = config_1.enableWithPropertyOrConfig(env.report, exports.DEFAULT_REPORTING_OPTIONS, true); // If reporting isn't enabled then bail out. if (!reportConfig) { return webpackConfig; } // webpack-bundle-analyzer adds time to builds // if verbose mode is turned on then we should inform developers about why re-builds are slower. if (reportConfig.verbose) { maybeWarnAboutRebuilds(env); } const reportDir = reportConfig.path; if (!Array.isArray(webpackConfig.plugins)) webpackConfig.plugins = []; webpackConfig.plugins.push( // Delete the report folder new clean_webpack_plugin_1.CleanWebpackPlugin({ cleanOnceBeforeBuildPatterns: [env_1.getAbsolute(env.projectRoot, reportDir)], dry: false, verbose: reportConfig.verbose, }), // Generate the report.html and stats.json // @ts-ignore new webpack_bundle_analyzer_1.BundleAnalyzerPlugin(Object.assign(Object.assign({}, reportConfig), { logLevel: reportConfig.verbose ? 'info' : 'silent', statsFilename: env_1.getAbsolute(env.projectRoot, reportDir, reportConfig.statsFilename), reportFilename: env_1.getAbsolute(env.projectRoot, reportDir, reportConfig.reportFilename) }))); return webpackConfig; } exports.default = withReporting; //# sourceMappingURL=withReporting.js.map