user1037355
user1037355

Reputation:

rollup output as string into JSON file instead of directly as a JS file

   export default {
      input: 'src/main.sidebar.ts',
      output: {
        sourcemap: false,
        format: 'iife',
        name: 'sidebar',
        file: 'public/build/sidebar.json'
      },

I have a mini swelte app as a sidebar.

The rollup config above pipes the compiled output as is into a .json file.

How can I write (pardon the noobness of rollup here) a "middleware" that will grab the output and wrap into a simple json object?

I need to transport this JS code in a JSON payload.

Upvotes: 1

Views: 2056

Answers (1)

Isidrok
Isidrok

Reputation: 2181

You can use a plugin, those have several hooks you can define, in particular the generateBundle one will allow you to inspect the generated bundle. Then using this.emitFile plugin context function you can actually output your JSON. Here is an example

plugins: [
    {
        name: 'whatever',
        generateBundle(outputOptions, bundle) {
            const entry = Object.values(bundle).find((chunk) => chunk.isEntry);
            this.emitFile({
               type: 'asset',
               fileName: 'entry.json',
               source: JSON.stringify(entry.code) 
            });
        }
    }
]

Upvotes: 4

Related Questions