How to use @nuxt/utils - 10 common examples

To help you get started, we’ve selected a few @nuxt/utils 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 / nuxt.js / packages / builder / src / builder.js View on Github external
watchClient () {
    let patterns = [
      r(this.options.srcDir, this.options.dir.layouts),
      r(this.options.srcDir, this.options.dir.middleware)
    ]

    if (this.options.store) {
      patterns.push(r(this.options.srcDir, this.options.dir.store))
    }

    if (this._nuxtPages && !this._defaultPage) {
      patterns.push(r(this.options.srcDir, this.options.dir.pages))
    }

    patterns = patterns.map((path, ...args) => upath.normalizeSafe(this.globPathWithExtensions(path), ...args))

    const refreshFiles = debounce(() => this.generateRoutesAndFiles(), 200)

    // Watch for src Files
    this.createFileWatcher(patterns, ['add', 'unlink'], refreshFiles, this.assignWatcher('files'))

    // Watch for custom provided files
    const customPatterns = uniq([
      ...this.options.build.watch,
      ...Object.values(omit(this.options.build.styleResources, ['options']))
    ]).map(upath.normalizeSafe)

    if (customPatterns.length === 0) {
github nuxt / nuxt.js / packages / builder / src / builder.js View on Github external
)
          }
        } else if (this.options.layouts[name]) {
          consola.warn(`Duplicate layout registration, "${name}" has been registered as "${this.options.layouts[name]}"`)
        } else if (!templateVars.layouts[name] || /\.vue$/.test(file)) {
          templateVars.layouts[name] = this.relativeToBuild(
            this.options.srcDir,
            file
          )
        }
      }
    }

    // If no default layout, create its folder and add the default folder
    if (!templateVars.layouts.default) {
      await fsExtra.mkdirp(r(this.options.buildDir, 'layouts'))
      templateFiles.push('layouts/default.vue')
      templateVars.layouts.default = './layouts/default.vue'
    }
  }
github nuxt / nuxt.js / packages / builder / src / builder.js View on Github external
this.createFileWatcher(patterns, ['add', 'unlink'], refreshFiles, this.assignWatcher('files'))

    // Watch for custom provided files
    const customPatterns = uniq([
      ...this.options.build.watch,
      ...Object.values(omit(this.options.build.styleResources, ['options']))
    ]).map(upath.normalizeSafe)

    if (customPatterns.length === 0) {
      return
    }

    this.createFileWatcher(customPatterns, ['change'], refreshFiles, this.assignWatcher('custom'))

    // Watch for app/ files
    this.createFileWatcher([r(this.options.srcDir, this.options.dir.app)], ['add', 'change', 'unlink'], refreshFiles, this.assignWatcher('app'))
  }
