chore: use docz-core instead of docz-bundler

This commit is contained in:
Pedro Nauck
2018-05-05 00:16:29 -03:00
parent f25c5271e3
commit ac6ca67534
27 changed files with 133 additions and 129 deletions

View File

@@ -1,103 +0,0 @@
import { load } from 'load-cfg'
import { FSWatcher } from 'chokidar'
import * as chokidar from 'chokidar'
import * as del from 'del'
import * as paths from './config/paths'
import { Entry } from './Entry'
import { Entries } from './Entries'
import { Bundler } from './Bundler'
import { Plugin } from './Plugin'
import { bundler } from './webpack'
process.env.BABEL_ENV = process.env.BABEL_ENV || 'development'
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
export interface Argv {
/* io args */
src: string
files: string
/* template args */
title: string
description: string
theme: string
/* bundler args */
env: string
debug: boolean
protocol: string
host: string
port: number
}
export interface ConfigArgs extends Argv {
paths: paths.Paths
plugins?: Plugin[]
mdxOptions?: {
mdPlugins: any[]
hastPlugins: any[]
}
}
const initialConfigArgs = {
paths,
plugins: [],
mdxOptions: {
mdPlugins: [],
hastPlugins: [],
},
}
export class Server {
private readonly config: ConfigArgs
private readonly watcher: FSWatcher
private readonly bundler: Bundler
constructor(args: ConfigArgs) {
const config = load('docz', { ...args, ...initialConfigArgs })
const ignoreWatch = {
ignored: /(^|[\/\\])\../,
}
this.config = config
this.watcher = chokidar.watch(config.files, ignoreWatch)
this.bundler = bundler(config)
}
public async start(): Promise<void> {
del.sync(paths.docz)
this.processEntries(this.config)
const config = this.bundler.getConfig()
const server = await this.bundler.createServer(config)
server.start()
}
private processEntries(config: ConfigArgs): void {
const entries = new Entries(config)
const update = () => entries.write()
const onUnlink = (file: string) => {
entries.remove(file)
update()
}
const onChange = (file: string) => {
const name = Entry.parseName(file)
const entry = entries.find(file)
if (name) {
!entry && entries.add(new Entry(file, config.src))
entry && entries.update(file)
update()
}
}
this.watcher.on('unlink', onUnlink)
this.watcher.on('change', onChange)
update()
}
}

View File

@@ -1,3 +0,0 @@
export { createArgs } from './args'
export { Server } from './Server'
export { createPlugin } from './Plugin'

View File

