|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
1 | 3 | const { EleventyRenderPlugin } = require("@11ty/eleventy"); |
2 | 4 |
|
| 5 | +function findFileInDir(dir, filename) { |
| 6 | + let files = fs.readdirSync(dir); |
| 7 | + |
| 8 | + for (const file of files) { |
| 9 | + let filepath = path.join(dir, file); |
| 10 | + |
| 11 | + if (fs.statSync(filepath).isDirectory()) { |
| 12 | + let result = findFileInDir(filepath, filename); |
| 13 | + if (result) return result; |
| 14 | + } else if (path.basename(filepath) === filename) { |
| 15 | + return filepath; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + return null; |
| 20 | +} |
| 21 | + |
3 | 22 | module.exports = function (eleventyConfig) { |
4 | 23 | eleventyConfig.addPlugin(EleventyRenderPlugin); |
5 | 24 |
|
6 | | - eleventyConfig.addPassthroughCopy("./src/static/js/**"); |
7 | | - eleventyConfig.addPassthroughCopy("./assets/**"); |
| 25 | + eleventyConfig.addPassthroughCopy("./src/static/js"); |
| 26 | + eleventyConfig.addPassthroughCopy("./assets"); |
8 | 27 | eleventyConfig.addPassthroughCopy("**.data.json"); |
9 | | - eleventyConfig.addGlobalData('repo', async () => fetch('https://api.github.com/repos/11ty/eleventy')); |
10 | 28 | eleventyConfig.setDataFileSuffixes([".data", ""]); |
| 29 | + eleventyConfig.setQuietMode(true); |
11 | 30 | eleventyConfig.setServerOptions({ |
12 | | - watch: ["_site/static/**"] |
| 31 | + watch: ["_site/static/css/**"] |
13 | 32 | }); |
14 | 33 |
|
15 | 34 | eleventyConfig.addShortcode("addScript", function (filename) { |
16 | | - return `<script src="/static/js/app/${filename}.js"></script>`; |
| 35 | + let filepath = findFileInDir("./src/static/js", `${filename}.js`); |
| 36 | + if (filepath) { |
| 37 | + return `<script src="${filepath.replace('src', '')}"></script>`; |
| 38 | + } |
| 39 | + |
| 40 | + console.log(`JS file ${filename} not found in ./src/static/js`); |
| 41 | + return ''; |
17 | 42 | }); |
18 | 43 |
|
19 | 44 | eleventyConfig.addShortcode("addStyle", function (filename) { |
20 | | - return `<link rel="stylesheet" href="/static/css/app/${filename}.css">`; |
| 45 | + let filepath = findFileInDir("./src/static/css", `${filename}.sass`); |
| 46 | + if (filepath) { |
| 47 | + return `<link rel="stylesheet" href="${filepath.replace('src', '').replace('.sass', '.css')}">`; |
| 48 | + } |
| 49 | + |
| 50 | + console.log(`JS file ${filename} not found in ./src/static/js`); |
| 51 | + return ''; |
21 | 52 | }); |
22 | 53 |
|
23 | 54 | eleventyConfig.addShortcode("keywords", function() { |
|
0 commit comments