Skip to content

Commit

Permalink
Bundle utility functions with the nuxt module build and use bundled f…
Browse files Browse the repository at this point in the history
…unctions directly

This is a tradeoff between using the usemods package through npm or using source code directly to import the utility functions into Nuxt.
This version copies the bundled utilities into the build process for the module and then references the bundled functions directly. It's a less elegant solution but solves for optimises for local development where changes are made to the base utility and needs fast rebuild of the module to use the new src.

Attempted to use npm link but had numerous issues.
  • Loading branch information
philliphartin committed Apr 18, 2024
1 parent 0427dda commit 481c475
Show file tree
Hide file tree
Showing 9 changed files with 635 additions and 27 deletions.
17 changes: 16 additions & 1 deletion build.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dts from 'bun-plugin-dts'
import { watch } from 'fs'
import { watch, mkdir, readFileSync, writeFileSync, copyFileSync } from 'fs'
import { join } from 'path'
import { processDocs } from './docs.ts'

async function build() {
Expand All @@ -19,6 +20,7 @@ async function build() {
// Check for --watch flag and if it's not present, just build once
if (!process.argv.includes('--watch')) {
await build()
await copyBuildFileToNuxtModules()
await processDocs()
process.exit(0)
} else {
Expand All @@ -27,6 +29,7 @@ if (!process.argv.includes('--watch')) {
if (filename.endsWith('.ts')) {
console.log(`Detected ${event} in ${filename}`)
await build()
await copyBuildFileToNuxtModules()
await processDocs()
}
})
Expand All @@ -37,3 +40,15 @@ if (!process.argv.includes('--watch')) {
process.exit(0)
})
}

async function copyBuildFileToNuxtModules() {
mkdir('nuxt-module/src/runtime/utils', { recursive: true }, (err) => {
if (err) throw err
})

const utils = readFileSync(join(process.cwd(), 'dist/index.js'), 'utf8')
const utilsTypeDefinitions = readFileSync(join(process.cwd(), 'dist/index.d.ts'), 'utf8')

writeFileSync(join(process.cwd(), 'nuxt-module/src/runtime/utils/index.js'), utils)
writeFileSync(join(process.cwd(), 'nuxt-module/src/runtime/utils/index.d.ts'), utilsTypeDefinitions)
}
8 changes: 1 addition & 7 deletions nuxt-module/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions nuxt-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"test:watch": "vitest watch"
},
"dependencies": {
"@nuxt/kit": "^3.10.3",
"usemods": "^0.0.11"
"@nuxt/kit": "^3.10.3"
},
"devDependencies": {
"@nuxt/devtools": "latest",
Expand Down
19 changes: 7 additions & 12 deletions nuxt-module/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { defineNuxtModule, addPlugin, createResolver, addImports, addImportsSources } from '@nuxt/kit'
import * as utils from 'usemods'
import { defineNuxtModule, createResolver, addPlugin, addImportsDir, addImports } from '@nuxt/kit'
import * as utils from './runtime/utils'

// Module options TypeScript interface definition
export interface ModuleOptions {
alias: [string, string][]
}
Expand All @@ -20,20 +19,16 @@ export default defineNuxtModule<ModuleOptions>({

const aliasMap = new Map<string, string>(options.alias)

const imports = []
for (const name of Object.keys(utils)) {
const alias = aliasMap.has(name) ? aliasMap.get(name!) : name
imports.push({
name,
alias
addImports({
name: name,
as: alias,
from: resolve('./runtime/utils')
})
}

addImportsSources({
from: 'usemods',
imports
})

// addImportsDir(resolve('./runtime/utils'))
addPlugin(resolve('./runtime/plugin'))
}
})
2 changes: 1 addition & 1 deletion nuxt-module/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineNuxtPlugin } from '#app'

export default defineNuxtPlugin((nuxtApp) => {
// console.log("mods injected by use-mods!")
console.log('mods injected by use-mods!')
})
Loading

0 comments on commit 481c475

Please sign in to comment.