stephancasas / presto

HTML componentization, specialized for Alpine JS.
MIT License
26 stars 0 forks source link

Handle a directory as input in presto.config? #4

Closed LaundroMat closed 3 years ago

LaundroMat commented 3 years ago

I'm trying to find a middle ground between a static site generator and hand-crafted html. I was wondering whether it is possible to create a folder (e.g. blog-posts/) and have presto traverse that whole folder and output all the files into ./dist ?

Basically, presto.config.js contain something like this:

{
    input: 'src/blog-posts/*.html',
    include: [
        'src/components/**/*.html',
    ],
    output: {
        file: 'dist/blog/*.html',
    }
}
stephancasas commented 3 years ago

Howdy, and thank you for your question.

At the moment, Presto doesn't support glob paths on the config input property. However, because presto.config.js is and can be written as natural ES6, you have the ability to handle globbing yourself where it's needed. When writing your presto.config.js file, the default export can be either a single object, or an array. You can build either of these according to your project's requirements.

For example, let's suppose I have several input/index documents located in src/inputs/. I could write my presto.config.js as the following:

import * as fs from 'fs';

const inputPath = './src/inputs/';
const include = ['src/components/**/*.html'];

const inputs = fs.readdirSync(inputPath);

const config = inputs.map((file) => ({
    input: `${inputPath}${file}`,
    include,
    output: { file: `dist/${file}` }
}));

export default config;

At runtime, Presto will use rollupjs to compile this into CJS, and then execute it to get the default export. As you can see, use of the fs.readdirSync() method yields the necessary file names for each input document, and then those are concatenated where necessary using a template string.

This particular example was written on macOS, so if you're on a Windows system, make sure to normalize your paths returned by fs where required.

I've attached a sample as a .ZIP where you can try this however, I can definitely see where glob paths on the input property would be useful, so I'll take a look at different implementations for future releases.

LaundroMat commented 3 years ago

Thanks for the quick and extensive reply!

stephancasas commented 3 years ago

Of course! Please keep the questions/ideas coming! Thanks! 🥳