/
fs.js
238 lines (205 loc) Β· 7.04 KB
/
fs.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
// @flow strict-local
import assert from 'assert';
import invariant from 'assert';
import path from 'path';
import {
assertBundles,
bundle,
removeDistDirectory,
run,
overlayFS,
outputFS,
distDir,
} from '@parcel/test-utils';
describe('fs', function () {
beforeEach(async () => {
await removeDistDirectory();
});
describe('browser environment', function () {
it('should not inline a file if disabled via config', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-disabled/index.js'),
{
inputFS: overlayFS,
},
);
// $FlowFixMe
await assert.rejects(() => run(b), /\.readFileSync is not a function/);
});
it('should not inline a file outside of the project root', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-outside-root/index.js'),
{
inputFS: overlayFS,
},
);
// $FlowFixMe
await assert.rejects(() => run(b), /\.readFileSync is not a function/);
});
it('should inline a file as a string', async function () {
let b = await bundle(path.join(__dirname, '/integration/fs/index.js'));
let output = await run(b);
assert.equal(output, 'hello');
});
it('should inline a file as a buffer', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-buffer/index.js'),
);
let output = await run(b);
invariant(typeof output === 'object' && output != null);
assert(output.constructor.name.includes('Buffer'));
assert.equal(output.length, 5);
});
it('should inline a file with fs require alias', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-alias/index.js'),
);
let output = await run(b);
assert.equal(output, 'hello');
});
it('should inline a file with fs require inline', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-inline/index.js'),
);
let output = await run(b);
assert.equal(output, 'hello');
});
it.skip('should inline a file with fs require assignment', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-assign/index.js'),
);
let output = await run(b);
assert.equal(output, 'hello');
});
it.skip('should inline a file with fs require assignment alias', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-assign-alias/index.js'),
);
let output = await run(b);
assert.equal(output, 'hello');
});
it('should inline a file with fs require destructure', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-destructure/index.js'),
);
let output = await run(b);
assert.equal(output, 'hello');
});
it.skip('should inline a file with fs require destructure assignment', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-destructure-assign/index.js'),
);
let output = await run(b);
assert.equal(output, 'hello');
});
it('should inline a file with fs ES6 import', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-import/index.js'),
);
let output = await run(b);
invariant(typeof output === 'object' && output != null);
assert.equal(output.default, 'hello');
});
it('should inline a file with fs ES6 import and path.join', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-import-path-join/index.js'),
);
let output = await run(b);
invariant(typeof output === 'object' && output != null);
assert.equal(output.default, 'hello');
});
it('should not evaluate fs calls when package.browser.fs is false', async function () {
let b = await bundle(
path.join(__dirname, '/integration/resolve-entries/ignore-fs.js'),
);
assertBundles(b, [
{
name: 'ignore-fs.js',
// empty.js is generated by require('fs'), it gets mocked with an empty module
assets: [
'_empty.js',
'esmodule-helpers.js',
'ignore-fs.js',
'index.js',
],
},
]);
// $FlowFixMe[incompatible-call]
let output = await run(b);
assert.equal(typeof output.test, 'function');
assert.equal(output.test(), 'test-pkg-ignore-fs-ok');
});
// TODO: check if the logger has warned the user
it('should ignore fs calls when the filename is not evaluable', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-file-non-evaluable/index.js'),
);
// $FlowFixMe
await assert.rejects(() => run(b), /\.readFileSync is not a function/);
});
it('should ignore fs calls when the filename is not evaluable after preset-env', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/fs-file-non-evaluable-template-env/index.js',
),
);
// $FlowFixMe
await assert.rejects(() => run(b), /\.readFileSync is not a function/);
});
it('should ignore fs calls when the options are not evaluable', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-options-non-evaluable/index.js'),
);
// $FlowFixMe
await assert.rejects(() => run(b), /\.readFileSync is not a function/);
});
});
describe('node environment', function () {
it('should not inline a file in a node environment', async function () {
let b = await bundle(
path.join(__dirname, '/integration/fs-node/index.js'),
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js'],
},
]);
let contents = await outputFS.readFile(
path.join(distDir, 'index.js'),
'utf8',
);
assert(contents.includes("require('fs')"));
assert(contents.includes('readFileSync'));
await outputFS.writeFile(
path.join(__dirname, '/integration/fs-node/', 'test.txt'),
'hey',
);
let output = await run(b);
assert.equal(output, 'hey');
});
});
describe.skip('electron environment', function () {
it('should not inline a file in an Electron environment', async function () {
let b = await bundle(path.join(__dirname, '/integration/fs/index.js'), {
targets: ['electron'],
});
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js'],
},
]);
let contents = await outputFS.readFile(
path.join(distDir, 'index.js'),
'utf8',
);
assert(contents.includes("require('fs')"));
assert(contents.includes('readFileSync'));
await outputFS.writeFile(path.join(distDir, 'test.txt'), 'hey');
let output = await run(b);
assert.equal(output, 'hey');
});
});
});