How to get files list of one folder only at the Rollup.js compile time?

Viewed 757

I would like to achieve the following, only at the Rollup.js compile time:

  1. Get files list of the ./src/markdown-files/ folder.
  2. Store this files list in a global variable.
  3. Use this global variable on DEV and PROD side.

I use Svelte v3.23.2 and Rollup.js v2.15.0.

Thanks for your help

1 Answers

You can include code at the start of a bundle (inside wrapper) by using output.intro.

const markdownFiles = fs.readdirSync("./src/markdown-files/");
export default {
    output: {
        intro: `const MARKDOWN_FILES = ${JSON.stringify(markdownFiles)};`
    }
}

There is no way to check if the script is being run to 'compile', as the config file returns nothing more than a plain object and has no globals but for imports.

Related