I'm attempting to create widgets for our customers to paste (embed) within their own site.
Ideally to be used by a third-party like so:
example.html
<div id="example"></div>
<script src="https://example.com/widgets/example.bundle.js"></script>
<script>
Example.init({
selector: "#example",
name: "example"
});
</script>
I just can't figure out how to "bundle" specific entry points down to a single file with next.js.
I'm using Vercel which builds from GitHub, so ideally I want these bundles to be generated on build, without breaking the existing next.js site.
What I've tried:
next.config.js
module.exports = {
entry: {
example: './pages/example.js'
},
output: {
path: "./public/widgets",
filename: '[name].bundle.js'
}
}
Result:
This achieves nothing, and I don't understand why! :(
Useful Sources:
Github (Next.js Discussions): Adding a Webpack entry to load separately from main.js
Next.js: Custom Webpack Config
Next.js: Setting a custom build directory
Stackoverflow: Writing embeddable Javascript plugin with React & Webpack