Skip to content

Commit

Permalink
perf: ⚡️ improve options dx and decrease final bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Aug 8, 2020
1 parent 1878152 commit d1c4a76
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"test": "yarn lint && jest"
},
"dependencies": {
"@nuxtjs/tailwindcss": "^3.0.0",
"defu": "^3.1.0"
},
"devDependencies": {
Expand All @@ -41,19 +40,27 @@
"@nuxt/typescript-runtime": "latest",
"@nuxtjs/eslint-config-typescript": "latest",
"@nuxtjs/module-test-utils": "2.0.0-1",
"@nuxtjs/tailwindcss": "^3.0.0",
"@release-it/conventional-changelog": "^1.1.4",
"@sanity/client": "^1.149.18",
"@types/jest": "latest",
"babel-jest": "latest",
"eslint": "latest",
"jest": "latest",
"lint-staged": "^10.2.11",
"nuxt-edge": "^2.14.2-26612668.4a9c9a15",
"nuxt-edge": "latest",
"puppeteer": "^5.2.1",
"release-it": "^13.6.6",
"siroc": "latest",
"yorkie": "^2.0.0"
},
"peerDependencies": {
"chalk": "^3.0.0",
"consola": "^2.14.0"
},
"optionalDependencies": {
"@sanity/client": "^1.149.18"
},
"publishConfig": {
"access": "public"
},
Expand Down
43 changes: 27 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { bold } from 'chalk'
import consola from 'consola'
import defu from 'defu'

import { Module } from '@nuxt/types'
import type { Module } from '@nuxt/types'
import type { SanityClient } from '@sanity/client'

import { SanityClient } from '@sanity/client'
import { SanityConfiguration, createClient } from './client'

export interface ModuleOptions extends Partial<SanityConfiguration> {
const isNuxtBuild = process.client || process.server

export interface SanityModuleOptions extends Partial<SanityConfiguration> {
/**
* Use a micro-client that only supports making queries.
* @default false
Expand All @@ -21,12 +23,12 @@ export interface ModuleOptions extends Partial<SanityConfiguration> {
*/
imageHelper?: boolean
}
const DEFAULTS: ModuleOptions = {
const DEFAULTS: SanityModuleOptions = {
imageHelper: true
}
const CONFIG_KEY = 'sanity'

function validateConfig ({ projectId, dataset }: ModuleOptions) {
function validateConfig ({ projectId, dataset }: SanityModuleOptions) {
if (!projectId) {
consola.warn(
`Make sure you specify a ${bold('projectId')} in your sanity config.`
Expand All @@ -42,19 +44,25 @@ function validateConfig ({ projectId, dataset }: ModuleOptions) {
}
}

const nuxtModule: Module<ModuleOptions> = function (moduleOptions) {
const nuxtModule: Module<SanityModuleOptions> = function (moduleOptions) {
if (isNuxtBuild) {
return
}

let sanityConfig: Record<string, any> = {}

try {
// eslint-disable-next-line
const { projectId, dataset } = require(resolve(
this.options.rootDir,
'./sanity.json'
)).api
sanityConfig = { projectId, dataset }
if (process.server) {
const fs = require('fs-extra')
const { projectId, dataset } = fs.readJSONSync(
resolve(this.options.rootDir, './sanity.json')
).api
sanityConfig = { projectId, dataset }
}
} catch {}

const options = defu<ModuleOptions>(
const options = defu<SanityModuleOptions>(
this.options[CONFIG_KEY],
moduleOptions,
sanityConfig,
Expand All @@ -78,7 +86,10 @@ const nuxtModule: Module<ModuleOptions> = function (moduleOptions) {
)} as it cannot be resolved in your project dependencies.
Try running ${bold('yarn add @sanity/client')} or ${bold(
'npm install @sanity/client'
)}.`
)}.
To disable this warning, set ${bold(
'sanity: { minimal: true }'
)} in your nuxt.config.js.`
)
}

Expand Down Expand Up @@ -111,14 +122,14 @@ const nuxtModule: Module<ModuleOptions> = function (moduleOptions) {
this.options.build.transpile = this.options.build.transpile || []
this.options.build.transpile.push(/^@nuxtjs[\\/]sanity/)
}
;(nuxtModule as any).meta = require('../package.json')
;(nuxtModule as any).meta = { name: '@nuxtjs/sanity' }

declare module '@nuxt/types' {
interface NuxtConfig {
sanity: ModuleOptions
sanity: SanityModuleOptions
} // Nuxt 2.14+
interface Configuration {
sanity: ModuleOptions
sanity: SanityModuleOptions
} // Nuxt 2.9 - 2.13
interface NuxtAppOptions {
$sanity: {
Expand Down

0 comments on commit d1c4a76

Please sign in to comment.