2
2
const fs = require ( 'fs' )
3
3
const path = require ( 'path' )
4
4
5
+ // ensure the filename passed to html-webpack-plugin is a relative path
6
+ // because it cannot correctly handle absolute paths
7
+ function ensureRelative ( outputDir , _path ) {
8
+ if ( path . isAbsolute ( _path ) ) {
9
+ return path . relative ( outputDir , _path )
10
+ } else {
11
+ return _path
12
+ }
13
+ }
14
+
5
15
module . exports = ( api , options ) => {
6
16
api . chainWebpack ( webpackConfig => {
7
17
// only apply when there's no alternative target
@@ -11,6 +21,7 @@ module.exports = (api, options) => {
11
21
12
22
const isProd = process . env . NODE_ENV === 'production'
13
23
const isLegacyBundle = process . env . VUE_CLI_MODERN_MODE && ! process . env . VUE_CLI_MODERN_BUILD
24
+ const outputDir = api . resolve ( options . outputDir )
14
25
15
26
// code splitting
16
27
if ( isProd ) {
@@ -56,6 +67,10 @@ module.exports = (api, options) => {
56
67
}
57
68
}
58
69
70
+ if ( options . indexPath ) {
71
+ htmlOptions . filename = ensureRelative ( outputDir , options . indexPath )
72
+ }
73
+
59
74
if ( isProd ) {
60
75
Object . assign ( htmlOptions , {
61
76
minify : {
@@ -151,7 +166,7 @@ module.exports = (api, options) => {
151
166
const pageHtmlOptions = Object . assign ( { } , htmlOptions , {
152
167
chunks : chunks || [ 'chunk-vendors' , 'chunk-common' , name ] ,
153
168
template : fs . existsSync ( template ) ? template : ( fs . existsSync ( htmlPath ) ? htmlPath : defaultHtmlPath ) ,
154
- filename,
169
+ filename : ensureRelative ( outputDir , filename ) ,
155
170
title
156
171
} )
157
172
@@ -162,9 +177,10 @@ module.exports = (api, options) => {
162
177
163
178
if ( ! isLegacyBundle ) {
164
179
pages . forEach ( name => {
165
- const {
166
- filename = `${ name } .html`
167
- } = normalizePageConfig ( multiPageConfig [ name ] )
180
+ const filename = ensureRelative (
181
+ outputDir ,
182
+ normalizePageConfig ( multiPageConfig [ name ] ) . filename || `${ name } .html`
183
+ )
168
184
webpackConfig
169
185
. plugin ( `preload-${ name } ` )
170
186
. use ( PreloadPlugin , [ {
@@ -192,12 +208,13 @@ module.exports = (api, options) => {
192
208
}
193
209
194
210
// copy static assets in public/
195
- if ( ! isLegacyBundle && fs . existsSync ( api . resolve ( 'public' ) ) ) {
211
+ const publicDir = api . resolve ( 'public' )
212
+ if ( ! isLegacyBundle && fs . existsSync ( publicDir ) ) {
196
213
webpackConfig
197
214
. plugin ( 'copy' )
198
215
. use ( require ( 'copy-webpack-plugin' ) , [ [ {
199
- from : api . resolve ( 'public' ) ,
200
- to : api . resolve ( options . outputDir ) ,
216
+ from : publicDir ,
217
+ to : outputDir ,
201
218
ignore : [ 'index.html' , '.DS_Store' ]
202
219
} ] ] )
203
220
}
0 commit comments