forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.js
339 lines (298 loc) · 8.82 KB
/
html.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
const assert = require('assert');
const fs = require('fs');
const {bundle, assertBundleTree} = require('./utils');
describe('html', function() {
it('should support bundling HTML', async function() {
let b = await bundle(__dirname + '/integration/html/index.html');
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'css',
assets: ['index.css'],
childBundles: []
},
{
type: 'js',
assets: ['index.js'],
childBundles: []
},
{
type: 'html',
assets: ['other.html'],
childBundles: [
{
type: 'css',
assets: ['index.css'],
childBundles: []
},
{
type: 'js',
assets: ['index.js'],
childBundles: []
}
]
}
]
});
let files = fs.readdirSync(__dirname + '/dist');
let html = fs.readFileSync(__dirname + '/dist/index.html');
for (let file of files) {
if (file !== 'index.html') {
assert(html.includes(file));
}
}
});
it('should find href attr when not first', async function() {
let b = await bundle(__dirname + '/integration/html-attr-order/index.html');
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'html',
assets: ['other.html'],
childBundles: []
}
]
});
});
it('should support transforming HTML with posthtml', async function() {
let b = await bundle(__dirname + '/integration/posthtml/index.html');
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: []
});
let html = fs.readFileSync(__dirname + '/dist/index.html');
assert(html.includes('<h1>Other page</h1>'));
});
it('should find assets inside posthtml', async function() {
let b = await bundle(__dirname + '/integration/posthtml-assets/index.html');
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'js',
assets: ['index.js'],
childBundles: []
}
]
});
});
it('should insert sibling CSS bundles for JS files in the HEAD', async function() {
let b = await bundle(__dirname + '/integration/html-css/index.html');
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'js',
assets: ['index.js', 'index.css'],
childBundles: [
{
type: 'css',
assets: ['index.css'],
childBundles: []
}
]
}
]
});
let html = fs.readFileSync(__dirname + '/dist/index.html');
assert(
/<link rel="stylesheet" href="[/\\]{1}dist[/\\]{1}[a-f0-9]+\.css">/.test(
html
)
);
});
it('should insert a HEAD element if needed when adding CSS bundles', async function() {
let b = await bundle(__dirname + '/integration/html-css-head/index.html');
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'js',
assets: ['index.js', 'index.css'],
childBundles: [
{
type: 'css',
assets: ['index.css'],
childBundles: []
}
]
}
]
});
let html = fs.readFileSync(__dirname + '/dist/index.html');
assert(
/<head><link rel="stylesheet" href="[/\\]{1}dist[/\\]{1}[a-f0-9]+\.css"><\/head>/.test(
html
)
);
});
it('should insert sibling JS bundles for CSS files in the HEAD', async function() {
let b = await bundle(__dirname + '/integration/html-css-js/index.html', {
hmr: true
});
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'css',
assets: ['index.css'],
childBundles: [
{
type: 'js',
assets: [
'index.css',
'bundle-url.js',
'css-loader.js',
'hmr-runtime.js'
],
childBundles: []
}
]
}
]
});
let html = fs.readFileSync(__dirname + '/dist/index.html');
assert(/<script src="[/\\]{1}dist[/\\]{1}[a-f0-9]+\.js">/.test(html));
});
it('should minify HTML in production mode', async function() {
await bundle(__dirname + '/integration/htmlnano/index.html', {
production: true
});
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
assert(html.includes('Other page'));
assert(!html.includes('\n'));
});
it('should read .htmlnanorc and minify HTML in production mode', async function() {
await bundle(__dirname + '/integration/htmlnano-config/index.html', {
production: true
});
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
// mergeStyles
assert(
html.includes(
'<style>h1{color:red}div{font-size:20px}</style><style media="print">div{color:blue}</style>'
)
);
// minifyJson
assert(
html.includes('<script type="application/json">{"user":"me"}</script>')
);
// minifySvg is false
assert(
html.includes(
'<svg version="1.1" baseprofile="full" width="300" height="200" xmlns="http://www.w3.org/2000/svg"><rect width="100%" height="100%" fill="red"></rect><circle cx="150" cy="100" r="80" fill="green"></circle><text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text></svg>'
)
);
});
it('should not prepend the public path to assets with remote URLs', async function() {
await bundle(__dirname + '/integration/html/index.html');
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
assert(
html.includes('<script src="https://unpkg.com/parcel-bundler"></script>')
);
});
it('should not prepend the public path to hash links', async function() {
await bundle(__dirname + '/integration/html/index.html');
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
assert(html.includes('<a href="#hash_link">'));
});
it('Should detect virtual paths', async function() {
let b = await bundle(
__dirname + '/integration/html-virtualpath/index.html'
);
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'html',
assets: ['other.html'],
childBundles: []
}
]
});
});
it('should not update root/main file in the bundles', async function() {
await bundle(__dirname + '/integration/html-root/index.html');
let files = fs.readdirSync(__dirname + '/dist');
for (let file of files) {
if (file !== 'index.html' && file.endsWith('.html')) {
let html = fs.readFileSync(__dirname + '/dist/' + file);
assert(html.includes('index.html'));
}
}
});
it('should conserve the spacing in the HTML tags', async function() {
await bundle(__dirname + '/integration/html/index.html', {
production: true
});
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
assert(/<i>hello<\/i> <i>world<\/i>/.test(html));
});
it('should support child bundles of different types', async function() {
let b = await bundle(
__dirname + '/integration/child-bundle-different-types/index.html'
);
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'js',
assets: ['main.js', 'util.js', 'other.js'],
childBundles: []
},
{
type: 'html',
assets: ['other.html'],
childBundles: [
{
type: 'js',
assets: ['index.js', 'util.js', 'other.js'],
childBundles: []
}
]
}
]
});
});
it('should support circular dependencies', async function() {
let b = await bundle(__dirname + '/integration/circular/index.html');
assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'html',
assets: ['about.html'],
childBundles: [
{
type: 'js',
assets: ['about.js', 'index.js'],
childBundles: []
},
{
type: 'html',
assets: ['test.html'],
childBundles: []
}
]
},
{
type: 'js',
assets: ['about.js', 'index.js'],
childBundles: []
}
]
});
});
});