33 createVuePartRequest ,
44 parseVuePartRequest ,
55 resolveVuePart ,
6- isVuePartRequest
6+ isVuePartRequest ,
7+ transformRequireToImport
78} from './utils'
89import {
910 createDefaultCompiler ,
@@ -13,26 +14,29 @@ import {
1314 TemplateOptions ,
1415 StyleCompileResult
1516} from '@vue/component-compiler'
16- import { Plugin } from 'rollup'
17+ import { Plugin } from 'rollup'
1718import * 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'
1921
2022const hash = require ( 'hash-sum' )
23+ const d = debug ( 'rollup-plugin-vue' )
24+ const { version } = require ( '../package.json' )
2125
2226export interface VuePluginOptions {
2327 /**
2428 * Include files or directories.
2529 * @default `'.vue'`
2630 */
27- include ?: Array < string | RegExp > | string | RegExp
31+ include ?: Array < string | RegExp > | string | RegExp
2832 /**
2933 * Exclude files or directories.
3034 * @default `undefined`
3135 */
32- exclude ?: Array < string | RegExp > | string | RegExp
36+ exclude ?: Array < string | RegExp > | string | RegExp
3337 /**
3438 * Default language for blocks.
35- *
39+ *
3640 * @default `{}`
3741 * @example
3842 * ```js
@@ -41,7 +45,7 @@ export interface VuePluginOptions {
4145 */
4246 defaultLang ?: {
4347 [ key : string ] : string
44- } ,
48+ }
4549 /**
4650 * Exclude customBlocks for final build.
4751 * @default `['*']`
@@ -99,7 +103,12 @@ export interface VuePluginOptions {
99103 */
100104export default function VuePlugin ( opts : VuePluginOptions = { } ) : Plugin {
101105 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' } ` )
103112
104113 createVuePartRequest . defaultLang = {
105114 ...createVuePartRequest . defaultLang ,
@@ -121,9 +130,23 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
121130 delete opts . include
122131 delete opts . exclude
123132
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+ }
124145 const compiler = createDefaultCompiler ( opts )
125146 const descriptors = new Map < string , SFCDescriptor > ( )
126147
148+ if ( opts . css === false ) d ( 'Running in CSS extract mode' )
149+
127150 return {
128151 name : 'VuePlugin' ,
129152
@@ -154,7 +177,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
154177 const element = resolveVuePart ( descriptors , request )
155178
156179 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 }
158181 : element . content
159182 } ,
160183
@@ -186,6 +209,10 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
186209 descriptor . template
187210 )
188211
212+ input . template . code = transformRequireToImport (
213+ input . template . code
214+ )
215+
189216 if ( input . template . errors && input . template . errors . length ) {
190217 input . template . errors . map ( ( error : Error ) => this . error ( error ) )
191218 }
@@ -197,7 +224,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
197224
198225 input . script = descriptor . script
199226 ? {
200- code : `
227+ code : `
201228 export * from '${ createVuePartRequest (
202229 filename ,
203230 descriptor . script . lang || 'js' ,
@@ -210,13 +237,13 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
210237 ) } '
211238 export default script
212239 `
213- }
214- : { code : '' }
240+ }
241+ : { code : '' }
215242
216243 if ( shouldExtractCss ) {
217244 input . styles = input . styles
218245 . map ( ( style : StyleCompileResult , index : number ) => {
219- ( descriptor . styles [ index ] as any ) . code = style . code
246+ ; ( descriptor . styles [ index ] as any ) . code = style . code
220247
221248 input . script . code +=
222249 '\n' +
@@ -228,7 +255,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
228255 ) } '`
229256
230257 if ( style . module || descriptor . styles [ index ] . scoped ) {
231- return { ...style , code : '' }
258+ return { ...style , code : '' }
232259 }
233260 } )
234261 . filter ( Boolean )
@@ -243,8 +270,8 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
243270 `export * from '${ createVuePartRequest (
244271 filename ,
245272 block . attrs . lang ||
246- createVuePartRequest . defaultLang [ block . type ] ||
247- block . type ,
273+ createVuePartRequest . defaultLang [ block . type ] ||
274+ block . type ,
248275 'customBlocks' ,
249276 index
250277 ) } '`
0 commit comments