Skip to content

Flatten entry array & resolve files without extension #157

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/@css-blocks/jsx/src/Analyzer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import traverse from "babel-traverse";
import * as babylon from "babylon";
import * as debugGenerator from "debug";
import * as fs from "fs";
import * as path from "path";

import { CssBlocksJSXOptions } from "../options";
import { JSXParseError } from "../utils/Errors";
Expand Down Expand Up @@ -143,7 +142,7 @@ export class CSSBlocksJSXAnalyzer extends Analyzer<TEMPLATE_TYPE> {
* @param opts Optional analytics parser options.
*/
public parseFile(file: string): Promise<JSXAnalysis> {
file = path.resolve(this.options.baseDir, file);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this change caused a few failing tests – what was the intent here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Totally possible that its a problem with our test harness 🙂 )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah probably caused the failing tests, the problem is it currently doesn't resolve files without extension

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

know of a better way?

file = require.resolve(file);
return new Promise((resolve, reject) => {
fs.readFile(file, "utf8", (err, data) => {
if (err) {
Expand Down
8 changes: 6 additions & 2 deletions packages/@css-blocks/webpack/src/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TemplateTypes } from "@opticss/template-api";
import { ObjectDictionary, objectValues } from "@opticss/util";
import { ObjectDictionary, objectValues, whatever } from "@opticss/util";
import * as debugGenerator from "debug";
import { postcss } from "opticss";
import * as path from "path";
Expand Down Expand Up @@ -114,7 +114,7 @@ export class CssBlocksPlugin
entries = webpackEntry;
}
else if (typeof webpackEntry === "object") {
entries = objectValues(webpackEntry);
entries = flatten(objectValues(webpackEntry)) as string[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the signature below this doesn’t need to be cast.

}

let pending: PendingResult = this.analyzer.analyze(...entries)
Expand Down Expand Up @@ -322,3 +322,7 @@ export class CssBlocksPlugin
this.applyPluginsAsync("block-compilation-complete", result, cb);
}
}

function flatten(arr: whatever[]): whatever[] {
return arr.reduce((acc, val) => (acc as whatever[]).concat(Array.isArray(val) ? flatten(val) : val), []) as whatever[];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better signature for this is:

function flatten<T extends whatever>(arr: T[] | T[][]): T[];

Then no casting will be needed.

Copy link
Contributor Author

@ForsakenHarmony ForsakenHarmony May 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with that I'm getting

error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '{ (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; (c...' has no compatible call signatures.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok this was a bit tricky. The type of reduce wasn't smart enough to let the type checker work out what was going on. Also, the definition I provided was only two levels of arrays, but the code would handle it recursively. I refactored that function and made it part of our opticss utilities: linkedin/opticss@f104638