-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathplugin.js
332 lines (296 loc) Β· 8.7 KB
/
plugin.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
// @flow
import assert from 'assert';
import invariant from 'assert';
import path from 'path';
import nullthrows from 'nullthrows';
import {
assertBundles,
bundle,
bundler,
distDir,
findAsset,
getNextBuild,
outputFS as fs,
overlayFS,
run,
} from '@parcel/test-utils';
import * as wasmmap from 'wasm-sourcemap';
import {relativePath} from '@parcel/utils';
describe('plugin', function () {
it("continue transformer pipeline on type change that doesn't change the pipeline", async function () {
await bundle(
path.join(__dirname, '/integration/pipeline-type-change/index.ini'),
);
let output = await fs.readFile(path.join(distDir, 'index.txt'), 'utf8');
assert.equal(
output,
`INPUT
parcel-transformer-a
parcel-transformer-b`,
);
});
it('should allow optimizer plugins to change the output file type', async function () {
await bundle(
path.join(__dirname, '/integration/optimizer-changing-type/index.js'),
);
assert.deepEqual(fs.readdirSync(distDir), ['index.test']);
});
it('should allow resolver plugins to disable deferring', async function () {
let b = await bundle(
path.join(__dirname, '/integration/resolver-canDefer/index.js'),
{mode: 'production'},
);
assertBundles(b, [
{
type: 'js',
assets: ['index.js', 'index.js', 'a.js', 'b.js'],
},
]);
});
it('should allow resolvers to return changes for dependency.meta', async function () {
let b = await bundle(
path.join(__dirname, '/integration/resolver-dependency-meta/a.js'),
{shouldDisableCache: false, shouldContentHash: false, inputFS: overlayFS},
);
let calls = [];
await run(b, {
sideEffect(v) {
calls.push(v);
},
});
assert.deepEqual(calls, [1234]);
await overlayFS.writeFile(
path.join(__dirname, '/integration/resolver-dependency-meta/a.js'),
(await overlayFS.readFile(
path.join(__dirname, '/integration/resolver-dependency-meta/a.js'),
'utf8',
)) + '\n// abc',
);
b = await bundle(
path.join(__dirname, '/integration/resolver-dependency-meta/a.js'),
{shouldDisableCache: false, shouldContentHash: false, inputFS: overlayFS},
);
calls = [];
await run(b, {
sideEffect(v) {
calls.push(v);
},
});
assert.deepEqual(calls, [1234]);
});
it('invalidate the cache based on loadBundleConfig in a packager', async function () {
let fixture = path.join(
__dirname,
'/integration/packager-loadBundleConfig',
);
let entry = path.join(fixture, 'index.html');
let b = await bundler(entry, {
inputFS: overlayFS,
shouldDisableCache: false,
});
let subscription = await b.watch();
try {
let bundleEvent = await getNextBuild(b);
invariant(bundleEvent.type === 'buildSuccess');
assert.strictEqual(
await overlayFS.readFile(
nullthrows(
bundleEvent.bundleGraph
.getBundles()
.find(b => b.getMainEntry()?.filePath.endsWith('a.txt')),
).filePath,
'utf8',
),
`Bundles: a.txt. Contents: Hello from a\n`,
);
await overlayFS.copyFile(path.join(fixture, 'index.2.html'), entry);
bundleEvent = await getNextBuild(b);
invariant(bundleEvent.type === 'buildSuccess');
assert.strictEqual(
await overlayFS.readFile(
nullthrows(
bundleEvent.bundleGraph
.getBundles()
.find(b => b.getMainEntry()?.filePath.endsWith('a.txt')),
).filePath,
'utf8',
),
`Bundles: a.txt,b.txt. Contents: Hello from a\n`,
);
assert.strictEqual(
await overlayFS.readFile(
nullthrows(
bundleEvent.bundleGraph
.getBundles()
.find(b => b.getMainEntry()?.filePath.endsWith('b.txt')),
).filePath,
'utf8',
),
`Bundles: a.txt,b.txt. Contents: Hello from b\n`,
);
} finally {
await subscription.unsubscribe();
}
});
it('invalidate the cache based on loadConfig in a packager', async function () {
let fixture = path.join(__dirname, '/integration/packager-loadConfig');
let entry = path.join(fixture, 'index.txt');
let config = path.join(fixture, 'foo.config.json');
let b = await bundle(entry, {
inputFS: overlayFS,
shouldDisableCache: false,
});
assert.strictEqual(
await overlayFS.readFile(b.getBundles()[0].filePath, 'utf8'),
'1234',
);
await overlayFS.writeFile(config, JSON.stringify({contents: 'xyz'}));
b = await bundle(entry, {
inputFS: overlayFS,
shouldDisableCache: false,
});
assert.strictEqual(
await overlayFS.readFile(b.getBundles()[0].filePath, 'utf8'),
'xyz',
);
});
it('merges symbol information when applying runtime assets', async function () {
let b = await bundle(
path.join(__dirname, '/integration/runtime-symbol-merging/entry.js'),
{
defaultTargetOptions: {
shouldScopeHoist: true,
},
mode: 'production',
},
);
assert(!findAsset(b, 'index.js'));
assert.deepStrictEqual(
new Set(b.getUsedSymbols(nullthrows(findAsset(b, 'a.js')))),
new Set(['a']),
);
assert.deepStrictEqual(
new Set(b.getUsedSymbols(nullthrows(findAsset(b, 'b.js')))),
new Set(['b']),
);
let calls = [];
await run(b, {
call(v) {
calls.push(v);
},
});
assert.deepStrictEqual(calls, [789, 123]);
});
it('properly excludes assets that are excluded and deferred by both app code and runtimes', async function () {
let b = await bundle(
path.join(__dirname, '/integration/runtime-deferred-excluded/index.js'),
{
defaultTargetOptions: {
shouldScopeHoist: true,
},
},
);
let calls = [];
let output = await run(b, {
f(v) {
calls.push(v);
},
});
assert.deepStrictEqual(
// `output` is from the vm and so is not deepStrictEqual
[...output],
['index', 'used'],
);
assert.deepStrictEqual(calls, ['used']);
});
it('handles multiple assets returned by a transformer', async function () {
let b = await bundle(
path.join(__dirname, '/integration/multi-asset-transformer/index.js'),
{
defaultTargetOptions: {
shouldScopeHoist: true,
},
},
);
assert.equal(await run(b), 2);
});
it('throws when multiple assets returned by a transformer import a missing symbol', async function () {
let source = path.join(
__dirname,
'/integration/multi-asset-transformer-export/index.js',
);
let message = `index.js does not export 'foo'`;
// $FlowFixMe[prop-missing]
await assert.rejects(
() =>
bundle(source, {
defaultTargetOptions: {
shouldScopeHoist: true,
},
}),
{
name: 'BuildError',
message,
diagnostics: [
{
message,
origin: '@parcel/core',
codeFrames: [
{
filePath: source,
language: 'js',
codeHighlights: [
{
message: undefined,
start: {
line: 1,
column: 9,
},
end: {
line: 1,
column: 11,
},
},
],
},
],
},
],
},
);
});
it('should allow resolvers to invalidateOnEnvChange', async () => {
async function assertAsset(replacedCode) {
let b = await bundle(
path.join(
__dirname,
'/integration/resolver-can-invalidateonenvchange/index.js',
),
{
shouldDisableCache: false,
inputFS: overlayFS,
env: {replacedCode},
},
);
let code = await b.getBundles()[0].getEntryAssets()[0].getCode();
assert(code.indexOf(replacedCode) !== -1);
}
await assertAsset('const replaced = 1;');
await assertAsset('const replaced = 2;');
});
it('should output sourcemaps when packaging Wasm', async () => {
let b = await bundle(
path.join(__dirname, '/integration/wasm-sourcemap-transformer/index.js'),
);
let wasmPath = nullthrows(
b.getBundles().find(b => b.type === 'wasm'),
).filePath;
let mapPath = wasmPath + '.map';
assert(await fs.exists(mapPath));
let wasm = await fs.readFile(wasmPath);
assert.equal(
wasmmap.GetSourceMapURL(wasm),
relativePath(distDir, mapPath, false),
);
});
});