Skip to content

Commit

Permalink
create tested & shared parsePath helper
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Feb 26, 2020
1 parent 61caf85 commit 3a75a11
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@
"@types/mustache": "^0.8.31",
"@types/node": "^10.12.27",
"@types/node-forge": "^0.9.0",
"@types/normalize-path": "^3.0.0",
"@types/numeral": "^0.0.26",
"@types/opn": "^5.1.0",
"@types/pegjs": "^0.10.1",
Expand Down
121 changes: 121 additions & 0 deletions packages/kbn-optimizer/src/worker/parse_path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { parsePath } from './parse_path';

it('figures out root and directories from all known formats', () => {
expect(parsePath('c:\\foo\\bar\\baz.json')).toMatchInlineSnapshot(`
Object {
"dirs": Array [
"foo",
"bar",
],
"filename": "baz.json",
"root": "c:",
}
`);
expect(parsePath('c:\\foo\\bar\\baz')).toMatchInlineSnapshot(`
Object {
"dirs": Array [
"foo",
"bar",
],
"filename": "baz",
"root": "c:",
}
`);
expect(parsePath('c:\\foo\\bar\\baz\\')).toMatchInlineSnapshot(`
Object {
"dirs": Array [
"foo",
"bar",
],
"filename": "baz",
"root": "c:",
}
`);
expect(parsePath('c:\\foo')).toMatchInlineSnapshot(`
Object {
"dirs": Array [],
"filename": "foo",
"root": "c:",
}
`);
expect(parsePath('c:\\')).toMatchInlineSnapshot(`
Object {
"dirs": Array [],
"filename": undefined,
"root": "c:",
}
`);
expect(parsePath('c:/foo/bar/baz.json')).toMatchInlineSnapshot(`
Object {
"dirs": Array [
"foo",
"bar",
],
"filename": "baz.json",
"root": "c:",
}
`);
expect(parsePath('/foo/bar/baz.json')).toMatchInlineSnapshot(`
Object {
"dirs": Array [
"foo",
"bar",
],
"filename": "baz.json",
"root": "/",
}
`);
expect(parsePath('/foo/bar/baz')).toMatchInlineSnapshot(`
Object {
"dirs": Array [
"foo",
"bar",
],
"filename": "baz",
"root": "/",
}
`);
expect(parsePath('/foo')).toMatchInlineSnapshot(`
Object {
"dirs": Array [],
"filename": "foo",
"root": "/",
}
`);
expect(parsePath('/')).toMatchInlineSnapshot(`
Object {
"dirs": Array [],
"filename": undefined,
"root": "/",
}
`);
expect(parsePath('/foo/bar/baz/')).toMatchInlineSnapshot(`
Object {
"dirs": Array [
"foo",
"bar",
],
"filename": "baz",
"root": "/",
}
`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@
* under the License.
*/

declare function NormalizePath(path: string, stripTrailing?: boolean): string;
import normalizePath from 'normalize-path';

declare module 'normalize-path' {
export = NormalizePath;
/**
* Parse an absolute path, supporting normalized paths from webpack,
* into a list of directories and root
*/
export function parsePath(path: string) {
const normalized = normalizePath(path);
const [root, ...others] = normalized.split('/');
return {
root: root === '' ? '/' : root,
dirs: others.slice(0, -1),
filename: others[others.length - 1] || undefined,
};
}
17 changes: 6 additions & 11 deletions packages/kbn-optimizer/src/worker/run_compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { mergeMap, map, mapTo, takeUntil } from 'rxjs/operators';
import { CompilerMsgs, CompilerMsg, maybeMap, Bundle, WorkerConfig, ascending } from '../common';
import { getWebpackConfig } from './webpack.config';
import { isFailureStats, failedStatsToErrorMessage } from './webpack_helpers';
import { parsePath } from './parse_path';
import {
isExternalModule,
isNormalModule,
Expand Down Expand Up @@ -108,25 +109,19 @@ const observeCompiler = (

for (const module of normalModules) {
const path = getModulePath(module);
const parsedPath = parsePath(path);

const parsedPath = Path.parse(path);

// parsedPath.dir starts with the root, remove it before
// splitting into dirs as the root is both not a directory
// and will put an empty string into dirs on unix systems
const dirs = parsedPath.dir.slice(parsedPath.root.length).split(Path.sep);

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

const nmIndex = dirs.lastIndexOf('node_modules');
const isScoped = dirs[nmIndex + 1].startsWith('@');
const nmIndex = parsedPath.dirs.lastIndexOf('node_modules');
const isScoped = parsedPath.dirs[nmIndex + 1].startsWith('@');
referencedFiles.add(
Path.join(
parsedPath.root,
...dirs.slice(0, nmIndex + 1 + (isScoped ? 2 : 1)),
...parsedPath.dirs.slice(0, nmIndex + 1 + (isScoped ? 2 : 1)),
'package.json'
)
);
Expand Down
8 changes: 2 additions & 6 deletions packages/kbn-optimizer/src/worker/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import * as SharedDeps from '@kbn/ui-shared-deps';

import { Bundle, WorkerConfig } from '../common';
import { parsePath } from './parse_path';

const IS_CODE_COVERAGE = !!process.env.CODE_COVERAGE;
const ISTANBUL_PRESET_PATH = require.resolve('@kbn/babel-preset/istanbul_preset');
Expand Down Expand Up @@ -135,12 +136,7 @@ export function getWebpackConfig(bundle: Bundle, worker: WorkerConfig) {
}

// manually force ui/* urls in legacy styles to resolve to ui/legacy/public
if (
uri.startsWith('ui/') &&
Path.resolve(base)
.split(Path.sep)
.includes('legacy')
) {
if (uri.startsWith('ui/') && parsePath(base).dirs.includes('legacy')) {
return Path.resolve(
worker.repoRoot,
'src/legacy/ui/public',
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4864,6 +4864,11 @@
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==

"@types/normalize-path@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/normalize-path/-/normalize-path-3.0.0.tgz#bb5c46cab77b93350b4cf8d7ff1153f47189ae31"
integrity sha512-Nd8y/5t/7CRakPYiyPzr/IAfYusy1FkcZYFEAcoMZkwpJv2n4Wm+olW+e7xBdHEXhOnWdG9ddbar0gqZWS4x5Q==

"@types/numeral@^0.0.25":
version "0.0.25"
resolved "https://registry.yarnpkg.com/@types/numeral/-/numeral-0.0.25.tgz#b6f55062827a4787fe4ab151cf3412a468e65271"
Expand Down

0 comments on commit 3a75a11

Please sign in to comment.