Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add option to build modern ES6 code #4964

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions packages/babel-preset-react-app/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = function(api, opts, env) {
var isEnvProduction = env === 'production';
var isEnvTest = env === 'test';
var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
var isModern = validateBoolOption('modern', opts.modern, false);

if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
throw new Error(
Expand All @@ -38,7 +39,30 @@ module.exports = function(api, opts, env) {
);
}

return {
function getEnvOptions({ isModern }) {
const defaultOpts = {
// `entry` transforms `@babel/polyfill` into individual requires for
// the targeted browsers. This is safer than `usage` which performs
// static code analysis to determine what's required.
// This is probably a fine default to help trim down bundles when
// end-users inevitably import '@babel/polyfill'.
useBuiltIns: 'entry',
// Do not transform modules to CJS
modules: false,
};

if (isModern) {
return Object.assign(defaultOpts, {
targets: {
esmodules: true,
},
});
}

return defaultOpts;
}

const babelPreset = {
presets: [
isEnvTest && [
// ES features necessary for user's Node version
Expand All @@ -52,16 +76,7 @@ module.exports = function(api, opts, env) {
(isEnvProduction || isEnvDevelopment) && [
// Latest stable ECMAScript features
require('@babel/preset-env').default,
{
// `entry` transforms `@babel/polyfill` into individual requires for
// the targeted browsers. This is safer than `usage` which performs
// static code analysis to determine what's required.
// This is probably a fine default to help trim down bundles when
// end-users inevitably import '@babel/polyfill'.
useBuiltIns: 'entry',
// Do not transform modules to CJS
modules: false,
},
getEnvOptions({ isModern }),
],
[
require('@babel/preset-react').default,
Expand Down Expand Up @@ -103,7 +118,7 @@ module.exports = function(api, opts, env) {
},
],
// Polyfills the runtime needed for async/await and generators
[
!isModern && [
require('@babel/plugin-transform-runtime').default,
{
helpers: false,
Expand All @@ -119,18 +134,23 @@ module.exports = function(api, opts, env) {
},
],
// function* () { yield 42; yield 43; }
!isEnvTest && [
require('@babel/plugin-transform-regenerator').default,
{
// Async functions are converted to generators by @babel/preset-env
async: false,
},
],
!isEnvTest &&
!isModern && [
require('@babel/plugin-transform-regenerator').default,
{
// Async functions are converted to generators by @babel/preset-env
async: false,
},
],
// Adds syntax support for import()
require('@babel/plugin-syntax-dynamic-import').default,
isEnvTest &&
// Transform dynamic import to require
require('babel-plugin-transform-dynamic-import').default,
].filter(Boolean),
};

// console.log('PRESET', JSON.stringify(babelPreset)

return babelPreset;
};
4 changes: 2 additions & 2 deletions packages/react-dev-utils/printBuildError.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ module.exports = function printBuildError(err) {
const message = err != null && err.message;
const stack = err != null && err.stack;

// Add more helpful message for UglifyJs error
// Add more helpful message for Terser error
if (
stack &&
typeof message === 'string' &&
message.indexOf('from UglifyJs') !== -1
message.indexOf('from Terser') !== -1
) {
try {
const matched = /(.+)\[(.+):(.+),(.+)\]\[.+\]/.exec(stack);
Expand Down
2 changes: 1 addition & 1 deletion packages/react-error-overlay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"rimraf": "^2.6.2",
"settle-promise": "1.0.0",
"source-map": "0.5.6",
"uglifyjs-webpack-plugin": "1.2.5",
"terser-webpack-plugin": "^1.0.0",
"webpack": "^4.8.1"
},
"jest": {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-error-overlay/webpack.config.iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const path = require('path');
const webpack = require('webpack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
Expand Down Expand Up @@ -49,8 +49,8 @@ module.exports = {
minimizer: [
// This code is embedded as a string, so it would never be optimized
// elsewhere.
new UglifyJsPlugin({
uglifyOptions: {
new TerserPlugin({
terserOptions: {
compress: {
warnings: false,
comparisons: false,
Expand Down
Loading