forked from parcel-bundler/parcel
/
fs.js
178 lines (150 loc) 路 5.22 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
const assert = require('assert');
const fs = require('@parcel/fs');
const path = require('path');
const {bundle, run, assertBundleTree} = require('@parcel/test-utils');
describe('fs', function() {
describe('--target=browser', 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);
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('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('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('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 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')
);
await assertBundleTree(b, {
name: 'ignore-fs.js',
// empty.js is generated by require('fs'), it gets mocked with an empty module
assets: ['_empty.js', 'ignore-fs.js', 'index.js'],
childBundles: [
{
type: 'map'
}
]
});
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')
);
let thrown = false;
try {
await run(b);
} catch (e) {
assert(e.message.includes('.readFileSync is not a function'));
thrown = true;
}
assert.equal(thrown, true);
});
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')
);
let thrown = false;
try {
await run(b);
} catch (e) {
assert(e.message.includes('.readFileSync is not a function'));
thrown = true;
}
assert.equal(thrown, true);
});
});
describe('--target=node', function() {
it('should leave an attempt to read a file unchanged', async function() {
let b = await bundle(path.join(__dirname, '/integration/fs/index.js'), {
target: 'node'
});
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js'],
childBundles: [
{
type: 'map'
}
]
});
assert((await fs.readFile(b.name)).includes("require('fs')"));
assert((await fs.readFile(b.name)).includes('readFileSync'));
await fs.writeFile(path.join(__dirname, '/dist/test.txt'), 'hey');
let output = await run(b);
assert.equal(output, 'hey');
});
});
describe('--target=electron', function() {
it('should leave an attempt to read a file unchanged', async function() {
let b = await bundle(path.join(__dirname, '/integration/fs/index.js'), {
target: 'electron'
});
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js'],
childBundles: [
{
type: 'map'
}
]
});
assert((await fs.readFile(b.name)).includes("require('fs')"));
assert((await fs.readFile(b.name)).includes('readFileSync'));
await fs.writeFile(path.join(__dirname, '/dist/test.txt'), 'hey');
let output = await run(b);
assert.equal(output, 'hey');
});
});
});