1
1
import assert from 'assert' ;
2
- import * as fs from 'fs' ;
3
- import { parse } from 'acorn' ;
4
- import { addLineNumbers , env , normalizeHtml , svelte } from '../helpers.js' ;
5
-
6
- function tryRequire ( file ) {
7
- try {
8
- const mod = require ( file ) ;
9
- return mod . default || mod ;
10
- } catch ( err ) {
11
- if ( err . code !== 'MODULE_NOT_FOUND' ) throw err ;
12
- return null ;
13
- }
14
- }
15
-
16
- function normalizeWarning ( warning ) {
17
- warning . frame = warning . frame . replace ( / ^ \n / , '' ) .
18
- replace ( / ^ \t + / gm, '' ) .
19
- replace ( / \s + $ / gm, '' ) ;
20
- delete warning . filename ;
21
- delete warning . toString ;
22
- return warning ;
23
- }
24
-
25
- function checkCodeIsValid ( code ) {
26
- try {
27
- parse ( code ) ;
28
- } catch ( err ) {
29
- console . error ( addLineNumbers ( code ) ) ;
30
- throw new Error ( err . message ) ;
31
- }
32
- }
2
+ import { svelte } from '../helpers.js' ;
33
3
34
4
describe . only ( 'preprocess' , ( ) => {
35
- fs . readdirSync ( 'test/preprocess/samples' ) . forEach ( dir => {
36
- if ( dir [ 0 ] === '.' ) return ;
37
-
38
- // add .solo to a sample directory name to only run that test
39
- const solo = / \. s o l o / . test ( dir ) ;
40
- const skip = / \. s k i p / . test ( dir ) ;
41
-
42
- if ( solo && process . env . CI ) {
43
- throw new Error ( 'Forgot to remove `solo: true` from test' ) ;
44
- }
45
-
46
- ( solo ? it . only : skip ? it . skip : it ) ( dir , ( ) => {
47
- const config = tryRequire ( `./samples/${ dir } /_config.js` ) || { } ;
48
- const input = fs . existsSync ( `test/preprocess/samples/${ dir } /input.pug` ) ?
49
- read ( `test/preprocess/samples/${ dir } /input.pug` ) :
50
- read ( `test/preprocess/samples/${ dir } /input.html` ) ;
51
-
52
- return svelte . preprocess ( input , config )
53
- . then ( processed => processed . toString ( ) )
54
- . then ( processed => {
55
- const expectedWarnings = ( config . warnings || [ ] ) . map (
56
- normalizeWarning ) ;
57
- const domWarnings = [ ] ;
58
- const ssrWarnings = [ ] ;
59
-
60
- const dom = svelte . compile (
61
- processed ,
62
- Object . assign ( config , {
63
- format : 'iife' ,
64
- name : 'SvelteComponent' ,
65
- onwarn : warning => {
66
- domWarnings . push ( warning ) ;
67
- } ,
68
- } )
69
- ) ;
70
-
71
- const ssr = svelte . compile (
72
- processed ,
73
- Object . assign ( config , {
74
- format : 'iife' ,
75
- generate : 'ssr' ,
76
- name : 'SvelteComponent' ,
77
- onwarn : warning => {
78
- ssrWarnings . push ( warning ) ;
79
- } ,
80
- } )
81
- ) ;
82
-
83
- // check the code is valid
84
- checkCodeIsValid ( dom . code ) ;
85
- checkCodeIsValid ( ssr . code ) ;
86
-
87
- assert . equal ( dom . css , ssr . css ) ;
88
-
89
- assert . deepEqual (
90
- domWarnings . map ( normalizeWarning ) ,
91
- ssrWarnings . map ( normalizeWarning )
92
- ) ;
93
- assert . deepEqual ( domWarnings . map ( normalizeWarning ) , expectedWarnings ) ;
94
-
95
- const expected = {
96
- html : read ( `test/preprocess/samples/${ dir } /expected.html` ) ,
97
- css : read ( `test/preprocess/samples/${ dir } /expected.css` ) ,
98
- } ;
99
-
100
- if ( expected . css !== null ) {
101
- fs . writeFileSync ( `test/preprocess/samples/${ dir } /_actual.css` ,
102
- dom . css ) ;
103
- assert . equal ( dom . css . replace ( / s v e l t e - \d + / g, 'svelte-xyz' ) ,
104
- expected . css ) ;
105
- }
106
-
107
- // verify that the right elements have scoping selectors
108
- if ( expected . html !== null ) {
109
- const window = env ( ) ;
110
-
111
- // dom
112
- try {
113
- const Component = eval (
114
- `(function () { ${ dom . code } ; return SvelteComponent; }())`
115
- ) ;
116
- const target = window . document . querySelector ( 'main' ) ;
117
-
118
- new Component ( { target, data : config . data } ) ;
119
- const html = target . innerHTML ;
120
-
121
- fs . writeFileSync ( `test/preprocess/samples/${ dir } /_actual.html` ,
122
- html ) ;
123
-
124
- assert . equal (
125
- normalizeHtml ( window ,
126
- html . replace ( / s v e l t e - \d + / g, 'svelte-xyz' ) ) ,
127
- normalizeHtml ( window , expected . html )
128
- ) ;
129
- } catch ( err ) {
130
- console . log ( dom . code ) ;
131
- throw err ;
132
- }
133
-
134
- // ssr
135
- try {
136
- const component = eval (
137
- `(function () { ${ ssr . code } ; return SvelteComponent; }())`
138
- ) ;
139
-
140
- assert . equal (
141
- normalizeHtml (
142
- window ,
143
- component . render ( config . data ) .
144
- replace ( / s v e l t e - \d + / g, 'svelte-xyz' )
145
- ) ,
146
- normalizeHtml ( window , expected . html )
147
- ) ;
148
- } catch ( err ) {
149
- console . log ( ssr . code ) ;
150
- throw err ;
151
- }
152
- }
5
+ it ( 'preprocesses entire component' , ( ) => {
6
+ const source = `
7
+ <h1>Hello __NAME__!</h1>
8
+ ` ;
9
+
10
+ const expected = `
11
+ <h1>Hello world!</h1>
12
+ ` ;
13
+
14
+ return svelte . preprocess ( source , {
15
+ markup : ( { content } ) => {
16
+ return {
17
+ code : content . replace ( '__NAME__' , 'world' )
18
+ } ;
19
+ }
20
+ } ) . then ( processed => {
21
+ assert . equal ( processed . toString ( ) , expected ) ;
22
+ } ) ;
23
+ } ) ;
24
+
25
+ it ( 'preprocesses style' , ( ) => {
26
+ const source = `
27
+ <div class='brand-color'>$brand</div>
28
+
29
+ <style>
30
+ .brand-color {
31
+ color: $brand;
32
+ }
33
+ </style>
34
+ ` ;
35
+
36
+ const expected = `
37
+ <div class='brand-color'>$brand</div>
38
+
39
+ <style>
40
+ .brand-color {
41
+ color: purple;
42
+ }
43
+ </style>
44
+ ` ;
45
+
46
+ return svelte . preprocess ( source , {
47
+ style : ( { content } ) => {
48
+ return {
49
+ code : content . replace ( '$brand' , 'purple' )
50
+ } ;
51
+ }
52
+ } ) . then ( processed => {
53
+ assert . equal ( processed . toString ( ) , expected ) ;
54
+ } ) ;
55
+ } ) ;
56
+
57
+ it ( 'preprocesses style asynchronously' , ( ) => {
58
+ const source = `
59
+ <div class='brand-color'>$brand</div>
60
+
61
+ <style>
62
+ .brand-color {
63
+ color: $brand;
64
+ }
65
+ </style>
66
+ ` ;
67
+
68
+ const expected = `
69
+ <div class='brand-color'>$brand</div>
70
+
71
+ <style>
72
+ .brand-color {
73
+ color: purple;
74
+ }
75
+ </style>
76
+ ` ;
77
+
78
+ return svelte . preprocess ( source , {
79
+ style : ( { content } ) => {
80
+ return Promise . resolve ( {
81
+ code : content . replace ( '$brand' , 'purple' )
153
82
} ) ;
83
+ }
84
+ } ) . then ( processed => {
85
+ assert . equal ( processed . toString ( ) , expected ) ;
86
+ } ) ;
87
+ } ) ;
88
+
89
+ it ( 'preprocesses script' , ( ) => {
90
+ const source = `
91
+ <script>
92
+ console.log(__THE_ANSWER__);
93
+ </script>
94
+ ` ;
95
+
96
+ const expected = `
97
+ <script>
98
+ console.log(42);
99
+ </script>
100
+ ` ;
101
+
102
+ return svelte . preprocess ( source , {
103
+ script : ( { content } ) => {
104
+ return {
105
+ code : content . replace ( '__THE_ANSWER__' , '42' )
106
+ } ;
107
+ }
108
+ } ) . then ( processed => {
109
+ assert . equal ( processed . toString ( ) , expected ) ;
110
+ } ) ;
111
+ } ) ;
112
+
113
+ it ( 'parses attributes' , ( ) => {
114
+ const source = `
115
+ <style type='text/scss' bool></style>
116
+ ` ;
117
+
118
+ return svelte . preprocess ( source , {
119
+ style : ( { attributes } ) => {
120
+ assert . deepEqual ( attributes , {
121
+ type : 'text/scss' ,
122
+ bool : true
123
+ } ) ;
124
+ }
125
+ } ) ;
126
+ } ) ;
127
+
128
+ it ( 'ignores null/undefined returned from preprocessor' , ( ) => {
129
+ const source = `
130
+ <script>
131
+ console.log('ignore me');
132
+ </script>
133
+ ` ;
134
+
135
+ const expected = `
136
+ <script>
137
+ console.log('ignore me');
138
+ </script>
139
+ ` ;
140
+
141
+ return svelte . preprocess ( source , {
142
+ script : ( ) => null
143
+ } ) . then ( processed => {
144
+ assert . equal ( processed . toString ( ) , expected ) ;
154
145
} ) ;
155
146
} ) ;
156
- } ) ;
157
-
158
- function read ( file ) {
159
- try {
160
- return fs . readFileSync ( file , 'utf-8' ) ;
161
- } catch ( err ) {
162
- return null ;
163
- }
164
- }
147
+ } ) ;
0 commit comments