@@ -1,5 +1,5 @@
{
"name": "docz-bundler",
"name": "docz-core",
"version": "0.0.1",
"main": "dist/index.js",
"umd:main": "dist/index.umd.js",

View File

@@ -1,5 +1,5 @@
import { Plugin } from './Plugin'
import { ConfigArgs } from './Server'
import { Config } from './commands/args'
export interface BundlerServer {
start(): any
@@ -9,13 +9,13 @@ export type ConfigFn<C> = () => C
export type ServerFn<C> = (config: C) => BundlerServer
export interface BundlerConstructor<C> {
args: ConfigArgs
args: Config
config: ConfigFn<C>
server: ServerFn<C>
}
export class Bundler<C = any> {
private readonly args: ConfigArgs
private readonly args: Config
private config: ConfigFn<C>
private server: ServerFn<C>

View File

@@ -10,7 +10,7 @@ import { propOf } from './utils/helpers'
import { format } from './utils/format'
import { Entry } from './Entry'
import { ConfigArgs } from './Server'
import { Config } from './commands/args'
const mkd = (dir: string): void => {
try {
@@ -36,10 +36,10 @@ const html = compiled('index.tpl.html')
export class Entries {
public files: string[]
public config: ConfigArgs
public config: Config
public entries: Entry[]
constructor(config: ConfigArgs) {
constructor(config: Config) {
const { files: pattern } = config
const ignoreGlob = '!node_modules'

View File

@@ -0,0 +1 @@
export { bundler as webpack } from './webpack'

View File

@@ -8,13 +8,14 @@ import Config from 'webpack-chain'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import friendlyErrors from 'friendly-errors-webpack-plugin'
import { ConfigArgs } from '../Server'
import { Config as ConfigObj } from '../../commands/args'
const INLINE_LIMIT = 10000
export const createConfig = (args: ConfigArgs) => (): Configuration => {
export const createConfig = (args: ConfigObj) => (): Configuration => {
const { paths, env, debug } = args
const srcPath = path.resolve(paths.root, args.src)
const isProd = env === 'production'
const config = new Config()
@@ -107,7 +108,7 @@ export const createConfig = (args: ConfigArgs) => (): Configuration => {
config.module
.rule('js')
.test(/\.js?x$/)
.include.add(paths.root)
.include.add(srcPath)
.add(paths.docz)
.end()
.exclude.add(/node_modules/)
@@ -122,7 +123,7 @@ export const createConfig = (args: ConfigArgs) => (): Configuration => {
config.module
.rule('mdx')
.test(/\.mdx$/)
.include.add(paths.root)
.include.add(srcPath)
.add(paths.docz)
.end()
.exclude.add(/node_modules/)
@@ -133,7 +134,10 @@ export const createConfig = (args: ConfigArgs) => (): Configuration => {
.end()
.use('@mdx-js/loader')
.loader(require.resolve('@mdx-js/loader'))
.options(args.mdxOptions || {})
.options({
mdPlugins: args.mdPlugins,
hastPlugins: args.hastPlugins,
})
config.module
.rule('images')

View File

@@ -1,6 +1,6 @@
import { ConfigArgs as Args } from '../Server'
import { Config } from '../../commands/args'
export const devServerConfig = ({ paths, host, debug, protocol }: Args) => ({
export const devServerConfig = ({ paths, host, debug, protocol }: Config) => ({
host,
clientLogLevel: !debug ? 'none' : 'error',
compress: true,

View File

@@ -4,10 +4,10 @@ import webpack from 'webpack'
import { devServerConfig } from './devserver'
import { createConfig as config } from './config'
import { Bundler, BundlerServer } from '../Bundler'
import { ConfigArgs as Args } from '../Server'
import { Bundler, BundlerServer } from '../../Bundler'
import { Config } from '../../commands/args'
export const server = (args: Args) => (config: CFG): BundlerServer => {
export const server = (args: Config) => (config: CFG): BundlerServer => {
const compiler = webpack(config)
const devserver = devServerConfig(args)
const server = new WebpackDevServer(compiler, devserver)
@@ -17,7 +17,7 @@ export const server = (args: Args) => (config: CFG): BundlerServer => {
}
}
export const bundler = (args: Args): Bundler<CFG> =>
export const bundler = (args: Config): Bundler<CFG> =>
new Bundler({
args,
config: config(args),

View File

@@ -1,3 +1,29 @@
import { Paths } from '../config/paths'
import { Plugin } from '../Plugin'
export interface Argv {
/* io args */
src: string
files: string
/* template args */
title: string
description: string
theme: string
/* bundler args */
env: string
debug: boolean
protocol: string
host: string
port: number
}
export interface Config extends Argv {
paths: Paths
plugins?: Plugin[]
mdPlugins: any[]
hastPlugins: any[]
}
const EXTS = '{j,t}{s,sx}'
const DEFAULT_FILES_GLOB = [
`docs/**.${EXTS}`,
@@ -5,7 +31,7 @@ const DEFAULT_FILES_GLOB = [
`**/*.doc.${EXTS}`,
]
export const createArgs = (yargs: any) => {
export const args = (yargs: any) => {
yargs.positional('source', {
alias: 'src',
type: 'string',

View File

@@ -0,0 +1,70 @@
import { load } from 'load-cfg'
import { FSWatcher } from 'chokidar'
import * as chokidar from 'chokidar'
import del from 'del'
import * as paths from '../config/paths'
import { Entry } from '../Entry'
import { Entries } from '../Entries'
import { Bundler } from '../Bundler'
import { webpack } from '../bundlers'
import { Config } from './args'
process.env.BABEL_ENV = process.env.BABEL_ENV || 'development'
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const writeEntriesAndWatch = (watcher: FSWatcher) => (config: Config) => {
const entries = new Entries(config)
const update = () => entries.write()
const onUnlink = (file: string) => {
entries.remove(file)
update()
}
const onChange = (file: string) => {
const name = Entry.parseName(file)
const entry = entries.find(file)
if (name) {
!entry && entries.add(new Entry(file, config.src))
entry && entries.update(file)
update()
}
}
watcher.on('unlink', onUnlink)
watcher.on('change', onChange)
update()
}
const start = async (bundler: Bundler): Promise<void> => {
const config = bundler.getConfig()
const server = await bundler.createServer(config)
server.start()
}
const INITIAL_CONFIG = {
paths,
plugins: [],
mdPlugins: [],
hastPlugins: [],
}
export const dev = async (args: Config) => {
const config = load('docz', { ...args, ...INITIAL_CONFIG })
const bundler = webpack(config)
const watcher = chokidar.watch(config.files, {
ignored: /(^|[\/\\])\../,
})
const writeEntries = writeEntriesAndWatch(watcher)
await del(paths.docz)
writeEntries(config)
start(bundler)
}

View File

@@ -0,0 +1,2 @@
export { dev } from './dev'
export { args } from './args'

View File

@@ -43,7 +43,7 @@ export interface Paths {
indexHtml: string
}
export const templates = path.join(resolve.sync('docz-bundler'), '../templates')
export const templates = path.join(resolve.sync('docz-core'), '../templates')
export const docz = resolveApp('.docz')
export const packageJson = resolveApp('package.json')

View File

@@ -0,0 +1,4 @@
import * as commands from './commands'
export { commands }
export { createPlugin } from './Plugin'

View File

@@ -1,11 +1,14 @@
#!/usr/bin/env node
const yargs = require('yargs')
const { Server, createArgs } = require('docz-bundler')
const { commands } = require('docz-core')
yargs
.command('dev [files]', 'initialize docz dev server', createArgs, argv =>
new Server(argv).start()
.command(
'dev [files]',
'initialize docz dev server',
commands.args,
commands.dev
)
.demandCommand()
.help()