Skip to content

Commit

Permalink
Reduce standalone build size (#10668)
Browse files Browse the repository at this point in the history
* infra: build standalone against src

* infra: add absoluteRuntime

# Conflicts:
#	babel.config.js

* chore: remove `process.env` replace

* add $ to signify exact match

credits to Nicol貌
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Nov 21, 2019
1 parent cc51f2a commit 683adcb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
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");
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 @@ -125,10 +137,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

0 comments on commit 683adcb

Please sign in to comment.