Files
react-native-devtools-frontend/scripts/build/devtools_plugin.js
T
Takuto Ikuta 2548a6cd6c introduce devtools_fast_bundle build config
Proposal: http://go/devtools-fast-bundle

This CL introduces build flag switching bundler from rollup.js to
esbuild by
* adding esbuild to npm without downloading binary packages
* making devtools_plugin for rollup.js re-usable to esbuild

On 24C/48T Z840 Linux machine, this shows following performance
difference by using
```
devtools_skip_typecheck = true
is_debug = false
```
as base build config.

esbuild (devtools_fast_bundle = true)
$ time ninja -C out/Default/
...
real    0m21.174s
user    2m47.513s
sys     0m38.549s

rollup.js (devtools_fast_bundle = false)
$ time ninja -C out/Default/
...
real    1m28.286s
user    30m19.220s
sys     5m36.392s


So esbuild is 3.2x faster and use only 9.6% of machine resouce
(user + sys) compared to rollup.js.

refs:
* https://esbuild.github.io/plugins/#on-resolve
* https://rollupjs.org/guide/en/#resolveid

Bug: 1278663
Cq-Include-Trybots: luci.devtools-frontend.try:devtools_frontend_linux_blink_light_rel_fastbuild,devtools_frontend_linux_dbg_fastbuild
Change-Id: If6b2e774f48091b0fe9c959e7ed1ed9bc2b0847c
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3401984
Reviewed-by: Tim Van der Lippe <tvanderlippe@chromium.org>
Commit-Queue: Takuto Ikuta <tikuta@chromium.org>
2022-02-01 12:49:12 +00:00

114 lines
4.4 KiB
JavaScript

// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @ts-check
const path = require('path');
/**
* `path.dirname` does not include trailing slashes. If we would always
* use `path.dirname` and then later perform comparisons on the paths that
* it returns, we could run into false positives. For example, given the
* the following two paths:
*
* front_end/timeline_model/TimelineModel.js
* front_end/timeline/Timeline.js
*
* And that would have the following values for `path.dirname`:
*
* front_end/timeline_model
* front_end/timeline
*
* If we would do a simple `.startswith` on the `path.dirname` of both of
* these paths, then the first path would start with the dirname of the
* second. However, they *are* part of different folders. To fix that problem,
* we need to force a path separator after each folder. That makes sure we
* and up with the following comparison of path dirnames:
*
* front_end/timeline_model/
* front_end/timeline/
*
* Now, the first path does *not* start with the second one, as expected.
*
* @param {string} file
* @return {string}
*/
function dirnameWithSeparator(file) {
return path.dirname(file) + path.sep;
}
function devtoolsPlugin(source, importer) {
if (!importer) {
return null;
}
if (source === '../../lib/codemirror' || source === 'fs') {
// These are imported via require(...), but we don't use
// @rollup/plugin-commonjs. So this check is not necessary for rollup. But
// need to have this for esbuild as it doesn't ignore require(...).
return {
external: true,
};
}
const currentDirectory = path.normalize(dirnameWithSeparator(importer));
const importedFilelocation = path.normalize(path.join(currentDirectory, source));
const importedFileDirectory = dirnameWithSeparator(importedFilelocation);
// Generated files are part of other directories, as they are only imported once
if (path.basename(importedFileDirectory) === 'generated') {
return null;
}
// An import is considered external (and therefore a separate
// bundle) if its filename matches its immediate parent's folder
// name (without the extension). For example:
// import * as Components from './components/components.js' = external
// import * as UI from '../ui/ui.js' = external
// import * as LitHtml from '../third_party/lit-html/lit-html.js' = external
// import {DataGrid} from './components/DataGrid.js' = not external
// import * as Components from './components/foo.js' = not external
// Note that we can't do a simple check for only `third_party`, as in Chromium
// our full path is `third_party/devtools-frontend/src/`, which thus *always*
// includes third_party. It also not possible to use the current directory
// as a check for the import, as the import will be different in Chromium and
// would therefore not match the path of `__dirname`.
// These should be removed because the new heuristic _should_ deal with these
// e.g. it'll pick up third_party/lit-html/lit-html.js is its own entrypoint
// Puppeteer has dynamic imports in its build gated on an ifNode
// flag, but our Rollup config doesn't know about that and tries
// to parse dynamic import('fs'). Let's ignore Puppeteer for now.
// The long term plan is probably for Puppeteer to ship a web
// bundle anyway. See go/pptr-agnostify for details.
if (importedFileDirectory.includes(path.join('front_end', 'third_party', 'puppeteer'))) {
return null;
}
// The CodeMirror addons look like bundles (addon/comment/comment.js) but are not.
if (importedFileDirectory.includes(path.join('front_end', 'third_party', 'codemirror', 'package'))) {
return null;
}
// The LightHouse bundle shouldn't be processed by `terser` again, as it is uniquely built
if (importedFilelocation.includes(path.join('front_end', 'third_party', 'lighthouse', 'lighthouse-dt-bundle.js'))) {
return {
id: importedFilelocation,
external: true,
};
}
const importedFileName = path.basename(importedFilelocation, '.js');
const importedFileParentDirectory = path.basename(path.dirname(importedFilelocation));
const isExternal = importedFileName === importedFileParentDirectory;
return {
id: importedFilelocation,
external: isExternal,
};
}
module.exports = {devtoolsPlugin};