Skip to content
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

Use Workerd for local development #1184

Merged
merged 44 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
4670ed0
Apply multiple log replacers instead of only the first match
frandiox Aug 2, 2023
93f7a5a
Basic workerd integration
frandiox Aug 2, 2023
bc79360
Cleanup logs
frandiox Aug 2, 2023
d120a1d
Add Oxygen headers to request
frandiox Aug 2, 2023
b15b016
Setup basic inspector connection
frandiox Aug 3, 2023
02c53a2
Full parity with MiniOxygen logging
frandiox Aug 3, 2023
a4cd5d0
Fix MiniOxygen entry worker
frandiox Aug 3, 2023
58848fd
Fix log replacer
frandiox Aug 3, 2023
ca93965
Only serve known assets
frandiox Aug 3, 2023
2a5e009
Find nearest free port
frandiox Mar 7, 2023
fbf1acd
Find port for inspector
frandiox Aug 3, 2023
44b9700
Use waitUntil for logger. Minor refactor
frandiox Aug 4, 2023
68bc4cf
Merge branch '2023-07' into fd-mini-oxygen-workerd
frandiox Aug 15, 2023
18f6b88
Merge branch '2023-07' into fd-mini-oxygen-workerd
frandiox Aug 17, 2023
2b5b755
Restore integrity in package-lock for turbo and ast-grep
frandiox Aug 17, 2023
7d9bacb
Update miniflare
frandiox Aug 28, 2023
7c06e94
Do not show undefined cause in errors
frandiox Aug 28, 2023
21e6c8e
Move workerd logic
frandiox Aug 28, 2023
e0d6b3b
Restore MiniOxygen v1
frandiox Aug 28, 2023
c466115
Merge branch 'main' into fd-mini-oxygen-workerd
frandiox Aug 28, 2023
844bf61
Restore logs for MiniOxygen
frandiox Aug 28, 2023
94a4bcb
Fix port after merge
frandiox Aug 28, 2023
40a5d6d
Move mini-oxygen v2 logic
frandiox Aug 28, 2023
28e4835
Unify API
frandiox Aug 28, 2023
11fcd70
Toggle between node and worker with a CLI flag
frandiox Aug 28, 2023
d4e643b
Use Oxygen headers in Node
frandiox Aug 28, 2023
24d22aa
Fix package-lock for turbo and ast-grep
frandiox Aug 28, 2023
a00d1f5
Fix package-lock for typescript
frandiox Aug 28, 2023
1d00754
Use mime lookup from cli-kit
frandiox Aug 28, 2023
8b6e88f
Extract oxygen headers logic
frandiox Aug 28, 2023
dd20432
Merge branch 'main' into fd-mini-oxygen-workerd
frandiox Aug 29, 2023
2cfda8b
Merge branch 'main' into fd-mini-oxygen-workerd
frandiox Sep 6, 2023
8b68552
Merge branch 'main' into fd-mini-oxygen-workerd
frandiox Sep 15, 2023
fb23734
Add notice about debug-network support
frandiox Sep 15, 2023
509855e
Rename native-unstable flag to worker-unstable
frandiox Sep 15, 2023
a82c09e
Filter out non-actionable warnings and errors
frandiox Sep 15, 2023
922ece1
Stream files
frandiox Sep 19, 2023
9d4b483
Load remote env vars in preview command
frandiox Sep 19, 2023
ab40e00
Avoid cloning requests
frandiox Sep 19, 2023
e5120cc
Use sets to find asset matches
frandiox Sep 19, 2023
2d6d64f
Update Miniflare version
frandiox Sep 19, 2023
5fffcdb
Changesets
frandiox Sep 19, 2023
288a977
Hide the unsupported debug-network line for worker runtime
frandiox Sep 19, 2023
828a5c1
Update Miniflare version to drop subdependencies
frandiox Sep 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6,487 changes: 1,387 additions & 5,100 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
"@types/gunzip-maybe": "^1.4.0",
"@types/prettier": "^2.7.2",
"@types/recursive-readdir": "^2.2.1",
"@types/stack-trace": "^0.0.30",
"@types/tar-fs": "^2.0.1",
"devtools-protocol": "^0.0.1177611",
"get-port": "^7.0.0",
"@vitest/coverage-v8": "^0.33.0",
"type-fest": "^3.6.0",
"vitest": "^0.33.0"
Expand All @@ -38,16 +41,20 @@
"@remix-run/dev": "1.19.1",
"@shopify/cli-kit": "3.48.0",
"@shopify/hydrogen-codegen": "^0.0.2",
"@shopify/mini-oxygen": "^1.7.0",
"ansi-escapes": "^6.2.0",
"diff": "^5.1.0",
"fast-glob": "^3.2.12",
"fs-extra": "^11.1.0",
"gunzip-maybe": "^1.4.2",
"mime": "^3.0.0",
"miniflare": "3.20230724.0",
"prettier": "^2.8.4",
"recursive-readdir": "^2.2.3",
"source-map": "^0.7.4",
"stack-trace": "^1.0.0-pre2",
"tar-fs": "^2.1.1",
"typescript": "^4.9.5"
"typescript": "^4.9.5",
"ws": "^8.13.0"
},
"bin": "dist/create-app.js",
"exports": {
Expand Down
14 changes: 11 additions & 3 deletions packages/cli/src/commands/hydrogen/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import {
muteDevLogs,
muteRemixLogs,
} from '../../lib/log.js';
import {deprecated, commonFlags, flagsToCamelObject} from '../../lib/flags.js';
import {
deprecated,
commonFlags,
flagsToCamelObject,
DEFAULT_PORT,
} from '../../lib/flags.js';
import Command from '@shopify/cli-kit/node/base-command';
import {Flags} from '@oclif/core';
import {type MiniOxygen, startMiniOxygen} from '../../lib/mini-oxygen.js';
Expand All @@ -27,6 +32,7 @@ import {spawnCodegenProcess} from '../../lib/codegen.js';
import {getAllEnvironmentVariables} from '../../lib/environment-variables.js';
import {getConfig} from '../../lib/shopify-config.js';
import {checkRemixVersions} from '../../lib/remix-version-check.js';
import {findPort} from '../../lib/find-port.js';

const LOG_REBUILDING = '🧱 Rebuilding...';
const LOG_REBUILT = '🚀 Rebuilt';
Expand Down Expand Up @@ -73,7 +79,7 @@ export default class Dev extends Command {
}

async function runDev({
port,
port: portFlag = DEFAULT_PORT,
path: appPath,
useCodegen = false,
codegenConfigPath,
Expand Down Expand Up @@ -154,7 +160,7 @@ async function runDev({

miniOxygen = await startMiniOxygen({
root,
port,
port: await findPort(portFlag),
watch: true,
buildPathWorkerFile,
buildPathClient,
Expand Down Expand Up @@ -227,6 +233,8 @@ async function runDev({
}

await safeStartMiniOxygen();
} else {
await miniOxygen.reload();
}
},
async onFileCreated(file: string) {
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/commands/hydrogen/preview.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Command from '@shopify/cli-kit/node/base-command';
import {muteDevLogs} from '../../lib/log.js';
import {getProjectPaths} from '../../lib/remix-config.js';
import {commonFlags} from '../../lib/flags.js';
import {commonFlags, DEFAULT_PORT} from '../../lib/flags.js';
import {startMiniOxygen} from '../../lib/mini-oxygen.js';
import {findPort} from '../../lib/find-port.js';

export default class Preview extends Command {
static description =
Expand All @@ -21,7 +22,7 @@ export default class Preview extends Command {
}

export async function runPreview({
port,
port = DEFAULT_PORT,
path: appPath,
}: {
port?: number;
Expand All @@ -35,7 +36,7 @@ export async function runPreview({

const miniOxygen = await startMiniOxygen({
frandiox marked this conversation as resolved.
Show resolved Hide resolved
root,
port,
port: await findPort(port),
Copy link
Contributor

Choose a reason for hiding this comment

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

🤟🏼

buildPathClient,
buildPathWorkerFile,
});
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/lib/find-port.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import getPort, {portNumbers} from 'get-port';

export function findPort(portPreference: number, range = 100) {
return getPort({
port: portNumbers(portPreference, portPreference + range),
});
}
4 changes: 3 additions & 1 deletion packages/cli/src/lib/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {CamelCasedProperties} from 'type-fest';
import {SETUP_CSS_STRATEGIES} from './setups/css/index.js';
import {I18N_CHOICES} from './setups/i18n/index.js';

export const DEFAULT_PORT = 3000;

export const commonFlags = {
path: Flags.string({
description:
Expand All @@ -16,7 +18,7 @@ export const commonFlags = {
port: Flags.integer({
description: 'Port to run the server on.',
env: 'SHOPIFY_HYDROGEN_FLAG_PORT',
default: 3000,
default: DEFAULT_PORT,
}),
force: Flags.boolean({
description:
Expand Down
71 changes: 38 additions & 33 deletions packages/cli/src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ function injectLogReplacer(
console[method] = (...args: unknown[]) => {
if (debounceMessage(args, debouncer?.(args))) return;

const replacer = messageReplacers.find(([matcher]) => matcher(args))?.[1];
if (!replacer) return originalConsole[method](...args);
const replacers = messageReplacers.reduce((acc, [matcher, replacer]) => {
if (matcher(args)) acc.push(replacer);
return acc;
}, [] as Replacer[]);

if (replacers.length === 0) return originalConsole[method](...args);

const result = replacers.reduce(
(resultArgs, replacer) => resultArgs && replacer(resultArgs),
args as void | string[],
);

const result = replacer(args);
if (result) return originalConsole[method](...result);
};
}
Expand All @@ -80,29 +88,13 @@ function injectLogReplacer(
* Mute logs from Miniflare
*/
export function muteDevLogs({workerReload}: {workerReload?: boolean} = {}) {
injectLogReplacer('log');
injectLogReplacer('error');

let isFirstWorkerReload = true;
addMessageReplacers('dev', [
([first]) => typeof first === 'string' && first.includes('[mf:'),
(args: string[]) => {
const first = args[0] as string;

if (workerReload !== false && first.includes('Worker reloaded')) {
if (isFirstWorkerReload) {
isFirstWorkerReload = false;
// return args as string[];
return;
}

return [first.replace('[mf:inf] ', '🔄 ') + '\n', ...args.slice(1)];
}

if (!first.includes('[mf:err]')) {
// Hide logs except errors
return;
}
},
// Workerd logs
([first]) =>
typeof first === 'string' && /^\x1B\[31m(workerd\/|stack:)/.test(first),
() => {},
]);
}

Expand All @@ -123,12 +115,18 @@ export function muteAuthLogs({
process.stdout.write = ((item, cb: any) => {
if (typeof item !== 'string') return write(item, cb);

const replacer = messageReplacers.find(([matcher]) =>
matcher([item]),
)?.[1];
if (!replacer) return write(item, cb);
const replacers = messageReplacers.reduce((acc, [matcher, replacer]) => {
if (matcher([item])) acc.push(replacer);
return acc;
}, [] as Replacer[]);

if (replacers.length === 0) return write(item, cb);

const result = replacers.reduce(
(resultArgs, replacer) => resultArgs && replacer(resultArgs),
[item] as void | string[],
);

const result = replacer([item]);
if (result) return write(result[0] as string, cb);
}) as typeof write;
}
Expand Down Expand Up @@ -193,7 +191,7 @@ export function enhanceH2Logs(options: {
addMessageReplacers('h2-warn', [
([first]) => {
const message = first?.message ?? first;
return typeof message === 'string' && message.startsWith('[h2:');
return typeof message === 'string' && message.includes('[h2:');
},
(args: any[]) => {
const firstArg = args[0];
Expand All @@ -205,7 +203,7 @@ export function enhanceH2Logs(options: {
const stringArg = errorObject?.message ?? (firstArg as string);

const [, type, scope, message] =
stringArg.match(/^\[h2:([^:]+):([^\]]+)\]\s+(.*)$/ims) || [];
stringArg.match(/\[h2:([^:]+):([^\]]+)\]\s+(.*)$/ims) || [];

if (!type || !scope || !message) return args;

Expand All @@ -219,11 +217,18 @@ export function enhanceH2Logs(options: {
if (type === 'error' || errorObject) {
let tryMessage = hasLinks ? lastLine : undefined;
let stack = errorObject?.stack;
const cause = errorObject?.cause as
let cause = errorObject?.cause as
| {[key: string]: any; graphql?: {query: string; variables: string}}
| string
| undefined;

if (!!cause?.graphql?.query) {
if (typeof cause === 'string') {
try {
cause = JSON.parse(cause);
} catch {}
}

if (typeof cause !== 'string' && !!cause?.graphql?.query) {
const {query, variables} = cause.graphql;
const link = `${options.graphiqlUrl}?query=${encodeURIComponent(
query,
Expand Down