chore(docz-core): decoupling bundler logic (#144)

* chore: update dependencies

* chore(docz-core): decoupling bundler logic

* chore: update dependencies again
This commit is contained in:
Pedro Nauck
2018-07-16 15:50:29 -03:00
committed by GitHub
parent 63b1011d29
commit 381ec297d7
24 changed files with 831 additions and 644 deletions

View File

@@ -9,14 +9,14 @@
"dependencies": {
"docz": "^0.5.8",
"docz-core": "^0.5.7",
"emotion": "^9.2.5",
"emotion": "^9.2.6",
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-emotion": "^9.2.5"
"react-emotion": "^9.2.6"
},
"devDependencies": {
"babel-plugin-emotion": "^9.2.5",
"babel-plugin-emotion": "^9.2.6",
"docz-plugin-babel6": "^0.5.7"
}
}

View File

@@ -9,13 +9,13 @@
"dependencies": {
"docz": "^0.5.8",
"docz-core": "^0.5.7",
"emotion": "^9.2.5",
"emotion": "^9.2.6",
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-emotion": "^9.2.5"
"react-emotion": "^9.2.6"
},
"devDependencies": {
"babel-plugin-emotion": "^9.2.5"
"babel-plugin-emotion": "^9.2.6"
}
}

View File

@@ -7,16 +7,16 @@
"build": "docz build"
},
"dependencies": {
"@babel/preset-flow": "^7.0.0-beta.52",
"@babel/preset-flow": "^7.0.0-beta.54",
"docz": "^0.5.8",
"emotion": "^9.2.5",
"emotion": "^9.2.6",
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-emotion": "^9.2.5"
"react-emotion": "^9.2.6"
},
"devDependencies": {
"babel-plugin-emotion": "^9.2.5",
"babel-plugin-emotion": "^9.2.6",
"flow-bin": "^0.76.0",
"flow-typed": "^2.5.1"
}

View File

@@ -8,12 +8,12 @@
},
"dependencies": {
"docz": "^0.5.8",
"emotion": "^9.2.5",
"emotion": "^9.2.6",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-emotion": "^9.2.5"
"react-emotion": "^9.2.6"
},
"devDependencies": {
"babel-plugin-emotion": "^9.2.5"
"babel-plugin-emotion": "^9.2.6"
}
}

View File

@@ -43,7 +43,7 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.13.7",
"trash-cli": "^1.4.0",
"tslint": "^5.10.0",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.13.0",
"typescript": "^2.9.2"
},

View File

@@ -20,11 +20,11 @@
"tslint": "tslint --project ."
},
"dependencies": {
"@babel/core": "7.0.0-beta.52",
"@babel/preset-typescript": "^7.0.0-beta.52",
"@babel/runtime": "^7.0.0-beta.52",
"@mdx-js/loader": "^0.13.1-1",
"@mdx-js/mdx": "^0.13.1-1",
"@babel/core": "7.0.0-beta.54",
"@babel/preset-typescript": "^7.0.0-beta.54",
"@babel/runtime": "^7.0.0-beta.54",
"@mdx-js/loader": "^0.14.0",
"@mdx-js/mdx": "^0.14.0",
"@mdx-js/mdxast": "^0.10.0",
"@sindresorhus/slugify": "^0.3.0",
"art-template": "^4.12.2",
@@ -77,14 +77,14 @@
"unist-util-remove": "^1.0.1",
"unist-util-visit": "^1.3.1",
"url-loader": "^1.0.1",
"webpack": "^4.15.1",
"webpack": "^4.16.1",
"webpack-chain": "^4.8.0",
"webpack-hot-client": "^4.1.1",
"webpack-manifest-plugin": "^2.0.3",
"webpack-serve": "^2.0.2",
"webpack-serve-waitpage": "^1.0.0",
"webpackbar": "^2.6.1",
"ws": "^5.2.1",
"ws": "^5.2.2",
"yargs": "^12.0.1"
},
"devDependencies": {
@@ -95,16 +95,16 @@
"@types/del": "^3.0.1",
"@types/express": "^4.16.0",
"@types/fs-extra": "^5.0.4",
"@types/html-webpack-plugin": "^2.30.3",
"@types/html-webpack-plugin": "^2.30.4",
"@types/lodash.get": "^4.4.3",
"@types/node": "10.5.2",
"@types/prettier": "^1.13.1",
"@types/prettier": "^1.13.2",
"@types/resolve": "^0.0.8",
"@types/webpack": "^4.4.5",
"@types/webpack": "^4.4.7",
"@types/webpack-chain": "^4.8.0",
"@types/ws": "^5.1.2",
"@types/yargs": "^11.1.0",
"tslint": "^5.10.0",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.13.0",
"typescript": "^2.9.2"
}

