Skip to content

Commit

Permalink
add tests to verify styles are built correctly and ui-rewrites are ha…
Browse files Browse the repository at this point in the history
…ppening
  • Loading branch information
spalger committed Feb 26, 2020
1 parent c24789a commit 0ff1d05
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 537 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

import './legacy/styles.scss';
import { fooLibFn } from '../../foo/public/index';
export * from './lib';
export { fooLibFn };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
size: $globalStyleConstant;
background-image: url("ui/icon.svg");
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$globalStyleConstant: 10;

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import { inspect } from 'util';
import cpy from 'cpy';
import del from 'del';
import { toArray, tap } from 'rxjs/operators';
import { createAbsolutePathSerializer } from '@kbn/dev-utils';
import { runOptimizer, OptimizerConfig, OptimizerUpdate } from '@kbn/optimizer';
import { createAbsolutePathSerializer, ToolingLog, REPO_ROOT } from '@kbn/dev-utils';
import { runOptimizer, OptimizerConfig, OptimizerUpdate, logOptimizerState } from '@kbn/optimizer';

const TMP_DIR = Path.resolve(__dirname, '../__fixtures__/__tmp__');
const MOCK_REPO_SRC = Path.resolve(__dirname, '../__fixtures__/mock_repo');
const MOCK_REPO_DIR = Path.resolve(TMP_DIR, 'mock_repo');

expect.addSnapshotSerializer(createAbsolutePathSerializer(MOCK_REPO_DIR));
expect.addSnapshotSerializer(createAbsolutePathSerializer(REPO_ROOT));

beforeAll(async () => {
await del(TMP_DIR);
Expand All @@ -51,20 +51,25 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
repoRoot: MOCK_REPO_DIR,
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')],
maxWorkerCount: 1,
dist: true,
});

expect(config).toMatchSnapshot('OptimizerConfig');

const msgs = await runOptimizer(config)
.pipe(
tap(state => {
if (state.event?.type === 'worker stdio') {
// eslint-disable-next-line no-console
console.log('worker', state.event.stream, state.event.chunk.toString('utf8'));
const log = new ToolingLog({
level: 'error',
writeTo: {
write(chunk) {
if (chunk.endsWith('\n')) {
chunk = chunk.slice(0, -1);
}
}),
toArray()
)
// eslint-disable-next-line no-console
console.error(chunk);
},
},
});
const msgs = await runOptimizer(config)
.pipe(logOptimizerState(log, config), toArray())
.toPromise();

const assert = (statement: string, truth: boolean, altStates?: OptimizerUpdate[]) => {
Expand Down Expand Up @@ -133,23 +138,31 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
expect(foo.cache.getModuleCount()).toBe(3);
expect(foo.cache.getReferencedFiles()).toMatchInlineSnapshot(`
Array [
<absolute path>/plugins/foo/public/ext.ts,
<absolute path>/plugins/foo/public/index.ts,
<absolute path>/plugins/foo/public/lib.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/ext.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/index.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/lib.ts,
]
`);

const bar = config.bundles.find(b => b.id === 'bar')!;
expect(bar).toBeTruthy();
bar.cache.refresh();
expect(bar.cache.getModuleCount()).toBe(5);
expect(bar.cache.getModuleCount()).toBe(
// code + styles + style/css-loader runtime
14
);

expect(bar.cache.getReferencedFiles()).toMatchInlineSnapshot(`
Array [
<absolute path>/plugins/foo/public/ext.ts,
<absolute path>/plugins/foo/public/index.ts,
<absolute path>/plugins/foo/public/lib.ts,
<absolute path>/plugins/bar/public/index.ts,
<absolute path>/plugins/bar/public/lib.ts,
<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/plugins/bar/public/index.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/legacy/styles.scss,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/lib.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/ext.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/index.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/foo/public/lib.ts,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/src/legacy/ui/public/icon.svg,
]
`);
});
Expand All @@ -159,6 +172,7 @@ it('uses cache on second run and exist cleanly', async () => {
repoRoot: MOCK_REPO_DIR,
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')],
maxWorkerCount: 1,
dist: true,
});

const msgs = await runOptimizer(config)
Expand Down
6 changes: 3 additions & 3 deletions packages/kbn-optimizer/src/worker/run_compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import webpack, { Stats } from 'webpack';
import * as Rx from 'rxjs';
import { mergeMap, map, mapTo, takeUntil } from 'rxjs/operators';

import { CompilerMsgs, CompilerMsg, maybeMap, Bundle, WorkerConfig } from '../common';
import { CompilerMsgs, CompilerMsg, maybeMap, Bundle, WorkerConfig, ascending } from '../common';
import { getWebpackConfig } from './webpack.config';
import { isFailureStats, failedStatsToErrorMessage } from './webpack_helpers';
import {
Expand Down Expand Up @@ -110,7 +110,7 @@ const observeCompiler = (
const path = getModulePath(module);

const parsedPath = Path.parse(path);
const dirSegments = parsedPath.dir.split(Path.sep);
const dirSegments = parsedPath.dir.slice(parsedPath.root.length).split(Path.sep);
if (!dirSegments.includes('node_modules')) {
referencedFiles.add(path);
continue;
Expand Down Expand Up @@ -146,7 +146,7 @@ const observeCompiler = (
optimizerCacheKey: workerConfig.optimizerCacheKey,
cacheKey: bundle.createCacheKey(files, mtimes),
moduleCount: normalModules.length,
files,
files: files.sort(ascending(f => f)),
});

return compilerMsgs.compilerSuccess({
Expand Down
7 changes: 6 additions & 1 deletion packages/kbn-optimizer/src/worker/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ export function getWebpackConfig(bundle: Bundle, worker: WorkerConfig) {
}

// manually force ui/* urls in legacy styles to resolve to ui/legacy/public
if (uri.startsWith('ui/') && base.split(Path.sep).includes('legacy')) {
if (
uri.startsWith('ui/') &&
Path.resolve(base)
.split(Path.sep)
.includes('legacy')
) {
return Path.resolve(
worker.repoRoot,
'src/legacy/ui/public',
Expand Down

0 comments on commit 0ff1d05

Please sign in to comment.