github nuxt / nuxt.js / packages / builder / src / builder.js View on Github external
srcDir: this.template.dir + '/pages',
        routeNameSplitter,
        trailingSlash
      })
    } else if (this._nuxtPages) {
      // Use nuxt.js createRoutes bases on pages/
      const files = {}
      const ext = new RegExp(`\\.(${this.supportedExtensions.join('|')})$`)
      for (const page of await this.resolveFiles(this.options.dir.pages)) {
        const key = page.replace(ext, '')
        // .vue file takes precedence over other extensions
        if (/\.vue$/.test(page) || !files[key]) {
          files[key] = page.replace(/(['"])/g, '\\$1')
        }
      }
      templateVars.router.routes = createRoutes({
        files: Object.values(files),
        srcDir: this.options.srcDir,
        pagesDir: this.options.dir.pages,
        routeNameSplitter,
        supportedExtensions: this.supportedExtensions,
        trailingSlash
      })
    } else { // If user defined a custom method to create routes
      templateVars.router.routes = await this.options.build.createRoutes(
        this.options.srcDir
      )
    }

    await this.nuxt.callHook(
      'build:extendRoutes',
      templateVars.router.routes,
github nuxt / nuxt.js / packages / builder / src / builder.js View on Github external
async resolveRoutes ({ templateVars }) {
    consola.debug('Generating routes...')
    const { routeNameSplitter, trailingSlash } = this.options.router

    if (this._defaultPage) {
      templateVars.router.routes = createRoutes({
        files: ['index.vue'],
        srcDir: this.template.dir + '/pages',
        routeNameSplitter,
        trailingSlash
      })
    } else if (this._nuxtPages) {
      // Use nuxt.js createRoutes bases on pages/
      const files = {}
      const ext = new RegExp(`\\.(${this.supportedExtensions.join('|')})$`)
      for (const page of await this.resolveFiles(this.options.dir.pages)) {
        const key = page.replace(ext, '')
        // .vue file takes precedence over other extensions
        if (/\.vue$/.test(page) || !files[key]) {
          files[key] = page.replace(/(['"])/g, '\\$1')
        }
      }
github nuxt / nuxt.js / packages / config / src / options.js View on Github external
}

  if (typeof options.layoutTransition === 'string') {
    options.layoutTransition = { name: options.layoutTransition }
  }

  if (typeof options.extensions === 'string') {
    options.extensions = [options.extensions]
  }

  options.globalName = (isNonEmptyString(options.globalName) && /^[a-zA-Z]+$/.test(options.globalName))
    ? options.globalName.toLowerCase()
    : `nuxt`

  // Resolve rootDir
  options.rootDir = isNonEmptyString(options.rootDir) ? path.resolve(options.rootDir) : process.cwd()

  // Apply defaults by ${buildDir}/dist/build.config.js
  // TODO: Unsafe operation.
  // const buildDir = options.buildDir || defaults.buildDir
  // const buildConfig = resolve(options.rootDir, buildDir, 'build.config.js')
  // if (existsSync(buildConfig)) {
  //   defaultsDeep(options, require(buildConfig))
  // }

  // Apply defaults
  const nuxtConfig = getDefaultNuxtConfig()

  nuxtConfig.build._publicPath = nuxtConfig.build.publicPath

  // Fall back to default if publicPath is falsy
  if (options.build && !options.build.publicPath) {
github nuxt / nuxt.js / packages / config / src / options.js View on Github external
nuxtConfig.build._publicPath = nuxtConfig.build.publicPath

  // Fall back to default if publicPath is falsy
  if (options.build && !options.build.publicPath) {
    options.build.publicPath = undefined
  }

  defaultsDeep(options, nuxtConfig)

  // Sanitize router.base
  if (!/\/$/.test(options.router.base)) {
    options.router.base += '/'
  }

  // Check srcDir and generate.dir existence
  const hasSrcDir = isNonEmptyString(options.srcDir)
  const hasGenerateDir = isNonEmptyString(options.generate.dir)

  // Resolve srcDir
  options.srcDir = hasSrcDir
    ? path.resolve(options.rootDir, options.srcDir)
    : options.rootDir

  // Resolve buildDir
  options.buildDir = path.resolve(options.rootDir, options.buildDir)

  // Aliases
  const { rootDir, srcDir, dir: { assets: assetsDir, static: staticDir } } = options
  options.alias = {
    '~~': rootDir,
    '@@': rootDir,
    '~': srcDir,
github nuxt / nuxt.js / packages / config / src / options.js View on Github external
delete options.transition
  }

  if (typeof options.pageTransition === 'string') {
    options.pageTransition = { name: options.pageTransition }
  }

  if (typeof options.layoutTransition === 'string') {
    options.layoutTransition = { name: options.layoutTransition }
  }

  if (typeof options.extensions === 'string') {
    options.extensions = [options.extensions]
  }

  options.globalName = (isNonEmptyString(options.globalName) && /^[a-zA-Z]+$/.test(options.globalName))
    ? options.globalName.toLowerCase()
    : `nuxt`

  // Resolve rootDir
  options.rootDir = isNonEmptyString(options.rootDir) ? path.resolve(options.rootDir) : process.cwd()

  // Apply defaults by ${buildDir}/dist/build.config.js
  // TODO: Unsafe operation.
  // const buildDir = options.buildDir || defaults.buildDir
  // const buildConfig = resolve(options.rootDir, buildDir, 'build.config.js')
  // if (existsSync(buildConfig)) {
  //   defaultsDeep(options, require(buildConfig))
  // }

  // Apply defaults
  const nuxtConfig = getDefaultNuxtConfig()
github nuxt / nuxt.js / packages / config / src / options.js View on Github external
// Fall back to default if publicPath is falsy
  if (options.build && !options.build.publicPath) {
    options.build.publicPath = undefined
  }

  defaultsDeep(options, nuxtConfig)

  // Sanitize router.base
  if (!/\/$/.test(options.router.base)) {
    options.router.base += '/'
  }

  // Check srcDir and generate.dir existence
  const hasSrcDir = isNonEmptyString(options.srcDir)
  const hasGenerateDir = isNonEmptyString(options.generate.dir)

  // Resolve srcDir
  options.srcDir = hasSrcDir
    ? path.resolve(options.rootDir, options.srcDir)
    : options.rootDir

  // Resolve buildDir
  options.buildDir = path.resolve(options.rootDir, options.buildDir)

  // Aliases
  const { rootDir, srcDir, dir: { assets: assetsDir, static: staticDir } } = options
  options.alias = {
    '~~': rootDir,
    '@@': rootDir,
    '~': srcDir,
    '@': srcDir,
github nuxt / nuxt.js / packages / core / src / resolver.js View on Github external
let lastError

    // Try to resolve path
    try {
      resolvedPath = this.resolvePath(path, { isAlias })
    } catch (e) {
      lastError = e
    }

    const isExternal = isExternalDependency(resolvedPath)

    // in dev mode make sure to clear the require cache so after
    // a dev server restart any changed file is reloaded
    if (this.options.dev && !isExternal) {
      clearRequireCache(resolvedPath)
    }

    // By default use esm only for js,mjs files outside of node_modules
    if (useESM === undefined) {
      useESM = !isExternal && /.(js|mjs)$/.test(resolvedPath)
    }

    // Try to require
    try {
      if (useESM) {
        requiredModule = this.esm(resolvedPath)
      } else {
        requiredModule = require(resolvedPath)
      }
    } catch (e) {
      lastError = e