Skip to content

Commit

Permalink
[kbn/optimizer] store references to bazel target for all package files (
Browse files Browse the repository at this point in the history
#106171) (#109513)

* [kbn/optimizer] store references to bazel target for all package files

* update jest snapshots

* remove unnecessary fixtures

Co-authored-by: spalger <[email protected]>

Co-authored-by: spalger <[email protected]>
  • Loading branch information
Spencer and spalger authored Aug 20, 2021
1 parent a77d449 commit 64b32ab
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 73 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import cpy from 'cpy';
import del from 'del';
import { tap, filter } from 'rxjs/operators';
import { REPO_ROOT } from '@kbn/utils';
import { ToolingLog } from '@kbn/dev-utils';
import { ToolingLog, createReplaceSerializer } from '@kbn/dev-utils';
import { runOptimizer, OptimizerConfig, OptimizerUpdate, logOptimizerState } from '../index';

import { allValuesFrom } from '../common';
Expand All @@ -29,6 +29,8 @@ expect.addSnapshotSerializer({
test: (value: any) => typeof value === 'string' && value.includes(REPO_ROOT),
});

expect.addSnapshotSerializer(createReplaceSerializer(/\w+-fastbuild/, '<platform>-fastbuild'));

const log = new ToolingLog({
level: 'error',
writeTo: {
Expand Down Expand Up @@ -130,7 +132,7 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
expect(foo.cache.getModuleCount()).toBe(6);
expect(foo.cache.getReferencedFiles()).toMatchInlineSnapshot(`
Array [
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/packages/kbn-ui-shared-deps/src/public_path_module_creator.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/bazel-out/<platform>-fastbuild/bin/packages/kbn-ui-shared-deps/target/public_path_module_creator.js,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/kibana.json,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/async_import.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/ext.ts,
Expand All @@ -153,7 +155,7 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
<absolute path>/node_modules/@kbn/optimizer/postcss.config.js,
<absolute path>/node_modules/css-loader/package.json,
<absolute path>/node_modules/style-loader/package.json,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/packages/kbn-ui-shared-deps/src/public_path_module_creator.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/bazel-out/<platform>-fastbuild/bin/packages/kbn-ui-shared-deps/target/public_path_module_creator.js,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/kibana.json,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.scss,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.ts,
Expand All @@ -173,7 +175,7 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {

expect(baz.cache.getReferencedFiles()).toMatchInlineSnapshot(`
Array [
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/packages/kbn-ui-shared-deps/src/public_path_module_creator.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/bazel-out/<platform>-fastbuild/bin/packages/kbn-ui-shared-deps/target/public_path_module_creator.js,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/x-pack/baz/kibana.json,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/x-pack/baz/public/index.ts,
<absolute path>/packages/kbn-optimizer/src/worker/entry_point_creator.ts,
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-optimizer/src/optimizer/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class Watcher {

private readonly watchpack = new Watchpack({
aggregateTimeout: 0,
ignored: /node_modules\/([^\/]+[\/])*(?!package.json)([^\/]+)$/,
});

private readonly change$ = Rx.fromEvent<[string]>(this.watchpack, 'change').pipe(share());
Expand Down
81 changes: 27 additions & 54 deletions packages/kbn-optimizer/src/worker/populate_bundle_cache_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import Fs from 'fs';
import Path from 'path';
import { inspect } from 'util';

Expand All @@ -21,20 +22,6 @@ import {
getModulePath,
} from './webpack_helpers';

function tryToResolveRewrittenPath(from: string, toResolve: string) {
try {
return require.resolve(toResolve);
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
throw new Error(
`attempted to rewrite bazel-out path [${from}] to [${toResolve}] but couldn't find the rewrite target`
);
}

throw error;
}
}

/**
* sass-loader creates about a 40% overhead on the overall optimizer runtime, and
* so this constant is used to indicate to assignBundlesToWorkers() that there is
Expand All @@ -44,6 +31,20 @@ function tryToResolveRewrittenPath(from: string, toResolve: string) {
*/
const EXTRA_SCSS_WORK_UNITS = 100;

const isBazelPackageCache = new Map<string, boolean>();
function isBazelPackage(pkgJsonPath: string) {
const cached = isBazelPackageCache.get(pkgJsonPath);
if (typeof cached === 'boolean') {
return cached;
}

const path = parseFilePath(Fs.realpathSync(pkgJsonPath, 'utf-8'));
const match = !!path.matchDirs('bazel-out', /-fastbuild$/, 'bin', 'packages');
isBazelPackageCache.set(pkgJsonPath, match);

return match;
}

export class PopulateBundleCachePlugin {
constructor(private readonly workerConfig: WorkerConfig, private readonly bundle: Bundle) {}

Expand Down Expand Up @@ -71,44 +72,16 @@ export class PopulateBundleCachePlugin {
let path = getModulePath(module);
let parsedPath = parseFilePath(path);

const bazelOut = parsedPath.matchDirs(
'bazel-out',
/-fastbuild$/,
'bin',
'packages',
/.*/,
'target'
);

// if the module is referenced from one of our packages and resolved to the `bazel-out` dir
// we should rewrite our reference to point to the source file so that we can track the
// modified time of that file rather than the built output which is rebuilt all the time
// without actually changing
if (bazelOut) {
const packageDir = parsedPath.dirs[bazelOut.endIndex - 1];
const subDirs = parsedPath.dirs.slice(bazelOut.endIndex + 1);
path = tryToResolveRewrittenPath(
path,
Path.join(
workerConfig.repoRoot,
'packages',
packageDir,
'src',
...subDirs,
parsedPath.filename
? Path.basename(parsedPath.filename, Path.extname(parsedPath.filename))
: ''
)
const bazelOutIndex = parsedPath.dirs.indexOf('bazel-out');
if (bazelOutIndex >= 0) {
path = Path.resolve(
this.workerConfig.repoRoot,
...parsedPath.dirs.slice(bazelOutIndex),
parsedPath.filename ?? ''
);
parsedPath = parseFilePath(path);
}

if (parsedPath.matchDirs('bazel-out')) {
throw new Error(
`a bazel-out dir is being referenced by module [${path}] and not getting rewritten to its source location`
);
}

if (!parsedPath.dirs.includes('node_modules')) {
referencedFiles.add(path);

Expand All @@ -125,13 +98,13 @@ export class PopulateBundleCachePlugin {

const nmIndex = parsedPath.dirs.lastIndexOf('node_modules');
const isScoped = parsedPath.dirs[nmIndex + 1].startsWith('@');
referencedFiles.add(
Path.join(
parsedPath.root,
...parsedPath.dirs.slice(0, nmIndex + 1 + (isScoped ? 2 : 1)),
'package.json'
)
const pkgJsonPath = Path.join(
parsedPath.root,
...parsedPath.dirs.slice(0, nmIndex + 1 + (isScoped ? 2 : 1)),
'package.json'
);

referencedFiles.add(isBazelPackage(pkgJsonPath) ? path : pkgJsonPath);
continue;
}

Expand Down
6 changes: 1 addition & 5 deletions src/dev/precommit_hook/casing_check_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ export const IGNORE_FILE_GLOBS = [
*
* @type {Array}
*/
export const KEBAB_CASE_DIRECTORY_GLOBS = [
'packages/*',
'x-pack',
'packages/kbn-optimizer/src/__fixtures__/mock_repo/packages/kbn-ui-shared-deps',
];
export const KEBAB_CASE_DIRECTORY_GLOBS = ['packages/*', 'x-pack'];

/**
* These patterns are matched against directories and indicate
Expand Down

0 comments on commit 64b32ab

Please sign in to comment.