-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
typescript.js
157 lines (127 loc) Β· 4.29 KB
/
typescript.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
const assert = require('assert');
const path = require('path');
const fs = require('@parcel/fs');
const {bundle, run, assertBundleTree} = require('@parcel/test-utils');
describe('typescript', function() {
it('should produce a ts bundle using ES6 imports', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript/index.ts')
);
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
});
it('should produce a ts bundle using commonJS require', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript-require/index.ts')
);
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
});
it('should support json require', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript-json/index.ts')
);
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
});
it('should support env variables', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript-env/index.ts')
);
assert.equal(b.assets.size, 1);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.env, 'function');
assert.equal(output.env(), 'test');
});
it('should support importing a URL to a raw asset', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript-raw/index.ts')
);
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.ts', 'test.txt'],
childBundles: [
{
type: 'map'
},
{
type: 'txt',
assets: ['test.txt'],
childBundles: []
}
]
});
let output = await run(b);
assert.equal(typeof output.getRaw, 'function');
assert(/^\/test\.[0-9a-f]+\.txt$/.test(output.getRaw()));
assert(await fs.exists(path.join(__dirname, '/dist/', output.getRaw())));
});
it('should minify in production mode', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript-require/index.ts'),
{production: true}
);
assert.equal(b.assets.size, 2);
assert.equal(b.childBundles.size, 1);
let output = await run(b);
assert.equal(typeof output.count, 'function');
assert.equal(output.count(), 3);
let js = await fs.readFile(path.join(__dirname, '/dist/index.js'), 'utf8');
assert(!js.includes('local.a'));
});
it('should support loading tsconfig.json', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript-config/index.ts')
);
let output = await run(b);
assert.equal(output, 2);
let js = await fs.readFile(path.join(__dirname, '/dist/index.js'), 'utf8');
assert(!js.includes('/* test comment */'));
});
it('should support compiling JSX', async function() {
await bundle(path.join(__dirname, '/integration/typescript-jsx/index.tsx'));
let file = await fs.readFile(
path.join(__dirname, '/dist/index.js'),
'utf8'
);
assert(file.includes('React.createElement("div"'));
});
it('should use esModuleInterop by default', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript-interop/index.ts')
);
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.ts', 'commonjs-module.js'],
childBundles: [
{
type: 'map'
}
]
});
let output = await run(b);
assert.equal(typeof output.test, 'function');
assert.equal(output.test(), 'test passed');
});
it('fs.readFileSync should inline a file as a string', async function() {
let b = await bundle(
path.join(__dirname, '/integration/typescript-fs/index.ts')
);
const text = 'export default <div>Hello</div>;';
let output = await run(b);
assert.deepEqual(output, {
fromTs: text,
fromTsx: text
});
});
});