Skip to content

Commit fbe3e9d

Browse files
author
Shane Osbourne
committed
separate runtime/startup runners
1 parent bb19e43 commit fbe3e9d

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

examples/tailwind/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<title>Document</title>
99
<link rel="stylesheet" href="./dist/output.css">
1010
</head>
11-
<body class="p-2">
12-
<p class=text-4xl>hello world 8.0</p>
11+
<body class="p-4">
12+
<p class=text-4xl>hello world 5.0</p>
1313
</body>
1414
</html>
1515

examples/tailwind/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"build": "tailwindcss -i base.css -o ./dist/output.css",
9+
"clean": "rm -rf dist/**",
910
"bs": "node run.js"
1011
},
1112
"keywords": [],

examples/tailwind/run.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ bs.init(
44
{
55
server: ".",
66
open: false,
7+
notify: false,
78
runners: [
89
{
9-
files: ["index.html", "base.css"],
10+
at: "startup",
11+
run: [{ npm: ["clean", "build"] }]
12+
},
13+
{
14+
at: "runtime",
15+
files: ["index.html"],
1016
run: [{ npm: ["build"] }, { bs: "reload" }]
1117
}
1218
]

packages/browser-sync/lib/cli/transforms/handleFilesOption.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fromJS, List } from "immutable";
22
import { BsTempOptions, makeFilesArg, TransformResult } from "../cli-options";
3-
import { FilesNamespace, FilesNamespaces, runnerOption } from "../../types";
3+
import { FilesNamespace, FilesNamespaces, runnerOption, runtimeRunnerOption } from "../../types";
44
import { z } from "zod";
55

66
export function handleFilesOption(incoming: BsTempOptions): TransformResult {
@@ -31,16 +31,20 @@ export function convertRunnerOption(incoming: BsTempOptions): FilesNamespaces |
3131
if (!runners) return null;
3232

3333
const parser = z.array(runnerOption);
34-
const parsed = parser.safeParse(incoming.get("runners").toJS());
34+
const incomingOpt = incoming.get("runners").toJS();
35+
const parsed = parser.safeParse(incomingOpt);
36+
3537
if (!parsed.success) {
3638
// todo: what to do in this case?
3739
console.log("failed to parse input", parsed.error);
3840
return null;
3941
}
42+
4043
const runnerData = parsed.data;
4144
const output: FilesNamespaces = {};
4245

4346
runnerData.forEach((runner, index) => {
47+
if (runner.at !== "runtime") return;
4448
const next: FilesNamespace = {
4549
index,
4650
globs: [],

packages/browser-sync/lib/internal-events.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var z = require("zod");
88
var fromEvent = Rx.Observable.fromEvent;
99
var fileHandler = require("./file-event-handler");
1010
const {
11-
runnerOption,
1211
toRunnerOption,
1312
toRunnerNotification,
1413
toSideEffect,

packages/browser-sync/lib/types.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ export type FilesObject = { match: string[]; fn?: Function; options?: any };
3636
export type FilesNamespace = { globs: string[]; objs: FilesObject[]; index?: number };
3737
export type FilesNamespaces = { [name: string]: FilesNamespace };
3838

39-
export type RunnerOption = {
40-
files: string[];
41-
run: Runner[];
42-
};
39+
// prettier-ignore
40+
export type RunnerOption =
41+
| { at: "startup"; run: Runner[]; }
42+
| { at: "runtime"; files: string[]; run: Runner[]; }
4343

4444
// prettier-ignore
4545
export type Runner =
@@ -66,13 +66,22 @@ const runnerParser = z.union([
6666
})
6767
]);
6868

69-
export const runnerOption = z.object({
69+
export const startupRunnerOption = z.object({
70+
at: z.literal("startup"),
71+
run: z.array(runnerParser)
72+
});
73+
74+
export const runtimeRunnerOption = z.object({
75+
at: z.literal("runtime"),
7076
files: z.array(z.string()),
7177
run: z.array(runnerParser)
7278
});
7379

80+
export const runnerOption = z.discriminatedUnion("at", [startupRunnerOption, runtimeRunnerOption]);
81+
7482
export function toRunnerOption(input: unknown): RunnerOption | null {
7583
const parsed = runnerOption.safeParse(input);
84+
// todo: give good errors on format mistakes here
7685
return parsed.success ? parsed.data : null;
7786
}
7887

0 commit comments

Comments
 (0)