Skip to content

Commit

Permalink
Use extension from compiled asset in manifest keys
Browse files Browse the repository at this point in the history
This means that .ts files will have .js extensions when referencing them, as before.
  • Loading branch information
timriley committed Feb 3, 2024
1 parent 3ee71a5 commit 6512383
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
5 changes: 4 additions & 1 deletion dist/esbuild-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ const hanamiEsbuild = (options) => {
}
// Process entrypoints
for (const compiledEntryPoint in compiledEntryPoints) {
// Convert "public/assets/app-2TLUHCQ6.js" to "app.js"
let sourceUrl = compiledEntryPoint
.replace(options.destDir + "/", "")
.replace(/(-[A-Z0-9]{8})(\.\S+)$/, "$2");
const destinationUrl = calulateDestinationUrl(compiledEntryPoint);
const sourceUrl = compiledEntryPoints[compiledEntryPoint].replace(`${options.baseDir}/assets/js/`, "");
assetsManifest[sourceUrl] = prepareAsset(compiledEntryPoint, destinationUrl);
}
// Process copied assets
Expand Down
9 changes: 5 additions & 4 deletions src/esbuild-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,12 @@ const hanamiEsbuild = (options: PluginOptions): Plugin => {

// Process entrypoints
for (const compiledEntryPoint in compiledEntryPoints) {
// Convert "public/assets/app-2TLUHCQ6.js" to "app.js"
let sourceUrl = compiledEntryPoint
.replace(options.destDir + "/", "")
.replace(/(-[A-Z0-9]{8})(\.\S+)$/, "$2")

const destinationUrl = calulateDestinationUrl(compiledEntryPoint);
const sourceUrl = compiledEntryPoints[compiledEntryPoint].replace(
`${options.baseDir}/assets/js/`,
"",
);

assetsManifest[sourceUrl] = prepareAsset(compiledEntryPoint, destinationUrl);
}
Expand Down
36 changes: 36 additions & 0 deletions test/hanami-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const watchTimeout = 60000; // ms (60 seconds)
async function createTestEnvironment() {
// Create temporary directories
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"));
await fs.ensureDir(path.join(dest, "app/assets/fonts"));
await fs.ensureDir(path.join(dest, "slices/admin/assets/js"));
Expand Down Expand Up @@ -159,6 +160,41 @@ describe("hanami-assets", () => {
});
});

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

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

const asset1Exists = await fs.pathExists(path.join("public/assets/app-2TLUHCQ6.js"));
expect(asset1Exists).toBe(true);
const asset2Exists = await fs.pathExists(path.join("public/assets/nested/app-5VHYTKP2.js"));
expect(asset2Exists).toBe(true);

const manifestExists = await fs.pathExists(path.join(dest, "public/assets/assets.json"));
expect(manifestExists).toBe(true);

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

// Check if the manifest contains the correct file paths
expect(manifest).toEqual({
"app.js": {
url: "/assets/app-2TLUHCQ6.js",
},
"nested/app.js": {
url: "/assets/nested/app-5VHYTKP2.js",
},
});
})

test(
"watch",
async () => {
Expand Down

0 comments on commit 6512383

Please sign in to comment.