Skip to content

Commit

Permalink
fix(docz-core): serve static files from public
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 19, 2018
1 parent b1c87b5 commit c866856
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/docz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"koa": "^2.5.2",
"koa-connect": "^2.0.1",
"koa-mount": "^3.0.0",
"koa-range": "^0.3.0",
"koa-static": "^5.0.0",
"load-cfg": "^0.5.9",
"lodash.get": "^4.4.2",
Expand Down
6 changes: 6 additions & 0 deletions packages/docz-core/src/Entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const fromTemplates = (file: string) => path.join(paths.templates, file)
const getHtmlFilepath = (indexHtml: string | undefined) =>
indexHtml ? path.join(paths.root, indexHtml) : fromTemplates('index.tpl.html')

const getPublicUrl = (config: Config, dev: boolean): string => {
const prefix = config.base === '/' ? '' : config.base
return dev ? prefix : `${prefix}/public`
}

const writeAppFiles = async (config: Config, dev: boolean): Promise<void> => {
const { plugins, title, description, theme, indexHtml } = config
const props = Plugin.propsOfPlugins(plugins)
Expand Down Expand Up @@ -42,6 +47,7 @@ const writeAppFiles = async (config: Config, dev: boolean): Promise<void> => {
const rawIndexHtml = html({
title,
description,
publicUrl: getPublicUrl(config, dev),
})

await touch(paths.rootJs, rawRootJs)
Expand Down
11 changes: 11 additions & 0 deletions packages/docz-core/src/bundlers/webpack/devserver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import * as path from 'path'
import * as fs from 'fs-extra'
import * as Koa from 'koa'
import { Configuration } from 'webpack'
import mount from 'koa-mount'
import range from 'koa-range'
import convert from 'koa-connect'
import serveStatic from 'koa-static'
import history from 'connect-history-api-fallback'
import serveWaitpage from 'webpack-serve-waitpage'

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

Expand Down Expand Up @@ -33,6 +38,12 @@ export const devServerConfig = (
middleware.webpack()
middleware.content()

app.use(range)

if (fs.existsSync(paths.appPublic)) {
app.use(mount(args.base, serveStatic(paths.appPublic)))
}

app.use(
convert(
history({
Expand Down
1 change: 1 addition & 0 deletions packages/docz-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare module 'webpackbar'
declare module 'webpack-serve'
declare module 'webpack-serve-waitpage'
declare module 'koa-mount'
declare module 'koa-range'
declare module 'koa-connect'
declare module 'koa-static'
declare module 'detect-port'
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6992,6 +6992,12 @@ koa-mount@^3.0.0:
debug "^2.6.1"
koa-compose "^3.2.1"

koa-range@^0.3.0:
version "0.3.0"
resolved "https://registry.npmjs.org/koa-range/-/koa-range-0.3.0.tgz#3588e3496473a839a1bd264d2a42b1d85bd7feac"
dependencies:
stream-slice "^0.1.2"

koa-send@^5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/koa-send/-/koa-send-5.0.0.tgz#5e8441e07ef55737734d7ced25b842e50646e7eb"
Expand Down Expand Up @@ -10891,6 +10897,10 @@ stream-shift@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/stream-shift/-/stream-shift-1.0.0.tgz#d5c752825e5367e786f78e18e445ea223a155952"

stream-slice@^0.1.2:
version "0.1.2"
resolved "https://registry.npmjs.org/stream-slice/-/stream-slice-0.1.2.tgz#2dc4f4e1b936fb13f3eb39a2def1932798d07a4b"

string-argv@^0.0.2:
version "0.0.2"
resolved "https://registry.npmjs.org/string-argv/-/string-argv-0.0.2.tgz#dac30408690c21f3c3630a3ff3a05877bdcbd736"
Expand Down

0 comments on commit c866856

Please sign in to comment.