Skip to content

Commit

Permalink
Add ability to ignore files
Browse files Browse the repository at this point in the history
This is aimed at .DS_Store files created by Finder on macOS to store view metadata.
  • Loading branch information
svoop committed Apr 2, 2024
1 parent fd1722e commit 7382916
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dist/esbuild-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import crypto from "node:crypto";
import { globSync } from "glob";
const URL_SEPARATOR = "/";
const assetsDirName = "assets";
const ignoredFileNames = [".DS_Store"];
const fileHashRegexp = /(-[A-Z0-9]{8})(\.\S+)$/;
const hanamiEsbuild = (options) => {
return {
Expand Down Expand Up @@ -104,6 +105,10 @@ const hanamiEsbuild = (options) => {
const files = fs.readdirSync(assetDir, { recursive: true });
const assets = [];
files.forEach((file) => {
// Skip ignored files
if (ignoredFileNames.includes(path.basename(file.toString()))) {
return;
}
const sourcePath = path.join(assetDir, file.toString());
// Skip files loaded by esbuild; those are added to the manifest separately
if (loadedFiles.has(sourcePath)) {
Expand Down
6 changes: 6 additions & 0 deletions src/esbuild-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface CopiedAsset {
}

const assetsDirName = "assets";
const ignoredFileNames = [".DS_Store"];
const fileHashRegexp = /(-[A-Z0-9]{8})(\.\S+)$/;

const hanamiEsbuild = (options: PluginOptions): Plugin => {
Expand Down Expand Up @@ -146,6 +147,11 @@ const hanamiEsbuild = (options: PluginOptions): Plugin => {
const assets: CopiedAsset[] = [];

files.forEach((file) => {
// Skip ignored files
if (ignoredFileNames.includes(path.basename(file.toString()))) {
return;
}

const sourcePath = path.join(assetDir, file.toString());

// Skip files loaded by esbuild; those are added to the manifest separately
Expand Down
27 changes: 27 additions & 0 deletions test/hanami-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,33 @@ describe("hanami-assets", () => {
});
});

test("ignores .DS_Store files", 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, "");

// Create .DS_Store files which are used by macOS to store meta data
await fs.writeFile(path.join(dest, "app/assets/.DS_Store"), "foobar");
await fs.writeFile(path.join(dest, "app/assets/js/.DS_Store"), "foobar");
await fs.writeFile(path.join(dest, "app/assets/css/.DS_Store"), "foobar");
await fs.ensureDir(path.join(dest, "app/assets/images"));
await fs.writeFile(path.join(dest, "app/assets/images/.DS_Store"), "foobar");
await fs.ensureDir(path.join(dest, "app/assets/images/subdir"));
await fs.writeFile(path.join(dest, "app/assets/images/subdir/.DS_Store"), "foobar");

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

const ignoredFiles = globSync("**/.DS_Store", { cwd: path.join("public", "assets") });
expect(ignoredFiles.length).toBe(0);

const manifestContent = await fs.readFile(
path.join(dest, "public/assets/assets.json"),
"utf-8",
);
expect(manifestContent).not.toMatch(/\.DS_Store/);
});

test("generates SRI", async () => {
const appEntryPoint = path.join(dest, "app/assets/js/app.js");
await fs.writeFile(appEntryPoint, "console.log('Hello, World!');");
Expand Down

0 comments on commit 7382916

Please sign in to comment.