1
1
/*!
2
- * rollup-plugin-vue v1 .0.3
2
+ * rollup-plugin-vue v2 .0.0
3
3
* (c) 2016 undefined
4
4
* Release under the MIT License.
5
5
*/
@@ -16,6 +16,7 @@ var htmlMinifier = _interopDefault(require('html-minifier'));
16
16
var chalk = _interopDefault ( require ( 'chalk' ) ) ;
17
17
var babel = _interopDefault ( require ( 'babel-core' ) ) ;
18
18
var fs = _interopDefault ( require ( 'fs' ) ) ;
19
+ var postcss = _interopDefault ( require ( 'postcss' ) ) ;
19
20
var objectAssign = _interopDefault ( require ( 'object-assign' ) ) ;
20
21
21
22
var babelHelpers = { } ;
@@ -77,6 +78,10 @@ var options = {
77
78
useShortDoctype : true ,
78
79
removeEmptyAttributes : true ,
79
80
removeOptionalTags : true
81
+ } ,
82
+ postcss : {
83
+ plugins : [ ] ,
84
+ options : { }
80
85
}
81
86
} ;
82
87
@@ -154,6 +159,12 @@ function checkLang(node) {
154
159
}
155
160
}
156
161
162
+ /**
163
+ * Pad content with empty lines to get correct line number in errors.
164
+ *
165
+ * @param content
166
+ * @returns {string }
167
+ */
157
168
function padContent ( content ) {
158
169
return content . split ( / \r ? \n / g) . map ( function ( ) {
159
170
return '' ;
@@ -162,7 +173,10 @@ function padContent(content) {
162
173
163
174
var Compiler = function ( ) {
164
175
function Compiler ( ) {
176
+ var options = arguments . length <= 0 || arguments [ 0 ] === undefined ? { } : arguments [ 0 ] ;
165
177
babelHelpers . classCallCheck ( this , Compiler ) ;
178
+
179
+ this . options = options ;
166
180
}
167
181
168
182
babelHelpers . createClass ( Compiler , [ {
@@ -215,6 +229,13 @@ var Compiler = function () {
215
229
} ) ;
216
230
return promise . then ( function ( ) {
217
231
return _this . processTemplate ( components . template , filePath , content ) ;
232
+ } ) . then ( function ( template ) {
233
+ if ( components . style ) {
234
+ return _this . processStyle ( components . style , filePath , content ) . then ( function ( style ) {
235
+ return { template : template . code , style : style . code } ;
236
+ } ) ;
237
+ }
238
+ return { template : template . code , style : '' } ;
218
239
} ) . then ( function ( compiled ) {
219
240
return _this . processScript ( components . script , filePath , content , compiled ) ;
220
241
} ) ;
@@ -236,7 +257,9 @@ var Compiler = function () {
236
257
// TODO: Up next. ${node}, ${filePath}
237
258
return null ;
238
259
}
260
+
239
261
/**
262
+ * Compile template: DeIndent and minify html.
240
263
* @param {Node } node
241
264
* @param {string } filePath
242
265
* @param {string } content
@@ -245,6 +268,8 @@ var Compiler = function () {
245
268
} , {
246
269
key : 'processTemplate' ,
247
270
value : function processTemplate ( node , filePath , content ) {
271
+ var _this2 = this ;
272
+
248
273
var template = deIndent ( this . checkSrc ( node , filePath ) || parse5 . serialize ( node . content ) ) ;
249
274
var lang = checkLang ( node ) ;
250
275
if ( ! lang ) {
@@ -258,9 +283,8 @@ var Compiler = function () {
258
283
} ) ( ) ;
259
284
}
260
285
}
261
-
262
286
return this . compileAsPromise ( 'template' , template , lang , filePath ) . then ( function ( res ) {
263
- res . code = htmlMinifier . minify ( res . code , options . htmlMinifier ) ;
287
+ res . code = htmlMinifier . minify ( res . code , _this2 . options . htmlMinifier ) ;
264
288
return res ;
265
289
} ) ;
266
290
}
@@ -276,36 +300,61 @@ var Compiler = function () {
276
300
value : function processScript ( node , filePath , content , compiled ) {
277
301
var lang = checkLang ( node ) || 'babel' ;
278
302
var script = this . checkSrc ( node , filePath ) ;
303
+ var template = compiled . template ;
304
+
279
305
if ( ! script ) {
280
306
script = parse5 . serialize ( node ) ;
281
307
// pad the script to ensure correct line number for syntax errors
282
308
var location = content . indexOf ( script ) ;
283
309
var before = padContent ( content . slice ( 0 , location ) ) ;
284
310
script = before + script ;
285
311
}
286
- script = this . injectTemplate ( script , compiled . code , lang ) ;
312
+ script = this . injectTemplate ( script , template , lang ) ;
287
313
script = deIndent ( script ) ;
288
- return this . compileAsPromise ( 'script' , script , lang , filePath ) ;
314
+ return this . compileAsPromise ( 'script' , script , lang , filePath ) . then ( function ( res ) {
315
+ return { code : res . code } ;
316
+ } ) ;
289
317
}
290
318
/**
291
319
* @param {Node } node
292
- * @param {string } path
320
+ * @param {string } filePath
293
321
* @param {string } content
294
322
*/
295
323
296
324
} , {
297
325
key : 'processStyle' ,
298
- value : function processStyle ( node , path , content ) { }
326
+ value : function processStyle ( node , filePath , content ) {
327
+ var _this3 = this ;
328
+
329
+ var lang = checkLang ( node ) || 'css' ;
330
+ var style = this . checkSrc ( node , filePath ) ;
331
+ var injectFnName = '__$styleInject' ;
332
+ if ( ! style ) {
333
+ style = parse5 . serialize ( node ) ;
334
+ var location = content . indexOf ( style ) ;
335
+ var before = padContent ( content . slice ( 0 , location ) ) ;
336
+ style = before + style ;
337
+ }
338
+ var options = this . options . postcss ;
339
+ options . from = filePath ;
340
+ options . to = filePath ;
341
+ return this . compileAsPromise ( 'style' , style , lang , filePath ) . then ( function ( res ) {
342
+ return postcss ( _this3 . options . postcss . plugins || [ ] ) . process ( res . code , options ) . then ( function ( res ) {
343
+ var code = 'export ' + injectFnName + '(' + JSON . stringify ( res . css ) + ');' ;
344
+ return { code : code , type : 'style' } ;
345
+ } ) ;
346
+ } ) ;
347
+ }
299
348
} , {
300
349
key : 'compileAsPromise' ,
301
350
value : function compileAsPromise ( type , code , lang , filePath ) {
302
- var _this2 = this ;
351
+ var _this4 = this ;
303
352
304
353
var compiler = compilers [ lang ] ;
305
354
if ( compiler ) {
306
355
return new Promise ( function ( resolve , reject ) {
307
356
try {
308
- var compiled = compiler . compile ( code , _this2 , filePath ) ;
357
+ var compiled = compiler . compile ( code , _this4 , filePath ) ;
309
358
resolve ( compiled ) ;
310
359
} catch ( e ) {
311
360
reject ( e ) ;
@@ -327,22 +376,26 @@ var Compiler = function () {
327
376
return Compiler ;
328
377
} ( ) ;
329
378
330
- var compiler = new Compiler ( ) ;
331
-
332
379
function plugin ( ) {
333
- var options = arguments . length <= 0 || arguments [ 0 ] === undefined ? { } : arguments [ 0 ] ;
380
+ var options$$ = arguments . length <= 0 || arguments [ 0 ] === undefined ? { } : arguments [ 0 ] ;
381
+
382
+ options$$ = objectAssign ( { } , options , options$$ , { extensions : [ '.vue' ] } ) ;
383
+ var filter = rollupPluginutils . createFilter ( options$$ . include , options$$ . exclude ) ;
384
+ var extensions = options$$ . extensions ;
385
+ delete options$$ . extensions ;
386
+ delete options$$ . include ;
387
+ delete options$$ . exclude ;
334
388
335
- options = objectAssign ( { } , options , { extensions : [ '.vue' ] } ) ;
336
- var filter = rollupPluginutils . createFilter ( options . include , options . exclude ) ;
337
- var extensions = options . extensions ;
338
- delete options . extensions ;
339
- delete options . include ;
340
- delete options . exclude ;
389
+ var compiler = new Compiler ( options$$ ) ;
341
390
342
391
return {
343
392
transform : function transform ( code , id ) {
344
- if ( ! filter ( id ) ) return null ;
345
- if ( extensions . indexOf ( path . extname ( id ) ) === - 1 ) return null ;
393
+ if ( ! filter ( id ) ) {
394
+ return null ;
395
+ }
396
+ if ( extensions . indexOf ( path . extname ( id ) ) === - 1 ) {
397
+ return null ;
398
+ }
346
399
347
400
return new Promise ( function ( resolve ) {
348
401
compiler . compile ( code , id ) . then ( function ( compiled ) {
0 commit comments