How to use defu - 10 common examples

To help you get started, we’ve selected a few defu examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nuxt / press / packages / utils / src / config.js View on Github external
const config = await importModule(path.join(this.options.rootDir, `nuxt.${rootId}.js`))

    await writeJson(path.join(buildDirRoot, 'config.json'), config, { spaces: 2 })
    return
  }

  const configPath = path.join(this.options.rootDir, `nuxt.${rootId}.json`)
  if (!await exists(configPath)) {
    await writeJson(configPath, config, { spaces: 2 })
    return
  }

  try {
    const existingConfig = await readJson(configPath, { throws: true })

    const updated = defu(existingConfig || {}, config)

    await writeJson(configPath, updated, { spaces: 2 })
    await writeJson(path.join(buildDirRoot, 'config.json'), updated, { spaces: 2 })
  } catch (err) {
    // eslint-disable-next-line no-console
    console.warn(err)
  }
}
github nuxt / press / packages / utils / src / config.js View on Github external
const config = await importModule(path.join(this.options.rootDir, `nuxt.${rootId}.js`))

    await writeJson(path.join(buildDirRoot, 'config.json'), config, { spaces: 2 })
    return
  }

  const configPath = path.join(this.options.rootDir, `nuxt.${rootId}.json`)
  if (!await exists(configPath)) {
    await writeJson(configPath, config, { spaces: 2 })
    return
  }

  try {
    const existingConfig = await readJson(configPath, { throws: true })

    const updated = defu(existingConfig || {}, config)

    await writeJson(configPath, updated, { spaces: 2 })
    await writeJson(path.join(buildDirRoot, 'config.json'), updated, { spaces: 2 })
  } catch (err) {
    // eslint-disable-next-line no-console
    console.warn(err)
  }
}
github nuxt / press / src / blueprint.js View on Github external
export async function registerBlueprint (blueprintId, rootId, id, rootOptions) {
  // Load blueprint specification
  const blueprint = blueprints[blueprintId]

  // Populate mode default options
  const options = defu(rootOptions[id] || {}, blueprint.options)
  // add ref back to rootOptions
  rootOptions[id] = options

  const context = {
    blueprintId,
    rootId,
    id,
    rootOptions,
    options,
    registeredBlueprintIds,
    availableBlueprintIds,
    data: undefined
  }

  // Determine if mode is enabled
  if (!blueprint.enabled.call(this, context)) {
github nuxt / press / packages / utils / src / config.js View on Github external
export async function loadConfig ({ rootId, rootDir, config }) {
  const fileExtensions = ['js', 'json']

  for (const fileExtension of fileExtensions) {
    const jsConfigPath = path.join(rootDir, `nuxt.${rootId}.${fileExtension}`)

    // JavaScript config has precedence over JSON config
    if (await exists(jsConfigPath)) {
      // load external config
      const externalConfig = await importModule(jsConfigPath)

      // apply defaults
      config = defu(externalConfig, config)
      config.configPath = jsConfigPath
      break
    }
  }

  return config
}
github nuxt / press / distributions / blueprint / src / blueprint.js View on Github external
constructor (nuxt, options = {}) {
    // singleton blueprints dont support being loaded twice
    if (new.target.features.singleton && !runOnceGuard(new.target, 'constructed')) {
      throw new Error(`${new.target.name}: trying to load a singleton blueprint which is already loaded`)
    }

    super(nuxt)

    this.id = options.id || this.constructor.id || 'blueprint'

    this.blueprintOptions = defu(options, defaultOptions)

    this.templateOptions = this.blueprintOptions
  }
github nuxt / blueprints / src / blueprint.js View on Github external
constructor (nuxt, options = {}) {
    // singleton blueprints dont support being loaded twice
    if (new.target.features.singleton && !runOnceGuard(new.target, 'constructed')) {
      throw new Error(`${new.target.name}: trying to load a singleton blueprint which is already loaded`)
    }

    super(nuxt)

    this.id = options.id || this.constructor.id || 'blueprint'

    this.blueprintOptions = defu(options, defaultOptions)

    this.templateOptions = this.blueprintOptions
  }
github nuxt / press / src / blueprints / docs / data.js View on Github external
let raw = await readFile(join(root, sourcePath))
  const { name: fileName } = path.parse(sourcePath)

  let meta
  if (raw.trimLeft().startsWith('---')) {
    const { content, data } = graymatter(raw)
    raw = content

    meta = defu(data, defaultMetaSettings)

    if (meta.sidebar === 'auto') {
      meta.sidebarDepth = maxSidebarDepth
    }
  } else {
    meta = defu({}, defaultMetaSettings)
  }

  const { toc, html: body } = await options.source.markdown.call(this, raw, mdProcessor)

  const title = await options.source.title.call(this, fileName, raw, toc)

  sourcePath = sourcePath.substr(0, sourcePath.lastIndexOf('.')).replace(isIndexRE, '') || 'index'

  const urlPath = sourcePath === 'index' ? '/' : `/${sourcePath.replace(/\/index$/, '')}/`

  let locale = ''
  const locales = rootOptions.i18n && rootOptions.i18n.locales
  if (locales) {
    ({ code: locale } = locales.find(l => l.code === sourcePath || sourcePath.startsWith(`${l.code}/`)) || {})
  }
github nuxt / press / src / blueprints / docs / data.js View on Github external
export async function parsePage ({ rootOptions, id, options }, { root, prefix: pagePrefix = '', path: sourcePath }, mdProcessor) {
  const src = sourcePath

  pagePrefix = normalizePath(pagePrefix, true, false, true)

  let raw = await readFile(join(root, sourcePath))
  const { name: fileName } = path.parse(sourcePath)

  let meta
  if (raw.trimLeft().startsWith('---')) {
    const { content, data } = graymatter(raw)
    raw = content

    meta = defu(data, defaultMetaSettings)

    if (meta.sidebar === 'auto') {
      meta.sidebarDepth = maxSidebarDepth
    }
  } else {
    meta = defu({}, defaultMetaSettings)
  }

  const { toc, html: body } = await options.source.markdown.call(this, raw, mdProcessor)

  const title = await options.source.title.call(this, fileName, raw, toc)

  sourcePath = sourcePath.substr(0, sourcePath.lastIndexOf('.')).replace(isIndexRE, '') || 'index'

  const urlPath = sourcePath === 'index' ? '/' : `/${sourcePath.replace(/\/index$/, '')}/`
github nuxt / press / packages / docs / src / blueprint / source.js View on Github external
metadata (source) {
    const defaultMetaSettings = this.constructor.defaultConfig.metaSettings

    if (source.trimLeft().startsWith('---')) {
      const { content, data } = graymatter(source)

      const meta = defu(data, defaultMetaSettings)

      if (meta.sidebar === 'auto') {
        meta.sidebarDepth = this.constructor.defaultConfig.maxSidebarDepth
      }

      return {
        content,
        meta
      }
    }

    return {
      meta: {
        ...defaultMetaSettings
      }
    }
github nuxt-community / auth-module / lib / schemes / refresh.js View on Github external
constructor (auth, options) {
    super(auth, defu(options, DEFAULTS))

    this.refreshInterval = undefined
    this.isRefreshing = false
    this.hasRefreshTokenChanged = false
  }

defu

Recursively assign default properties. Lightweight and Fast!

MIT
Latest version published 12 months ago

Package Health Score

77 / 100
Full package analysis

Popular defu functions