1
- const { compileTemplate } = require ( ' @vue/component-compiler-utils' ) ;
2
- const compiler = require ( ' vue-template-compiler' ) ;
1
+ const { compileTemplate } = require ( " @vue/component-compiler-utils" ) ;
2
+ const compiler = require ( " vue-template-compiler" ) ;
3
3
4
4
function stripScript ( content ) {
5
- const result = content . match ( / < ( s c r i p t ) > ( [ \s \S ] + ) < \/ \1> / ) ;
6
- return result && result [ 2 ] ? result [ 2 ] . trim ( ) : '' ;
5
+ const result = content . match ( / < ( s c r i p t ) > ( [ \s \S ] + ) < \/ \1> / ) ;
6
+ return result && result [ 2 ] ? result [ 2 ] . trim ( ) : "" ;
7
7
}
8
8
9
9
function stripStyle ( content ) {
10
- const result = content . match ( / < ( s t y l e ) \s * > ( [ \s \S ] + ) < \/ \1> / ) ;
11
- return result && result [ 2 ] ? result [ 2 ] . trim ( ) : '' ;
10
+ const result = content . match ( / < ( s t y l e ) [ \s \S ] * > ( [ \s \S ] + ) < \/ \1> / ) ;
11
+ return result && result [ 2 ] ? result [ 2 ] . trim ( ) : "" ;
12
12
}
13
13
14
14
// 编写例子时不一定有 template。所以采取的方案是剔除其他的内容
15
15
function stripTemplate ( content ) {
16
- content = content . trim ( ) ;
17
- if ( ! content ) {
18
- return content ;
19
- }
20
- return content . replace ( / < ( s c r i p t | s t y l e ) [ \s \S ] + < \/ \1> / g, '' ) . trim ( ) ;
16
+ content = content . trim ( ) ;
17
+ if ( ! content ) {
18
+ return content ;
19
+ }
20
+ return content . replace ( / < ( s c r i p t | s t y l e ) [ \s \S ] + < \/ \1> / g, "" ) . trim ( ) ;
21
21
}
22
22
23
23
function pad ( source ) {
24
- return source
25
- . split ( / \r ? \n / )
26
- . map ( line => ` ${ line } ` )
27
- . join ( '\n' ) ;
24
+ return source
25
+ . split ( / \r ? \n / )
26
+ . map ( line => ` ${ line } ` )
27
+ . join ( "\n" ) ;
28
28
}
29
29
30
30
function genInlineComponentText ( template , script ) {
31
-
32
- // https://github.com/vuejs/vue-loader/blob/423b8341ab368c2117931e909e2da9af74503635/lib/loaders/templateLoader.js#L46
33
- const finalOptions = {
34
- source : `<div>${ template } </div>` ,
35
- filename : 'inline-component' , // TODO:这里有待调整
36
- compiler
37
- } ;
38
- const compiled = compileTemplate ( finalOptions ) ;
39
- // tips
40
- if ( compiled . tips && compiled . tips . length ) {
41
- compiled . tips . forEach ( tip => {
42
- console . warn ( tip ) ;
43
- } ) ;
44
- }
45
- // errors
46
- if ( compiled . errors && compiled . errors . length ) {
47
- console . error (
48
- `\n Error compiling template:\n${ pad ( compiled . source ) } \n` +
49
- compiled . errors . map ( e => ` - ${ e } ` ) . join ( '\n' ) +
50
- '\n'
51
- ) ;
52
- }
53
- let demoComponentContent = `
31
+ // https://github.com/vuejs/vue-loader/blob/423b8341ab368c2117931e909e2da9af74503635/lib/loaders/templateLoader.js#L46
32
+ const finalOptions = {
33
+ source : `<div>${ template } </div>` ,
34
+ filename : "inline-component" , // TODO:这里有待调整
35
+ compiler
36
+ } ;
37
+ const compiled = compileTemplate ( finalOptions ) ;
38
+ // tips
39
+ if ( compiled . tips && compiled . tips . length ) {
40
+ compiled . tips . forEach ( tip => {
41
+ console . warn ( tip ) ;
42
+ } ) ;
43
+ }
44
+ // errors
45
+ if ( compiled . errors && compiled . errors . length ) {
46
+ console . error (
47
+ `\n Error compiling template:\n${ pad ( compiled . source ) } \n` +
48
+ compiled . errors . map ( e => ` - ${ e } ` ) . join ( "\n" ) +
49
+ "\n"
50
+ ) ;
51
+ }
52
+ let demoComponentContent = `
54
53
${ compiled . code }
55
54
` ;
56
55
57
- // todo: 这里采用了硬编码有待改进
58
- script = script . trim ( ) ;
59
- if ( script ) {
60
- script = script . replace ( / e x p o r t \s + d e f a u l t / , 'const democomponentExport =' ) ;
61
- } else {
62
- script = 'const democomponentExport = {}' ;
63
- }
64
- demoComponentContent = `(function() {
56
+ // todo: 这里采用了硬编码有待改进
57
+ script = script . trim ( ) ;
58
+ if ( script ) {
59
+ script = script . replace (
60
+ / e x p o r t \s + d e f a u l t / ,
61
+ "const democomponentExport ="
62
+ ) ;
63
+ } else {
64
+ script = "const democomponentExport = {}" ;
65
+ }
66
+ demoComponentContent = `(function() {
65
67
${ demoComponentContent }
66
68
${ script }
67
69
return {
@@ -70,12 +72,12 @@ function genInlineComponentText(template, script) {
70
72
...democomponentExport
71
73
}
72
74
})()` ;
73
- return demoComponentContent ;
75
+ return demoComponentContent ;
74
76
}
75
77
76
78
module . exports = {
77
- stripScript,
78
- stripStyle,
79
- stripTemplate,
80
- genInlineComponentText
79
+ stripScript,
80
+ stripStyle,
81
+ stripTemplate,
82
+ genInlineComponentText
81
83
} ;
0 commit comments