Skip to content

Commit

Permalink
refactror: separate lib
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 4, 2020
1 parent d8e8d29 commit 6cc53a4
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 331 deletions.
21 changes: 3 additions & 18 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,18 @@ import { resolve } from 'path'
import consola from 'consola'
import { rollup, OutputOptions } from 'rollup'
import Hookable from 'hookable'
import defu from 'defu'
import prettyBytes from 'pretty-bytes'
import gzipSize from 'gzip-size'
import chalk from 'chalk'
import { copy, emptyDir, existsSync } from 'fs-extra'
import { getRollupConfig } from './rollup/config'
import { tryImport, hl, prettyPath, renderTemplate, compileTemplateToJS } from './utils'
import { getTargetConfig } from './config'
import { hl, prettyPath, renderTemplate, compileTemplateToJS } from './utils'

export async function build (baseConfig, target) {
consola.info(`Generating bundle for ${hl(target.target)}`)

const _targetDefaults = tryImport(__dirname, `./targets/${target.target}`) ||
tryImport(baseConfig.rootDir, target.target)
if (!_targetDefaults) {
throw new Error('Cannot resolve target: ' + target.target)
}

const config: any = defu(
// Target specific config by user
target,
// Global user config
baseConfig,
// Target defaults
_targetDefaults,
// Generic defaults
{ outDir: resolve(baseConfig.buildDir, `dist/${target.target}`), outName: 'index.js' }
)
const config: any = getTargetConfig(baseConfig, target)

const hooks = new Hookable()
hooks.addHooks(config.hooks)
Expand Down
10 changes: 1 addition & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { resolve } from 'path'
import consola from 'consola'
import { build, compileHTMLTemplate, ensureDist } from './build'
import { getBaseConfig } from './config'

async function _runCLI () {
export async function runCLI () {
const rootDir = resolve(process.cwd(), process.argv[2] || '.')

// Config
Expand All @@ -23,10 +22,3 @@ async function _runCLI () {
await build(baseConfig, target)
}
}

export function runCLI () {
_runCLI().catch((err) => {
consola.error(err)
process.exit(1)
})
}
34 changes: 31 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { resolve } from 'path'
import { tryImport } from './utils'
import defu from 'defu'
import { tryImport, LIB_DIR } from './utils'

export function getBaseConfig (rootDir) {
const baseConfig = {
let baseConfig = {
rootDir,
buildDir: '',
targets: [],
Expand All @@ -14,7 +15,13 @@ export function getBaseConfig (rootDir) {
logStartup: true
}

Object.assign(baseConfig, tryImport(rootDir, './nuxt.config')!.serverless)
const nuxtConfig = tryImport(rootDir, './nuxt.config')
if (!nuxtConfig) {
throw new Error('`nuxt.config` file not found in: ' + rootDir)
}
if (nuxtConfig.serverless) {
baseConfig = defu(nuxtConfig.serverless, baseConfig)
}

baseConfig.buildDir = resolve(baseConfig.rootDir, baseConfig.buildDir || '.nuxt')

Expand All @@ -25,3 +32,24 @@ export function getBaseConfig (rootDir) {

return baseConfig
}

export function getTargetConfig (baseConfig, target) {
const _targetDefaults = tryImport(LIB_DIR, `./targets/${target.target}`) ||
tryImport(baseConfig.rootDir, target.target)
if (!_targetDefaults) {
throw new Error('Cannot resolve target: ' + target.target)
}

// TODO: Merge hooks

return defu(
// Target specific config by user
target,
// Global user config
baseConfig,
// Target defaults
_targetDefaults,
// Generic defaults
{ outDir: resolve(baseConfig.buildDir, `dist/${target.target}`), outName: 'index.js' }
)
}
6 changes: 6 additions & 0 deletions src/nuxt-serverless.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

require('../dist').runCLI().catch((error) => {
const consola = require('consola')
consola.error(error)
process.exit(1)
})
16 changes: 4 additions & 12 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import alias from '@rollup/plugin-alias'
import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'
import analyze from 'rollup-plugin-analyzer'
import ts from 'rollup-plugin-ts'
import { RUNTIME_DIR } from '../utils'
import dynamicRequire from './dynamic-require'

export type RollupConfig = InputOptions & { output: OutputOptions }
Expand Down Expand Up @@ -83,22 +83,14 @@ export const getRollupConfig = (config) => {
const renderer = config.renderer || (config.nuxt === 2 ? 'vue2' : 'vue3')
options.plugins.push(alias({
entries: {
'~runtime': path.resolve(__dirname, '../runtime'),
'~renderer': require.resolve('../runtime/' + renderer),
'~runtime': RUNTIME_DIR,
'~renderer': require.resolve(path.resolve(RUNTIME_DIR, renderer)),
'~build': config.buildDir,
'~mock': require.resolve('../runtime/mock'),
'~mock': require.resolve(path.resolve(RUNTIME_DIR, 'mock')),
...mocks.reduce((p, c) => ({ ...p, [c]: '~mock' }), {})
}
}))

// https://github.com/wessberg/rollup-plugin-ts
options.plugins.push(ts({
transpileOnly: true,
transpiler: 'babel',
include: ['**/*.ts'],
exclude: ['*.json', 'node_modules']
}))

// https://github.com/rollup/plugins/tree/master/packages/node-resolve
options.plugins.push(resolve({
extensions,
Expand Down
25 changes: 0 additions & 25 deletions src/runtime/mock.js

This file was deleted.

55 changes: 0 additions & 55 deletions src/runtime/server.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/runtime/vue2.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/runtime/vue3.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/targets/cloudflare/entry.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/targets/cloudflare/index.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/targets/node/entry.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/targets/node/index.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/targets/sw/entry.ts

This file was deleted.

42 changes: 0 additions & 42 deletions src/targets/sw/index.html

This file was deleted.

Loading

0 comments on commit 6cc53a4

Please sign in to comment.