Babel prepend javascript on top of a file

74 Views Asked by At

I am trying to prepend Javascript to a javascript file using a babel plugin. This is my current babel plugin.

const babel = require("@babel/core")

module.exports = () => {
    return {
        visitor: {
            Program: {
                enter: (path, state) => {
                    const options = state.opts
                    const file = state.file
    
                    if (!options.accept(file.opts.filename)) return
                    path.node.body.unshift(
                        babel.parse(options.prepend).program.body[0]
                    )             
                }
            }
        }
    }
}

This code does work but it does not prepend at the actual top of the file. Here is an example of where it prepends:

Example of where it prepends

Is there any way to change this plugin to actually prepend the javascript at the top?

0

There are 0 best solutions below