-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassetgraph-rollup.js
329 lines (277 loc) · 10.2 KB
/
assetgraph-rollup.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
const pathModule = require('path');
const expect = require('unexpected')
.clone()
.use(require('unexpected-assetgraph'));
const AssetGraph = require('assetgraph');
const assetgraphRollup = require('../lib/assetgraph-rollup');
describe('assetgraph-rollup', function () {
it('should bundle a "trivial site"', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'airquote-trivial-site',
'gridfinder'
),
});
const [htmlAsset] = await assetGraph.loadAssets('/src/index.html');
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetgraphRollup(assetGraph, htmlAsset);
});
it('should bundle assets that are not found on disc', async function () {
const assetGraph = new AssetGraph();
assetGraph.addAsset({
type: 'JavaScript',
url: 'https://example.com/imported.js',
text: 'export function foo (){console.log("Hello")}',
});
assetGraph.addAsset({
type: 'JavaScript',
url: 'https://example.com/entrypoint.js',
text: 'import { foo } from "./imported.js"; foo();',
});
const htmlAsset = assetGraph.addAsset({
type: 'Html',
url: 'https://example.com/index.html',
text: '<!DOCTYPE html><script type="module" src="entrypoint.js"></script>',
});
await assetgraphRollup(assetGraph, htmlAsset);
const bundleJavaScript = htmlAsset.outgoingRelations[0].to;
expect(bundleJavaScript.text, 'to contain', 'Hello');
});
it('should support multiple entry points in one Html asset', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'one-page-multiple-entry-points'
),
});
const [htmlAsset] = await assetGraph.loadAssets('/index.html');
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetGraph.applySourceMaps();
await assetgraphRollup(assetGraph, htmlAsset);
const firstBundleJavaScript = htmlAsset.outgoingRelations[0].to;
const secondBundleJavaScript = htmlAsset.outgoingRelations[1].to;
expect(
firstBundleJavaScript.text,
'to contain',
`greet('the first entry point')`
);
expect(
secondBundleJavaScript.text,
'to contain',
`greet('the second entry point')`
);
const firstBundleDep = firstBundleJavaScript.outgoingRelations[0].to;
const secondBundleDep = secondBundleJavaScript.outgoingRelations[0].to;
expect(firstBundleDep, 'to be', secondBundleDep);
expect(firstBundleDep.text, 'to contain', 'export function greet');
});
it('should bundle entry points on multiple HTML pages with shared bundles', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'two-pages-multiple-entry-points'
),
});
const htmlAssets = await assetGraph.loadAssets([
'/first-page.html',
'/second-page.html',
]);
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetgraphRollup(assetGraph, htmlAssets);
const firstPageFirstBundleJavaScript =
htmlAssets[0].outgoingRelations[0].to;
const firstPageSecondBundleJavaScript =
htmlAssets[0].outgoingRelations[1].to;
expect(
firstPageFirstBundleJavaScript.text,
'to contain',
`greet('the first entry point of the first page')`
);
expect(
firstPageSecondBundleJavaScript.text,
'to contain',
`greet('the second entry point of the first page')`
);
const secondPageFirstBundleJavaScript =
htmlAssets[1].outgoingRelations[0].to;
const secondPageSecondBundleJavaScript =
htmlAssets[1].outgoingRelations[1].to;
expect(
secondPageFirstBundleJavaScript.text,
'to contain',
`greet('the first entry point of the second page')`
);
expect(
secondPageSecondBundleJavaScript.text,
'to contain',
`greet('the second entry point of the second page')`
);
const firstPageFirstBundleDep =
firstPageFirstBundleJavaScript.outgoingRelations[0].to;
const firstPageSecondBundleDep =
firstPageSecondBundleJavaScript.outgoingRelations[0].to;
expect(firstPageFirstBundleDep, 'to be', firstPageSecondBundleDep);
expect(firstPageFirstBundleDep.text, 'to contain', 'export function greet');
const secondPageFirstBundleDep =
secondPageFirstBundleJavaScript.outgoingRelations[0].to;
const secondPageSecondBundleDep =
secondPageSecondBundleJavaScript.outgoingRelations[0].to;
expect(secondPageFirstBundleDep, 'to be', secondPageSecondBundleDep);
expect(firstPageFirstBundleDep, 'to be', secondPageSecondBundleDep);
});
it('should bundle a standalone JavaScript asset', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'one-page-multiple-entry-points'
),
});
const [javaScriptAsset] = await assetGraph.loadAssets(
'/first-entrypoint.js'
);
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetGraph.applySourceMaps();
await assetgraphRollup(assetGraph, javaScriptAsset);
expect(
javaScriptAsset.text,
'to contain',
`greet('the first entry point')`
);
});
it('should bundle multiple standalone JavaScript assets with shared bundles', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'one-page-multiple-entry-points'
),
});
const javaScriptAssets = await assetGraph.loadAssets([
'/first-entrypoint.js',
'/second-entrypoint.js',
]);
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetgraphRollup(assetGraph, javaScriptAssets);
expect(
javaScriptAssets[0].text,
'to contain',
`greet('the first entry point')`
);
expect(
javaScriptAssets[1].text,
'to contain',
`greet('the second entry point')`
);
const firstBundleDep = javaScriptAssets[0].outgoingRelations[0].to;
const secondBundleDep = javaScriptAssets[1].outgoingRelations[0].to;
expect(firstBundleDep, 'to be', secondBundleDep);
expect(firstBundleDep.text, 'to contain', 'export function greet');
});
it('should bundle an inline script', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'inline-script-entry-point'
),
});
const [htmlAsset] = await assetGraph.loadAssets('/index.html');
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetgraphRollup(assetGraph, htmlAsset);
const bundleJavaScript = htmlAsset.outgoingRelations[0].to;
expect(
bundleJavaScript.incomingRelations[0].hrefType,
'to equal',
'inline'
);
expect(
htmlAsset.text,
'to contain',
`<script type="module">function greet(greeter) {
console.log(\`Hello from \${ greeter }\`);
}
greet('the first entry point');</script>`
);
});
it('should extract a source map from rollup and apply it to the bundle asset', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(__dirname, '..', 'testdata', 'single-import'),
});
const [htmlAsset] = await assetGraph.loadAssets('/index.html');
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetgraphRollup(assetGraph, htmlAsset);
const bundleJavaScript = htmlAsset.outgoingRelations[0].to;
const consoleLogStatement = bundleJavaScript.parseTree.body[0].body.body[0];
expect(consoleLogStatement.loc, 'to satisfy', {
source: `${assetGraph.root}imported.js`,
start: { line: 1, column: 22 },
end: { line: 1, column: 22 },
});
});
it('should support existing source maps in the files that go into the bundle', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'existing-source-map'
),
});
const [htmlAsset] = await assetGraph.loadAssets('/index.html');
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetGraph.applySourceMaps();
await assetgraphRollup(assetGraph, htmlAsset);
const bundleJavaScript = htmlAsset.outgoingRelations[0].to;
const iife = bundleJavaScript.parseTree.body[0].expression.callee;
expect(iife.loc, 'to satisfy', {
source: `${assetGraph.root}jquery-1.10.1.js`,
start: { line: 14, column: 0 },
});
});
describe('with a rollup.config.js', function () {
it('should honor the configuration options', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(__dirname, '..', 'testdata', 'config'),
});
const [htmlAsset] = await assetGraph.loadAssets('/index.html');
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetGraph.applySourceMaps();
await assetgraphRollup(assetGraph, htmlAsset, {
configFileName: pathModule.resolve(
__dirname,
'..',
'testdata',
'config',
'rollup.config.js'
),
});
const bundleJavaScript = htmlAsset.outgoingRelations[0].to;
expect(
bundleJavaScript.text,
'to start with',
'/*! Here is a banner specified in the config */'
).and('not to contain', 'true'); // Check that the unnecessary if (true) is eliminated
});
});
// Regression test for https://github.com/assetgraph/assetgraph-rollup/issue/2
it('should not break when there are no JavaScript entry points in a specified HTML asset', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(__dirname, '..', 'testdata', 'no-entry-point'),
});
const [htmlAsset] = await assetGraph.loadAssets('/index.html');
await assetGraph.populate({ followRelations: { crossorigin: false } });
await assetGraph.applySourceMaps();
await assetgraphRollup(assetGraph, htmlAsset);
});
});