Skip to content

Commit

Permalink
chore(next): connect velite with next.js
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 committed Feb 26, 2024
1 parent 7bfa69a commit 13942da
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
class VeliteWebpackPlugin {
static started = false
constructor(/** @type {import('velite').Options} */ options = {}) {
this.options = options
}
apply(/** @type {import('webpack').Compiler} */ compiler) {
// executed three times in nextjs !!!
// twice for the server (nodejs / edge runtime) and once for the client
compiler.hooks.beforeCompile.tapPromise('VeliteWebpackPlugin', async () => {
if (VeliteWebpackPlugin.started) return
VeliteWebpackPlugin.started = true
const dev = compiler.options.mode === 'development'
this.options.watch = this.options.watch ?? dev
this.options.clean = this.options.clean ?? !dev
const { build } = await import('velite')
await build(this.options) // start velite
})
}
}

/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
Expand Down Expand Up @@ -39,6 +59,10 @@ const nextConfig = {
headers: [{ key: 'Access-Control-Allow-Origin', value: '*' }]
}
]
},
webpack: config => {
config.plugins.push(new VeliteWebpackPlugin())
return config
}
}

Expand Down

0 comments on commit 13942da

Please sign in to comment.