Skip to content

Commit

Permalink
fix: PR feedback 2
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmenPopoviciu committed Sep 20, 2024
1 parent dabea9f commit e6a366d
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 62 deletions.
195 changes: 192 additions & 3 deletions packages/wrangler/e2e/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,57 @@ describe("custom builds", () => {
});

describe("watch mode", () => {
describe.each([{ cmd: "wrangler dev" }, { cmd: "wrangler dev --x-dev-env" }])(
"Workers watch mode: $cmd",
({ cmd }) => {
it(`supports modifying the Worker script during dev session`, async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/workerA.ts"
compatibility_date = "2023-01-01"
`,
"src/workerA.ts": dedent`
export default {
fetch(request) {
return new Response("Hello from user Worker A!")
}
}`,
});

const worker = helper.runLongLived(cmd);
const { url } = await worker.waitForReady();

let text = await fetchText(url);
expect(text).toBe("Hello from user Worker A!");

await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/workerB.ts"
compatibility_date = "2023-01-01"
`,
"src/workerB.ts": dedent`
export default {
fetch(request) {
return new Response("Hello from user Worker B!")
}
}`,
});

await worker.waitForReload();
text = await retry(
(s) => s != "Hello from user Worker B!",
async () => {
return await fetchText(url);
}
);
expect(text).toBe("Hello from user Worker B!");
});
}
);

describe.each([{ cmd: "wrangler dev" }, { cmd: "wrangler dev --x-dev-env" }])(
"Workers + Assets watch mode: $cmd",
({ cmd }) => {
Expand Down Expand Up @@ -1088,7 +1139,145 @@ describe("watch mode", () => {
);
});

it(`supports switching from assets-only to Workers with assets during the current dev session`, async () => {
it(`supports switching from Workers without assets to assets-only Workers during the current dev session`, async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/index.ts"
compatibility_date = "2023-01-01"
`,
"src/index.ts": dedent`
export default {
fetch(request) {
return new Response("Hello from user Worker!")
}
}`,
});

const worker = helper.runLongLived(cmd);
const { url } = await worker.waitForReady();

let response = await fetch(url);
expect(response.status).toBe(200);
expect(await response.text()).toBe("Hello from user Worker!");

response = await fetch(`${url}/hey`);
expect(response.status).toBe(200);
expect(await response.text()).toBe("Hello from user Worker!");

response = await fetch(`${url}/index.html`);
expect(response.status).toBe(200);
expect(await response.text()).toBe("Hello from user Worker!");

await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
compatibility_date = "2023-01-01"
[experimental_assets]
directory = "./public"
`,
"public/index.html": dedent`
<h1>Hello Workers + Assets</h1>`,
});

await worker.waitForReload();

// verify response from Asset Worker
const { status, text } = await retry(
(s) => s.text !== "<h1>Hello Workers + Assets</h1>",
async () => {
const fetchResponse = await fetch(url);
return {
status: fetchResponse.status,
text: await fetchResponse.text(),
};
}
);
expect(status).toBe(200);
expect(text).toBe("<h1>Hello Workers + Assets</h1>");

response = await fetch(`${url}/index.html`);
expect(response.status).toBe(200);
expect(await response.text()).toBe("<h1>Hello Workers + Assets</h1>");

// verify we no longer get a response from the User Worker
response = await fetch(`${url}/hey`);
expect(response.status).toBe(404);
});

it(`supports switching from Workers without assets to Workers with assets during the current dev session`, async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/index.ts"
compatibility_date = "2023-01-01"
`,
"src/index.ts": dedent`
export default {
fetch(request) {
return new Response("Hello from user Worker!")
}
}`,
});

const worker = helper.runLongLived(cmd);
const { url } = await worker.waitForReady();

let response = await fetch(url);
expect(response.status).toBe(200);
expect(await response.text()).toBe("Hello from user Worker!");

response = await fetch(`${url}/hey`);
expect(response.status).toBe(200);
expect(await response.text()).toBe("Hello from user Worker!");

response = await fetch(`${url}/index.html`);
expect(response.status).toBe(200);
expect(await response.text()).toBe("Hello from user Worker!");

await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/index.ts"
compatibility_date = "2023-01-01"
[experimental_assets]
directory = "./public"
`,
"public/index.html": dedent`
<h1>Hello Workers + Assets</h1>`,
});

await worker.waitForReload();

// verify response from Asset Worker
const { status, text } = await retry(
(s) => s.text !== "<h1>Hello Workers + Assets</h1>",
async () => {
const fetchResponse = await fetch(url);
return {
status: fetchResponse.status,
text: await fetchResponse.text(),
};
}
);
expect(status).toBe(200);
expect(text).toBe("<h1>Hello Workers + Assets</h1>");

response = await fetch(`${url}/index.html`);
expect(response.status).toBe(200);
expect(await response.text()).toBe("<h1>Hello Workers + Assets</h1>");

