Skip to content

Commit

Permalink
Fix esbuild metafile output [publish]
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Oct 6, 2022
1 parent a597782 commit 79de497
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Changelog

## Unreleased
## 0.2.1

- esbuild plugin: Remove CSS map from output
esbuild plugin: Fix metafile output (remove map & update cssBundle prop)

## 0.2.0

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@arnaud-barre/downwind",
"description": "A PostCSS-less implementation of Tailwind based on Lightning CSS",
"version": "0.2.0",
"version": "0.2.1",
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
"license": "MIT",
"scripts": {
Expand Down
59 changes: 39 additions & 20 deletions src/esbuildPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const esbuildPlugin: typeof declaration = ({ scannedExtension } = {}) => ({
build.initialOptions.entryNames?.includes("[hash]") ?? false;
const minify = build.initialOptions.minify ?? false;
const write = build.initialOptions.write ?? true;
const sourcemap = build.initialOptions.sourcemap ?? false;
if (write) build.initialOptions.metafile = true;

const getPlaceholder = () =>
Expand Down Expand Up @@ -106,7 +107,7 @@ const esbuildPlugin: typeof declaration = ({ scannedExtension } = {}) => ({
minify,
targets,
}).code;
if (!useHash) return { output, path: cssPath };
if (!useHash) return { output, newPath: cssPath };
const hexHash = createHash("sha256")
.update(output)
.digest("hex")
Expand All @@ -116,39 +117,57 @@ const esbuildPlugin: typeof declaration = ({ scannedExtension } = {}) => ({
.toUpperCase()
.padStart(8, "0")
.slice(0, 8);
return { output, path: cssPath.replace(/[A-Z\d]{8}/, hash) };
return { output, newPath: cssPath.replace(/[A-Z\d]{8}/, hash) };
};

if (write) {
const outputs = result.metafile!.outputs;
const paths = Object.keys(outputs);
const cssPath = paths.find((p) => p.endsWith(".css"))!;
const cssMapPath = `${cssPath}.map`;
const content = readFileSync(cssPath, "utf-8");
const { output, path } = transform(cssPath, content);
writeFileSync(path, output);
outputs[path] = { ...outputs[path], bytes: output.byteLength };
const oldPath = Object.keys(outputs).find((p) => p.endsWith(".css"))!;
const content = readFileSync(oldPath, "utf-8");
const { output, newPath } = transform(oldPath, content);
writeFileSync(newPath, output);
outputs[newPath] = { ...outputs[oldPath], bytes: output.byteLength };
for (const key in outputs) {
if (outputs[key].cssBundle === oldPath) {
outputs[key].cssBundle = newPath;
}
}
if (useHash) {
rmSync(cssPath);
rmSync(oldPath);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete outputs[cssPath];
delete outputs[oldPath];
}
if (cssMapPath in outputs) {
rmSync(cssMapPath);
if (sourcemap) {
rmSync(`${oldPath}.map`);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete outputs[cssMapPath];
delete outputs[`${oldPath}.map`];
}
} else {
const cssOutput = result.outputFiles!.find((p) =>
p.path.endsWith(".css"),
)!;
const { output, path } = transform(cssOutput.path, cssOutput.text);
cssOutput.path = path;
const oldPath = cssOutput.path;
const { output, newPath } = transform(oldPath, cssOutput.text);
cssOutput.path = newPath;
cssOutput.contents = output;
// https://github.com/evanw/esbuild/issues/2423
Object.defineProperty(cssOutput, "text", {
value: output.toString(),
});
if (sourcemap) {
result.outputFiles = result.outputFiles!.filter(
(f) => f.path !== `${oldPath}.map`,
);
}
if (result.metafile) {
const outputs = result.metafile.outputs;
outputs[newPath] = { ...outputs[oldPath], bytes: output.byteLength };
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
if (useHash) delete outputs[oldPath];
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
if (sourcemap) delete outputs[`${oldPath}.map`];
for (const key in outputs) {
if (outputs[key].cssBundle === oldPath) {
outputs[key].cssBundle = newPath;
}
}
}
}
});
},
Expand Down

0 comments on commit 79de497

Please sign in to comment.