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

feat: module improvements #188

Merged
merged 5 commits into from
Dec 22, 2021
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
9 changes: 4 additions & 5 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
declaration: true,
rollup: { cjsBridge: true },
entries: [
'./src/module',
{ input: 'src/plugins/', outDir: 'dist/plugins' },
{ input: 'src/composables/', outDir: 'dist/composables' },
{ input: 'src/types/', outDir: 'dist/types' }
{ input: 'src/runtime/', outDir: 'dist/runtime' }
],
externals: ['@nuxt/kit', '@nuxt/schema', 'defu', 'pathe', 'vue'],
declaration: true
externals: ['@nuxt/kit', '@nuxt/schema', 'vue']
})
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,35 @@
"license": "MIT",
"exports": {
".": {
"require": "./dist/module.cjs",
"import": "./dist/module.mjs"
}
},
"main": "dist/module.mjs",
"module": "dist/module.mjs",
"types": "dist/module.d.ts",
"main": "./dist/module.mjs",
"types": "./dist/module.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"dev": "nuxt dev example",
"dev": "nuxi dev example",
"lint": "eslint --ext .js,.ts,.vue .",
"prepublishOnly": "yarn build",
"release": "yarn lint && standard-version && git push --follow-tags"
},
"dependencies": {
"@nuxt/kit": "npm:@nuxt/kit-edge@latest",
"defu": "^5.0.0",
"pathe": "^0.2.0"
},
"devDependencies": {
"@nuxt/schema": "npm:@nuxt/schema-edge@latest",
"@nuxtjs/eslint-config-typescript": "^8.0.0",
"eslint": "^8.5.0",
"nuxt3": "^3.0.0-27334958.ca9761d",
"nuxt3": "latest",
"ohmyfetch": "^0.4.11",
"standard-version": "^9.3.2",
"typescript": "^4.5.4",
"unbuild": "0.6.3"
"unbuild": "^0.6.7"
},
"publishConfig": {
"access": "public"
Expand Down
32 changes: 21 additions & 11 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
import defu from 'defu'
import { resolve } from 'pathe'
import { defineNuxtModule, addPlugin } from '@nuxt/kit'
import type { Nuxt } from '@nuxt/schema'
import type { StrapiOptions } from './types'

export default defineNuxtModule<StrapiOptions>({
meta: {
name: '@nuxtjs/strapi',
compatibility: { nuxt: '^3.0.0' },
configKey: 'strapi'
configKey: 'strapi',
compatibility: {
nuxt: '^3.0.0',
bridge: true
}
},
defaults: {
url: 'http://localhost:1337',
url: process.env.STRAPI_URL || 'http://localhost:1337',
prefix: '/api',
version: 'v4'
},
setup (_options: StrapiOptions, nuxt: Nuxt) {
// Default runtimeConfig
const strapiConfig = nuxt.options.publicRuntimeConfig.strapi = defu({ url: process.env.STRAPI_URL }, _options)

setup (options: StrapiOptions, nuxt) {
// Make sure url is set
if (!strapiConfig.url) {
if (!options.url) {
throw new Error('Missing `STRAPI_URL` in `.env`')
}

// Default runtimeConfig
nuxt.options.publicRuntimeConfig.strapi = defu(nuxt.options.publicRuntimeConfig.strapi, {
url: options.url,
prefix: options.prefix,
version: options.version
})

// Transpile runtime
const runtimeDir = resolve(__dirname, './runtime')
nuxt.options.build.transpile.push(runtimeDir)

// Add plugin to load user before bootstrap
addPlugin(resolve(__dirname, './plugins/strapi'))
addPlugin(resolve(runtimeDir, 'strapi.plugin'))

// Add strapi composables
nuxt.hook('autoImports:dirs', (dirs) => {
dirs.push(resolve(__dirname, './composables'))
dirs.push(resolve(runtimeDir, 'composables'))
})
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Strapi3RequestParams } from '../types/v3'
import type { Strapi3RequestParams } from '../../types/v3'
import { useStrapiVersion } from './useStrapiVersion'
import { useStrapiClient } from './useStrapiClient'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Strapi4RequestParams } from '../types/v4'
import type { Strapi4RequestParams } from '../../types/v4'
import { useStrapiVersion } from './useStrapiVersion'
import { useStrapiClient } from './useStrapiClient'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
StrapiRegistrationData,
StrapiResetPasswordData,
StrapiUser
} from '../types'
} from '../../types'
import { useStrapiToken } from './useStrapiToken'
import { useStrapiUser } from './useStrapiUser'
import { useStrapiClient } from './useStrapiClient'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FetchError, FetchOptions } from 'ohmyfetch'
import type { Strapi4Error } from '../types/v4'
import type { Strapi3Error } from '../types/v3'
import type { Strapi4Error } from '../../types/v4'
import type { Strapi3Error } from '../../types/v3'
import { useStrapiUrl } from './useStrapiUrl'
import { useStrapiVersion } from './useStrapiVersion'
import { useStrapiToken } from './useStrapiToken'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Ref } from 'vue'
import type { StrapiUser } from '../types'
import type { StrapiUser } from '../../types'
import { useState } from '#app'

export const useStrapiUser = (): Ref<StrapiUser> => useState<StrapiUser>('strapi_user')
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { StrapiOptionsVersion } from '../types'
import type { StrapiOptionsVersion } from '../../types'
import { useRuntimeConfig } from '#app'

export const useStrapiVersion = (): StrapiOptionsVersion => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/strapi.ts → src/runtime/strapi.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useStrapiAuth } from '../composables/useStrapiAuth'
import { useStrapiAuth } from './composables/useStrapiAuth'
import { defineNuxtPlugin } from '#app'

export default defineNuxtPlugin(async () => {
Expand Down
Loading