Skip to content

Commit

Permalink
Remove source map annotation to prevent browser warnings (#1364)
Browse files Browse the repository at this point in the history
This is necessary because we don't expose the sourcemap files to the browser in production
  • Loading branch information
blittle authored Sep 25, 2023
1 parent 21909ac commit 425791c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tender-singers-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-hydrogen': patch
---

Remove sourcemap annotations from client bundles. This prevents errors showing up in the devtools when the sourcemaps fail to load.
21 changes: 20 additions & 1 deletion packages/cli/src/commands/hydrogen/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
copyFile,
rmdir,
glob,
removeFile,
fileExists,
readFile,
writeFile,
} from '@shopify/cli-kit/node/fs';
import {resolvePath, relativePath, joinPath} from '@shopify/cli-kit/node/path';
import {getPackageManager} from '@shopify/cli-kit/node/node-package-manager';
Expand Down Expand Up @@ -186,6 +187,10 @@ export async function runBuild({
}
}

if (process.env.NODE_ENV !== 'development') {
await cleanClientSourcemaps(buildPathClient);
}

// The Remix compiler hangs due to a bug in ESBuild:
// https://github.com/evanw/esbuild/issues/2727
// The actual build has already finished so we can kill the process.
Expand All @@ -194,6 +199,20 @@ export async function runBuild({
}
}

async function cleanClientSourcemaps(buildPathClient: string) {
const bundleFiles = await glob(joinPath(buildPathClient, '**/*.js'));

await Promise.all(
bundleFiles.map(async (filePath) => {
const file = await readFile(filePath);
return await writeFile(
filePath,
file.replace(/\/\/# sourceMappingURL=.+\.js\.map$/gm, ''),
);
}),
);
}

async function writeBundleAnalysis(
buildPath: string,
root: string,
Expand Down

0 comments on commit 425791c

Please sign in to comment.