// verify response from the User Worker
response = await fetch(`${url}/hey`);
expect(response.status).toBe(200);
expect(await response.text()).toBe("Hello from user Worker!");
});

it(`supports switching from assets-only Workers to Workers with assets during the current dev session`, async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed({
"wrangler.toml": dedent`
Expand Down Expand Up @@ -1144,7 +1333,7 @@ describe("watch mode", () => {
expect(await response.text()).toBe("Hello from user Worker!");
});

it(`supports switching from Workers with assets to assets-only during the current dev session`, async () => {
it(`supports switching from Workers with assets to assets-only Workers during the current dev session`, async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed({
"wrangler.toml": dedent`
Expand Down Expand Up @@ -1289,7 +1478,7 @@ describe("watch mode", () => {
expect(response.status).toBe(404);
});

it(`supports switching from assets-only to Workers with assets during the current dev session`, async () => {
it(`supports switching from assets-only Workers to Workers with assets during the current dev session`, async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed({
"wrangler.toml": dedent`
Expand Down
40 changes: 15 additions & 25 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -845,32 +845,22 @@ export async function startDev(args: StartDevOptions) {

logger.log(`${path.basename(config.configPath)} changed...`);

/*
* If this is a Worker with assets, we want to enable switching
* from assets only Worker to Worker with assets and vice versa
* during local development. For that, we need to watch for changes
* in the `wrangler.toml` file, specifically the `main` configuration
* key, and re-asses the entry point every time.
*/
if (experimentalAssetsOptions?.directory) {
entry = await getEntry(
{
legacyAssets: args.legacyAssets,
script: args.script,
moduleRoot: args.moduleRoot,
experimentalAssets: args.experimentalAssets,
},
config,
"dev"
);
// ensure we reflect config changes in the `main` entry point
entry = await getEntry(
{
legacyAssets: args.legacyAssets,
script: args.script,
moduleRoot: args.moduleRoot,
experimentalAssets: args.experimentalAssets,
},
config,
"dev"
);

// this gets passed into the Dev React element, so ensure we don't
// block scope this var
experimentalAssetsOptions = processExperimentalAssetsArg(
args,
config
);
}
experimentalAssetsOptions = processExperimentalAssetsArg(
args,
config
);

/*
* Handle experimental assets watching on config file changes
Expand Down
69 changes: 35 additions & 34 deletions packages/wrangler/src/experimental-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,45 +328,46 @@ export function processExperimentalAssetsArg(
const experimentalAssets = args.experimentalAssets
? { directory: args.experimentalAssets }
: config.experimental_assets;
let experimentalAssetsOptions: ExperimentalAssetsOptions | undefined;
if (experimentalAssets) {
const experimentalAssetsBasePath = getExperimentalAssetsBasePath(
config,
args.experimentalAssets
);
const resolvedExperimentalAssetsPath = path.resolve(
experimentalAssetsBasePath,
experimentalAssets.directory
);

if (!existsSync(resolvedExperimentalAssetsPath)) {
const sourceOfTruthMessage = args.experimentalAssets
? '"--experimental-assets" command line argument'
: '"experimental_assets.directory" field in your configuration file';
if (!experimentalAssets) {
return;
}

throw new UserError(
`The directory specified by the ${sourceOfTruthMessage} does not exist:\n` +
`${resolvedExperimentalAssetsPath}`
);
}
const experimentalAssetsBasePath = getExperimentalAssetsBasePath(
config,
args.experimentalAssets
);
const resolvedExperimentalAssetsPath = path.resolve(
experimentalAssetsBasePath,
experimentalAssets.directory
);

experimentalAssets.directory = resolvedExperimentalAssetsPath;
const routingConfig = {
has_user_worker: Boolean(args.script || config.main),
};
// defaults are set in asset worker
const assetConfig = {
html_handling: config.experimental_assets?.html_handling,
not_found_handling: config.experimental_assets?.not_found_handling,
};
experimentalAssetsOptions = {
...experimentalAssets,
routingConfig,
assetConfig,
};
if (!existsSync(resolvedExperimentalAssetsPath)) {
const sourceOfTruthMessage = args.experimentalAssets
? '"--experimental-assets" command line argument'
: '"experimental_assets.directory" field in your configuration file';

throw new UserError(
`The directory specified by the ${sourceOfTruthMessage} does not exist:\n` +
`${resolvedExperimentalAssetsPath}`
);
}

return experimentalAssetsOptions;
experimentalAssets.directory = resolvedExperimentalAssetsPath;
const routingConfig = {
has_user_worker: Boolean(args.script || config.main),
};
// defaults are set in asset worker
const assetConfig = {
html_handling: config.experimental_assets?.html_handling,
not_found_handling: config.experimental_assets?.not_found_handling,
};

return {
...experimentalAssets,
routingConfig,
assetConfig,
};
}

/**
Expand Down

0 comments on commit e6a366d

Please sign in to comment.