Bundle standalone using rollup (#10779)

* chore: bundle babel-standalone via rollup

Co-authored-by: Daniel Tschinder <daniel.tschinder@researchgate.net>

* chore: build babel-preset-env-standalone via Rollup

# Conflicts:
#	yarn.lock

* chore: add babelEnvName

* chore: farewell webpack

* chore: add terser minification

* chore: dedupe some popular dependencies

* chore: remove unused devDependencies

* chore: move plugin-babel-source to scripts/

* make prettier happy

* fix: rewrite available-plugins into esm

* chore: add extra unambiguous sources when env is rollup/standalone
This commit is contained in:
Huáng Jùnliàng
2019-12-04 23:09:52 -05:00
committed by GitHub
parent 2b472912e4
commit a0bed42aec
7 changed files with 703 additions and 1103 deletions

View File

@@ -1,185 +0,0 @@
"use strict";
/**
* This file contains the Gulp tasks for babel-standalone. Note that
* babel-standalone is compiled using Webpack, and performs its own Babel
* compilation of all the JavaScript files. This is because it targets web
* browsers, so more transforms are needed than the regular Babel builds that
* only target Node.js.
*
* The tasks in this file are designed to be reusable, so that they can be used
* 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");
const through = require("through2");
const fancyLog = require("fancy-log");
const rename = require("gulp-rename");
const webpack = require("webpack");
const { RootMostResolvePlugin } = require("webpack-dependency-suite");
const DuplicatePackageCheckerPlugin = require("duplicate-package-checker-webpack-plugin");
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;
let version = opts.version || babelVersion;
// If this build is part of a pull request, include the pull request number in
// the version number.
if (process.env.CIRCLE_PR_NUMBER) {
const prVersion = "+pr." + process.env.CIRCLE_PR_NUMBER;
babelVersion += prVersion;
version += prVersion;
}
const config = {
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
options: {
// Use the bundled config so that module syntax is passed through
// for Webpack.
envName: "standalone",
},
},
],
},
node: {
// Mock Node.js modules that Babel require()s but that we don't
// particularly care about.
fs: "empty",
module: "empty",
net: "empty",
},
output: {
filename: opts.filename,
library: opts.library,
libraryTarget: "umd",
},
plugins: [
new WarningsToErrorsPlugin(),
new DuplicatePackageCheckerPlugin({
exclude(instance) {
return instance.name === "semver";
},
}),
new webpack.DefinePlugin({
"process.env.NODE_ENV": '"production"',
BABEL_VERSION: JSON.stringify(babelVersion),
VERSION: JSON.stringify(version),
}),
/*new webpack.NormalModuleReplacementPlugin(
/..\/..\/package/,
"../../../../src/babel-package-shim"
),*/
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
new RootMostResolvePlugin(__dirname, true),
],
},
};
if (opts.library !== "Babel") {
config.externals = {
"@babel/standalone": "Babel",
};
}
return webpackStream(config, webpack);
// To write JSON for debugging:
/*return webpackStream(config, webpack, (err, stats) => {
require("fancy-log")(stats.toString({ colors: true }));
require("fs").writeFileSync(
"webpack-debug.json",
JSON.stringify(stats.toJson())
);
});*/
}
function logUglify() {
return through.obj(function(file, enc, callback) {
fancyLog(
`Minifying '${chalk.cyan(
path.relative(path.join(__dirname, ".."), file.path)
)}'...`
);
callback(null, file);
});
}
function logNoUglify() {
return through.obj(function(file, enc, callback) {
fancyLog(
chalk.yellow(
`Skipped minification of '${chalk.cyan(
path.relative(path.join(__dirname, ".."), file.path)
)}' because not publishing`
)
);
callback(null, file);
});
}
function registerStandalonePackageTask(
gulp,
name,
exportName,
pathname,
version,
plugins
) {
const standaloneName = name + "-standalone";
const standalonePath = path.join(pathname, standaloneName);
gulp.task("build-" + standaloneName, cb => {
pump(
[
gulp.src(path.join(standalonePath, "src/index.js")),
webpackBuild({
filename: name + ".js",
library: exportName,
version,
plugins,
}),
gulp.dest(standalonePath),
].concat(
// Minification is super slow, so we skip it unless we are publishing
process.env.IS_PUBLISH ? logUglify() : logNoUglify(),
process.env.IS_PUBLISH ? uglify() : [],
rename({ extname: ".min.js" }),
gulp.dest(standalonePath)
),
cb
);
});
}
module.exports = {
webpackBuild: webpackBuild,
registerStandalonePackageTask: registerStandalonePackageTask,
};

View File

@@ -0,0 +1,66 @@
const path = require("path");
const fs = require("fs");
const dirname = path.join(__dirname, "..");
module.exports = function() {
return {
name: "babel-source",
load(id) {
const matches = id.match(/packages\/(babel-[^/]+)\/src\//);
if (matches) {
// check if browser field exists for this file and replace
const packageFolder = path.join(dirname, "packages", matches[1]);
const packageJson = require(path.join(packageFolder, "package.json"));
if (
packageJson["browser"] &&
typeof packageJson["browser"] === "object"
) {
for (let nodeFile in packageJson["browser"]) {
const browserFile = packageJson["browser"][nodeFile].replace(
/^(\.\/)?lib\//,
"src/"
);
nodeFile = nodeFile.replace(/^(\.\/)?lib\//, "src/");
if (id.endsWith(nodeFile)) {
if (browserFile === false) {
return "";
}
return fs.readFileSync(
path.join(packageFolder, browserFile),
"UTF-8"
);
}
}
}
}
return null;
},
resolveId(importee) {
let packageFolderName;
const matches = importee.match(/^@babel\/([^/]+)$/);
if (matches) {
packageFolderName = `babel-${matches[1]}`;
}
if (packageFolderName) {
// resolve babel package names to their src index file
const packageFolder = path.join(dirname, "packages", packageFolderName);
const packageJson = require(path.join(packageFolder, "package.json"));
const filename =
typeof packageJson["browser"] === "string"
? packageJson["browser"]
: packageJson["main"];
return path.join(
packageFolder,
// replace lib with src in the pkg.json entry
filename.replace(/^(\.\/)?lib\//, "src/")
);
}
return null;
},
};
};