N
Nuxt7mo ago
CopyPasta

Is there any way to build custom js files?

Basically I want to build src/sdk.ts to {output}/sdk.js. Sort of like library mode in vite. Currently I'm using this module I wrote to render an entry file
import { defineNuxtModule } from 'nuxt/kit'
import { type LibraryOptions, build } from 'vite'
import { unocss } from 'config/uno'

export default defineNuxtModule<LibraryOptions>({
meta: {
name: 'lib',
configKey: 'lib'
},

setup(lib, nuxt) {
if (!lib || !lib.entry)
return

const libDir = `${nuxt.options.buildDir}/libs`

nuxt.hook('nitro:config', (nitro) => {
nitro.publicAssets ||= []
nitro.publicAssets.push({
dir: libDir,
maxAge: 3600
})
})

nuxt.hook('nitro:build:before', async () => {
await build({
publicDir: false,
plugins: [
unocss()
],

build: {
lib,
outDir: libDir
}
})
})
}
})
import { defineNuxtModule } from 'nuxt/kit'
import { type LibraryOptions, build } from 'vite'
import { unocss } from 'config/uno'

export default defineNuxtModule<LibraryOptions>({
meta: {
name: 'lib',
configKey: 'lib'
},

setup(lib, nuxt) {
if (!lib || !lib.entry)
return

const libDir = `${nuxt.options.buildDir}/libs`

nuxt.hook('nitro:config', (nitro) => {
nitro.publicAssets ||= []
nitro.publicAssets.push({
dir: libDir,
maxAge: 3600
})
})

nuxt.hook('nitro:build:before', async () => {
await build({
publicDir: false,
plugins: [
unocss()
],

build: {
lib,
outDir: libDir
}
})
})
}
})
If there's any better way to do this please let me know. Thanks!
1 Reply
ManUtopiK
ManUtopiK7mo ago
If you're building a module, you can use nuxt-module-build to write this file. Here's an example :
// File build.config.ts
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
entries: [{
builder: 'mkdist',
input: './src/runtime/libs',
}],
stub: false,
})
// File build.config.ts
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
entries: [{
builder: 'mkdist',
input: './src/runtime/libs',
}],
stub: false,
})
Want results from more Discord servers?
Add your server