Merge pull request #102 from boulangerseb/bugfix/11-tpl-extention-build-issue
Refactor: externalize CSS and JS compilation to avoid dependency-based execution issues in Eleventy
This commit is contained in:
commit
d4fe9f47a3
5 changed files with 96 additions and 86 deletions
|
|
@ -20,8 +20,19 @@ import events from './src/_config/events.js';
|
||||||
import filters from './src/_config/filters.js';
|
import filters from './src/_config/filters.js';
|
||||||
import plugins from './src/_config/plugins.js';
|
import plugins from './src/_config/plugins.js';
|
||||||
import shortcodes from './src/_config/shortcodes.js';
|
import shortcodes from './src/_config/shortcodes.js';
|
||||||
|
import {buildAllCss} from './src/_config/plugins/css-config.js';
|
||||||
|
import {buildAllJs} from './src/_config/plugins/js-config.js';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default async function (eleventyConfig) {
|
export default async function (eleventyConfig) {
|
||||||
|
|
||||||
|
eleventyConfig.on('eleventy.before', async () => {
|
||||||
|
await buildAllCss();
|
||||||
|
await buildAllJs();
|
||||||
|
});
|
||||||
|
|
||||||
eleventyConfig.addWatchTarget('./src/assets/**/*.{css,js,svg,png,jpeg}');
|
eleventyConfig.addWatchTarget('./src/assets/**/*.{css,js,svg,png,jpeg}');
|
||||||
eleventyConfig.addWatchTarget('./src/_includes/**/*.{webc}');
|
eleventyConfig.addWatchTarget('./src/_includes/**/*.{webc}');
|
||||||
|
|
||||||
|
|
@ -38,8 +49,6 @@ export default async function (eleventyConfig) {
|
||||||
|
|
||||||
// --------------------- Plugins
|
// --------------------- Plugins
|
||||||
eleventyConfig.addPlugin(plugins.htmlConfig);
|
eleventyConfig.addPlugin(plugins.htmlConfig);
|
||||||
eleventyConfig.addPlugin(plugins.cssConfig);
|
|
||||||
eleventyConfig.addPlugin(plugins.jsConfig);
|
|
||||||
eleventyConfig.addPlugin(plugins.drafts);
|
eleventyConfig.addPlugin(plugins.drafts);
|
||||||
|
|
||||||
eleventyConfig.addPlugin(plugins.EleventyRenderPlugin);
|
eleventyConfig.addPlugin(plugins.EleventyRenderPlugin);
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@
|
||||||
"url": "https://github.com/madrilene/eleventy-excellent.git"
|
"url": "https://github.com/madrilene/eleventy-excellent.git"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@11ty/eleventy": "^3.1.1",
|
"@11ty/eleventy": "^3.1.2",
|
||||||
"@11ty/eleventy-fetch": "^5.1.0",
|
"@11ty/eleventy-fetch": "^5.1.0",
|
||||||
"@11ty/eleventy-img": "^6.0.4",
|
"@11ty/eleventy-img": "^6.0.4",
|
||||||
"@11ty/eleventy-plugin-rss": "^2.0.4",
|
"@11ty/eleventy-plugin-rss": "^2.0.4",
|
||||||
|
|
@ -44,6 +44,7 @@
|
||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
"dotenv": "^17.2.0",
|
"dotenv": "^17.2.0",
|
||||||
"esbuild": "^0.25.8",
|
"esbuild": "^0.25.8",
|
||||||
|
"fast-glob": "^3.3.3",
|
||||||
"html-minifier-terser": "^7.2.0",
|
"html-minifier-terser": "^7.2.0",
|
||||||
"js-yaml": "^4.1.0",
|
"js-yaml": "^4.1.0",
|
||||||
"markdown-it": "^14.1.0",
|
"markdown-it": "^14.1.0",
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,6 @@ import {drafts} from './plugins/drafts.js';
|
||||||
// Custom transforms
|
// Custom transforms
|
||||||
import {htmlConfig} from './plugins/html-config.js';
|
import {htmlConfig} from './plugins/html-config.js';
|
||||||
|
|
||||||
// Custom template language
|
|
||||||
import {cssConfig} from './plugins/css-config.js';
|
|
||||||
import {jsConfig} from './plugins/js-config.js';
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
EleventyRenderPlugin,
|
EleventyRenderPlugin,
|
||||||
rss,
|
rss,
|
||||||
|
|
@ -24,7 +20,5 @@ export default {
|
||||||
eleventyImageTransformPlugin,
|
eleventyImageTransformPlugin,
|
||||||
markdownLib,
|
markdownLib,
|
||||||
drafts,
|
drafts,
|
||||||
htmlConfig,
|
htmlConfig
|
||||||
cssConfig,
|
|
||||||
jsConfig
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -6,43 +6,53 @@ import postcssImportExtGlob from 'postcss-import-ext-glob';
|
||||||
import tailwindcss from 'tailwindcss';
|
import tailwindcss from 'tailwindcss';
|
||||||
import autoprefixer from 'autoprefixer';
|
import autoprefixer from 'autoprefixer';
|
||||||
import cssnano from 'cssnano';
|
import cssnano from 'cssnano';
|
||||||
|
import fg from 'fast-glob';
|
||||||
|
|
||||||
export const cssConfig = eleventyConfig => {
|
const buildCss = async (inputPath, outputPaths) => {
|
||||||
eleventyConfig.addTemplateFormats('css');
|
const inputContent = await fs.readFile(inputPath, 'utf-8');
|
||||||
|
|
||||||
eleventyConfig.addExtension('css', {
|
const result = await postcss([
|
||||||
outputFileExtension: 'css',
|
postcssImportExtGlob,
|
||||||
compile: async (inputContent, inputPath) => {
|
postcssImport,
|
||||||
const paths = [];
|
tailwindcss,
|
||||||
if (inputPath.endsWith('/src/assets/css/global/global.css')) {
|
autoprefixer,
|
||||||
paths.push('src/_includes/css/global.css');
|
cssnano
|
||||||
} else if (inputPath.includes('/src/assets/css/local/')) {
|
]).process(inputContent, { from: inputPath });
|
||||||
const baseName = path.basename(inputPath);
|
|
||||||
paths.push(`src/_includes/css/${baseName}`);
|
|
||||||
} else if (inputPath.includes('/src/assets/css/components/')) {
|
|
||||||
const baseName = path.basename(inputPath);
|
|
||||||
paths.push(`dist/assets/css/components/${baseName}`);
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return async () => {
|
for (const outputPath of outputPaths) {
|
||||||
let result = await postcss([
|
await fs.mkdir(path.dirname(outputPath), { recursive: true });
|
||||||
postcssImportExtGlob,
|
await fs.writeFile(outputPath, result.css);
|
||||||
postcssImport,
|
}
|
||||||
tailwindcss,
|
|
||||||
autoprefixer,
|
|
||||||
cssnano
|
|
||||||
]).process(inputContent, {from: inputPath});
|
|
||||||
|
|
||||||
// Write the output to all specified paths
|
return result.css;
|
||||||
for (const outputPath of paths) {
|
}
|
||||||
await fs.mkdir(path.dirname(outputPath), {recursive: true});
|
|
||||||
await fs.writeFile(outputPath, result.css);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.css;
|
|
||||||
};
|
export const buildAllCss = async () => {
|
||||||
}
|
const tasks = [];
|
||||||
});
|
|
||||||
};
|
tasks.push(
|
||||||
|
buildCss(
|
||||||
|
'src/assets/css/global/global.css',
|
||||||
|
['src/_includes/css/global.css']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
const localCssFiles = await fg(['src/assets/css/local/*.css']);
|
||||||
|
for (const inputPath of localCssFiles) {
|
||||||
|
const baseName = path.basename(inputPath);
|
||||||
|
tasks.push(
|
||||||
|
buildCss(inputPath, [`src/_includes/css/${baseName}`])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const componentCssFiles = await fg(['src/assets/css/components/*.css']);
|
||||||
|
for (const inputPath of componentCssFiles) {
|
||||||
|
const baseName = path.basename(inputPath);
|
||||||
|
tasks.push(
|
||||||
|
buildCss(inputPath, [`dist/assets/css/components/${baseName}`])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(tasks);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,45 +1,41 @@
|
||||||
import esbuild from 'esbuild';
|
import fs from 'node:fs/promises';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import fg from 'fast-glob';
|
||||||
|
import esbuild from 'esbuild';
|
||||||
|
|
||||||
export const jsConfig = eleventyConfig => {
|
export const buildJs = async (inputPath, outputPath) => {
|
||||||
eleventyConfig.addTemplateFormats('js');
|
const result = await esbuild.build({
|
||||||
|
target: 'es2020',
|
||||||
eleventyConfig.addExtension('js', {
|
entryPoints: [inputPath],
|
||||||
outputFileExtension: 'js',
|
bundle: true,
|
||||||
compile: async (content, inputPath) => {
|
minify: true,
|
||||||
// Skip processing if not in the designated scripts directories
|
write: false
|
||||||
if (!inputPath.startsWith('./src/assets/scripts/')) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Inline scripts processing
|
|
||||||
if (inputPath.startsWith('./src/assets/scripts/bundle/')) {
|
|
||||||
const filename = path.basename(inputPath);
|
|
||||||
const outputFilename = filename;
|
|
||||||
const outputPath = `./src/_includes/scripts/${outputFilename}`;
|
|
||||||
|
|
||||||
await esbuild.build({
|
|
||||||
target: 'es2020',
|
|
||||||
entryPoints: [inputPath],
|
|
||||||
outfile: outputPath,
|
|
||||||
bundle: true,
|
|
||||||
minify: true
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default handling for other scripts, excluding inline scripts
|
|
||||||
return async () => {
|
|
||||||
let output = await esbuild.build({
|
|
||||||
target: 'es2020',
|
|
||||||
entryPoints: [inputPath],
|
|
||||||
bundle: true,
|
|
||||||
minify: true,
|
|
||||||
write: false
|
|
||||||
});
|
|
||||||
|
|
||||||
return output.outputFiles[0].text;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const output = result.outputFiles[0].text;
|
||||||
|
|
||||||
|
await fs.mkdir(path.dirname(outputPath), { recursive: true });
|
||||||
|
await fs.writeFile(outputPath, output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const buildAllJs = async () => {
|
||||||
|
const tasks = [];
|
||||||
|
|
||||||
|
const inlineBundleFiles = await fg(['src/assets/scripts/bundle/*.js']);
|
||||||
|
for (const inputPath of inlineBundleFiles) {
|
||||||
|
const baseName = path.basename(inputPath);
|
||||||
|
const outputPath = `src/_includes/scripts/${baseName}`;
|
||||||
|
tasks.push(buildJs(inputPath, outputPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
const componentFiles = await fg(['src/assets/scripts/components/*.js']);
|
||||||
|
for (const inputPath of componentFiles) {
|
||||||
|
const baseName = path.basename(inputPath);
|
||||||
|
const outputPath = `dist/assets/scripts/components/${baseName}`;
|
||||||
|
tasks.push(buildJs(inputPath, outputPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(tasks);
|
||||||
};
|
};
|
||||||
Loading…
Reference in a new issue