Skip to content

Commit

Permalink
fix: rollup build
Browse files Browse the repository at this point in the history
fix: consola tags
  • Loading branch information
pimlie committed Jan 13, 2019
1 parent 4fabca5 commit f17a668
Show file tree
Hide file tree
Showing 22 changed files with 223 additions and 124 deletions.
47 changes: 20 additions & 27 deletions build/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,41 @@
import { resolve } from 'path'
import rollupBabel from 'rollup-plugin-babel'
import commonJS from 'rollup-plugin-commonjs'
import nodeResolve from 'rollup-plugin-node-resolve'
import license from 'rollup-plugin-license'

import packageJson from '../package.json'
import pck from '../package.json'
import nuxt from '../node_modules/nuxt/package.json'

const dependencies = [
'os',
'cluster',
...Object.keys(packageJson.dependencies),
...Object.keys(require('../node_modules/nuxt/package.json').dependencies)
'fs',
'os',
'path',
'util',
...Object.keys(pck.dependencies),
...Object.keys(nuxt.dependencies)
]

const version = packageJson.version || process.env.VERSION

// -----------------------------
// Banner
// -----------------------------
const banner =
'/*!\n' +
' * Nuxt-Generate-Cluster v' + version + '\n' +
' * Released under the MIT License.\n' +
' */'

const plugins = []
Object.keys(nuxt.dependencies).forEach((nuxtPkg) => {
const pck = require(`../node_modules/${nuxtPkg}/package.json`)
Array.prototype.push.apply(dependencies, Object.keys(pck.dependencies).filter(p => !dependencies.includes(p)))
})

// -----------------------------
// Aliases
// -----------------------------
const rootDir = resolve(__dirname, '..')

export default {
export default [{
input: resolve(rootDir, 'lib', 'index.js'),
output: {
name: 'Nuxt Generate Cluster',
file: resolve(rootDir, 'dist', 'generator.js'),
format: 'cjs',
preferConst: true,
sourcemap: true
},
external: dependencies,
plugins: [
nodeResolve({ preferBuiltins: true }),
commonJS(),
license({ banner })
].concat(plugins)
}
nodeResolve({
only: [/lodash/]
}),
commonJS()
]
}]
4 changes: 4 additions & 0 deletions lib/async/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default class Worker extends Messaging(GenerateWorker) {
this.setId(id)

this.hook(Commands.sendRoutes, this.generateRoutes.bind(this))
}

