File size: 16,443 Bytes
c0a0e96 fdaa134 c0a0e96 9b2eb8e c0a0e96 9b2eb8e c0a0e96 9b2eb8e c0a0e96 9b2eb8e c0a0e96 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const Handlebars = require("handlebars");
const fs = require("fs");
const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
const FRAGMENTS_PATH = "src/fragments";
// Load the fragments from the fragments directory and caches it
const loadFragmentsMap = (() => {
let cachedFragments = null;
return async () => {
if (cachedFragments === null) {
cachedFragments = {};
const walkDir = async (dir, basePath = '') => {
const files = fs.readdirSync(dir);
await Promise.all(files.map(async file => {
const filePath = path.join(dir, file);
const relativePath = path.join(basePath, file);
if (fs.statSync(filePath).isDirectory()) {
await walkDir(filePath, relativePath);
} else {
// Remove the .html extension before creating the dotted path
const nameWithoutExt = relativePath.replace(/\.html$/, '');
const dottedPath = 'fragment-' + nameWithoutExt.replace(/\\/g, '-').replace(/\//g, '-').replace(/\./g, '-');
const content = fs.readFileSync(filePath, "utf8");
// Minify the HTML content using swcMinifyFragment
const minifiedRes = await HtmlMinimizerPlugin.swcMinifyFragment({"tmp.html": content})
if (minifiedRes.errors) {
console.error(minifiedRes.errors)
}
const minifiedContent = minifiedRes.code;
cachedFragments[dottedPath] = minifiedContent;
}
}));
};
await walkDir(FRAGMENTS_PATH);
}
return cachedFragments;
};
})();
const transformMarkdownWithFragments = async (data, filepath) => {
const fragments = await loadFragmentsMap();
console.log(`Available fragments: ${Object.keys(fragments).join(', ')}`);
// Read the markdown file
const markdown = require('markdown-it')({
html: true,
linkify: true,
typographer: true
});
const markdownContent = data.toString('utf8');
const htmlContent = markdown.render(markdownContent);
// Process with Handlebars for fragment insertion
const template = Handlebars.compile(htmlContent);
return template(fragments);
};
module.exports = {
entry: {
distill: "./src/distill.js",
main: "./src/index.js",
},
output: {
filename: "[name].bundle.js",
path: path.resolve(__dirname, "dist"),
},
module: {
rules: [
{ test: /\.css$/, use: ["style-loader", "css-loader"] },
{
test: /\.(js|mjs)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
},
},
},
{}
],
},
plugins: [
new CleanWebpackPlugin(),
new CopyPlugin({
patterns: [
{ from: "src/fragments/*", to: "fragments/[name].html" },
{ from: "src/style.css", to: "style.css" },
{ from: "content/*.png", to: "static/[name][ext]" },
{ from: "content/*.svg", to: "static/[name][ext]" },
{ from: "content/*.html", to: "static/[name][ext]" },
{
from: "content/article.md",
to: "index.html",
transform: async (content, path) => {
const fragments = await loadFragmentsMap();
// Convert markdown to HTML
const markdown = require('markdown-it')({
html: true,
linkify: true,
typographer: true
});
const markdownContent = content.toString('utf8');
const htmlContent = markdown.render(markdownContent);
// Extract headings for TOC generation
const tocScript = `
<script>
function initializeTOC() {
const article = document.querySelector('d-article');
const toc = document.querySelector('d-contents');
if (toc) {
const headings = article.querySelectorAll('h1, h2, h3, h4');
let ToC = '<nav role="navigation" class="l-text figcaption">';
ToC += '<div class="toc-header"><span class="toc-title">Table of Contents</span></div>';
ToC += '<div class="toc-content">';
headings.forEach((heading, index) => {
const id = heading.id || 'heading-' + index;
if (!heading.id) heading.id = id;
const level = parseInt(heading.tagName.charAt(1));
const indent = level === 1 ? '' : 'style="margin-left: ' + ((level - 1) * 1.2) + 'em;"';
ToC += '<div ' + indent + '><a href="#' + id + '">' + heading.textContent + '</a></div>';
});
ToC += '</div></nav>';
toc.innerHTML = ToC;
toc.setAttribute('prerendered', 'true');
// Extract tenet text for tooltips
const tenetTooltips = {
'source-of-truth': 'We should be a source of truth for all model definitions. Model implementations should be reliable, reproducible, and faithful to the original performances.',
'one-model-one-file': 'All inference (and most of training, loss is separate, not a part of model) logic visible, top‑to‑bottom.',
'code-is-product': 'Optimize for reading, diffing, and tweaking, our users are power users. Variables can be explicit, full words, even several words, readability is primordial.',
'standardize-dont-abstract': 'If it\\'s model behavior, keep it in the file; abstractions only for generic infra.',
'do-repeat-yourself': 'Copy when it helps users; keep successors in sync without centralizing behavior.',
'minimal-user-api': 'Config, model, preprocessing; from_pretrained, save_pretrained, push_to_hub. We want the least amount of codepaths.',
'backwards-compatibility': 'Evolve by additive standardization, never break public APIs.',
'consistent-public-surface': 'Same argument names, same outputs, hidden states and attentions exposed.',
};
// Add smooth scrolling and active state
const tocLinks = document.querySelectorAll('d-contents a');
tocLinks.forEach(link => {
const href = link.getAttribute('href');
const anchor = href ? href.substring(1) : '';
// Add tooltip if this is a tenet link
if (tenetTooltips[anchor]) {
link.setAttribute('title', tenetTooltips[anchor]);
link.style.position = 'relative';
}
link.addEventListener('click', function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({ behavior: 'smooth' });
}
});
});
// Update active state on scroll
window.addEventListener('scroll', function() {
const scrollPos = window.scrollY + 100;
headings.forEach((heading) => {
const link = document.querySelector('d-contents a[href="#' + heading.id + '"]');
if (link) {
if (heading.offsetTop <= scrollPos &&
heading.offsetTop + heading.offsetHeight > scrollPos) {
link.classList.add('active');
} else {
link.classList.remove('active');
}
}
});
});
}
}
// Initialize Prism syntax highlighting
function initializeSyntaxHighlighting() {
if (typeof Prism !== 'undefined') {
Prism.highlightAll();
}
}
// Try multiple times to ensure it runs after distill.js
document.addEventListener('DOMContentLoaded', function() {
initializeTOC();
initializeSyntaxHighlighting();
});
setTimeout(function() {
initializeTOC();
initializeSyntaxHighlighting();
}, 100);
setTimeout(function() {
initializeTOC();
initializeSyntaxHighlighting();
}, 500);
setTimeout(function() {
initializeTOC();
initializeSyntaxHighlighting();
}, 1000);
</script>`;
// Create full HTML document with distill template
const template = `<!DOCTYPE html>
<html>
<head>
<script src="distill.bundle.js" type="module" fetchpriority="high" blocking></script>
<script src="main.bundle.js" type="module" fetchpriority="low" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<script src="https://d3js.org/d3.v7.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf8">
<title>Transformers Feature Showcase</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css">
</head>
<body>
<d-front-matter>
<script id='distill-front-matter' type="text/json">{
"title": "Transformers Feature Showcase",
"description": "An interactive demonstration of transformers library features and design philosophy.",
"published": "Aug 21, 2025",
"authors": [{"author": "Pablo Pernot", "authorURL": "https://huggingface.co/pablo"}]
}</script>
</d-front-matter>
<d-title>
<h1>Transformers Feature Showcase</h1>
<p>An interactive demonstration of transformers library features and design philosophy.</p>
</d-title>
<d-byline></d-byline>
<d-article>
<d-contents>
<nav role="navigation" class="l-text figcaption">
<div class="toc-header"><span class="toc-title">Table of Contents</span></div>
<div class="toc-content">
<div><a href="#introduction">Introduction</a></div>
<div style="margin-left: 1.2em;"><a href="#what-you-will-learn">What you will learn</a></div>
<div><a href="#source-of-truth">0. Source of truth</a></div>
<div><a href="#one-model-one-file">1. One model, one file</a></div>
<div><a href="#code-is-product">2. Code is product</a></div>
<div><a href="#standardize-dont-abstract">3. Standardize, don't abstract</a></div>
<div><a href="#do-repeat-yourself">4. DRY* (DO Repeat Yourself)</a></div>
<div><a href="#minimal-user-api">5. Minimal user API</a></div>
<div><a href="#backwards-compatibility">6. Backwards compatibility</a></div>
<div><a href="#consistent-public-surface">7. Consistent public surface</a></div>
<div><a href="#modular">Going modular</a></div>
<div><a href="#attention-classes">External Attention classes</a></div>
<div><a href="#encoders-ftw">Encoders win!</a></div>
</div>
</nav>
</d-contents>
${htmlContent}
</d-article>
${tocScript}
</body>
</html>`;
// Process with Handlebars for fragment insertion
const handlebars = Handlebars.compile(template);
return handlebars(fragments);
}
},
],
}),
],
devtool: process.env.NODE_ENV === 'production' ? 'source-map' : 'eval-source-map',
devServer: {
static: {
directory: path.join(__dirname, 'dist'),
},
hot: true,
watchFiles: ['src/**/*'],
client: {
overlay: true,
},
},
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
optimization: {
minimizer: [
new ImageMinimizerPlugin({
minimizer: [{
implementation: ImageMinimizerPlugin.sharpMinify,
options: {
encodeOptions: {
jpeg: {
quality: 80
},
png: {
quality: 80
},
webp: {
quality: 80
}
}
}
},
{
implementation: ImageMinimizerPlugin.svgoMinify,
options: {
encodeOptions: {
multipass: true,
plugins: [
'preset-default',
]
}
}
}
]
}),
new HtmlMinimizerPlugin({
test: /fragments\/.*\.html$/i,
minify: HtmlMinimizerPlugin.swcMinifyFragment,
})
]
},
};
console.log(process.env.NODE_ENV) |