Skip to content

Commit

Permalink
feat(docz-core): add base config argument
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jun 8, 2018
1 parent 97859e2 commit 20f29c2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
8 changes: 7 additions & 1 deletion packages/docz-core/src/bundlers/webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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)
.publicPath(paths.servedPath(args.base))

const outputDev = (output: Config.Output) =>
output
Expand Down Expand Up @@ -198,6 +198,12 @@ export const createConfig = (babelrc: BabelRC) => (
cfg.plugin('friendly-errors').use(friendlyErrors)
})

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

config.performance.hints(false)
return config
}
6 changes: 5 additions & 1 deletion packages/docz-core/src/commands/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Plugin } from '../Plugin'

export interface Argv {
/* io args */
base: string
src: string
files: string
/* bundler args */
env: string
debug: boolean
typescript: boolean
propsParser: boolean
Expand All @@ -32,6 +32,10 @@ export interface Config extends Argv {
}

export const args = (yargs: any) => {
yargs.positional('base', {
type: 'string',
default: '/',
})
yargs.positional('source', {
alias: 'src',
type: 'string',
Expand Down
17 changes: 2 additions & 15 deletions packages/docz-core/src/config/paths.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import * as fs from 'fs'
import * as path from 'path'
import * as url from 'url'
import resolve from 'resolve'

const ENV_PUBLIC_URL = process.env.PUBLIC_URL

const ensureSlash = (filepath: any, needsSlash: boolean) => {
const hasSlash = filepath.endsWith('/')

Expand All @@ -17,24 +14,14 @@ const ensureSlash = (filepath: any, needsSlash: boolean) => {
}
}

const getPublicUrl = (appPackageJson: string) =>
ENV_PUBLIC_URL || require(appPackageJson).homepage

const getServedPath = (appPackageJson: string) => {
const publicUrl = getPublicUrl(appPackageJson)
const servedUrl =
ENV_PUBLIC_URL || (publicUrl ? url.parse(publicUrl).pathname : '/')
return ensureSlash(servedUrl, true)
}

export const root = fs.realpathSync(process.cwd())
const resolveApp = (to: string) => path.resolve(root, to)

export interface Paths {
root: string
templates: string
packageJson: string
servedPath: string
servedPath: (base: string) => string
docz: string
app: string
dist: string
Expand All @@ -49,7 +36,7 @@ export interface Paths {
export const templates = path.join(resolve.sync('docz-core'), '../templates')

export const packageJson = resolveApp('package.json')
export const servedPath = getServedPath(resolveApp('package.json'))
export const servedPath = (base: string) => ensureSlash(base, true)

export const docz = resolveApp('.docz')
export const app = path.resolve(docz, 'app/')
Expand Down
4 changes: 3 additions & 1 deletion packages/docz/src/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import merge from 'deepmerge'

import { ComponentsMap } from './components/DocPreview'

declare var BASE_URL: string

export type MSXComponent = CT<{
components: ComponentsMap
}>
Expand Down Expand Up @@ -67,7 +69,7 @@ export function theme(

return (
<dataContext.Provider value={value}>
<BrowserRouter>
<BrowserRouter basename={BASE_URL}>
<Wrapper>
<WrappedComponent />
</Wrapper>
Expand Down

0 comments on commit 20f29c2

Please sign in to comment.