Skip to content
Draft
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
9 changes: 9 additions & 0 deletions .changeset/source-maps-starters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"create-cloudflare": patch
---

Enable source maps in JavaScript and TypeScript Worker starter templates

New Worker starters explicitly enable source map uploads so production stack traces and profiles can resolve to original source. Cloudflare Vite starters also explicitly generate external maps for their Worker environments, without enabling browser source maps.

Preserve explicit source map upload opt-outs when updating a generated Wrangler configuration. Existing Wrangler and Vite plugin defaults are unchanged; custom build tools must still emit external maps for Wrangler to upload.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ describe("update wrangler config", () => {
);
});

test("preserves an explicit source map opt-out in JSON config", async ({
expect,
}) => {
vi.mocked(existsSync).mockImplementation((file) =>
String(file).endsWith(".jsonc")
);
vi.mocked(readFile).mockReturnValue(
'{"main":"src/index.ts","upload_source_maps":false}'
);
await updateWranglerConfig(ctx);
const output = vi
.mocked(writeFile)
.mock.calls.find(([file]) => String(file).endsWith(".jsonc"));
expect(output?.[1]).toContain('"upload_source_maps": false');
});

test("placeholder replacement `<TBD>`", async ({ expect }) => {
const toml = [
`name = "<TBD>"`,
Expand Down
2 changes: 1 addition & 1 deletion packages/create-cloudflare/src/wrangler/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const updateWranglerConfig = async (ctx: C3Context) => {
wranglerJson = appendJSONProperty(
wranglerJson,
"upload_source_maps",
true
wranglerJson.upload_source_maps ?? true
);
wranglerJson = applyNodejsCompatFlags(wranglerJson, compatibilityDate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "<WORKER_NAME>",
"compatibility_date": "<COMPATIBILITY_DATE>",
"main": "dist/analog/server/index.mjs",
"upload_source_maps": true,
"assets": {
"binding": "ASSETS",
"directory": "dist/analog/public",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "./dist/server/server.mjs",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"assets": {
"binding": "ASSETS",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"observability": {
"enabled": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"observability": {
"enabled": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"assets": {
// The path to the directory containing the `index.html` file to be served at `/`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"migrations": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"migrations": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"migrations": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"compatibility_flags": [
"global_fetch_strictly_public"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"compatibility_flags": [
"global_fetch_strictly_public"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",

"observability": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",

"observability": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"observability": {
"enabled": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"observability": {
"enabled": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"assets": {
"binding": "ASSETS",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "./.output/server/index.mjs",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"assets": {
"binding": "ASSETS",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"observability": {
"enabled": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"observability": {
"enabled": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"observability": {
"enabled": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "./dist/_worker.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"compatibility_flags": [
"global_fetch_strictly_public"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default defineConfig({
tailwindcss(),
reactRouter(),
],
environments: {
ssr: { build: { sourcemap: true } },
},
resolve: {
tsconfigPaths: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"name": "<WORKER_NAME>",
"compatibility_date": "<COMPATIBILITY_DATE>",
"main": "./workers/app.ts",
"upload_source_maps": true,
"vars": {
"VALUE_FROM_CLOUDFLARE": "Hello from Cloudflare"
}
Expand Down
44 changes: 41 additions & 3 deletions packages/create-cloudflare/templates/react/workers/c3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
inputPrompt,
spinner,
} from "@cloudflare/cli-shared-helpers/interactive";
import { transformFile } from "@cloudflare/shared-ast-primitives";
import {
mergeObjectProperties,
transformFile,
} from "@cloudflare/shared-ast-primitives";
import { runFrameworkGenerator } from "frameworks/index";
import { readJSON, usesTypescript, writeJSON } from "helpers/files";
import { detectPackageManager } from "helpers/packageManagers";
Expand Down Expand Up @@ -72,7 +75,8 @@ function transformViteConfig(ctx: C3Context) {
// Add the imported plugin to the config
// ```
// defineConfig({
// plugins: [react(), cloudflare()],
// plugins: [react(), cloudflare({ viteEnvironment: { name: "worker" } })],
// environments: { worker: { build: { sourcemap: true } } },
// });
const callee = n.node.callee as types.namedTypes.Identifier;
if (callee.name !== "defineConfig") {
Expand All @@ -84,9 +88,43 @@ function transformViteConfig(ctx: C3Context) {
const pluginsProp = config.properties.find((prop) => isPluginsProp(prop));
assert(pluginsProp && t.ArrayExpression.check(pluginsProp.value));
pluginsProp.value.elements.push(
b.callExpression(b.identifier("cloudflare"), [])
b.callExpression(b.identifier("cloudflare"), [
b.objectExpression([
b.objectProperty(
b.identifier("viteEnvironment"),
b.objectExpression([
b.objectProperty(
b.identifier("name"),
b.stringLiteral("worker")
),
])
),
]),
])
);

mergeObjectProperties(config, [
b.objectProperty(
b.identifier("environments"),
b.objectExpression([
b.objectProperty(
b.identifier("worker"),
b.objectExpression([
b.objectProperty(
b.identifier("build"),
b.objectExpression([
b.objectProperty(
b.identifier("sourcemap"),
b.booleanLiteral(true)
),
])
),
])
),
])
),
]);

return false;
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "worker/index.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"assets": { "not_found_handling": "single-page-application" },
"observability": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "worker/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"assets": { "not_found_handling": "single-page-application" },
"observability": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"observability": {
"enabled": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": "src/index.ts",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"observability": {
"enabled": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"compatibility_date": "<COMPATIBILITY_DATE>",
"upload_source_maps": true,
"observability": {
"enabled": true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<WORKER_NAME>",
"main": ".svelte-kit/cloudflare/_worker.js",
"upload_source_maps": true,
"compatibility_date": "<COMPATIBILITY_DATE>",
"compatibility_flags": ["nodejs_als"],
"assets": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ export default defineConfig({
plugins: [
vue(),
vueDevTools(),
cloudflare()
cloudflare({ viteEnvironment: { name: "worker" } })
],
environments: {
worker: { build: { sourcemap: true } },
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "<WORKER_NAME>",
"compatibility_date": "<COMPATIBILITY_DATE>",
"main": "server/index.js",
"upload_source_maps": true,
"assets": {
"not_found_handling": "single-page-application",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ export default defineConfig({
plugins: [
vue(),
vueDevTools(),
cloudflare()
cloudflare({ viteEnvironment: { name: "worker" } })
],
environments: {
worker: { build: { sourcemap: true } },
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "<WORKER_NAME>",
"compatibility_date": "<COMPATIBILITY_DATE>",
"main": "server/index.ts",
"upload_source_maps": true,
"assets": {
"not_found_handling": "single-page-application",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default defineConfig({
include: ['hono/tiny'],
},
build: {
sourcemap: true,
rolldownOptions: {
platform: 'neutral',
},
Expand All @@ -22,6 +23,7 @@ export default defineConfig({
include: ['waku > rsc-html-stream/server'],
},
build: {
sourcemap: true,
rolldownOptions: {
platform: 'neutral',
},
Expand Down
Loading
Loading