Skip to content

Commit

Permalink
fix(docz-core): use assets from public folder
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Nov 15, 2018
1 parent a0ed2ab commit 210c3a1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
5 changes: 3 additions & 2 deletions packages/docz-core/src/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface BundlerServer {
}

export type ConfigFn<C> = (babelrc: BabelRC) => Promise<C>
export type BuildFn<C> = (config: C, dist: string) => void
export type BuildFn<C> = (config: C, dist: string, publicDir: string) => void

export type ServerFnReturn = BundlerServer | Promise<BundlerServer>
export type ServerFn<C> = (config: C, hooks: ServerHooks) => ServerFnReturn
Expand Down Expand Up @@ -77,6 +77,7 @@ export class Bundler<C = ConfigObj> {

public async build(config: C): Promise<void> {
const dist = paths.getDist(this.args.dest)
const publicDir = path.join(paths.root, this.args.public)

if (paths.root === path.resolve(dist)) {
logger.fatal(
Expand All @@ -87,7 +88,7 @@ export class Bundler<C = ConfigObj> {
process.exit(1)
}

await this.builder(config, dist)
await this.builder(config, dist, publicDir)
}

private mountConfig(config: C, env: Env): any {
Expand Down
5 changes: 5 additions & 0 deletions packages/docz-core/src/commands/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface Argv {
src: string
files: string
ignore: string[]
public: string
dest: string
editBranch: string
config: string
Expand Down Expand Up @@ -114,6 +115,10 @@ export const args = (env: Env) => (yargs: any) => {
type: 'array',
default: getEnv('docz.ignore', []),
})
yargs.positional('public', {
type: 'string',
default: getEnv('docz.public', '/public'),
})
yargs.positional('dest', {
alias: 'd',
type: 'string',
Expand Down
13 changes: 8 additions & 5 deletions packages/docz-core/src/webpack/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024

const hasCiEnvVar = () => envDotProp.get('ci', false, { parse: true })

const copyPublicFolder = async (dest: string): Promise<void> => {
if (await fs.pathExists(paths.appPublic)) {
await fs.copySync(paths.appPublic, paths.distPublic(dest), {
const copyPublicFolder = async (
dest: string,
publicDir: string
): Promise<void> => {
if (await fs.pathExists(publicDir)) {
await fs.copy(publicDir, paths.distPublic(dest), {
dereference: true,
filter: file => file !== paths.indexHtml,
})
Expand Down Expand Up @@ -108,13 +111,13 @@ const onError = (err: Error) => {
process.exit(1)
}

export const build = async (config: CFG, dist: string) => {
export const build = async (config: CFG, dist: string, publicDir: string) => {
try {
await fs.ensureDir(dist)
const previousFileSizes = await measureFileSizesBeforeBuild(dist)

await fs.emptyDir(dist)
await copyPublicFolder(dist)
await copyPublicFolder(dist, publicDir)

const result = await builder(config, previousFileSizes)
onSuccess(dist, result)
Expand Down
5 changes: 3 additions & 2 deletions packages/docz-core/src/webpack/devserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const devServerConfig = (
) => {
const nonExistentDir = path.resolve(__dirname, 'non-existent')
const logLevel = (level: string) => (args.debug ? 'debug' : level)
const publicDir = path.join(paths.root, args.public)

return {
config,
Expand All @@ -42,8 +43,8 @@ export const devServerConfig = (

app.use(range)

if (fs.existsSync(paths.appPublic)) {
app.use(mount(args.base, serveStatic(paths.appPublic)))
if (fs.existsSync(publicDir)) {
app.use(mount(path.join(args.base, '/public'), serveStatic(publicDir)))
}

app.use(
Expand Down

0 comments on commit 210c3a1

Please sign in to comment.