Last active
January 25, 2023 12:12
-
-
Save dixpac/1a12cb8839589dd0a48a6602e3d3ec27 to your computer and use it in GitHub Desktop.
Helpers esbuild alias
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// helpers/current_helpers.js | |
// On-demand JavaScript objects from "current" HTML <meta> elements. Example: | |
// | |
// <meta name="current-account-id" content="123"> | |
// <meta name="current-account-subdomain" content="Wizard Lab"> | |
// | |
// >> current.account | |
// => { id: "123", subdomain: "Wizard Lab" } | |
// | |
// >> current.foo | |
// => {} | |
export const current = new Proxy({}, { | |
get(target, propertyName) { | |
const result = {} | |
const prefix = `current-${propertyName}-` | |
for (const { name, content } of document.head.querySelectorAll(`meta[name^=${prefix}]`)) { | |
const key = camelize(name.slice(prefix.length)) | |
result[key] = content | |
} | |
return result | |
} | |
}) | |
function camelize(string) { | |
return string.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path") | |
const rails = require("esbuild-rails") | |
const alias = require("esbuild-plugin-alias"); | |
const watch = process.argv.includes("--watch") && { | |
onRebuild(error) { | |
if (error) console.error("[watch] build failed", error); | |
else console.log("[watch] build finished"); | |
}, | |
}; | |
require("esbuild").build({ | |
entryPoints: ["application.js"], | |
sourcemap: true, | |
inject: ["application_controller.js"], | |
bundle: true, | |
outdir: path.join(process.cwd(), "app/assets/builds"), | |
absWorkingDir: path.join(process.cwd(), "app/javascript"), | |
watch: watch, | |
plugins: [ | |
rails(), | |
alias({ | |
"helpers": path.resolve(__dirname, "./app/javascript/helpers/index.js"), | |
}) | |
], | |
}).catch(() => process.exit(1)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// controllers/hello_controller.js | |
import { current } from "helpers" | |
export class ApplicationController extends Controller { | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// helpers/index.js | |
export * from "./current_helpers" | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment