Skip to content

Commit

Permalink
feat(docz-core): add dest config property
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jun 9, 2018
1 parent 20f29c2 commit d6c5506
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
27 changes: 18 additions & 9 deletions packages/docz-core/src/bundlers/webpack/build.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as fs from 'fs-extra'
import * as path from 'path'
import chalk from 'chalk'
import logger from 'signale'
import webpack, { Configuration } from 'webpack'
import FSR from 'react-dev-utils/FileSizeReporter'
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages'
import printBuildError from 'react-dev-utils/printBuildError'

import { Config as Args } from '../../commands/args'
import * as paths from '../../config/paths'

process.env.BABEL_ENV = process.env.BABEL_ENV || 'production'
Expand All @@ -20,9 +22,9 @@ const hasCiEnvVar = () =>
(typeof process.env.CI !== 'string' ||
process.env.CI.toLowerCase() !== 'false')

const copyPublicFolder = async (): Promise<void> => {
const copyPublicFolder = async (dest: string): Promise<void> => {
if (await fs.pathExists(paths.appPublic)) {
await fs.copySync(paths.appPublic, paths.distPublic, {
await fs.copySync(paths.appPublic, paths.distPublic(dest), {
dereference: true,
filter: file => file !== paths.indexHtml,
})
Expand Down Expand Up @@ -75,7 +77,10 @@ const builder = async (config: Configuration, previousFileSizes: any) => {
})
}

const onSuccess = ({ stats, previousFileSizes, warnings }: any) => {
const onSuccess = (
dist: string,
{ stats, previousFileSizes, warnings }: any
) => {
if (warnings.length) {
logger.warn('Compiled with warnings.\n')
logger.warn(warnings.join('\n\n'))
Expand All @@ -97,7 +102,7 @@ const onSuccess = ({ stats, previousFileSizes, warnings }: any) => {
printFileSizesAfterBuild(
stats,
previousFileSizes,
paths.dist,
dist,
WARN_AFTER_BUNDLE_GZIP_SIZE,
WARN_AFTER_CHUNK_GZIP_SIZE
)
Expand All @@ -110,15 +115,19 @@ const onError = (err: Error) => {
process.exit(1)
}

export const build = async (config: Configuration) => {
export const build = (args: Args) => async (config: Configuration) => {
const base = paths.servedPath(args.base)
const dist = path.join(paths.getDist(args.dest), base)

try {
const previousFileSizes = await measureFileSizesBeforeBuild(paths.dist)
await fs.ensureDir(dist)
const previousFileSizes = await measureFileSizesBeforeBuild(dist)

await fs.emptyDir(paths.dist)
await copyPublicFolder()
await fs.emptyDir(dist)
await copyPublicFolder(dist)

const result = await builder(config, previousFileSizes)
onSuccess(result)
onSuccess(dist, result)
} catch (err) {
onError(err)
}
Expand Down
12 changes: 7 additions & 5 deletions packages/docz-core/src/bundlers/webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export const createConfig = (babelrc: BabelRC) => (
): Config => {
const { paths, debug } = args

const srcPath = path.resolve(paths.root, args.src)
const isProd = env === 'production'
const config = new Config()
const isProd = env === 'production'
const base = paths.servedPath(args.base)
const dist = paths.getDist(args.dest)
const srcPath = path.resolve(paths.root, args.src)

/**
* general
Expand All @@ -69,7 +71,7 @@ export const createConfig = (babelrc: BabelRC) => (
output
.filename('static/js/[name].[chunkhash:8].js')
.sourceMapFilename('static/js/[name].[chunkhash:8].js.map')
.publicPath(paths.servedPath(args.base))
.publicPath(base)

const outputDev = (output: Config.Output) =>
output
Expand All @@ -79,7 +81,7 @@ export const createConfig = (babelrc: BabelRC) => (

config.output
.pathinfo(true)
.path(paths.dist)
.path(isProd ? path.join(dist, base) : dist)
.when(isProd, outputProd, outputDev)
.crossOriginLoading('anonymous')

Expand Down Expand Up @@ -200,7 +202,7 @@ export const createConfig = (babelrc: BabelRC) => (

config.plugin('injections').use(require('webpack/lib/DefinePlugin'), [
{
BASE_URL: JSON.stringify(args.base),
BASE_URL: JSON.stringify(base),
},
])

Expand Down
2 changes: 1 addition & 1 deletion packages/docz-core/src/bundlers/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const bundler = (args: Args, env: Env): Bundler<CFG> => {

return new Bundler({
args,
build,
config,
build: build(args),
server: server(args),
})
}
6 changes: 6 additions & 0 deletions packages/docz-core/src/commands/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Argv {
base: string
src: string
files: string
dest: string
/* bundler args */
debug: boolean
typescript: boolean
Expand Down Expand Up @@ -45,6 +46,11 @@ export const args = (yargs: any) => {
type: 'string',
default: '**/*.mdx',
})
yargs.positional('dest', {
alias: 'd',
type: 'string',
default: '.docz/dist',
})
yargs.positional('title', {
type: 'string',
default: 'MyDoc',
Expand Down
8 changes: 5 additions & 3 deletions packages/docz-core/src/config/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export interface Paths {
servedPath: (base: string) => string
docz: string
app: string
dist: string
getDist: (dest: string) => string
distPublic: (dest: string) => string
importsJs: string
rootJs: string
indexJs: string
Expand All @@ -41,8 +42,9 @@ export const servedPath = (base: string) => ensureSlash(base, true)
export const docz = resolveApp('.docz')
export const app = path.resolve(docz, 'app/')
export const appPublic = path.resolve(docz, 'public/')
export const dist = path.resolve(docz, 'dist/')
export const distPublic = path.resolve(dist, 'public/')

export const getDist = (dest: string) => path.join(root, dest)
export const distPublic = (dest: string) => path.join(dest, 'public/')

export const importsJs = path.resolve(app, 'imports.js')
export const rootJs = path.resolve(app, 'root.jsx')
Expand Down

0 comments on commit d6c5506

Please sign in to comment.