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

🏗️🚀 Babel compile with sourcemaps #27447

Merged
merged 6 commits into from Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions build-system/compile/compile.js
Expand Up @@ -367,8 +367,8 @@ function compile(
if (options.typeCheckOnly) {
return gulp
.src(srcs, {base: '.'})
.pipe(sourcemaps.init())
.pipe(preClosureBabel())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(gulpClosureCompile(compilerOptionsArray, checkTypesNailgunPort))
.on('error', err => {
handleTypeCheckError();
Expand All @@ -380,8 +380,8 @@ function compile(
timeInfo.startTime = Date.now();
return gulp
.src(srcs, {base: '.'})
.pipe(sourcemaps.init())
.pipe(preClosureBabel())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(gulpClosureCompile(compilerOptionsArray, distNailgunPort))
.on('error', err => {
handleCompilerError(outputFilename);
Expand Down
63 changes: 41 additions & 22 deletions build-system/compile/pre-closure-babel.js
Expand Up @@ -16,15 +16,12 @@
'use strict';

const argv = require('minimist')(process.argv.slice(2));
const babel = require('@babel/core');
const conf = require('./build.conf');
const globby = require('globby');
const path = require('path');
const gulpBabel = require('gulp-babel');
const through = require('through2');
const {BABEL_SRC_GLOBS, THIRD_PARTY_TRANSFORM_GLOBS} = require('./sources');

const ROOT_DIR = path.resolve(__dirname, '../../');

/**
* Files on which to run pre-closure babel transforms.
*
Expand All @@ -37,7 +34,7 @@ const filesToTransform = getFilesToTransform();
*
* @private @const {!Object<string, string>}
*/
const cachedTransforms = {};
const cachedTransforms = Object.create(null);

/**
* Computes the set of files on which to run pre-closure babel transforms.
Expand All @@ -60,26 +57,48 @@ function getFilesToTransform() {
* @return {!Promise}
rsimha marked this conversation as resolved.
Show resolved Hide resolved
*/
function preClosureBabel() {
const babelPlugins = conf.plugins({
isForTesting: !!argv.fortesting,
isEsmBuild: !!argv.esm,
isSinglePass: !!argv.single_pass,
isChecktypes: argv._.includes('check-types'),
});
const babel = gulpBabel({
plugins: babelPlugins,
retainLines: true,
compact: false,
});

return through.obj((file, enc, next) => {
const cachedTransform = cachedTransforms[file.path];
const {relative, path} = file;
rsimha marked this conversation as resolved.
Show resolved Hide resolved
if (!filesToTransform.includes(relative)) {
return next(null, file);
}

const cachedTransform = cachedTransforms[path];
rsimha marked this conversation as resolved.
Show resolved Hide resolved
if (cachedTransform) {
file.contents = Buffer.from(cachedTransform);
} else if (filesToTransform.includes(path.relative(ROOT_DIR, file.path))) {
const babelPlugins = conf.plugins({
isForTesting: !!argv.fortesting,
isEsmBuild: !!argv.esm,
isSinglePass: !!argv.single_pass,
isChecktypes: argv._.includes('check-types'),
});
const {code} = babel.transformFileSync(file.path, {
plugins: babelPlugins,
retainLines: true,
compact: false,
});
cachedTransforms[file.path] = code;
file.contents = Buffer.from(code);
return next(null, cachedTransform.clone());
}

let data, err;
function onData(d) {
babel.off('error', onError);
data = d;
}
return next(null, file);
function onError(e) {
babel.off('data', onData);
err = e;
}
babel.once('data', onData);
babel.once('error', onError);
babel.write(file, enc, () => {
if (err) {
return next(err);
}

cachedTransforms[path] = data;
next(null, data.clone());
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion build-system/compile/single-pass.js
Expand Up @@ -686,8 +686,8 @@ function compile(flagsArray) {
return new Promise(function(resolve, reject) {
gulp
.src(srcs, {base: '.'})
.pipe(sourcemaps.init())
.pipe(preClosureBabel())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(gulpClosureCompile(flagsArray))
.on('error', err => {
handleSinglePassCompilerError();
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -94,6 +94,7 @@
"gulp": "4.0.2",
"gulp-append-prepend": "1.0.8",
"gulp-ava": "3.0.0",
"gulp-babel": "8.0.0",
"gulp-connect": "5.7.0",
"gulp-eslint": "6.0.0",
"gulp-eslint-if-fixed": "1.0.0",
Expand Down Expand Up @@ -181,8 +182,8 @@
"typescript": "3.8.3",
"uglifyify": "5.0.2",
"vinyl-buffer": "1.0.1",
"vinyl-sourcemaps-apply": "0.2.1",
"vinyl-source-stream": "2.0.0",
"vinyl-sourcemaps-apply": "0.2.1",
"watchify": "3.11.1"
},
"filesize": {
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Expand Up @@ -7831,6 +7831,16 @@ gulp-ava@3.0.0:
resolve-cwd "^3.0.0"
through2 "^3.0.0"

gulp-babel@8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/gulp-babel/-/gulp-babel-8.0.0.tgz#e0da96f4f2ec4a88dd3a3030f476e38ab2126d87"
integrity sha512-oomaIqDXxFkg7lbpBou/gnUkX51/Y/M2ZfSjL2hdqXTAlSWZcgZtd2o0cOH0r/eE8LWD0+Q/PsLsr2DKOoqToQ==
dependencies:
plugin-error "^1.0.1"
replace-ext "^1.0.0"
through2 "^2.0.0"
vinyl-sourcemaps-apply "^0.2.0"

gulp-cli@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/gulp-cli/-/gulp-cli-2.2.0.tgz#5533126eeb7fe415a7e3e84a297d334d5cf70ebc"
Expand Down