@@ -25,7 +25,7 @@ export type Stage =
25
25
26
26
export interface Options {
27
27
disabled ?: boolean
28
- name : string | ( ( hash : Compilation [ 'hash' ] ) => string )
28
+ name : string | string [ ] | ( ( hash : Compilation [ 'hash' ] ) => string | string [ ] )
29
29
content : RawContent | ( ( assets : Compilation [ 'assets' ] ) => PromiseLike < RawContent > | RawContent )
30
30
stage ?: Stage
31
31
}
@@ -39,9 +39,7 @@ export const emitFile = (options: Options): WebpackPluginFunction => {
39
39
return
40
40
}
41
41
compiler . hooks . thisCompilation . tap ( pluginName , ( compilation ) => {
42
- const basePath = compilation . options . output . path ?? ''
43
- const fileName = typeof options . name === 'function' ? options . name ( compilation . hash ) : options . name
44
- const filePath = path . relative ( basePath , path . resolve ( basePath , fileName ) )
42
+ const filePath = resolveFilePath ( compilation . options . output . path ?? '' , options . name , compilation . hash )
45
43
compilation . hooks . processAssets . tapPromise ( tapOptions , async ( ) => {
46
44
const source = await toSource ( options . content , compilation . assets )
47
45
if ( source !== null ) {
@@ -76,3 +74,11 @@ async function toSource(input: Options['content'], assets: Compilation['assets']
76
74
}
77
75
return null
78
76
}
77
+
78
+ function resolveFilePath ( basePath : string , name : Options [ 'name' ] , hash ?: string ) {
79
+ let parts = typeof name === 'function' ? name ( hash ) : name
80
+ if ( ! Array . isArray ( parts ) ) {
81
+ parts = [ parts ]
82
+ }
83
+ return path . relative ( basePath , path . resolve ( basePath , ...parts ) )
84
+ }
0 commit comments