Skip to content

Commit

Permalink
fix(bundling): check for browserslist when setting terser ecma nrwl#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Jun 21, 2023
1 parent 5af50b9 commit bc22a61
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js"
},
"peerDependencies": {
"browserslist": "^4.21.4"
},
"publishConfig": {
"access": "public"
}
Expand Down
36 changes: 31 additions & 5 deletions packages/webpack/src/utils/with-nx.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
import * as path from 'path';
import { Configuration, WebpackPluginInstance, ProgressPlugin } from 'webpack';
import { join } from 'path';
import { Configuration, ProgressPlugin, WebpackPluginInstance } from 'webpack';
import { ExecutorContext } from 'nx/src/config/misc-interfaces';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import { readTsConfig } from '@nx/js';
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
import TerserPlugin = require('terser-webpack-plugin');
import nodeExternals = require('webpack-node-externals');
import ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

import { NormalizedWebpackExecutorOptions } from '../executors/webpack/schema';
import { StatsJsonPlugin } from '../plugins/stats-json-plugin';
import { createCopyPlugin } from './create-copy-plugin';
import { GeneratePackageJsonPlugin } from '../plugins/generate-package-json-plugin';
import { getOutputHashFormat } from './hash-format';
import { NxWebpackPlugin } from './config';
import { existsSync } from 'fs';
import TerserPlugin = require('terser-webpack-plugin');
import nodeExternals = require('webpack-node-externals');
import ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
import browserslist = require('browserslist');

const VALID_BROWSERSLIST_FILES = ['.browserslistrc', 'browserslist'];

function getTerserEcmaVersion(projectRoot: string) {
let pathToBrowserslistFile = '';
for (const browserslistFile of VALID_BROWSERSLIST_FILES) {
const fullPathToFile = join(projectRoot, browserslistFile);
if (existsSync(fullPathToFile)) {
pathToBrowserslistFile = fullPathToFile;
break;
}
}

if (!pathToBrowserslistFile) {
return 2020;
}

const env = browserslist.loadConfig({ path: pathToBrowserslistFile });
const browsers = browserslist(env);
return browsers.includes('ie 11') ? 5 : 2020;
}

const IGNORED_WEBPACK_WARNINGS = [
/The comment file/i,
Expand Down Expand Up @@ -213,7 +237,9 @@ export function withNx(pluginOptions?: WithNxOptions): NxWebpackPlugin {
parallel: true,
terserOptions: {
keep_classnames: true,
ecma: 2020,
ecma: getTerserEcmaVersion(
join(options.root, options.projectRoot)
),
safari10: true,
format: {
ascii_only: true,
Expand Down

0 comments on commit bc22a61

Please sign in to comment.