3
3
createVuePartRequest ,
4
4
parseVuePartRequest ,
5
5
resolveVuePart ,
6
- isVuePartRequest
6
+ isVuePartRequest ,
7
+ transformRequireToImport
7
8
} from './utils'
8
9
import {
9
10
createDefaultCompiler ,
@@ -13,26 +14,29 @@ import {
13
14
TemplateOptions ,
14
15
StyleCompileResult
15
16
} from '@vue/component-compiler'
16
- import { Plugin } from 'rollup'
17
+ import { Plugin } from 'rollup'
17
18
import * as path from 'path'
18
- import { parse , SFCDescriptor , SFCBlock } from '@vue/component-compiler-utils'
19
+ import { parse , SFCDescriptor , SFCBlock } from '@vue/component-compiler-utils'
20
+ import debug from 'debug'
19
21
20
22
const hash = require ( 'hash-sum' )
23
+ const d = debug ( 'rollup-plugin-vue' )
24
+ const { version } = require ( '../package.json' )
21
25
22
26
export interface VuePluginOptions {
23
27
/**
24
28
* Include files or directories.
25
29
* @default `'.vue'`
26
30
*/
27
- include ?: Array < string | RegExp > | string | RegExp
31
+ include ?: Array < string | RegExp > | string | RegExp
28
32
/**
29
33
* Exclude files or directories.
30
34
* @default `undefined`
31
35
*/
32
- exclude ?: Array < string | RegExp > | string | RegExp
36
+ exclude ?: Array < string | RegExp > | string | RegExp
33
37
/**
34
38
* Default language for blocks.
35
- *
39
+ *
36
40
* @default `{}`
37
41
* @example
38
42
* ```js
@@ -41,7 +45,7 @@ export interface VuePluginOptions {
41
45
*/
42
46
defaultLang ?: {
43
47
[ key : string ] : string
44
- } ,
48
+ }
45
49
/**
46
50
* Exclude customBlocks for final build.
47
51
* @default `['*']`
@@ -99,7 +103,12 @@ export interface VuePluginOptions {
99
103
*/
100
104
export default function VuePlugin ( opts : VuePluginOptions = { } ) : Plugin {
101
105
const isVue = createVueFilter ( opts . include , opts . exclude )
102
- const isProduction = process . env . NODE_ENV === 'production'
106
+ const isProduction =
107
+ process . env . NODE_ENV === 'production' || process . env . BUILD === 'production'
108
+
109
+ d ( 'Version ' + version )
110
+ d ( `Build environment: ${ isProduction ? 'production' : 'development' } ` )
111
+ d ( `Build target: ${ process . env . VUE_ENV || 'browser' } ` )
103
112
104
113
createVuePartRequest . defaultLang = {
105
114
...createVuePartRequest . defaultLang ,
@@ -121,9 +130,23 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
121
130
delete opts . include
122
131
delete opts . exclude
123
132
133
+ opts . template = {
134
+ transformAssetUrls : {
135
+ video : [ 'src' , 'poster' ] ,
136
+ source : 'src' ,
137
+ img : 'src' ,
138
+ image : 'xlink:href'
139
+ } ,
140
+ ...opts . template
141
+ } as any
142
+ if ( opts . template && typeof opts . template . isProduction === 'undefined' ) {
143
+ opts . template . isProduction = isProduction
144
+ }
124
145
const compiler = createDefaultCompiler ( opts )
125
146
const descriptors = new Map < string , SFCDescriptor > ( )
126
147
148
+ if ( opts . css === false ) d ( 'Running in CSS extract mode' )
149
+
127
150
return {
128
151
name : 'VuePlugin' ,
129
152
@@ -154,7 +177,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
154
177
const element = resolveVuePart ( descriptors , request )
155
178
156
179
return 'code' in element
157
- ? ( element as any ) . code as string // .code is set when extract styles is used. { css: false }
180
+ ? ( ( element as any ) . code as string ) // .code is set when extract styles is used. { css: false }
158
181
: element . content
159
182
} ,
160
183
@@ -186,6 +209,10 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
186
209
descriptor . template
187
210
)
188
211
212
+ input . template . code = transformRequireToImport (
213
+ input . template . code
214
+ )
215
+
189
216
if ( input . template . errors && input . template . errors . length ) {
190
217
input . template . errors . map ( ( error : Error ) => this . error ( error ) )
191
218
}
@@ -197,7 +224,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
197
224
198
225
input . script = descriptor . script
199
226
? {
200
- code : `
227
+ code : `
201
228
export * from '${ createVuePartRequest (
202
229
filename ,
203
230
descriptor . script . lang || 'js' ,
@@ -210,13 +237,13 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
210
237
) } '
211
238
export default script
212
239
`
213
- }
214
- : { code : '' }
240
+ }
241
+ : { code : '' }
215
242
216
243
if ( shouldExtractCss ) {
217
244
input . styles = input . styles
218
245
. map ( ( style : StyleCompileResult , index : number ) => {
219
- ( descriptor . styles [ index ] as any ) . code = style . code
246
+ ; ( descriptor . styles [ index ] as any ) . code = style . code
220
247
221
248
input . script . code +=
222
249
'\n' +
@@ -228,7 +255,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
228
255
) } '`
229
256
230
257
if ( style . module || descriptor . styles [ index ] . scoped ) {
231
- return { ...style , code : '' }
258
+ return { ...style , code : '' }
232
259
}
233
260
} )
234
261
. filter ( Boolean )
@@ -243,8 +270,8 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
243
270
`export * from '${ createVuePartRequest (
244
271
filename ,
245
272
block . attrs . lang ||
246
- createVuePartRequest . defaultLang [ block . type ] ||
247
- block . type ,
273
+ createVuePartRequest . defaultLang [ block . type ] ||
274
+ block . type ,
248
275
'customBlocks' ,
249
276
index
250
277
) } '`
0 commit comments