async init() {
await super.init()

this.generator.nuxt.hook('generate:routeCreated', ({ route, path }) => {
path = path.replace(this.generator.distPath, '')
Expand Down
4 changes: 3 additions & 1 deletion lib/cluster/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default class Master extends GenerateMaster {
cluster.on('fork', this.onFork.bind(this))
cluster.on('exit', this.onExit.bind(this))

global._ngc_log_tag = 'master'

messaging.on(Commands.sendRoutes, (data, senderId, worker) => {
this.sendRoutes(senderId, worker)
})
Expand All @@ -39,7 +41,7 @@ export default class Master extends GenerateMaster {
const routes = this.getBatchRoutes()

if (!routes.length) {
consola.cluster(`no more routes, exiting worker ${worker.id}`)
consola.master(`no more routes, exiting worker ${worker.id}`)

worker.disconnect()
} else {
Expand Down
23 changes: 13 additions & 10 deletions lib/cluster/worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cluster from 'cluster'
import chalk from 'chalk'
import { consola, messaging } from '../utils'
import { Worker as GenerateWorker, Commands } from '../generate'

Expand All @@ -11,7 +10,17 @@ export default class Worker extends GenerateWorker {
this.setId(cluster.worker.id)
}

consola.tag = chalk.green(`${this.id}`)
global._ngc_log_tag = `worker ${this.id}`

messaging.alias = `worker ${this.id}`
messaging.on(Commands.sendRoutes, (data) => {
/* istanbul ignore next */
this.generateRoutes(data)
})
}

async init() {
await super.init()

let renderingStartTime
/* istanbul ignore next */
Expand All @@ -27,7 +36,7 @@ export default class Worker extends GenerateWorker {

this.generator.nuxt.hook('generate:routeCreated', ({ route, path, errors }) => {
let durationMessage = ''
if (consola._level > 3) {
if (consola.level > 3) {
const taken = process.hrtime(renderingStartTime)
const duration = Math.round((taken[0] * 1e9 + taken[1]) / 1e6)
durationMessage += ` (${duration}ms)`
Expand All @@ -37,15 +46,9 @@ export default class Worker extends GenerateWorker {
if (errors.length) {
consola.error(`error generating: ${path}` + durationMessage)
} else {
consola.worker(`generated: ${path}` + durationMessage)
consola.success(`generated: ${path}` + durationMessage)
}
})

messaging.alias = `worker ${this.id}`
messaging.on(Commands.sendRoutes, (data) => {
/* istanbul ignore next */
this.generateRoutes(data)
})
}

async run() {
Expand Down
28 changes: 19 additions & 9 deletions lib/generate/master.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uniq from 'lodash/uniq'
import { Hookable } from '../mixins'
import { consola } from '../utils'
import { Nuxt, Builder, BundleBuilder, Generator } from '../utils/nuxt'
import { getNuxt, getGenerator } from '../utils/nuxt'
import Watchdog from './watchdog'

export default class Master extends Hookable() {
Expand All @@ -13,23 +13,33 @@ export default class Master extends Hookable() {
this.watchdog = new Watchdog()
this.startTime = process.hrtime()

const nuxt = new Nuxt(options)
const builder = new Builder(nuxt, BundleBuilder)
this.generator = new Generator(nuxt, builder)

this.workerCount = parseInt(workerCount) || parseInt(nuxt.options.generate.workers) || require('os').cpus().length
this.workerConcurrency = parseInt(workerConcurrency) || parseInt(nuxt.options.generate.workerConcurrency) || 500
this.workerCount = parseInt(workerCount)
this.workerConcurrency = parseInt(workerConcurrency)

if (adjustLogLevel) {
consola._level = Math.max(0, Math.min(consola._maxLevel, consola._defaultLevel + adjustLogLevel))
this.options.__workerLogLevel = consola._level
consola.level = Math.max(0, Math.min(consola._maxLevel, consola._defaultLevel + adjustLogLevel))
this.options.__workerLogLevel = consola.level
}

this.routes = []
this.errors = []
}

async init() {
if (this.generator) {
return
}

const nuxt = await getNuxt(this.options)
this.generator = await getGenerator(nuxt)

this.workerCount = this.workerCount || parseInt(nuxt.options.generate.workers) || require('os').cpus().length
this.workerConcurrency = this.workerConcurrency || parseInt(nuxt.options.generate.workerConcurrency) || 500
}

async run({ build, params } = {}) {
await this.init()

if (build) {
await this.build()
await this.callHook('built', params)
Expand Down
19 changes: 14 additions & 5 deletions lib/generate/worker.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import { consola } from '../utils'
import { Hookable } from '../mixins'
import { Nuxt, Generator } from '../utils/nuxt'
import { getNuxt, getGenerator } from '../utils/nuxt'

export default class Worker extends Hookable() {
constructor(options) {
super()
this.options = options
this.id = -1

const nuxt = new Nuxt(this.options)
this.generator = new Generator(nuxt)

if (this.options.__workerLogLevel) {
consola._level = Math.max(0, Math.min(consola._maxLevel, this.options.__workerLogLevel))
consola.level = Math.max(0, Math.min(consola._maxLevel, this.options.__workerLogLevel))
}
}

setId(id) {
this.id = id
}

async init() {
/* istanbul ignore next */
if (this.generator) {
return
}

const nuxt = await getNuxt(this.options)
this.generator = await getGenerator(nuxt)
}

async run() {
await this.init()

await this.generator.initiate({ build: false, init: false })
}

Expand Down
17 changes: 8 additions & 9 deletions lib/utils/consola.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import env from 'std-env'
// import figures from 'figures'
import figures from 'figures'
import chalk from 'chalk'
import { Consola, Types } from 'consola/dist/consola.cjs.js'
import { BasicReporter, FancyReporter } from './reporters'

Expand All @@ -16,15 +17,13 @@ if (!consola) {
types: Object.assign(Types, {
cluster: {
level: 4,
color: 'blue'
color: 'blue',
icon: chalk.magenta(figures.radioOn)
},
master: {
level: 2,
color: 'blue'
},
worker: {
level: 3,
color: 'green'
color: 'blue',
icon: chalk.cyan(figures.info)
},
debug: {
level: 5,
Expand All @@ -37,10 +36,10 @@ if (!consola) {
})
})

consola._defaultLevel = consola._level
consola._defaultLevel = consola.level
consola._maxLevel = 6

if (env.minimalCLI) {
if (env.ci || env.test) {
/* istanbul ignore next */
consola.add(new BasicReporter())
} else {
Expand Down
3 changes: 0 additions & 3 deletions lib/utils/nuxt.js

This file was deleted.

33 changes: 33 additions & 0 deletions lib/utils/nuxt/imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from 'path'
import consola from 'consola'

const localNodeModules = path.resolve(process.cwd(), 'node_modules')

// Prefer importing modules from local node_modules (for NPX and global bin)
async function _import(modulePath) {
let m
for (const mp of [ path.resolve(localNodeModules, modulePath), modulePath ]) {
try {
m = await import(mp)
} catch (e) {
/* istanbul ignore next */
if (e.code !== 'MODULE_NOT_FOUND') {
throw e
} else if (mp === modulePath) {
consola.fatal(
`Module ${modulePath} not found.\n\n`,
`Please install missing dependency:\n\n`,
`Using npm: npm i ${modulePath}\n\n`,
`Using yarn: yarn add ${modulePath}`
)
}
}
}
return m
}

export const builder = () => _import('@nuxt/builder')
export const webpack = () => _import('@nuxt/webpack')
export const generator = () => _import('@nuxt/generator')
export const core = () => _import('@nuxt/core')
export const importModule = _import
20 changes: 20 additions & 0 deletions lib/utils/nuxt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as imports from './imports'

export const getNuxt = async function getNuxt(options) {
const { Nuxt } = await imports.core()
const nuxt = new Nuxt(options)
await nuxt.ready()
return nuxt
}

export const getBuilder = async function getBuilder(nuxt) {
const { Builder } = await imports.builder()
const { BundleBuilder } = await imports.webpack()
return new Builder(nuxt, BundleBuilder)
}

export const getGenerator = async function getGenerator(nuxt) {
const { Generator } = await imports.generator()
const builder = await getBuilder(nuxt)
return new Generator(nuxt, builder)
}
13 changes: 9 additions & 4 deletions lib/utils/reporters/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class Reporter extends BasicReporter {
/* istanbul ignore next */
messaging.on('consola', ({ logObj, stream }) => {
logObj.date = new Date(logObj.date)

this.superLog(logObj, {
async: stream.async,
stdout: process.stdout,
Expand All @@ -18,16 +19,20 @@ export default class Reporter extends BasicReporter {
}

superLog(logObj, stream) {
/* istanbul ignore next */
super.log(logObj, stream)
}

log(logObj, { async } = {}) {
if (logObj.type === 'success' && logObj.args[0].startsWith('Generated ')) {
// Ignore success messages from Nuxt.Generator::generateRoute
return
}

/* istanbul ignore next */
super.log(logObj, stream)
}
if (global._ngc_log_tag) {
logObj.tag = global._ngc_log_tag
}

log(logObj, { async } = {}) {
messaging.send(null, 'consola', { logObj, stream: { async } })
}
}

0 comments on commit f17a668

Please sign in to comment.