UNPKG

10.1 kBJavaScriptView Raw
1// @flow
2
3import assert from 'assert';
4import AssetGraph, {nodeFromAssetGroup} from '../src/AssetGraph';
5import {createDependency} from '../src/Dependency';
6import {createAsset} from '../src/InternalAsset';
7import {createEnvironment} from '../src/Environment';
8
9const DEFAULT_ENV = createEnvironment({
10 context: 'browser',
11 engines: {
12 browsers: ['> 1%']
13 }
14});
15
16const TARGETS = [
17 {
18 name: 'test',
19 distDir: 'dist',
20 distEntry: 'out.js',
21 env: DEFAULT_ENV,
22 publicUrl: null
23 }
24];
25
26const stats = {size: 0, time: 0};
27
28describe('AssetGraph', () => {
29 it('initialization should create one root node with edges to entry_specifier nodes for each entry', () => {
30 let graph = new AssetGraph();
31 graph.initialize({
32 entries: ['/path/to/index1', '/path/to/index2']
33 });
34
35 assert(graph.nodes.has('@@root'));
36 assert(graph.nodes.has('entry_specifier:/path/to/index1'));
37 assert(graph.nodes.has('entry_specifier:/path/to/index2'));
38 });
39
40 it('resolveEntry should connect an entry_specifier node to entry_file nodes', () => {
41 let graph = new AssetGraph();
42 graph.initialize({
43 entries: ['/path/to/index1', '/path/to/index2']
44 });
45
46 graph.resolveEntry('/path/to/index1', ['/path/to/index1/src/main.js']);
47
48 assert(graph.nodes.has('entry_file:/path/to/index1/src/main.js'));
49 assert(
50 graph.hasEdge(
51 'entry_specifier:/path/to/index1',
52 'entry_file:/path/to/index1/src/main.js'
53 )
54 );
55 });
56
57 it('resolveTargets should connect an entry_file node to dependencies for each target', () => {
58 let graph = new AssetGraph();
59 graph.initialize({
60 entries: ['/path/to/index1', '/path/to/index2']
61 });
62
63 graph.resolveEntry('/path/to/index1', ['/path/to/index1/src/main.js']);
64 graph.resolveEntry('/path/to/index2', ['/path/to/index2/src/main.js']);
65
66 graph.resolveTargets('/path/to/index1/src/main.js', TARGETS);
67 graph.resolveTargets('/path/to/index2/src/main.js', TARGETS);
68
69 assert(
70 graph.nodes.has(
71 createDependency({
72 moduleSpecifier: '/path/to/index1/src/main.js',
73 pipeline: 'test',
74 target: TARGETS[0],
75 env: DEFAULT_ENV
76 }).id
77 )
78 );
79 assert(
80 graph.nodes.has(
81 createDependency({
82 moduleSpecifier: '/path/to/index2/src/main.js',
83 pipeline: 'test',
84 target: TARGETS[0],
85 env: DEFAULT_ENV
86 }).id
87 )
88 );
89 assert.deepEqual(graph.getAllEdges(), [
90 {
91 from: '@@root',
92 to: 'entry_specifier:/path/to/index1',
93 type: null
94 },
95 {
96 from: '@@root',
97 to: 'entry_specifier:/path/to/index2',
98 type: null
99 },
100 {
101 from: 'entry_specifier:/path/to/index1',
102 to: 'entry_file:/path/to/index1/src/main.js',
103 type: null
104 },
105 {
106 from: 'entry_specifier:/path/to/index2',
107 to: 'entry_file:/path/to/index2/src/main.js',
108 type: null
109 },
110 {
111 from: 'entry_file:/path/to/index1/src/main.js',
112 to: createDependency({
113 moduleSpecifier: '/path/to/index1/src/main.js',
114 pipeline: 'test',
115 target: TARGETS[0],
116 env: DEFAULT_ENV
117 }).id,
118 type: null
119 },
120 {
121 from: 'entry_file:/path/to/index2/src/main.js',
122 to: createDependency({
123 moduleSpecifier: '/path/to/index2/src/main.js',
124 pipeline: 'test',
125 target: TARGETS[0],
126 env: DEFAULT_ENV
127 }).id,
128 type: null
129 }
130 ]);
131 });
132
133 it('resolveDependency should update the file a dependency is connected to', () => {
134 let graph = new AssetGraph();
135 graph.initialize({
136 targets: TARGETS,
137 entries: ['/path/to/index']
138 });
139
140 graph.resolveEntry('/path/to/index', ['/path/to/index/src/main.js']);
141 graph.resolveTargets('/path/to/index/src/main.js', TARGETS);
142
143 let dep = createDependency({
144 moduleSpecifier: '/path/to/index/src/main.js',
145 pipeline: 'test',
146 target: TARGETS[0],
147 env: DEFAULT_ENV
148 });
149 let req = {filePath: '/index.js', env: DEFAULT_ENV};
150
151 graph.resolveDependency(dep, nodeFromAssetGroup(req));
152 assert(graph.nodes.has(nodeFromAssetGroup(req).id));
153 assert(graph.hasEdge(dep.id, nodeFromAssetGroup(req).id));
154
155 let req2 = {filePath: '/index.jsx', env: DEFAULT_ENV};
156 graph.resolveDependency(dep, nodeFromAssetGroup(req2));
157 assert(!graph.nodes.has(nodeFromAssetGroup(req).id));
158 assert(graph.nodes.has(nodeFromAssetGroup(req2).id));
159 assert(graph.hasEdge(dep.id, nodeFromAssetGroup(req2).id));
160 assert(!graph.hasEdge(dep.id, nodeFromAssetGroup(req).id));
161
162 graph.resolveDependency(dep, nodeFromAssetGroup(req2));
163 assert(graph.nodes.has(nodeFromAssetGroup(req2).id));
164 assert(graph.hasEdge(dep.id, nodeFromAssetGroup(req2).id));
165 });
166
167 it('resolveAssetGroup should update the asset and dep nodes a file is connected to', () => {
168 let graph = new AssetGraph();
169 graph.initialize({
170 targets: TARGETS,
171 entries: ['/path/to/index']
172 });
173
174 graph.resolveEntry('/path/to/index', ['/path/to/index/src/main.js']);
175 graph.resolveTargets('/path/to/index/src/main.js', TARGETS);
176
177 let dep = createDependency({
178 moduleSpecifier: '/path/to/index/src/main.js',
179 pipeline: 'test',
180 target: TARGETS[0],
181 env: DEFAULT_ENV,
182 sourcePath: ''
183 });
184 let filePath = '/index.js';
185 let req = {filePath, env: DEFAULT_ENV};
186 graph.resolveDependency(dep, nodeFromAssetGroup(req));
187 let sourcePath = filePath;
188 let assets = [
189 createAsset({
190 id: '1',
191 filePath,
192 type: 'js',
193 isSource: true,
194 hash: '#1',
195 stats,
196 dependencies: new Map([
197 [
198 'utils',
199 createDependency({
200 moduleSpecifier: './utils',
201 env: DEFAULT_ENV,
202 sourcePath
203 })
204 ]
205 ]),
206 env: DEFAULT_ENV,
207 includedFiles: new Map()
208 }),
209 createAsset({
210 id: '2',
211 filePath,
212 type: 'js',
213 isSource: true,
214 hash: '#2',
215 stats,
216 dependencies: new Map([
217 [
218 'styles',
219 createDependency({
220 moduleSpecifier: './styles',
221 env: DEFAULT_ENV,
222 sourcePath
223 })
224 ]
225 ]),
226 env: DEFAULT_ENV,
227 includedFiles: new Map()
228 }),
229 createAsset({
230 id: '3',
231 filePath,
232 type: 'js',
233 isSource: true,
234 hash: '#3',
235 dependencies: new Map(),
236 env: DEFAULT_ENV,
237 stats,
238 includedFiles: new Map()
239 })
240 ];
241
242 graph.resolveAssetGroup(req, assets);
243 assert(graph.nodes.has('1'));
244 assert(graph.nodes.has('2'));
245 assert(graph.nodes.has('3'));
246 assert(graph.nodes.has([...assets[0].dependencies.values()][0].id));
247 assert(graph.nodes.has([...assets[1].dependencies.values()][0].id));
248 assert(graph.hasEdge(nodeFromAssetGroup(req).id, '1'));
249 assert(graph.hasEdge(nodeFromAssetGroup(req).id, '2'));
250 assert(graph.hasEdge(nodeFromAssetGroup(req).id, '3'));
251 assert(graph.hasEdge('1', [...assets[0].dependencies.values()][0].id));
252 assert(graph.hasEdge('2', [...assets[1].dependencies.values()][0].id));
253
254 let assets2 = [
255 createAsset({
256 id: '1',
257 filePath,
258 type: 'js',
259 isSource: true,
260 hash: '#1',
261 stats,
262 dependencies: new Map([
263 [
264 'utils',
265 createDependency({
266 moduleSpecifier: './utils',
267 env: DEFAULT_ENV,
268 sourcePath
269 })
270 ]
271 ]),
272 env: DEFAULT_ENV,
273 includedFiles: new Map()
274 }),
275 createAsset({
276 id: '2',
277 filePath,
278 type: 'js',
279 isSource: true,
280 hash: '#2',
281 stats,
282 dependencies: new Map(),
283 env: DEFAULT_ENV,
284 includedFiles: new Map()
285 })
286 ];
287
288 graph.resolveAssetGroup(req, assets2);
289 assert(graph.nodes.has('1'));
290 assert(graph.nodes.has('2'));
291 assert(!graph.nodes.has('3'));
292 assert(graph.nodes.has([...assets[0].dependencies.values()][0].id));
293 assert(!graph.nodes.has([...assets[1].dependencies.values()][0].id));
294 assert(graph.hasEdge(nodeFromAssetGroup(req).id, '1'));
295 assert(graph.hasEdge(nodeFromAssetGroup(req).id, '2'));
296 assert(!graph.hasEdge(nodeFromAssetGroup(req).id, '3'));
297 assert(graph.hasEdge('1', [...assets[0].dependencies.values()][0].id));
298 assert(!graph.hasEdge('2', [...assets[1].dependencies.values()][0].id));
299 });
300
301 it('resolveAssetRequest should add connected file nodes', () => {
302 let graph = new AssetGraph();
303 graph.initialize({
304 targets: TARGETS,
305 entries: ['./index']
306 });
307
308 graph.resolveEntry('./index', ['/path/to/index/src/main.js']);
309 graph.resolveTargets('/path/to/index/src/main.js', TARGETS);
310
311 let dep = createDependency({
312 moduleSpecifier: '/path/to/index/src/main.js',
313 pipeline: 'test',
314 env: DEFAULT_ENV,
315 target: TARGETS[0]
316 });
317 let filePath = '/index.js';
318 let req = {filePath, env: DEFAULT_ENV};
319 graph.resolveDependency(dep, nodeFromAssetGroup(req));
320 let sourcePath = filePath;
321 let assets = [
322 createAsset({
323 id: '1',
324 filePath,
325 type: 'js',
326 isSource: true,
327 hash: '#1',
328 stats,
329 dependencies: new Map([
330 [
331 'utils',
332 createDependency({
333 moduleSpecifier: './utils',
334 env: DEFAULT_ENV,
335 sourcePath
336 })
337 ]
338 ]),
339 env: DEFAULT_ENV,
340 includedFiles: new Map([
341 [
342 '/foo/bar',
343 {
344 filePath: '/foo/bar'
345 }
346 ]
347 ])
348 })
349 ];
350
351 graph.resolveAssetGroup(req, assets);
352 assert(graph.nodes.has('1'));
353 assert(graph.hasEdge(nodeFromAssetGroup(req).id, '1'));
354 });
355});