From ac6ca67534d9bfed5441b1c75ea09005fad0c7fe Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Sat, 5 May 2018 00:16:29 -0300 Subject: [PATCH] chore: use docz-core instead of docz-bundler --- packages/docz-bundler/src/Server.ts | 103 ------------------ packages/docz-bundler/src/index.ts | 3 - packages/{docz-bundler => docz-core}/librc.js | 0 .../{docz-bundler => docz-core}/package.json | 2 +- .../src/Bundler.ts | 6 +- .../src/Entries.ts | 6 +- .../{docz-bundler => docz-core}/src/Entry.ts | 0 .../{docz-bundler => docz-core}/src/Plugin.ts | 0 packages/docz-core/src/bundlers/index.ts | 1 + .../src/bundlers}/webpack/config.ts | 14 ++- .../src/bundlers}/webpack/devserver.ts | 4 +- .../src/bundlers}/webpack/index.ts | 8 +- .../src => docz-core/src/commands}/args.ts | 28 ++++- packages/docz-core/src/commands/dev.ts | 70 ++++++++++++ packages/docz-core/src/commands/index.ts | 2 + .../src/config/paths.ts | 2 +- packages/docz-core/src/index.ts | 4 + .../src/types.d.ts | 0 .../src/utils/format.ts | 0 .../src/utils/helpers.ts | 0 .../src/utils/traverse.ts | 0 .../templates/app.tpl.js | 0 .../templates/index.tpl.html | 0 .../templates/index.tpl.js | 0 .../{docz-bundler => docz-core}/tsconfig.json | 0 .../{docz-bundler => docz-core}/tslint.json | 0 packages/docz/bin/index.js | 9 +- 27 files changed, 133 insertions(+), 129 deletions(-) delete mode 100644 packages/docz-bundler/src/Server.ts delete mode 100644 packages/docz-bundler/src/index.ts rename packages/{docz-bundler => docz-core}/librc.js (100%) rename packages/{docz-bundler => docz-core}/package.json (98%) rename packages/{docz-bundler => docz-core}/src/Bundler.ts (93%) rename packages/{docz-bundler => docz-core}/src/Entries.ts (96%) rename packages/{docz-bundler => docz-core}/src/Entry.ts (100%) rename packages/{docz-bundler => docz-core}/src/Plugin.ts (100%) create mode 100644 packages/docz-core/src/bundlers/index.ts rename packages/{docz-bundler/src => docz-core/src/bundlers}/webpack/config.ts (93%) rename packages/{docz-bundler/src => docz-core/src/bundlers}/webpack/devserver.ts (88%) rename packages/{docz-bundler/src => docz-core/src/bundlers}/webpack/index.ts (69%) rename packages/{docz-bundler/src => docz-core/src/commands}/args.ts (69%) create mode 100644 packages/docz-core/src/commands/dev.ts create mode 100644 packages/docz-core/src/commands/index.ts rename packages/{docz-bundler => docz-core}/src/config/paths.ts (95%) create mode 100644 packages/docz-core/src/index.ts rename packages/{docz-bundler => docz-core}/src/types.d.ts (100%) rename packages/{docz-bundler => docz-core}/src/utils/format.ts (100%) rename packages/{docz-bundler => docz-core}/src/utils/helpers.ts (100%) rename packages/{docz-bundler => docz-core}/src/utils/traverse.ts (100%) rename packages/{docz-bundler => docz-core}/templates/app.tpl.js (100%) rename packages/{docz-bundler => docz-core}/templates/index.tpl.html (100%) rename packages/{docz-bundler => docz-core}/templates/index.tpl.js (100%) rename packages/{docz-bundler => docz-core}/tsconfig.json (100%) rename packages/{docz-bundler => docz-core}/tslint.json (100%) diff --git a/packages/docz-bundler/src/Server.ts b/packages/docz-bundler/src/Server.ts deleted file mode 100644 index 5d3605d..0000000 --- a/packages/docz-bundler/src/Server.ts +++ /dev/null @@ -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 { - 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() - } -} diff --git a/packages/docz-bundler/src/index.ts b/packages/docz-bundler/src/index.ts deleted file mode 100644 index 86859f5..0000000 --- a/packages/docz-bundler/src/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { createArgs } from './args' -export { Server } from './Server' -export { createPlugin } from './Plugin' diff --git a/packages/docz-bundler/librc.js b/packages/docz-core/librc.js similarity index 100% rename from packages/docz-bundler/librc.js rename to packages/docz-core/librc.js diff --git a/packages/docz-bundler/package.json b/packages/docz-core/package.json similarity index 98% rename from packages/docz-bundler/package.json rename to packages/docz-core/package.json index e406f4a..c82ef21 100644 --- a/packages/docz-bundler/package.json +++ b/packages/docz-core/package.json @@ -1,5 +1,5 @@ { - "name": "docz-bundler", + "name": "docz-core", "version": "0.0.1", "main": "dist/index.js", "umd:main": "dist/index.umd.js", diff --git a/packages/docz-bundler/src/Bundler.ts b/packages/docz-core/src/Bundler.ts similarity index 93% rename from packages/docz-bundler/src/Bundler.ts rename to packages/docz-core/src/Bundler.ts index cb22a43..4461fd8 100644 --- a/packages/docz-bundler/src/Bundler.ts +++ b/packages/docz-core/src/Bundler.ts @@ -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 export type ServerFn = (config: C) => BundlerServer export interface BundlerConstructor { - args: ConfigArgs + args: Config config: ConfigFn server: ServerFn } export class Bundler { - private readonly args: ConfigArgs + private readonly args: Config private config: ConfigFn private server: ServerFn diff --git a/packages/docz-bundler/src/Entries.ts b/packages/docz-core/src/Entries.ts similarity index 96% rename from packages/docz-bundler/src/Entries.ts rename to packages/docz-core/src/Entries.ts index a671d19..8e41e87 100644 --- a/packages/docz-bundler/src/Entries.ts +++ b/packages/docz-core/src/Entries.ts @@ -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' diff --git a/packages/docz-bundler/src/Entry.ts b/packages/docz-core/src/Entry.ts similarity index 100% rename from packages/docz-bundler/src/Entry.ts rename to packages/docz-core/src/Entry.ts diff --git a/packages/docz-bundler/src/Plugin.ts b/packages/docz-core/src/Plugin.ts similarity index 100% rename from packages/docz-bundler/src/Plugin.ts rename to packages/docz-core/src/Plugin.ts diff --git a/packages/docz-core/src/bundlers/index.ts b/packages/docz-core/src/bundlers/index.ts new file mode 100644 index 0000000..9c8a2bd --- /dev/null +++ b/packages/docz-core/src/bundlers/index.ts @@ -0,0 +1 @@ +export { bundler as webpack } from './webpack' diff --git a/packages/docz-bundler/src/webpack/config.ts b/packages/docz-core/src/bundlers/webpack/config.ts similarity index 93% rename from packages/docz-bundler/src/webpack/config.ts rename to packages/docz-core/src/bundlers/webpack/config.ts index bb6d2c6..9cada55 100644 --- a/packages/docz-bundler/src/webpack/config.ts +++ b/packages/docz-core/src/bundlers/webpack/config.ts @@ -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') diff --git a/packages/docz-bundler/src/webpack/devserver.ts b/packages/docz-core/src/bundlers/webpack/devserver.ts similarity index 88% rename from packages/docz-bundler/src/webpack/devserver.ts rename to packages/docz-core/src/bundlers/webpack/devserver.ts index 94ce501..c0f1038 100644 --- a/packages/docz-bundler/src/webpack/devserver.ts +++ b/packages/docz-core/src/bundlers/webpack/devserver.ts @@ -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, diff --git a/packages/docz-bundler/src/webpack/index.ts b/packages/docz-core/src/bundlers/webpack/index.ts similarity index 69% rename from packages/docz-bundler/src/webpack/index.ts rename to packages/docz-core/src/bundlers/webpack/index.ts index 01fc8ea..f2e3931 100644 --- a/packages/docz-bundler/src/webpack/index.ts +++ b/packages/docz-core/src/bundlers/webpack/index.ts @@ -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 => +export const bundler = (args: Config): Bundler => new Bundler({ args, config: config(args), diff --git a/packages/docz-bundler/src/args.ts b/packages/docz-core/src/commands/args.ts similarity index 69% rename from packages/docz-bundler/src/args.ts rename to packages/docz-core/src/commands/args.ts index 66a273f..15d7138 100644 --- a/packages/docz-bundler/src/args.ts +++ b/packages/docz-core/src/commands/args.ts @@ -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', diff --git a/packages/docz-core/src/commands/dev.ts b/packages/docz-core/src/commands/dev.ts new file mode 100644 index 0000000..0b8ed22 --- /dev/null +++ b/packages/docz-core/src/commands/dev.ts @@ -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 => { + 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) +} diff --git a/packages/docz-core/src/commands/index.ts b/packages/docz-core/src/commands/index.ts new file mode 100644 index 0000000..76dfd49 --- /dev/null +++ b/packages/docz-core/src/commands/index.ts @@ -0,0 +1,2 @@ +export { dev } from './dev' +export { args } from './args' diff --git a/packages/docz-bundler/src/config/paths.ts b/packages/docz-core/src/config/paths.ts similarity index 95% rename from packages/docz-bundler/src/config/paths.ts rename to packages/docz-core/src/config/paths.ts index 6d48efc..6ee0505 100644 --- a/packages/docz-bundler/src/config/paths.ts +++ b/packages/docz-core/src/config/paths.ts @@ -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') diff --git a/packages/docz-core/src/index.ts b/packages/docz-core/src/index.ts new file mode 100644 index 0000000..08c9b8f --- /dev/null +++ b/packages/docz-core/src/index.ts @@ -0,0 +1,4 @@ +import * as commands from './commands' + +export { commands } +export { createPlugin } from './Plugin' diff --git a/packages/docz-bundler/src/types.d.ts b/packages/docz-core/src/types.d.ts similarity index 100% rename from packages/docz-bundler/src/types.d.ts rename to packages/docz-core/src/types.d.ts diff --git a/packages/docz-bundler/src/utils/format.ts b/packages/docz-core/src/utils/format.ts similarity index 100% rename from packages/docz-bundler/src/utils/format.ts rename to packages/docz-core/src/utils/format.ts diff --git a/packages/docz-bundler/src/utils/helpers.ts b/packages/docz-core/src/utils/helpers.ts similarity index 100% rename from packages/docz-bundler/src/utils/helpers.ts rename to packages/docz-core/src/utils/helpers.ts diff --git a/packages/docz-bundler/src/utils/traverse.ts b/packages/docz-core/src/utils/traverse.ts similarity index 100% rename from packages/docz-bundler/src/utils/traverse.ts rename to packages/docz-core/src/utils/traverse.ts diff --git a/packages/docz-bundler/templates/app.tpl.js b/packages/docz-core/templates/app.tpl.js similarity index 100% rename from packages/docz-bundler/templates/app.tpl.js rename to packages/docz-core/templates/app.tpl.js diff --git a/packages/docz-bundler/templates/index.tpl.html b/packages/docz-core/templates/index.tpl.html similarity index 100% rename from packages/docz-bundler/templates/index.tpl.html rename to packages/docz-core/templates/index.tpl.html diff --git a/packages/docz-bundler/templates/index.tpl.js b/packages/docz-core/templates/index.tpl.js similarity index 100% rename from packages/docz-bundler/templates/index.tpl.js rename to packages/docz-core/templates/index.tpl.js diff --git a/packages/docz-bundler/tsconfig.json b/packages/docz-core/tsconfig.json similarity index 100% rename from packages/docz-bundler/tsconfig.json rename to packages/docz-core/tsconfig.json diff --git a/packages/docz-bundler/tslint.json b/packages/docz-core/tslint.json similarity index 100% rename from packages/docz-bundler/tslint.json rename to packages/docz-core/tslint.json diff --git a/packages/docz/bin/index.js b/packages/docz/bin/index.js index e06589f..3c09501 100755 --- a/packages/docz/bin/index.js +++ b/packages/docz/bin/index.js @@ -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()