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

Reduce standalone build size #10668

Merged
merged 4 commits into from Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions babel.config.js
Expand Up @@ -11,6 +11,7 @@ module.exports = function(api) {
exclude: ["transform-typeof-symbol"],
};
const envOpts = Object.assign({}, envOptsNoTargets);
let transformRuntimeOpts = null;

let convertESM = true;
let ignoreLib = true;
Expand Down Expand Up @@ -50,6 +51,17 @@ module.exports = function(api) {
break;
}

if (includeRuntime) {
const babelRuntimePackageJSONPath = require.resolve(
"@babel/runtime/package.json"
);
const path = require("path");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a simple question. What is the advantage of declaring within this scope rather than at the top?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a strong reason: require("path") is used only when building babel-*-standalone. However, this config is shared with other building process (build/jest), by doing so we could avoid the overhead of requiring a builtin module.

transformRuntimeOpts = {
version: require(babelRuntimePackageJSONPath).version,
absoluteRuntime: path.dirname(babelRuntimePackageJSONPath),
};
}

const config = {
// Our dependencies are all standard CommonJS, along with all sorts of
// other random files in Babel's codebase, so we use script as the default,
Expand Down Expand Up @@ -123,10 +135,7 @@ module.exports = function(api) {
],
plugins: [
includeRuntime
? [
"@babel/transform-runtime",
{ version: require("@babel/runtime/package").version },
]
? ["@babel/transform-runtime", transformRuntimeOpts]
: null,
].filter(Boolean),
},
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-core/package.json
Expand Up @@ -34,7 +34,9 @@
},
"browser": {
"./lib/config/files/index.js": "./lib/config/files/index-browser.js",
"./lib/transform-file.js": "./lib/transform-file-browser.js"
"./lib/transform-file.js": "./lib/transform-file-browser.js",
"./src/config/files/index.js": "./src/config/files/index-browser.js",
"./src/transform-file.js": "./src/transform-file-browser.js"
},
"dependencies": {
"@babel/code-frame": "^7.5.5",
Expand Down
17 changes: 16 additions & 1 deletion scripts/gulp-tasks.js
Expand Up @@ -11,6 +11,7 @@
* to make standalone builds of other Babel plugins/presets (such as babel-minify)
*/

const fs = require("fs");
const path = require("path");
const pump = require("pump");
const chalk = require("chalk");
Expand All @@ -24,6 +25,19 @@ const WarningsToErrorsPlugin = require("warnings-to-errors-webpack-plugin");
const webpackStream = require("webpack-stream");
const uglify = require("gulp-uglify");

function generateResolveAlias() {
const alias = {};
const packagePath = path.resolve(process.cwd(), "packages");
fs.readdirSync(packagePath).forEach(folder => {
alias[folder.replace("babel-", "@babel/") + "$"] = path.resolve(
packagePath,
folder,
"src"
);
});
return alias;
}

function webpackBuild(opts) {
const plugins = opts.plugins || [];
let babelVersion = require("../packages/babel-core/package.json").version;
Expand Down Expand Up @@ -71,7 +85,6 @@ function webpackBuild(opts) {
}),
new webpack.DefinePlugin({
"process.env.NODE_ENV": '"production"',
"process.env": JSON.stringify({ NODE_ENV: "production" }),
BABEL_VERSION: JSON.stringify(babelVersion),
VERSION: JSON.stringify(version),
}),
Expand All @@ -82,6 +95,8 @@ function webpackBuild(opts) {
new webpack.optimize.ModuleConcatenationPlugin(),
].concat(plugins),
resolve: {
//todo: remove resolve.alias when babel packages offer ESModule entry
alias: generateResolveAlias(),
plugins: [
// Dedupe packages that are used across multiple plugins.
// This replaces DedupePlugin from Webpack 1.x
Expand Down