Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Nuxt] Auto-import usemods functions directly from npm package #93

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions docs.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import process from 'process'
import { resolve, extname, basename, join } from 'path'
import { watch, readFileSync, readdirSync, writeFileSync, copyFileSync } from 'fs'
import { watch, readFileSync, writeFileSync, copyFileSync } from 'fs'

// Arguments
const args = process.argv.slice(2);
const args = process.argv.slice(2)

// Paths
const srcPath = resolve('src')
const nuxtModulePath = resolve('nuxt-module')
const nuxtWebPath = resolve('nuxt-web')

// Functions
Expand All @@ -20,7 +20,6 @@ function generateMarkdown(file, name) {

// Copy file to Website
copyFileSync(file, join(nuxtWebPath, 'utils', basename(file)))
copyFileSync(file, join(nuxtModulePath, 'src/runtime/utils', basename(file)))

let markdown = ''

Expand Down Expand Up @@ -57,23 +56,11 @@ function generateMarkdown(file, name) {
writeFileSync(join(nuxtWebPath, 'content/2.docs', `${name}.md`), markdown)
}

// Find any files in ./src/utils and move them to ./nuxt-module/src/runtime/utils and ./nuxt-web/utils
function copyFiles(src, dest)
{
const files = readdirSync(src)
files.forEach((file) => {
if (file.endsWith('.ts')) {
copyFileSync(join(src, file), join(dest, file))
}
})
}

// Generate Markdown for each File
function generateAll() {
const files = ['actions', 'formatters', 'modifiers', 'detections', 'generators', 'numbers', 'data', 'validators', 'animations', 'goodies']
files.forEach((file, index) => generateMarkdown(join(srcPath, `${file}.ts`), `${index + 1}.${file}`))
copyFileSync(join(srcPath, 'config.ts'), join(nuxtWebPath, 'utils', 'config.ts'))
copyFileSync(join(srcPath, 'config.ts'), join(nuxtModulePath, 'src/runtime/utils', 'config.ts'))
}

// Run Once
Expand All @@ -83,12 +70,12 @@ generateAll()
if (args.includes('--watch')) {
watch(srcPath, { recursive: true }, async (event, filename) => {
if (filename.endsWith('.ts')) {
console.log(`Detected ${event} in ${filename}`);
generateAll();
console.log(`Detected ${event} in ${filename}`)
generateAll()
}
});
})
} else if (args.includes('--build')) {
generateAll();
generateAll()
} else {
console.log('No valid command provided. Use --watch or --build.');
console.log('No valid command provided. Use --watch or --build.')
}
12 changes: 9 additions & 3 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: 2 additions & 1 deletion nuxt-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
},
"dependencies": {
"@nuxt/kit": "^3.11.2"
"@nuxt/kit": "^3.11.2",
"usemods": "^1.2.1"
},
"devDependencies": {
"@nuxt/devtools": "^1.2.0",
Expand Down
17 changes: 12 additions & 5 deletions nuxt-module/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineNuxtModule, createResolver, addPlugin, addImportsDir } from '@nuxt/kit'
import { defineNuxtModule, addImports } from '@nuxt/kit'
import * as utils from 'usemods'

export interface ModuleOptions {
alias: [string, string][]
Expand All @@ -13,9 +14,15 @@ export default defineNuxtModule<ModuleOptions>({
defaults: {
alias: []
},
setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)
addImportsDir(resolve('./runtime/utils'))
addPlugin(resolve('./runtime/plugin'))
setup(options) {
const aliasMap = new Map<string, string>(options.alias)
for (const name of Object.keys(utils)) {
const alias = aliasMap.has(name) ? aliasMap.get(name!) : name
addImports({
name: name,
as: alias,
from: 'usemods'
})
}
}
})
5 changes: 0 additions & 5 deletions nuxt-module/src/runtime/plugin.ts

This file was deleted.

2 changes: 0 additions & 2 deletions nuxt-module/src/runtime/utils/README.txt

This file was deleted.

244 changes: 0 additions & 244 deletions nuxt-module/src/runtime/utils/config.ts

This file was deleted.

Loading