Skip to content

Commit

Permalink
Shorten assets directories (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha authored Sep 13, 2023
1 parent d7ff0d4 commit f8d7f1c
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 36 deletions.
8 changes: 4 additions & 4 deletions dist/hanami-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const externalEsbuildDirectories = () => {
path_1.default.join("app", "assets", "*"),
path_1.default.join("slices", "*", "assets", "*"),
];
const excludeDirs = ['javascripts', 'stylesheets'];
const excludeDirs = ['js', 'css'];
try {
const dirs = (0, glob_1.globSync)(assetDirsPattern, { nodir: false });
const filteredDirs = dirs.filter((dir) => {
Expand Down Expand Up @@ -83,11 +83,11 @@ const loader = {
};
const entryPointExtensions = "app.{js,ts,mjs,mts,tsx,jsx}";
const entryPoints = (0, glob_1.globSync)([
path_1.default.join("app", "assets", "javascripts", "**", entryPointExtensions),
path_1.default.join("slices", "*", "assets", "javascripts", "**", entryPointExtensions),
path_1.default.join("app", "assets", "js", "**", entryPointExtensions),
path_1.default.join("slices", "*", "assets", "js", "**", entryPointExtensions),
]);
// FIXME: make cross platform
const entryPointsMatcher = /(app\/assets\/javascripts\/|slices\/(.*\/)assets\/javascripts\/)/;
const entryPointsMatcher = /(app\/assets\/js\/|slices\/(.*\/)assets\/js\/)/;
const mappedEntryPoints = mapEntryPoints(entryPoints);
const externalDirs = externalEsbuildDirectories();
var sriAlgorithms = [];
Expand Down
8 changes: 4 additions & 4 deletions src/hanami-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const externalEsbuildDirectories = (): string[] => {
path.join("slices", "*", "assets", "*"),
]

const excludeDirs = ['javascripts', 'stylesheets'];
const excludeDirs = ['js', 'css'];

try {
const dirs = globSync(assetDirsPattern, { nodir: false });
Expand Down Expand Up @@ -95,12 +95,12 @@ const loader: { [ext: string]: Loader } = {

const entryPointExtensions = "app.{js,ts,mjs,mts,tsx,jsx}";
const entryPoints = globSync([
path.join("app", "assets", "javascripts", "**", entryPointExtensions),
path.join("slices", "*", "assets", "javascripts", "**", entryPointExtensions),
path.join("app", "assets", "js", "**", entryPointExtensions),
path.join("slices", "*", "assets", "js", "**", entryPointExtensions),
]);

// FIXME: make cross platform
const entryPointsMatcher = /(app\/assets\/javascripts\/|slices\/(.*\/)assets\/javascripts\/)/
const entryPointsMatcher = /(app\/assets\/js\/|slices\/(.*\/)assets\/js\/)/
const mappedEntryPoints = mapEntryPoints(entryPoints);
const externalDirs = externalEsbuildDirectories();
var sriAlgorithms : Array<string> = [];
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
import '../stylesheets/app.css'; // Import the main stylesheet
import '../css/app.css'; // Import the main stylesheet
import TodoList from './TodoList';

const App = () => {
Expand Down
54 changes: 27 additions & 27 deletions test/hanami-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const binPath = path.join(originalWorkingDir, 'dist', 'hanami-assets.js');

const dest = path.resolve(__dirname, '..', 'tmp', crypto.randomUUID());
const watchTimeout = 60000; // ms (60 seconds)
let watchProcess : ChildProcess;
let watchProcess: ChildProcess;

// Helper function to create a test environment
async function createTestEnvironment() {
// Create temporary directories
await fs.ensureDir(path.join(dest, 'app/assets/javascripts'));
await fs.ensureDir(path.join(dest, 'app/assets/js'));
await fs.ensureDir(path.join(dest, 'app/assets/images'));
await fs.ensureDir(path.join(dest, 'slices/admin/assets/javascripts'));
await fs.ensureDir(path.join(dest, 'slices/metrics/assets/javascripts'));
await fs.ensureDir(path.join(dest, 'slices/admin/assets/js'));
await fs.ensureDir(path.join(dest, 'slices/metrics/assets/js'));
await fs.ensureDir(path.join(dest, 'public'));

process.chdir(dest);
Expand All @@ -43,9 +43,9 @@ describe('hanami-assets', () => {
});

test('copies assets from app/assets to public/assets and generates a manifest file', async () => {
const entryPoint1 = path.join(dest, 'app/assets/javascripts/app.js');
const entryPoint2 = path.join(dest, 'slices/admin/assets/javascripts/app.js');
const entryPoint3 = path.join(dest, 'slices/metrics/assets/javascripts/app.ts');
const entryPoint1 = path.join(dest, 'app/assets/js/app.js');
const entryPoint2 = path.join(dest, 'slices/admin/assets/js/app.js');
const entryPoint3 = path.join(dest, 'slices/metrics/assets/js/app.ts');
await fs.writeFile(entryPoint1, "console.log('Hello, World!');");
await fs.writeFile(entryPoint2, "console.log('Hello, Admin!');");
await fs.writeFile(entryPoint3, "console.log('Hello, Metrics!');");
Expand Down Expand Up @@ -77,19 +77,19 @@ describe('hanami-assets', () => {
// Check if the manifest contains the correct file paths
expect(manifest).toEqual({
"admin/app.js": {
"url": "/assets/admin/app-G3TTFD5I.js"
"url": "/assets/admin/app-NLRESL5A.js",
},
"app.js": {
"url": "/assets/app-SMJS4SYG.js"
"url": "/assets/app-JLSTK5SN.js",
},
"metrics/app.js": {
"url": "/assets/metrics/app-62A4ZWTV.js"
},
"url": "/assets/metrics/app-27Z7ZALS.js",
}
});
});

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

execFileSync(binPath, ['--sri=sha256,sha384,sha512'], { stdio: "inherit" })
Expand All @@ -101,11 +101,11 @@ describe('hanami-assets', () => {
// Check if the manifest contains the correct file paths
expect(manifest).toEqual({
"app.js": {
"url": "/assets/app-SMJS4SYG.js",
"url": "/assets/app-JLSTK5SN.js",
"sri": [
"sha256-NXy3RVHksHBJVqQXCvl4bXhNrOLPyI6JY6aQwwscD3s=",
"sha384-QJusYGV1R3duafMvNtieaCc3dWSULtYdTItexlRLPkpSbxdPLOtv7cDIt4xOtpTP",
"sha512-Yer6vTccJNUWJaKZ54hAmwUSmjqfhFLyR/dXLQz0jlNAOCVw40+bSBBvWZCJOzRm8MYmqzElLdA7z2yeGPpVFg==",
"sha256-p4j9argOiwyiBIBi7v4H0WUnv6z3kmFjqmManMEJXfo=",
"sha384-gkA54jmSv7TBjiSzGrfO/uCR5CyUrQSUSUYrnM0lICIaP5ppqcN8PLVE3mNj87sN",
"sha512-ZCJAQgHAxcBP7xEuQ3W/pZqqI611aX9oEk0QvDG4Etq6bkvxQMWHrHk2npCytWchOTN1yKM7TLj9Vsp1Id0j6g=="
]
},
});
Expand All @@ -123,9 +123,9 @@ describe('hanami-assets', () => {
// Check if the manifest contains the correct file paths
expect(manifest).toEqual({
"app.js": {
"url": "/assets/app-F66C5HBN.js",
"url": "/assets/app-QMPF4E7L.js",
"sri": [
"sha384-HyjZlrfiFrDd3kcjk++Tkbv908eTWAXXMu6Nha8liVSuH9gr+3fG4OrYYBLY/iiw"
"sha384-k/bXXyKadrRJgpY1dFmJgeHONHtO6HE+SxDgP1jgtYQ0NazikfTc3fDCqUfrOr8Z"
]
},
"background.jpg": {
Expand All @@ -135,21 +135,21 @@ describe('hanami-assets', () => {
]
},
"app.css": {
"url": "/assets/app-ISMEPBMJ.css",
"url": "/assets/app-REBB4IZN.css",
"sri": [
"sha384-XI8KeiPqBbtxWX3JvD4OIos7cNGNHIPL8/MteaUxGWLS+Yp/nkE1fb4M9Gc2tvN2"
"sha384-ZqNcOOnG1W+Udcquy7qBs4b51X1cq8Bkv0CMEIt5I20+TWmW33pLUHl54ueCnyFD"
]
},
"login/app.js": {
"url": "/assets/login/app-FUSCFK37.js",
"url": "/assets/login/app-SV7Q442Q.js",
"sri": [
"sha384-Wj7sxFDKOiC2c2nPfyDRvBHKG0LiwNiQYkoSKHD/COISJbiAlSLNwHhm0FGR8+KB"
"sha384-fkk3ZtSsrBOPtmKaOSoHC5IKdaphOeG05j0Z3iQPrJdbQAxsAmCkJMNQphDyL8E2"
]
},
"admin/app.js": {
"url": "/assets/admin/app-3ROSYITC.js",
"url": "/assets/admin/app-XJBJOVJT.js",
"sri": [
"sha384-DoWFNj0ynI1lJUvCY2R1DMBIoF10HoKT08ya5at4/jlGOOgPwWYfu0RF9Sq+Kcne"
"sha384-aqVgGQ+qnJfgFqVoOlHBEEQ1eUtNDN5cQu9EdUW7gIGT9VaUN8H2yw5ai+lW+jc9"
]
},
"font.otf": {
Expand All @@ -172,13 +172,13 @@ describe('hanami-assets', () => {
await fs.ensureDir(images);
fs.copySync(path.join(__dirname, "fixtures", "todo", "app", "assets", "images", "background.jpg"), path.join(images, "background.jpg"));

const entryPoint = path.join(dest, "app", "assets", "javascripts", "app.js");
const entryPoint = path.join(dest, "app", "assets", "js", "app.js");
await fs.writeFile(entryPoint, "console.log('Hello, World!');");

const appAsset = path.join(dest, "public", "assets", "app.js");
const imageAsset = path.join(dest, "public", "assets", "background.jpg");

watchProcess = spawn(binPath, ["--watch"], {cwd: dest});
watchProcess = spawn(binPath, ["--watch"], { cwd: dest });
await fs.writeFile(entryPoint, "console.log('Hello, Watch!');");

const appAssetExists = (timeout = watchTimeout): Promise<boolean> => {
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('hanami-assets', () => {
const manifestContent = await fs.readFile(path.join(dest, 'public/assets.json'), 'utf-8');
const manifest = JSON.parse(manifestContent);

expect(manifest["background.jpg"]).toEqual({"url": "/assets/background.jpg"})
expect(manifest["background.jpg"]).toEqual({ "url": "/assets/background.jpg" })

// childProcess.kill("SIGHUP");
}, watchTimeout + 1000);
Expand Down

0 comments on commit f8d7f1c

Please sign in to comment.