Skip to content

Commit 9c28ac0

Browse files
committed
feat: Support plain string and object syntax for webpack entries.
1 parent d83c724 commit 9c28ac0

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

packages/@css-blocks/webpack/src/Plugin.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TemplateTypes } from "@opticss/template-api";
2-
import { ObjectDictionary } from "@opticss/util";
2+
import { ObjectDictionary, objectValues } from "@opticss/util";
33
import * as debugGenerator from "debug";
44
import { postcss } from "opticss";
55
import * as path from "path";
@@ -64,6 +64,8 @@ export interface BlockCompilationComplete {
6464

6565
type Assets = ObjectDictionary<Source>;
6666

67+
type EntryTypes = string | string[] | ObjectDictionary<string>;
68+
6769
interface CompilationResult {
6870
optimizationResult: OptimizationResult;
6971
blocks: Set<Block>;
@@ -100,8 +102,21 @@ export class CssBlocksPlugin
100102
this.trace(`starting analysis.`);
101103
this.analyzer.reset();
102104

103-
// Try to run our analysis.
104-
let entries = compilation.options.entry as string[];
105+
// Fetch our app's entry points.
106+
let webpackEntry = compilation.options.entry as EntryTypes;
107+
let entries: string[] = [];
108+
109+
// Zomg webpack, so many config format options.
110+
if (typeof webpackEntry === "string") {
111+
entries = [ webpackEntry ];
112+
}
113+
else if (Array.isArray(webpackEntry)) {
114+
entries = webpackEntry;
115+
}
116+
else if (typeof webpackEntry === "object") {
117+
entries = objectValues(webpackEntry);
118+
}
119+
105120
let pending: PendingResult = this.analyzer.analyze(...entries)
106121
// If analysis fails, drain our BlockFactory, add error to compilation error list and propagate.
107122
.catch((err: Error) => {

packages/@css-blocks/webpack/test/configs/basicConfig.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ObjectDictionary } from "@opticss/util";
2+
13
import { Configuration as WebpackConfiguration } from "webpack";
24
import * as merge from "webpack-merge";
35

@@ -9,7 +11,9 @@ import { BLOCK_LOADER_PATH } from "../util/testPaths";
911

1012
import { config as defaultOutputConfig } from "./defaultOutputConfig";
1113

12-
export function config(entry: string, options?: LoaderOptions): WebpackConfiguration {
14+
export type EntryTypes = string | string[] | ObjectDictionary<string>;
15+
16+
export function config(entry: EntryTypes, options?: LoaderOptions): WebpackConfiguration {
1317
const baseConfig: WebpackConfiguration = {
1418
entry: entry,
1519
output: {

packages/@css-blocks/webpack/test/util/execTest.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,33 @@ import * as webpack from "webpack";
55

66
import { LoaderOptions } from "../../src/LoaderOptions";
77
import { WebpackAny } from "../../src/Plugin";
8-
import { config as basicConfig } from "../configs/basicConfig";
8+
import { EntryTypes, config as basicConfig } from "../configs/basicConfig";
99

1010
import { BLOCK_FIXTURES_DIRECTORY, DIST_DIRECTORY } from "./testPaths";
1111
const CR = /\r/g;
1212

1313
// This test harness was adapted from the sass-loader test suite.
1414

15-
export function execTest(testId: string, options?: LoaderOptions) {
16-
const entryPath = path.join(BLOCK_FIXTURES_DIRECTORY, testId + ".block.css");
17-
return runWebpackAsPromise(basicConfig(entryPath, options))
18-
.then(() => {
19-
const actualCss = readBundle("bundle.block.css.js");
20-
const expectedCss = readCss(testId);
15+
export function execTest(testId: string, options?: LoaderOptions, entryFormat: "string" | "object" | "array" = "string") {
16+
const entryPath: string = path.join(BLOCK_FIXTURES_DIRECTORY, testId + ".block.css");
17+
let entry: EntryTypes = entryPath;
2118

22-
// writing the actual css to output-dir for better debugging
23-
// fs.writeFileSync(path.join(__dirname, "output", `${ testId }.${ ext }.css`), actualCss, "utf8");
24-
assert.deepEqual(actualCss, expectedCss);
25-
});
19+
if (entryFormat === "array") {
20+
entry = [entryPath];
21+
}
22+
else if (entryFormat === "object") {
23+
entry = { main: entryPath };
24+
}
25+
26+
return runWebpackAsPromise(basicConfig(entry, options))
27+
.then(() => {
28+
const actualCss = readBundle("bundle.block.css.js");
29+
const expectedCss = readCss(testId);
30+
31+
// writing the actual css to output-dir for better debugging
32+
// fs.writeFileSync(path.join(__dirname, "output", `${ testId }.${ ext }.css`), actualCss, "utf8");
33+
assert.deepEqual(actualCss, expectedCss);
34+
});
2635
}
2736

2837
export function readCss(id: string): string {

0 commit comments

Comments
 (0)