Skip to content

Commit

Permalink
feat: 完善 setPublicPath
Browse files Browse the repository at this point in the history
  • Loading branch information
imsunhao committed Sep 23, 2019
1 parent 5a3aa54 commit c9b23cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/bin/start.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getUserConfigSync, serverInit, serverStart, setStaticFileExts, setVersion, setBuildServiceConfig } from 'src/utils'
import { getUserConfigSync, serverInit, serverStart, setStaticFileExts, setVersion, setBuildServiceConfig, setPublicPath } from 'src/utils'

import { BuildService } from '@types'
import consola from 'consola'
Expand All @@ -19,7 +19,12 @@ async function main(argv: BuildService.parsedArgs) {

setBuildServiceConfig(options)

process.env.PUBLIC_PATH = options.injectContext.STATIC_HOST || ''
if (options.webpack && options.webpack.base && options.webpack.base.output && options.webpack.base.output.path) {
setPublicPath(options.rootDir, options.webpack.base.output.path)
} else {
console.warn('未找到用户设置的 options.webpack.base.output.path. PUBLIC_PATH = injectContext.STATIC_HOST')
process.env.PUBLIC_PATH = options.injectContext.STATIC_HOST || ''
}

const app = serverInit()

Expand Down
17 changes: 10 additions & 7 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,23 @@ export function setVersion(options: ConfigOptions.options) {
}
}

export function setPublicPath(rootDir, path) {
if (path[path.length - 1] !== '/') {
path += '/'
}
const rootDirLength = rootDir ? rootDir.length : 0
path = path.slice(rootDirLength)
process.env.PUBLIC_PATH = path
}

function baseSetWebpack(
options: ConfigOptions.options
) {
options.webpack.server = getServerConfig(options)
// const isProduction = mode ? mode !== 'development' : true
// if (!isProduction) {
// }
const rootDirLength = options.rootDir ? options.rootDir.length : 0
let path = (options.webpack.client as any).output.path
if (path[path.length - 1] !== '/') {
path += '/'
}
path = path.slice(rootDirLength)
process.env.PUBLIC_PATH = path
setPublicPath(options.rootDir, (options.webpack.client as any).output.path)
return options
}

Expand Down

0 comments on commit c9b23cd

Please sign in to comment.