forked from parcel-bundler/parcel
/
plugin.js
213 lines (188 loc) Β· 5.56 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
// @flow
import assert from 'assert';
import path from 'path';
import nullthrows from 'nullthrows';
import {
assertBundles,
bundle,
distDir,
findAsset,
findDependency,
outputFS as fs,
overlayFS,
run,
} from '@parcel/test-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 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,
},
},
);
assert.deepStrictEqual(
new Set(b.getUsedSymbols(nullthrows(findAsset(b, 'index.js')))),
new Set([]),
);
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']),
);
assert.deepStrictEqual(
new Set(b.getUsedSymbols(findDependency(b, 'index.js', './a.js'))),
new Set(['a']),
);
assert.deepStrictEqual(
new Set(b.getUsedSymbols(findDependency(b, 'index.js', './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('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;');
});
});