View File

@@ -1,5 +1,8 @@
import { Plugin } from './Plugin'
import { Config as Args } from './commands/args'
import { Config as Args, Env } from './commands/args'
import { babelrc, BabelRC } from './utils/babelrc'
import * as paths from './config/paths'
export interface Server {
app: any
@@ -11,19 +14,24 @@ export interface BundlerServer {
start(): Promise<Server>
}
export type ConfigFn<C> = (babelrc: BabelRC) => C
export type ServerFn<C> = (config: C) => BundlerServer | Promise<BundlerServer>
export type BuildFn<C> = (config: C) => void
export type BuildFn<C> = (config: C, dist: string) => void
export interface BundlerConstructor<Config> {
args: Args
config: Config
config: ConfigFn<Config>
server: ServerFn<Config>
build: BuildFn<Config>
}
export class Bundler<C = any> {
export interface ConfigObj {
[key: string]: any
}
export class Bundler<C = ConfigObj> {
private readonly args: Args
private config: C
private config: ConfigFn<C>
private server: ServerFn<C>
private builder: BuildFn<C>
@@ -36,8 +44,10 @@ export class Bundler<C = any> {
this.builder = build
}
public getConfig(): C {
const config = this.mountConfig(this.config)
public getConfig(env: Env): C {
const babelConfig = babelrc(this.args, env)
const config = this.mountConfig(this.config(babelConfig))
return this.args.modifyBundlerConfig(config, !this.isProd(), this.args)
}
@@ -46,7 +56,8 @@ export class Bundler<C = any> {
}
public async build(config: C): Promise<void> {
await this.builder(config)
const dist = paths.getDist(this.args.dest)
await this.builder(config, dist)
}
private mountConfig(config: C): any {

View File

@@ -1,7 +1,7 @@
import * as fs from 'fs-extra'
import chalk from 'chalk'
import logger from 'signale'
import webpack, { Configuration } from 'webpack'
import webpack, { Configuration as CFG } from 'webpack'
import FSR from 'react-dev-utils/FileSizeReporter'
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages'
import printBuildError from 'react-dev-utils/printBuildError'
@@ -30,7 +30,7 @@ const copyPublicFolder = async (dest: string): Promise<void> => {
}
}
const compile = (config: Configuration) =>
const compile = (config: CFG) =>
new Promise((resolve, reject) => {
let compiler
try {
@@ -45,7 +45,7 @@ const compile = (config: Configuration) =>
})
})
const builder = async (config: Configuration, previousFileSizes: any) => {
const builder = async (config: CFG, previousFileSizes: any) => {
logger.start('Creating an optimized production build...')
return new Promise(async (resolve, reject) => {
@@ -114,9 +114,7 @@ const onError = (err: Error) => {
process.exit(1)
}
export const build = (args: Args) => async (config: Configuration) => {
const dist = paths.getDist(args.dest)
export const build = (args: Args) => async (config: CFG, dist: string) => {
try {
await fs.ensureDir(dist)
const previousFileSizes = await measureFileSizesBeforeBuild(dist)

View File

@@ -1,4 +1,5 @@
import * as path from 'path'
import { Configuration } from 'webpack'
import webpackBarPlugin from 'webpackbar'
import Config from 'webpack-chain'
import friendlyErrors from 'friendly-errors-webpack-plugin'
@@ -6,11 +7,10 @@ import htmlWebpackPlugin from 'html-webpack-plugin'
import manifestPlugin from 'webpack-manifest-plugin'
import UglifyJs from 'uglifyjs-webpack-plugin'
import { Config as Args } from '../../commands/args'
import { Config as Args, Env } from '../../commands/args'
import { BabelRC } from '../../utils/babelrc'
import * as paths from '../../config/paths'
import * as loaders from './loaders'
import { Env } from './'
const uglify = new UglifyJs({
parallel: true,
@@ -36,10 +36,9 @@ const uglify = new UglifyJs({
},
})
export const createConfig = (babelrc: BabelRC) => (
args: Args,
env: Env
): Config => {
export const createConfig = (args: Args, env: Env) => (
babelrc: BabelRC
): Configuration => {
const { debug, host, port, protocol } = args
const config = new Config()
@@ -70,8 +69,9 @@ export const createConfig = (babelrc: BabelRC) => (
*/
const outputProd = (output: Config.Output) =>
output
.filename('static/js/[name].[chunkhash:8].js')
.sourceMapFilename('static/js/[name].[chunkhash:8].js.map')
.filename('static/js/[name].[hash].js')
.sourceMapFilename('static/js/[name].[hash].js.map')
.chunkFilename('static/js/[name].[chunkhash:8].js')
.publicPath(base)
const outputDev = (output: Config.Output) =>
@@ -233,5 +233,5 @@ export const createConfig = (babelrc: BabelRC) => (
])
config.performance.hints(false)
return config
return config.toConfig() as Configuration
}

View File

@@ -1,22 +1,16 @@
import { Configuration as CFG } from 'webpack'
import { Bundler } from '../../Bundler'
import { babelrc } from '../../utils/babelrc'
import { Config as Args } from '../../commands/args'
import { Config as Args, Env } from '../../commands/args'
import { createConfig } from './config'
import { server } from './server'
import { build } from './build'
export type Env = 'production' | 'development'
export const bundler = (args: Args, env: Env): Bundler<CFG> => {
const create = createConfig(babelrc(args))
const config: any = create(args, env).toConfig()
return new Bundler({
export const bundler = (args: Args, env: Env): Bundler<CFG> =>
new Bundler({
args,
config,
config: createConfig(args, env),
build: build(args),
server: server(args),
})
}

View File

@@ -1,36 +1,21 @@
import * as path from 'path'
import HappyPack from 'happypack'
import merge from 'deepmerge'
import matter from 'remark-frontmatter'
import slug from 'rehype-slug'
import headings from 'rehype-autolink-headings'
import Config from 'webpack-chain'
import { plugin as mdastPlugin } from '../../utils/plugin-mdast'
import { plugin as hastPlugin } from '../../utils/plugin-hast'
import { Config as Args } from '../../commands/args'
import * as paths from '../../config/paths'
import * as mdxConfig from '../../config/mdx'
export const setupHappypack = (config: Config, args: Args, babelrc: any) => {
const babelLoader: any = {
cacheDirectory: true,
loader: require.resolve('babel-loader'),
options: merge(babelrc, {
plugins: [require.resolve('react-hot-loader/babel')],
}),
}
const jsx = {
id: 'jsx',
verbose: args.debug,
loaders: [babelLoader],
}
if (args.propsParser && !args.typescript) {
babelLoader.options.plugins.push([
require.resolve('babel-plugin-react-docgen'),
{ resolver: 'findAllExportedComponentDefinitions' },
])
loaders: [
{
loader: require.resolve('babel-loader'),
options: babelrc,
},
] as any[],
}
if (args.propsParser && args.typescript) {
@@ -45,7 +30,12 @@ export const setupHappypack = (config: Config, args: Args, babelrc: any) => {
loaders: [
{
loader: require.resolve('babel-loader'),
options: babelrc,
options: {
...babelrc,
plugins: babelrc.plugins
.filter((p: string) => /react\-hot\-loader\/babel/.test(p))
.filter((p: string) => /babel\-plugin\-react\-docgen/.test(p)),
},
},
],
}
@@ -100,26 +90,9 @@ export const mdx = (config: Config, args: Args) => {
.use('mdx-loader')
.loader(require.resolve('@mdx-js/loader'))
.options({
type: 'yaml',
marker: '-',
properties: {
'aria-hidden': true,
},
content: {
type: 'element',
tagName: 'span',
properties: {
className: ['icon-link'],
},
children: [
{
type: 'text',
value: '#',
},
],
},
mdPlugins: mdPlugins.concat([matter, mdastPlugin]),
hastPlugins: hastPlugins.concat([hastPlugin, slug, headings]),
...mdxConfig.config,
mdPlugins: mdPlugins.concat(mdxConfig.remarkPlugins),
hastPlugins: hastPlugins.concat(mdxConfig.rehypePlugins),
})
}

View File

@@ -1,6 +1,5 @@
import { Configuration as Config } from 'webpack'
import serve from 'webpack-serve'
import detectPort from 'detect-port'
import { devServerConfig } from './devserver'
import { BundlerServer } from '../../Bundler'
@@ -9,8 +8,7 @@ import { Config as Args } from '../../commands/args'
type Server = Promise<BundlerServer>
export const server = (args: Args) => async (config: Config): Server => {
const port = await detectPort(args.port)
const devserver = devServerConfig({ ...args, port }, config)
const devserver = devServerConfig(args, config)
return {
start: async () => {

View File

@@ -38,6 +38,8 @@ export interface Argv {
indexHtml?: string
}
export type Env = 'production' | 'development'
export interface ThemeConfig {
[key: string]: any
}

View File

@@ -1,3 +1,6 @@
process.env.BABEL_ENV = process.env.BABEL_ENV || 'production'
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
import * as fs from 'fs-extra'
import logger from 'signale'
@@ -5,15 +8,14 @@ import * as paths from '../config/paths'
import { loadConfig } from '../utils/load-config'
import { webpack } from '../bundlers'
import { Entries } from '../Entries'
import { Config } from './args'
import { Config, Env } from './args'
import { Plugin } from '../Plugin'
process.env.BABEL_ENV = process.env.BABEL_ENV || 'production'
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const env = process.env.NODE_ENV as Env
export const build = async (args: Config) => {
const config = loadConfig(args)
const bundler = webpack(config, 'production')
const bundler = webpack(config, env)
const entries = new Entries(config)
const map = await entries.get()
const run = Plugin.runPluginsMethod(config.plugins)
@@ -25,7 +27,7 @@ export const build = async (args: Config) => {
try {
await run('onPreBuild')
await bundler.build(bundler.getConfig())
await bundler.build(bundler.getConfig(env))
await run('onPostBuild')
} catch (err) {
logger.fatal(err)

View File

@@ -1,26 +1,30 @@
process.env.BABEL_ENV = 'development'
process.env.NODE_ENV = 'development'
import * as fs from 'fs-extra'
import logger from 'signale'
import detectPort from 'detect-port'
import * as paths from '../config/paths'
import { Config } from './args'
import { Config, Env } from './args'
import { DataServer } from '../DataServer'
import { webpack } from '../bundlers'
import { Entries } from '../Entries'
import { Plugin } from '../Plugin'
import { loadConfig } from '../utils/load-config'
process.env.BABEL_ENV = process.env.BABEL_ENV || 'development'
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const env = process.env.NODE_ENV as Env
export const dev = async (args: Config) => {
const config = loadConfig(args)
const port = await detectPort(config.port)
const websocketPort = await detectPort(config.websocketPort)
const entries = new Entries(config)
const bundler = webpack({ ...config, port }, 'development')
const server = await bundler.createServer(bundler.getConfig())
const bundler = webpack({ ...config, port }, env)
const server = await bundler.createServer(bundler.getConfig(env))
const { app } = await server.start()
const run = Plugin.runPluginsMethod(config.plugins)
const newConfig = { ...config, websocketPort }
const dataServer = new DataServer({

View File

@@ -0,0 +1,30 @@
import matter from 'remark-frontmatter'
import slug from 'rehype-slug'
import headings from 'rehype-autolink-headings'
import { plugin as mdastPlugin } from '../utils/plugin-mdast'
import { plugin as hastPlugin } from '../utils/plugin-hast'
export const config = {
type: 'yaml',
marker: '-',
properties: {
'aria-hidden': true,
},
content: {
type: 'element',
tagName: 'span',
properties: {
className: ['icon-link'],
},
children: [
{
type: 'text',
value: '#',
},
],
},
}
export const remarkPlugins = [matter, mdastPlugin]
export const rehypePlugins = [hastPlugin, slug, headings]

View File

@@ -1,7 +1,7 @@
import { load } from 'load-cfg'
import merge from 'deepmerge'
import { Config } from '../commands/args'
import { Config, Env } from '../commands/args'
import { Plugin } from '../Plugin'
export interface BabelRC {
@@ -11,15 +11,42 @@ export interface BabelRC {
babelrc?: boolean
}
export const babelrc = (args: Config): BabelRC => {
const getPresets = (args: Config) => {
const presets: any[] = [
[require.resolve('babel-preset-react-app'), { flow: !args.typescript }],
]
if (args.typescript) presets.push(require.resolve('@babel/preset-typescript'))
return presets
}
const getPlugins = (args: Config, env: Env) => {
const isProd = env === 'production'
const plugins: any[] = []
if (!isProd) {
plugins.push(require.resolve('react-hot-loader/babel'))
}
if (args.propsParser && !args.typescript) {
plugins.push([
require.resolve('babel-plugin-react-docgen'),
{ resolver: 'findAllExportedComponentDefinitions' },
])
}
return plugins
}
export const babelrc = (args: Config, env: Env): BabelRC => {
const presets = getPresets(args)
const plugins = getPlugins(args, env)
const config = merge(load('babel', null), {
babelrc: false,
presets,
plugins,
cacheDirectory: !args.debug,
presets: [
[require.resolve('babel-preset-react-app'), { flow: !args.typescript }],
...(args.typescript ? [require.resolve('@babel/preset-typescript')] : []),
],
plugins: [],
babelrc: false,
})
const reduce = Plugin.reduceFromPlugins<BabelRC>(args.plugins)

View File

@@ -36,8 +36,6 @@ export const babel = () =>
modifyHappypackLoader(config.plugins[happypackJsx])
modifyHappypackLoader(config.plugins[happypackMdx])
console.log(args)
if (tsxRule) {
config.module.rules[tsxIdx] = {
...tsxRule,

View File

@@ -25,12 +25,12 @@
"css-loader": "^1.0.0",
"deepmerge": "^2.1.1",
"docz-core": "^0.5.7",
"less": "^3.6.0",
"less": "^3.7.1",
"less-loader": "^4.1.0",
"loader-utils": "^1.1.0",
"mini-css-extract-plugin": "^0.4.1",
"node-sass": "^4.9.2",
"optimize-css-assets-webpack-plugin": "^4.0.3",
"optimize-css-assets-webpack-plugin": "^5.0.0",
"postcss": "^6.0.23",
"postcss-flexbugs-fixes": "^3.3.1",
"postcss-loader": "^2.1.6",
@@ -38,7 +38,7 @@
"style-loader": "^0.21.0",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
"webpack": "^4.15.1",
"webpack": "^4.16.1",
"webpack-chain": "^4.8.0"
}
}

View File

@@ -21,8 +21,8 @@
},
"dependencies": {
"docz": "^0.5.8",
"emotion": "^9.2.5",
"emotion-theming": "^9.2.5",
"emotion": "^9.2.6",
"emotion-theming": "^9.2.6",
"facepaint": "^1.2.1",
"fast-deep-equal": "^2.0.1",
"fuse.js": "^3.2.1",
@@ -33,7 +33,7 @@
"react-breakpoints": "^3.0.0",
"react-content-loader": "^3.1.2",
"react-dom": "^16.2.0",
"react-emotion": "^9.2.5",
"react-emotion": "^9.2.6",
"react-feather": "^1.1.1",
"react-lightweight-tooltip": "^1.0.0",
"react-powerplug": "^1.0.0-rc.1",
@@ -47,7 +47,7 @@
"@types/prismjs": "^1.9.0",
"@types/react": "^16.4.6",
"@types/react-dom": "^16.0.6",
"babel-plugin-emotion": "^9.2.5",
"babel-plugin-emotion": "^9.2.6",
"rollup-plugin-svg": "^1.0.1"
}
}

View File

@@ -24,7 +24,7 @@
"tslint": "tslint --project ."
},
"dependencies": {
"@mdx-js/tag": "^0.13.1-1",
"@mdx-js/tag": "^0.14.0",
"@sindresorhus/slugify": "^0.3.0",
"create-react-context": "^0.2.2",
"deepmerge": "^2.1.1",
@@ -44,7 +44,7 @@
"react-dom": "^16.2.0"
},
"devDependencies": {
"@types/bluebird": "^3.5.21",
"@types/bluebird": "^3.5.22",
"@types/deepmerge": "^2.1.0",
"@types/react": "^16.4.6",
"@types/react-dom": "^16.0.6",

View File

@@ -1,7 +1,9 @@
import * as React from 'react'
import { CSSProperties, Component, SFC } from 'react'
export interface StylesMap { [s: string]: CSSProperties; }
export interface StylesMap {
[s: string]: CSSProperties
}
const styles: StylesMap = {
wrapper: {
overflowY: 'auto',

View File

@@ -22,7 +22,7 @@
},
"dependencies": {
"deepmerge": "^2.1.1",
"esm": "^3.0.66",
"esm": "^3.0.71",
"find-up": "^3.0.0"
},
"devDependencies": {

1164
yarn.lock

File diff suppressed because it is too large Load Diff