Skip to content

Commit

Permalink
Put CSS, etc. back in the manifest
Browse files Browse the repository at this point in the history
Whoops
  • Loading branch information
timriley committed Feb 3, 2024
1 parent 6512383 commit 246826b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
7 changes: 3 additions & 4 deletions dist/esbuild-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,13 @@ const hanamiEsbuild = (options) => {
// To this:
//
// {
// 'public/assets/admin/app-ITGLRDE7.js': 'slices/admin/assets/js/app.js'
// 'public/assets/admin/app-ITGLRDE7.js': true
// }
function extractEsbuildCompiledEntrypoints(esbuildOutputs) {
const entryPoints = {};
for (const key in esbuildOutputs) {
const output = esbuildOutputs[key];
if (output.entryPoint) {
entryPoints[key] = output.entryPoint;
if (!key.endsWith(".map")) {
entryPoints[key] = true;
}
}
return entryPoints;
Expand Down
14 changes: 6 additions & 8 deletions src/esbuild-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ const hanamiEsbuild = (options: PluginOptions): Plugin => {
// To this:
//
// {
// 'public/assets/admin/app-ITGLRDE7.js': 'slices/admin/assets/js/app.js'
// 'public/assets/admin/app-ITGLRDE7.js': true
// }
function extractEsbuildCompiledEntrypoints(
esbuildOutputs: Record<string, any>,
): Record<string, string> {
const entryPoints: Record<string, string> = {};
): Record<string, boolean> {
const entryPoints: Record<string, boolean> = {};

for (const key in esbuildOutputs) {
const output = esbuildOutputs[key];

if (output.entryPoint) {
entryPoints[key] = output.entryPoint;
if (!key.endsWith(".map")) {
entryPoints[key] = true;
}
}

Expand Down Expand Up @@ -124,7 +122,7 @@ const hanamiEsbuild = (options: PluginOptions): Plugin => {

const processAssetDirectory = (
pattern: string,
compiledEntryPoints: Record<string, string>,
compiledEntryPoints: Record<string, boolean>,
options: PluginOptions,
): string[][] => {
const dirPath = path.dirname(pattern);
Expand Down
30 changes: 30 additions & 0 deletions test/hanami-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const watchTimeout = 60000; // ms (60 seconds)
// Helper function to create a test environment
async function createTestEnvironment() {
// Create temporary directories
await fs.ensureDir(path.join(dest, "app/assets/css"));
await fs.ensureDir(path.join(dest, "app/assets/js"));
await fs.ensureDir(path.join(dest, "app/assets/js/nested"));
await fs.ensureDir(path.join(dest, "app/assets/images/nested"));
Expand Down Expand Up @@ -160,6 +161,35 @@ describe("hanami-assets", () => {
});
});

test("handles CSS", async () => {
const entryPoint = path.join(dest, "app/assets/js/app.js");
await fs.writeFile(entryPoint, 'import "../css/app.css";');
const cssFile = path.join(dest, "app/assets/css/app.css");
await fs.writeFile(cssFile, ".btn { background: #f00; }");

await assets.run({ root: dest, argv: ["--path=app", "--target=public/assets"] });

const entryPointExists = await fs.pathExists(path.join("public/assets/app-6PW7FGD5.js"));
expect(entryPointExists).toBe(true);
const cssExists = await fs.pathExists(path.join("public/assets/app-HYVEQYF6.css"));
expect(cssExists).toBe(true);

const manifestContent = await fs.readFile(
path.join(dest, "public/assets/assets.json"),
"utf-8",
);
const manifest = JSON.parse(manifestContent);

expect(manifest).toEqual({
"app.css": {
url: "/assets/app-HYVEQYF6.css",
},
"app.js": {
url: "/assets/app-6PW7FGD5.js",
},
});
})

test("handles TypeScript", async () => {
const entryPoint1 = path.join(dest, "app/assets/js/app.ts");
await fs.writeFile(entryPoint1, "console.log('Hello from TS!');");
Expand Down

0 comments on commit 246826b

Please sign in to comment.