mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
refactor: @nuxt/utils (#4609)
This commit is contained in:
parent
5458ba5b10
commit
324135df51
@ -50,7 +50,6 @@
|
|||||||
"@babel/register": "^7.0.0",
|
"@babel/register": "^7.0.0",
|
||||||
"@nuxt/builder": "2.3.4",
|
"@nuxt/builder": "2.3.4",
|
||||||
"@nuxt/cli": "2.3.4",
|
"@nuxt/cli": "2.3.4",
|
||||||
"@nuxt/common": "2.3.4",
|
|
||||||
"@nuxt/core": "2.3.4",
|
"@nuxt/core": "2.3.4",
|
||||||
"@nuxt/generator": "2.3.4",
|
"@nuxt/generator": "2.3.4",
|
||||||
"@nuxt/opencollective": "^0.2.1",
|
"@nuxt/opencollective": "^0.2.1",
|
||||||
|
@ -47,7 +47,6 @@
|
|||||||
"bin": "bin/nuxt-start.js",
|
"bin": "bin/nuxt-start.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/cli": "2.3.4",
|
"@nuxt/cli": "2.3.4",
|
||||||
"@nuxt/common": "2.3.4",
|
|
||||||
"@nuxt/core": "2.3.4"
|
"@nuxt/core": "2.3.4"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
@ -50,7 +50,6 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/builder": "2.3.4",
|
"@nuxt/builder": "2.3.4",
|
||||||
"@nuxt/cli": "2.3.4",
|
"@nuxt/cli": "2.3.4",
|
||||||
"@nuxt/common": "2.3.4",
|
|
||||||
"@nuxt/core": "2.3.4",
|
"@nuxt/core": "2.3.4",
|
||||||
"@nuxt/generator": "2.3.4",
|
"@nuxt/generator": "2.3.4",
|
||||||
"@nuxt/opencollective": "^0.2.1",
|
"@nuxt/opencollective": "^0.2.1",
|
||||||
|
@ -8,8 +8,8 @@
|
|||||||
],
|
],
|
||||||
"main": "dist/builder.js",
|
"main": "dist/builder.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/common": "2.3.4",
|
|
||||||
"@nuxt/devalue": "^1.2.0",
|
"@nuxt/devalue": "^1.2.0",
|
||||||
|
"@nuxt/utils": "2.3.4",
|
||||||
"@nuxt/vue-app": "2.3.4",
|
"@nuxt/vue-app": "2.3.4",
|
||||||
"chokidar": "^2.0.4",
|
"chokidar": "^2.0.4",
|
||||||
"consola": "^2.3.0",
|
"consola": "^2.3.0",
|
||||||
|
@ -27,7 +27,7 @@ import {
|
|||||||
determineGlobals,
|
determineGlobals,
|
||||||
stripWhitespace,
|
stripWhitespace,
|
||||||
isString
|
isString
|
||||||
} from '@nuxt/common'
|
} from '@nuxt/utils'
|
||||||
|
|
||||||
import BuildContext from './context'
|
import BuildContext from './context'
|
||||||
|
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
# Change Log
|
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
||||||
|
|
||||||
## [2.3.4](https://github.com/nuxt/nuxt.js/compare/v2.3.2...v2.3.4) (2018-11-26)
|
|
||||||
|
|
||||||
|
|
||||||
### Performance Improvements
|
|
||||||
|
|
||||||
* **pkg:** remove lodash dependency from packages ([#4411](https://github.com/nuxt/nuxt.js/issues/4411)) ([7e1beed](https://github.com/nuxt/nuxt.js/commit/7e1beed))
|
|
@ -1,12 +0,0 @@
|
|||||||
{
|
|
||||||
"Edge": "16",
|
|
||||||
"Firefox": "60",
|
|
||||||
"Chrome": "61",
|
|
||||||
"Chrome Headless": "61",
|
|
||||||
"Chromium": "61",
|
|
||||||
"Safari": "10.1",
|
|
||||||
"Opera": "48",
|
|
||||||
"Yandex": "18",
|
|
||||||
"Vivaldi": "1.14",
|
|
||||||
"Mobile Safari": "10.3"
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
export { default as Hookable } from './hookable'
|
|
||||||
export { default as ModernBrowsers } from '../data/modern-browsers.json'
|
|
||||||
export * from './utils'
|
|
@ -8,7 +8,7 @@
|
|||||||
],
|
],
|
||||||
"main": "dist/config.js",
|
"main": "dist/config.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/common": "2.3.4",
|
"@nuxt/utils": "2.3.4",
|
||||||
"consola": "^2.3.0",
|
"consola": "^2.3.0",
|
||||||
"std-env": "^2.2.1"
|
"std-env": "^2.2.1"
|
||||||
},
|
},
|
||||||
|
@ -6,7 +6,7 @@ import pick from 'lodash/pick'
|
|||||||
import isObject from 'lodash/isObject'
|
import isObject from 'lodash/isObject'
|
||||||
import uniq from 'lodash/uniq'
|
import uniq from 'lodash/uniq'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import { guardDir, isNonEmptyString, isPureObject, isUrl } from '@nuxt/common'
|
import { guardDir, isNonEmptyString, isPureObject, isUrl } from '@nuxt/utils'
|
||||||
import { getDefaultNuxtConfig } from './config'
|
import { getDefaultNuxtConfig } from './config'
|
||||||
|
|
||||||
export function getNuxtConfig(_options) {
|
export function getNuxtConfig(_options) {
|
||||||
|
@ -8,10 +8,10 @@
|
|||||||
],
|
],
|
||||||
"main": "dist/core.js",
|
"main": "dist/core.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/common": "2.3.4",
|
|
||||||
"@nuxt/config": "2.3.4",
|
"@nuxt/config": "2.3.4",
|
||||||
"@nuxt/devalue": "^1.2.0",
|
"@nuxt/devalue": "^1.2.0",
|
||||||
"@nuxt/server": "2.3.4",
|
"@nuxt/server": "2.3.4",
|
||||||
|
"@nuxt/utils": "2.3.4",
|
||||||
"@nuxt/vue-renderer": "2.3.4",
|
"@nuxt/vue-renderer": "2.3.4",
|
||||||
"consola": "^2.3.0",
|
"consola": "^2.3.0",
|
||||||
"debug": "^4.1.0",
|
"debug": "^4.1.0",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
|
|
||||||
import { sequence } from './utils'
|
import { sequence } from '@nuxt/utils'
|
||||||
|
|
||||||
export default class Hookable {
|
export default class Hookable {
|
||||||
constructor() {
|
constructor() {
|
@ -3,7 +3,7 @@ import fs from 'fs'
|
|||||||
import hash from 'hash-sum'
|
import hash from 'hash-sum'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
|
|
||||||
import { chainFn, sequence } from '@nuxt/common'
|
import { chainFn, sequence } from '@nuxt/utils'
|
||||||
|
|
||||||
export default class ModuleContainer {
|
export default class ModuleContainer {
|
||||||
constructor(nuxt) {
|
constructor(nuxt) {
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
import isPlainObject from 'lodash/isPlainObject'
|
import isPlainObject from 'lodash/isPlainObject'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
|
|
||||||
import { Hookable, defineAlias } from '@nuxt/common'
|
import { defineAlias } from '@nuxt/utils'
|
||||||
import { getNuxtConfig } from '@nuxt/config'
|
import { getNuxtConfig } from '@nuxt/config'
|
||||||
import { Server } from '@nuxt/server'
|
import { Server } from '@nuxt/server'
|
||||||
|
|
||||||
import { version } from '../package.json'
|
import { version } from '../package.json'
|
||||||
|
|
||||||
import ModuleContainer from './module'
|
import ModuleContainer from './module'
|
||||||
|
import Hookable from './hookable'
|
||||||
import Resolver from './resolver'
|
import Resolver from './resolver'
|
||||||
|
|
||||||
export default class Nuxt extends Hookable {
|
export default class Nuxt extends Hookable {
|
||||||
|
@ -3,7 +3,7 @@ import { resolve, join } from 'path'
|
|||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
import esm from 'esm'
|
import esm from 'esm'
|
||||||
|
|
||||||
import { startsWithRootAlias, startsWithSrcAlias } from '@nuxt/common'
|
import { startsWithRootAlias, startsWithSrcAlias } from '@nuxt/utils'
|
||||||
|
|
||||||
export default class Resolver {
|
export default class Resolver {
|
||||||
constructor(nuxt) {
|
constructor(nuxt) {
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
],
|
],
|
||||||
"main": "dist/generator.js",
|
"main": "dist/generator.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/common": "2.3.4",
|
"@nuxt/utils": "2.3.4",
|
||||||
"chalk": "^2.4.1",
|
"chalk": "^2.4.1",
|
||||||
"consola": "^2.3.0",
|
"consola": "^2.3.0",
|
||||||
"fs-extra": "^7.0.1",
|
"fs-extra": "^7.0.1",
|
||||||
|
@ -4,7 +4,7 @@ import consola from 'consola'
|
|||||||
import fsExtra from 'fs-extra'
|
import fsExtra from 'fs-extra'
|
||||||
import htmlMinifier from 'html-minifier'
|
import htmlMinifier from 'html-minifier'
|
||||||
|
|
||||||
import { flatRoutes, isUrl, promisifyRoute, waitFor, isString } from '@nuxt/common'
|
import { flatRoutes, isUrl, promisifyRoute, waitFor, isString } from '@nuxt/utils'
|
||||||
|
|
||||||
export default class Generator {
|
export default class Generator {
|
||||||
constructor(nuxt, builder) {
|
constructor(nuxt, builder) {
|
||||||
|
@ -8,8 +8,8 @@
|
|||||||
],
|
],
|
||||||
"main": "dist/server.js",
|
"main": "dist/server.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/common": "2.3.4",
|
|
||||||
"@nuxt/config": "2.3.4",
|
"@nuxt/config": "2.3.4",
|
||||||
|
"@nuxt/utils": "2.3.4",
|
||||||
"@nuxtjs/youch": "^4.2.3",
|
"@nuxtjs/youch": "^4.2.3",
|
||||||
"chalk": "^2.4.1",
|
"chalk": "^2.4.1",
|
||||||
"compression": "^1.7.3",
|
"compression": "^1.7.3",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import { timeout } from '@nuxt/common'
|
import { timeout } from '@nuxt/utils'
|
||||||
|
|
||||||
export default async function renderAndGetWindow(
|
export default async function renderAndGetWindow(
|
||||||
url = 'http://localhost:3000',
|
url = 'http://localhost:3000',
|
||||||
|
12
packages/server/src/middleware/modern-browsers.js
Normal file
12
packages/server/src/middleware/modern-browsers.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
export default {
|
||||||
|
Edge: '16',
|
||||||
|
Firefox: '60',
|
||||||
|
Chrome: '61',
|
||||||
|
'Chrome Headless': '61',
|
||||||
|
Chromium: '61',
|
||||||
|
Safari: '10.1',
|
||||||
|
Opera: '48',
|
||||||
|
Yandex: '18',
|
||||||
|
Vivaldi: '1.14',
|
||||||
|
'Mobile Safari': '10.3'
|
||||||
|
}
|
@ -1,9 +1,10 @@
|
|||||||
import chalk from 'chalk'
|
import chalk from 'chalk'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import { ModernBrowsers } from '@nuxt/common'
|
|
||||||
import UAParser from 'ua-parser-js'
|
import UAParser from 'ua-parser-js'
|
||||||
import semver from 'semver'
|
import semver from 'semver'
|
||||||
|
|
||||||
|
import ModernBrowsers from './modern-browsers'
|
||||||
|
|
||||||
const modernBrowsers = Object.keys(ModernBrowsers)
|
const modernBrowsers = Object.keys(ModernBrowsers)
|
||||||
.reduce((allBrowsers, browser) => {
|
.reduce((allBrowsers, browser) => {
|
||||||
allBrowsers[browser] = semver.coerce(ModernBrowsers[browser])
|
allBrowsers[browser] = semver.coerce(ModernBrowsers[browser])
|
||||||
|
@ -2,7 +2,7 @@ import generateETag from 'etag'
|
|||||||
import fresh from 'fresh'
|
import fresh from 'fresh'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
|
|
||||||
import { getContext } from '@nuxt/common'
|
import { getContext } from '@nuxt/utils'
|
||||||
|
|
||||||
export default ({ options, nuxt, renderRoute, resources }) => async function nuxtMiddleware(req, res, next) {
|
export default ({ options, nuxt, renderRoute, resources }) => async function nuxtMiddleware(req, res, next) {
|
||||||
// Get context
|
// Get context
|
||||||
|
@ -4,7 +4,7 @@ import launchMiddleware from 'launch-editor-middleware'
|
|||||||
import serveStatic from 'serve-static'
|
import serveStatic from 'serve-static'
|
||||||
import servePlaceholder from 'serve-placeholder'
|
import servePlaceholder from 'serve-placeholder'
|
||||||
import connect from 'connect'
|
import connect from 'connect'
|
||||||
import { determineGlobals, isUrl } from '@nuxt/common'
|
import { determineGlobals, isUrl } from '@nuxt/utils'
|
||||||
|
|
||||||
import ServerContext from './context'
|
import ServerContext from './context'
|
||||||
import renderAndGetWindow from './jsdom'
|
import renderAndGetWindow from './jsdom'
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@nuxt/common",
|
"name": "@nuxt/utils",
|
||||||
"version": "2.3.4",
|
"version": "2.3.4",
|
||||||
"repository": "nuxt/nuxt.js",
|
"repository": "nuxt/nuxt.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"main": "dist/common.js",
|
"main": "dist/utils.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"consola": "^2.3.0",
|
"consola": "^2.3.0",
|
||||||
"serialize-javascript": "^1.5.0"
|
"serialize-javascript": "^1.5.0"
|
16
packages/utils/src/context.js
Normal file
16
packages/utils/src/context.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
export const getContext = function getContext(req, res) {
|
||||||
|
return { req, res }
|
||||||
|
}
|
||||||
|
|
||||||
|
export const determineGlobals = function determineGlobals(globalName, globals) {
|
||||||
|
const _globals = {}
|
||||||
|
for (const global in globals) {
|
||||||
|
if (typeof globals[global] === 'function') {
|
||||||
|
_globals[global] = globals[global](globalName)
|
||||||
|
} else {
|
||||||
|
_globals[global] = globals[global]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _globals
|
||||||
|
}
|
7
packages/utils/src/index.js
Normal file
7
packages/utils/src/index.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export * from './context'
|
||||||
|
export * from './lang'
|
||||||
|
export * from './resolve'
|
||||||
|
export * from './route'
|
||||||
|
export * from './serialize'
|
||||||
|
export * from './task'
|
||||||
|
export * from './timer'
|
46
packages/utils/src/lang.js
Normal file
46
packages/utils/src/lang.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
export const encodeHtml = function encodeHtml(str) {
|
||||||
|
return str.replace(/</g, '<').replace(/>/g, '>')
|
||||||
|
}
|
||||||
|
|
||||||
|
export const isString = obj => typeof obj === 'string' || obj instanceof String
|
||||||
|
|
||||||
|
export const isNonEmptyString = obj => obj && isString(obj)
|
||||||
|
|
||||||
|
export const isPureObject = function isPureObject(o) {
|
||||||
|
return !Array.isArray(o) && typeof o === 'object'
|
||||||
|
}
|
||||||
|
|
||||||
|
export const isUrl = function isUrl(url) {
|
||||||
|
return ['http', '//'].some(str => url.startsWith(str))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const urlJoin = function urlJoin() {
|
||||||
|
return [].slice
|
||||||
|
.call(arguments)
|
||||||
|
.join('/')
|
||||||
|
.replace(/\/+/g, '/')
|
||||||
|
.replace(':/', '://')
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps value in array if it is not already an array
|
||||||
|
*
|
||||||
|
* @param {any} value
|
||||||
|
* @return {array}
|
||||||
|
*/
|
||||||
|
export const wrapArray = value => Array.isArray(value) ? value : [value]
|
||||||
|
|
||||||
|
const WHITESPACE_REPLACEMENTS = [
|
||||||
|
[/[ \t\f\r]+\n/g, '\n'], // strip empty indents
|
||||||
|
[/{\n{2,}/g, '{\n'], // strip start padding from blocks
|
||||||
|
[/\n{2,}([ \t\f\r]*})/g, '\n$1'], // strip end padding from blocks
|
||||||
|
[/\n{3,}/g, '\n\n'], // strip multiple blank lines (1 allowed)
|
||||||
|
[/\n{2,}$/g, '\n'] // strip blank lines EOF (0 allowed)
|
||||||
|
]
|
||||||
|
|
||||||
|
export const stripWhitespace = function stripWhitespace(string) {
|
||||||
|
WHITESPACE_REPLACEMENTS.forEach(([regex, newSubstr]) => {
|
||||||
|
string = string.replace(regex, newSubstr)
|
||||||
|
})
|
||||||
|
return string
|
||||||
|
}
|
100
packages/utils/src/resolve.js
Normal file
100
packages/utils/src/resolve.js
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
import path from 'path'
|
||||||
|
import consola from 'consola'
|
||||||
|
import escapeRegExp from 'lodash/escapeRegExp'
|
||||||
|
|
||||||
|
export const startsWithAlias = aliasArray => str => aliasArray.some(c => str.startsWith(c))
|
||||||
|
|
||||||
|
export const startsWithSrcAlias = startsWithAlias(['@', '~'])
|
||||||
|
|
||||||
|
export const startsWithRootAlias = startsWithAlias(['@@', '~~'])
|
||||||
|
|
||||||
|
export const isWindows = /^win/.test(process.platform)
|
||||||
|
|
||||||
|
export const wp = function wp(p = '') {
|
||||||
|
/* istanbul ignore if */
|
||||||
|
if (isWindows) {
|
||||||
|
return p.replace(/\\/g, '\\\\')
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
export const wChunk = function wChunk(p = '') {
|
||||||
|
/* istanbul ignore if */
|
||||||
|
if (isWindows) {
|
||||||
|
return p.replace(/\//g, '_')
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
const reqSep = /\//g
|
||||||
|
const sysSep = escapeRegExp(path.sep)
|
||||||
|
const normalize = string => string.replace(reqSep, sysSep)
|
||||||
|
|
||||||
|
export const r = function r(...args) {
|
||||||
|
const lastArg = args[args.length - 1]
|
||||||
|
|
||||||
|
if (startsWithSrcAlias(lastArg)) {
|
||||||
|
return wp(lastArg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return wp(path.resolve(...args.map(normalize)))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const relativeTo = function relativeTo() {
|
||||||
|
const args = Array.prototype.slice.apply(arguments)
|
||||||
|
const dir = args.shift()
|
||||||
|
|
||||||
|
// Keep webpack inline loader intact
|
||||||
|
if (args[0].includes('!')) {
|
||||||
|
const loaders = args.shift().split('!')
|
||||||
|
|
||||||
|
return loaders.concat(relativeTo(dir, loaders.pop(), ...args)).join('!')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve path
|
||||||
|
const _path = r(...args)
|
||||||
|
|
||||||
|
// Check if path is an alias
|
||||||
|
if (startsWithSrcAlias(_path)) {
|
||||||
|
return _path
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make correct relative path
|
||||||
|
let rp = path.relative(dir, _path)
|
||||||
|
if (rp[0] !== '.') {
|
||||||
|
rp = './' + rp
|
||||||
|
}
|
||||||
|
|
||||||
|
return wp(rp)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function defineAlias(src, target, prop, opts = {}) {
|
||||||
|
const { bind = true, warn = false } = opts
|
||||||
|
|
||||||
|
if (Array.isArray(prop)) {
|
||||||
|
for (const p of prop) {
|
||||||
|
defineAlias(src, target, p, opts)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let targetVal = target[prop]
|
||||||
|
if (bind && typeof targetVal === 'function') {
|
||||||
|
targetVal = targetVal.bind(target)
|
||||||
|
}
|
||||||
|
|
||||||
|
let warned = false
|
||||||
|
|
||||||
|
Object.defineProperty(src, prop, {
|
||||||
|
get: () => {
|
||||||
|
if (warn && !warned) {
|
||||||
|
warned = true
|
||||||
|
consola.warn({
|
||||||
|
message: `'${prop}' is deprecated'`,
|
||||||
|
additional: new Error().stack.split('\n').splice(2).join('\n')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return targetVal
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -1,193 +1,8 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import escapeRegExp from 'lodash/escapeRegExp'
|
|
||||||
import get from 'lodash/get'
|
import get from 'lodash/get'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import serialize from 'serialize-javascript'
|
|
||||||
|
|
||||||
export const encodeHtml = function encodeHtml(str) {
|
import { r } from './resolve'
|
||||||
return str.replace(/</g, '<').replace(/>/g, '>')
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getContext = function getContext(req, res) {
|
|
||||||
return { req, res }
|
|
||||||
}
|
|
||||||
|
|
||||||
export const waitFor = function waitFor(ms) {
|
|
||||||
return new Promise(resolve => setTimeout(resolve, ms || 0))
|
|
||||||
}
|
|
||||||
|
|
||||||
export const isString = obj => typeof obj === 'string' || obj instanceof String
|
|
||||||
|
|
||||||
export const isNonEmptyString = obj => obj && isString(obj)
|
|
||||||
|
|
||||||
export const startsWithAlias = aliasArray => str => aliasArray.some(c => str.startsWith(c))
|
|
||||||
|
|
||||||
export const startsWithSrcAlias = startsWithAlias(['@', '~'])
|
|
||||||
|
|
||||||
export const startsWithRootAlias = startsWithAlias(['@@', '~~'])
|
|
||||||
|
|
||||||
async function promiseFinally(fn, finalFn) {
|
|
||||||
let result
|
|
||||||
try {
|
|
||||||
if (typeof fn === 'function') {
|
|
||||||
result = await fn()
|
|
||||||
} else {
|
|
||||||
result = await fn
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
finalFn()
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
export const timeout = function timeout(fn, ms, msg) {
|
|
||||||
let timerId
|
|
||||||
const warpPromise = promiseFinally(fn, () => clearTimeout(timerId))
|
|
||||||
const timerPromise = new Promise((resolve, reject) => {
|
|
||||||
timerId = setTimeout(() => reject(new Error(msg)), ms)
|
|
||||||
})
|
|
||||||
return Promise.race([warpPromise, timerPromise])
|
|
||||||
}
|
|
||||||
|
|
||||||
export const urlJoin = function urlJoin() {
|
|
||||||
return [].slice
|
|
||||||
.call(arguments)
|
|
||||||
.join('/')
|
|
||||||
.replace(/\/+/g, '/')
|
|
||||||
.replace(':/', '://')
|
|
||||||
}
|
|
||||||
|
|
||||||
export const isUrl = function isUrl(url) {
|
|
||||||
return ['http', '//'].some(str => url.startsWith(str))
|
|
||||||
}
|
|
||||||
|
|
||||||
export const promisifyRoute = function promisifyRoute(fn, ...args) {
|
|
||||||
// If routes is an array
|
|
||||||
if (Array.isArray(fn)) {
|
|
||||||
return Promise.resolve(fn)
|
|
||||||
}
|
|
||||||
// If routes is a function expecting a callback
|
|
||||||
if (fn.length === arguments.length) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
fn((err, routeParams) => {
|
|
||||||
if (err) {
|
|
||||||
reject(err)
|
|
||||||
}
|
|
||||||
resolve(routeParams)
|
|
||||||
}, ...args)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
let promise = fn(...args)
|
|
||||||
if (
|
|
||||||
!promise ||
|
|
||||||
(!(promise instanceof Promise) && typeof promise.then !== 'function')
|
|
||||||
) {
|
|
||||||
promise = Promise.resolve(promise)
|
|
||||||
}
|
|
||||||
return promise
|
|
||||||
}
|
|
||||||
|
|
||||||
export const sequence = function sequence(tasks, fn) {
|
|
||||||
return tasks.reduce(
|
|
||||||
(promise, task) => promise.then(() => fn(task)),
|
|
||||||
Promise.resolve()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const parallel = function parallel(tasks, fn) {
|
|
||||||
return Promise.all(tasks.map(fn))
|
|
||||||
}
|
|
||||||
|
|
||||||
export const chainFn = function chainFn(base, fn) {
|
|
||||||
/* istanbul ignore if */
|
|
||||||
if (typeof fn !== 'function') {
|
|
||||||
return base
|
|
||||||
}
|
|
||||||
return function () {
|
|
||||||
if (typeof base !== 'function') {
|
|
||||||
return fn.apply(this, arguments)
|
|
||||||
}
|
|
||||||
let baseResult = base.apply(this, arguments)
|
|
||||||
// Allow function to mutate the first argument instead of returning the result
|
|
||||||
if (baseResult === undefined) {
|
|
||||||
baseResult = arguments[0]
|
|
||||||
}
|
|
||||||
const fnResult = fn.call(
|
|
||||||
this,
|
|
||||||
baseResult,
|
|
||||||
...Array.prototype.slice.call(arguments, 1)
|
|
||||||
)
|
|
||||||
// Return mutated argument if no result was returned
|
|
||||||
if (fnResult === undefined) {
|
|
||||||
return baseResult
|
|
||||||
}
|
|
||||||
return fnResult
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const isPureObject = function isPureObject(o) {
|
|
||||||
return !Array.isArray(o) && typeof o === 'object'
|
|
||||||
}
|
|
||||||
|
|
||||||
export const isWindows = /^win/.test(process.platform)
|
|
||||||
|
|
||||||
export const wp = function wp(p = '') {
|
|
||||||
/* istanbul ignore if */
|
|
||||||
if (isWindows) {
|
|
||||||
return p.replace(/\\/g, '\\\\')
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
export const wChunk = function wChunk(p = '') {
|
|
||||||
/* istanbul ignore if */
|
|
||||||
if (isWindows) {
|
|
||||||
return p.replace(/\//g, '_')
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
const reqSep = /\//g
|
|
||||||
const sysSep = escapeRegExp(path.sep)
|
|
||||||
const normalize = string => string.replace(reqSep, sysSep)
|
|
||||||
|
|
||||||
export const r = function r(...args) {
|
|
||||||
const lastArg = args[args.length - 1]
|
|
||||||
|
|
||||||
if (startsWithSrcAlias(lastArg)) {
|
|
||||||
return wp(lastArg)
|
|
||||||
}
|
|
||||||
|
|
||||||
return wp(path.resolve(...args.map(normalize)))
|
|
||||||
}
|
|
||||||
|
|
||||||
export const relativeTo = function relativeTo() {
|
|
||||||
const args = Array.prototype.slice.apply(arguments)
|
|
||||||
const dir = args.shift()
|
|
||||||
|
|
||||||
// Keep webpack inline loader intact
|
|
||||||
if (args[0].includes('!')) {
|
|
||||||
const loaders = args.shift().split('!')
|
|
||||||
|
|
||||||
return loaders.concat(relativeTo(dir, loaders.pop(), ...args)).join('!')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resolve path
|
|
||||||
const _path = r(...args)
|
|
||||||
|
|
||||||
// Check if path is an alias
|
|
||||||
if (startsWithSrcAlias(_path)) {
|
|
||||||
return _path
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make correct relative path
|
|
||||||
let rp = path.relative(dir, _path)
|
|
||||||
if (rp[0] !== '.') {
|
|
||||||
rp = './' + rp
|
|
||||||
}
|
|
||||||
|
|
||||||
return wp(rp)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const flatRoutes = function flatRoutes(router, _path = '', routes = []) {
|
export const flatRoutes = function flatRoutes(router, _path = '', routes = []) {
|
||||||
router.forEach((r) => {
|
router.forEach((r) => {
|
||||||
@ -383,18 +198,6 @@ export const guardDir = function guardDir(options, key1, key2) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const determineGlobals = function determineGlobals(globalName, globals) {
|
|
||||||
const _globals = {}
|
|
||||||
for (const global in globals) {
|
|
||||||
if (typeof globals[global] === 'function') {
|
|
||||||
_globals[global] = globals[global](globalName)
|
|
||||||
} else {
|
|
||||||
_globals[global] = globals[global]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return _globals
|
|
||||||
}
|
|
||||||
|
|
||||||
const getRoutePathExtension = (key) => {
|
const getRoutePathExtension = (key) => {
|
||||||
if (key === '_') {
|
if (key === '_') {
|
||||||
return '*'
|
return '*'
|
||||||
@ -407,76 +210,28 @@ const getRoutePathExtension = (key) => {
|
|||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export const promisifyRoute = function promisifyRoute(fn, ...args) {
|
||||||
* Wraps value in array if it is not already an array
|
// If routes is an array
|
||||||
*
|
if (Array.isArray(fn)) {
|
||||||
* @param {any} value
|
return Promise.resolve(fn)
|
||||||
* @return {array}
|
}
|
||||||
*/
|
// If routes is a function expecting a callback
|
||||||
export const wrapArray = value => Array.isArray(value) ? value : [value]
|
if (fn.length === arguments.length) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
const WHITESPACE_REPLACEMENTS = [
|
fn((err, routeParams) => {
|
||||||
[/[ \t\f\r]+\n/g, '\n'], // strip empty indents
|
if (err) {
|
||||||
[/{\n{2,}/g, '{\n'], // strip start padding from blocks
|
reject(err)
|
||||||
[/\n{2,}([ \t\f\r]*})/g, '\n$1'], // strip end padding from blocks
|
}
|
||||||
[/\n{3,}/g, '\n\n'], // strip multiple blank lines (1 allowed)
|
resolve(routeParams)
|
||||||
[/\n{2,}$/g, '\n'] // strip blank lines EOF (0 allowed)
|
}, ...args)
|
||||||
]
|
|
||||||
|
|
||||||
export const stripWhitespace = function stripWhitespace(string) {
|
|
||||||
WHITESPACE_REPLACEMENTS.forEach(([regex, newSubstr]) => {
|
|
||||||
string = string.replace(regex, newSubstr)
|
|
||||||
})
|
})
|
||||||
return string
|
}
|
||||||
|
let promise = fn(...args)
|
||||||
|
if (
|
||||||
|
!promise ||
|
||||||
|
(!(promise instanceof Promise) && typeof promise.then !== 'function')
|
||||||
|
) {
|
||||||
|
promise = Promise.resolve(promise)
|
||||||
|
}
|
||||||
|
return promise
|
||||||
}
|
}
|
||||||
|
|
||||||
export function defineAlias(src, target, prop, opts = {}) {
|
|
||||||
const { bind = true, warn = false } = opts
|
|
||||||
|
|
||||||
if (Array.isArray(prop)) {
|
|
||||||
for (const p of prop) {
|
|
||||||
defineAlias(src, target, p, opts)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let targetVal = target[prop]
|
|
||||||
if (bind && typeof targetVal === 'function') {
|
|
||||||
targetVal = targetVal.bind(target)
|
|
||||||
}
|
|
||||||
|
|
||||||
let warned = false
|
|
||||||
|
|
||||||
Object.defineProperty(src, prop, {
|
|
||||||
get: () => {
|
|
||||||
if (warn && !warned) {
|
|
||||||
warned = true
|
|
||||||
consola.warn({
|
|
||||||
message: `'${prop}' is deprecated'`,
|
|
||||||
additional: new Error().stack.split('\n').splice(2).join('\n')
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return targetVal
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function serializeFunction(func) {
|
|
||||||
let open = false
|
|
||||||
return serialize(func)
|
|
||||||
.replace(serializeFunction.assignmentRE, (_, spaces) => {
|
|
||||||
return `${spaces}:function(`
|
|
||||||
})
|
|
||||||
.replace(serializeFunction.internalFunctionRE, (_, spaces, name, args) => {
|
|
||||||
if (open) {
|
|
||||||
return `${spaces}${name}:function(${args}) {`
|
|
||||||
} else {
|
|
||||||
open = true
|
|
||||||
return _
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.replace(`${func.name}(`, 'function(')
|
|
||||||
}
|
|
||||||
|
|
||||||
serializeFunction.internalFunctionRE = /^(\s*)(?!(?:if)|(?:for)|(?:while)|(?:switch))(\w+)\s*\((.*?)\)\s*\{/gm
|
|
||||||
serializeFunction.assignmentRE = /^(\s*):(\w+)\(/gm
|
|
22
packages/utils/src/serialize.js
Normal file
22
packages/utils/src/serialize.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
import serialize from 'serialize-javascript'
|
||||||
|
|
||||||
|
export function serializeFunction(func) {
|
||||||
|
let open = false
|
||||||
|
return serialize(func)
|
||||||
|
.replace(serializeFunction.assignmentRE, (_, spaces) => {
|
||||||
|
return `${spaces}:function(`
|
||||||
|
})
|
||||||
|
.replace(serializeFunction.internalFunctionRE, (_, spaces, name, args) => {
|
||||||
|
if (open) {
|
||||||
|
return `${spaces}${name}:function(${args}) {`
|
||||||
|
} else {
|
||||||
|
open = true
|
||||||
|
return _
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.replace(`${func.name}(`, 'function(')
|
||||||
|
}
|
||||||
|
|
||||||
|
serializeFunction.internalFunctionRE = /^(\s*)(?!(?:if)|(?:for)|(?:while)|(?:switch))(\w+)\s*\((.*?)\)\s*\{/gm
|
||||||
|
serializeFunction.assignmentRE = /^(\s*):(\w+)\(/gm
|
37
packages/utils/src/task.js
Normal file
37
packages/utils/src/task.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
export const sequence = function sequence(tasks, fn) {
|
||||||
|
return tasks.reduce(
|
||||||
|
(promise, task) => promise.then(() => fn(task)),
|
||||||
|
Promise.resolve()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const parallel = function parallel(tasks, fn) {
|
||||||
|
return Promise.all(tasks.map(fn))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const chainFn = function chainFn(base, fn) {
|
||||||
|
/* istanbul ignore if */
|
||||||
|
if (typeof fn !== 'function') {
|
||||||
|
return base
|
||||||
|
}
|
||||||
|
return function () {
|
||||||
|
if (typeof base !== 'function') {
|
||||||
|
return fn.apply(this, arguments)
|
||||||
|
}
|
||||||
|
let baseResult = base.apply(this, arguments)
|
||||||
|
// Allow function to mutate the first argument instead of returning the result
|
||||||
|
if (baseResult === undefined) {
|
||||||
|
baseResult = arguments[0]
|
||||||
|
}
|
||||||
|
const fnResult = fn.call(
|
||||||
|
this,
|
||||||
|
baseResult,
|
||||||
|
...Array.prototype.slice.call(arguments, 1)
|
||||||
|
)
|
||||||
|
// Return mutated argument if no result was returned
|
||||||
|
if (fnResult === undefined) {
|
||||||
|
return baseResult
|
||||||
|
}
|
||||||
|
return fnResult
|
||||||
|
}
|
||||||
|
}
|
26
packages/utils/src/timer.js
Normal file
26
packages/utils/src/timer.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
async function promiseFinally(fn, finalFn) {
|
||||||
|
let result
|
||||||
|
try {
|
||||||
|
if (typeof fn === 'function') {
|
||||||
|
result = await fn()
|
||||||
|
} else {
|
||||||
|
result = await fn
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
finalFn()
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
export const timeout = function timeout(fn, ms, msg) {
|
||||||
|
let timerId
|
||||||
|
const warpPromise = promiseFinally(fn, () => clearTimeout(timerId))
|
||||||
|
const timerPromise = new Promise((resolve, reject) => {
|
||||||
|
timerId = setTimeout(() => reject(new Error(msg)), ms)
|
||||||
|
})
|
||||||
|
return Promise.race([warpPromise, timerPromise])
|
||||||
|
}
|
||||||
|
|
||||||
|
export const waitFor = function waitFor(ms) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms || 0))
|
||||||
|
}
|
@ -8,8 +8,8 @@
|
|||||||
],
|
],
|
||||||
"main": "dist/vue-renderer.js",
|
"main": "dist/vue-renderer.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/common": "2.3.4",
|
|
||||||
"@nuxt/devalue": "^1.2.0",
|
"@nuxt/devalue": "^1.2.0",
|
||||||
|
"@nuxt/utils": "2.3.4",
|
||||||
"consola": "^2.3.0",
|
"consola": "^2.3.0",
|
||||||
"fs-extra": "^7.0.1",
|
"fs-extra": "^7.0.1",
|
||||||
"lru-cache": "^5.1.1",
|
"lru-cache": "^5.1.1",
|
||||||
|
@ -5,7 +5,7 @@ import consola from 'consola'
|
|||||||
import devalue from '@nuxt/devalue'
|
import devalue from '@nuxt/devalue'
|
||||||
import invert from 'lodash/invert'
|
import invert from 'lodash/invert'
|
||||||
import template from 'lodash/template'
|
import template from 'lodash/template'
|
||||||
import { waitFor } from '@nuxt/common'
|
import { waitFor } from '@nuxt/utils'
|
||||||
import { createBundleRenderer } from 'vue-server-renderer'
|
import { createBundleRenderer } from 'vue-server-renderer'
|
||||||
|
|
||||||
import SPAMetaRenderer from './spa-meta'
|
import SPAMetaRenderer from './spa-meta'
|
||||||
|
@ -11,8 +11,8 @@
|
|||||||
"@babel/core": "^7.2.2",
|
"@babel/core": "^7.2.2",
|
||||||
"@babel/polyfill": "^7.2.3",
|
"@babel/polyfill": "^7.2.3",
|
||||||
"@nuxt/babel-preset-app": "2.3.4",
|
"@nuxt/babel-preset-app": "2.3.4",
|
||||||
"@nuxt/common": "2.3.4",
|
|
||||||
"@nuxt/friendly-errors-webpack-plugin": "^2.4.0",
|
"@nuxt/friendly-errors-webpack-plugin": "^2.4.0",
|
||||||
|
"@nuxt/utils": "2.3.4",
|
||||||
"babel-loader": "^8.0.4",
|
"babel-loader": "^8.0.4",
|
||||||
"cache-loader": "^1.2.5",
|
"cache-loader": "^1.2.5",
|
||||||
"caniuse-lite": "^1.0.30000923",
|
"caniuse-lite": "^1.0.30000923",
|
||||||
|
@ -11,7 +11,7 @@ import {
|
|||||||
parallel,
|
parallel,
|
||||||
sequence,
|
sequence,
|
||||||
wrapArray
|
wrapArray
|
||||||
} from '@nuxt/common'
|
} from '@nuxt/utils'
|
||||||
|
|
||||||
import { ClientConfig, ModernConfig, ServerConfig } from './config'
|
import { ClientConfig, ModernConfig, ServerConfig } from './config'
|
||||||
import PerfLoader from './utils/perf-loader'
|
import PerfLoader from './utils/perf-loader'
|
||||||
|
@ -11,7 +11,7 @@ import TerserWebpackPlugin from 'terser-webpack-plugin'
|
|||||||
import WebpackBar from 'webpackbar'
|
import WebpackBar from 'webpackbar'
|
||||||
import env from 'std-env'
|
import env from 'std-env'
|
||||||
|
|
||||||
import { isUrl, urlJoin } from '@nuxt/common'
|
import { isUrl, urlJoin } from '@nuxt/utils'
|
||||||
|
|
||||||
import PerfLoader from '../utils/perf-loader'
|
import PerfLoader from '../utils/perf-loader'
|
||||||
import StyleLoader from '../utils/style-loader'
|
import StyleLoader from '../utils/style-loader'
|
||||||
|
@ -5,7 +5,7 @@ import merge from 'lodash/merge'
|
|||||||
import cloneDeep from 'lodash/cloneDeep'
|
import cloneDeep from 'lodash/cloneDeep'
|
||||||
import createResolver from 'postcss-import-resolver'
|
import createResolver from 'postcss-import-resolver'
|
||||||
|
|
||||||
import { isPureObject } from '@nuxt/common'
|
import { isPureObject } from '@nuxt/utils'
|
||||||
|
|
||||||
export const orderPresets = {
|
export const orderPresets = {
|
||||||
cssnanoLast: (names) => {
|
cssnanoLast: (names) => {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import ExtractCssChunksPlugin from 'extract-css-chunks-webpack-plugin'
|
import ExtractCssChunksPlugin from 'extract-css-chunks-webpack-plugin'
|
||||||
|
|
||||||
import { wrapArray } from '@nuxt/common'
|
import { wrapArray } from '@nuxt/utils'
|
||||||
|
|
||||||
import PostcssConfig from './postcss'
|
import PostcssConfig from './postcss'
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { waitUntil } from '../utils'
|
import { waitUntil } from '../utils'
|
||||||
import * as Utils from '../../packages/common/src/index'
|
import * as Utils from '../../packages/utils/src/index'
|
||||||
|
|
||||||
describe('utils', () => {
|
describe('utils', () => {
|
||||||
test('encodeHtml', () => {
|
test('encodeHtml', () => {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import klawSync from 'klaw-sync'
|
import klawSync from 'klaw-sync'
|
||||||
import { waitFor } from '../../packages/common'
|
import { waitFor } from '../../packages/utils'
|
||||||
|
|
||||||
export { getNuxtConfig } from '../../packages/config'
|
export { getNuxtConfig } from '../../packages/config'
|
||||||
export { default as getPort } from 'get-port'
|
export { default as getPort } from 'get-port'
|
||||||
|
@ -8,7 +8,7 @@ export { Nuxt } from '../../packages/core/src/index'
|
|||||||
export { Builder } from '../../packages/builder/src/index'
|
export { Builder } from '../../packages/builder/src/index'
|
||||||
export { Generator } from '../../packages/generator/src/index'
|
export { Generator } from '../../packages/generator/src/index'
|
||||||
export { BundleBuilder } from '../../packages/webpack/src/index'
|
export { BundleBuilder } from '../../packages/webpack/src/index'
|
||||||
export * from '../../packages/common/src/index'
|
export * from '../../packages/utils/src/index'
|
||||||
|
|
||||||
export const loadFixture = async function (fixture, overrides) {
|
export const loadFixture = async function (fixture, overrides) {
|
||||||
const rootDir = path.resolve(__dirname, '..', 'fixtures', fixture)
|
const rootDir = path.resolve(__dirname, '..', 'fixtures', fixture)
|
||||||
|
Loading…
Reference in New Issue
Block a user