File size: 1,386 Bytes
d5a0951 | 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 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Preprocessor Example</title>
<style id="processed-css"></style>
</head>
<body>
<div class="container">
<h1>Hello, World!</h1>
<p>This is a simple example of using a CSS preprocessor.</p>
</div>
<script>
// Define variables
const variables = {
primaryColor: '#007bff',
secondaryColor: '#6c757d',
};
// Define mixins
const mixins = {
flexCenter: `
display: flex;
justify-content: center;
align-items: center;
`,
};
// Parse and process CSS
function preprocessCSS(css) {
// Resolve variables
for (const [key, value] of Object.entries(variables)) {
css = css.replace(new RegExp(`\\$${key}`, 'g'), value);
}
// Expand mixins
for (const [key, value] of Object.entries(mixins)) {
css = css.replace(new RegExp(`@mixin ${key}`, 'g'), value);
}
return css;
}
// Example usage
const inputCSS = `
body {
background-color: $primaryColor;
color: $secondaryColor;
@mixin flexCenter;
}
`;
const processedCSS = preprocessCSS(inputCSS);
document.getElementById('processed-css').innerText = processedCSS;
</script>
</body>
</html>
|