Skip to content

Instantly share code, notes, and snippets.

@Lexicality
Created May 5, 2021 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lexicality/fefe952f22a0f9622e47d995a4a7a7c1 to your computer and use it in GitHub Desktop.
Save Lexicality/fefe952f22a0f9622e47d995a4a7a7c1 to your computer and use it in GitHub Desktop.
Foundry with Webpack example
import { preloadTemplates, getPartial } from "@/templates";
Hooks.once("init", async function () {
Handlebars.registerHelper("partial", getPartial);
// Preload templates.
await preloadTemplates();
});
const TEMPLATES = require.context("templates/", true, /\.html$/, "sync");
export function preloadTemplates() {
// Use Webpack trickery to get the filenames for the templates
return loadTemplates(TEMPLATES.keys().map(TEMPLATES) as string[]);
}
export function getPartial(partialName: string) {
return TEMPLATES(`./parts/${partialName}.html`);
}
.foo {
background: url("images/foo.jpg") repeat;
}
<div class="foo">
{{> (partial "example_part") name="foo"}}
</div>
{
"name": "example",
"esmodules": ["dist/main.js"],
"styles": ["dist/main.css"],
// ...
}
/* eslint-env node */
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
/** @type {import("webpack").Configuration} */
module.exports = {
mode: "production",
devtool: "source-map",
entry: ["scripts/index.ts", "styles/index.scss"],
output: {
clean: true,
publicPath: "/systems/example/dist/",
// assetModuleFilename: "[path][name][ext]",
assetModuleFilename: (p) => p.filename.replace("src/", ""),
},
module: {
rules: [
{
test: /\.(t|j)s$/,
use: "ts-loader",
exclude: /node_modules/,
},
{
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
},
{
test: /\.(png|jpe?g|gif|svg|html)$/,
type: "asset/resource",
},
],
},
resolve: {
modules: ["node_modules", "src"],
extensions: [".ts", `...`],
alias: {
"@": "scripts",
},
},
plugins: [new MiniCssExtractPlugin()],
optimization: {
minimizer: [
`...`,
new CssMinimizerPlugin(),
new HtmlMinimizerPlugin({
minimizerOptions: {
customAttrSurround: [[/\{\{#[^}]+\}\}/, /\{\{\/[^}]+\}\}/]],
// ignoreCustomFragments: [/\{\{.+?\}\}/],
},
}),
],
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment