/
elm.js
155 lines (136 loc) Β· 4.61 KB
/
elm.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
import assert from 'assert';
import path from 'path';
import {
bundle,
distDir,
assertBundles,
run,
outputFS,
} from '@parcel/test-utils';
describe('elm', function () {
it('should produce a basic Elm bundle', async function () {
let b = await bundle(path.join(__dirname, '/integration/elm/index.js'));
assertBundles(b, [
{
type: 'js',
assets: ['Main.elm', 'index.js'],
},
]);
let output = await run(b);
assert.equal(typeof output().Elm.Main.init, 'function');
});
it('should produce a elm bundle with debugger', async function () {
let b = await bundle(path.join(__dirname, '/integration/elm/index.js'));
await run(b);
let js = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(js.includes('elm$browser$Debugger'));
});
it('should apply elm-hot if HMR is enabled', async function () {
let b = await bundle(path.join(__dirname, '/integration/elm/index.js'), {
hmrOptions: true,
});
assertBundles(b, [
{
type: 'js',
assets: ['Main.elm', 'index.js'],
},
]);
let js = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(js.includes('[elm-hot]'));
});
it('should remove debugger in production', async function () {
let b = await bundle(path.join(__dirname, '/integration/elm/index.js'), {
mode: 'production',
});
await run(b);
let js = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(!js.includes('elm$browser$Debugger'));
});
it('should remove debugger when environment variable `PARCEL_ELM_NO_DEBUG` is set to true', async function () {
let b = await bundle(path.join(__dirname, '/integration/elm/index.js'), {
env: {PARCEL_ELM_NO_DEBUG: true},
});
await run(b);
let js = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(!js.includes('elm$browser$Debugger'));
});
it('should minify Elm in production mode', async function () {
let b = await bundle(path.join(__dirname, '/integration/elm/index.js'), {
mode: 'production',
defaultTargetOptions: {
shouldOptimize: true,
},
});
await run(b);
let js = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(!js.includes('elm$core'));
assert(js.includes('Elm'));
assert(js.includes('init'));
});
it('should produce correct formatting and indentation when compilation fails', async function () {
const normalizedPath = path.normalize(
'test/integration/elm-compile-error/src/Main.elm',
);
await assert.rejects(
() =>
bundle(path.join(__dirname, 'integration/elm-compile-error/index.js'), {
mode: 'production',
}),
{
name: 'BuildError',
diagnostics: [
{
message:
'\n' +
`-- TYPE MISMATCH --------------- ${normalizedPath}\n` +
'\n' +
'The 1st argument to `text` is not what I expect:\n' +
'\n' +
'7| Html.text 5 "Hello, world!"\n' +
' **^**\n' +
'This argument is a number of type:\n' +
'\n' +
' **number**\n' +
'\n' +
'But `text` needs the 1st argument to be:\n' +
'\n' +
' **String**\n' +
'\n' +
'__Hint__: Try using **String.fromInt** to convert it to a string?',
origin: '@parcel/elm-transformer',
stack: '',
},
{
message:
'\n' +
`-- TOO MANY ARGS --------------- ${normalizedPath}\n` +
'\n' +
'The `text` function expects 1 argument, but it got 2 instead.\n' +
'\n' +
'7| Html.text 5 "Hello, world!"\n' +
' **^^^^^^^^^**\n' +
'Are there any missing commas? Or missing parentheses?',
origin: '@parcel/elm-transformer',
stack: '',
},
],
},
);
});
it('should produce extra Modules given in "with" query param', async function () {
const b = await bundle(
path.join(__dirname, '/integration/elm-multiple-apps/src/index.js'),
);
assertBundles(b, [
{
type: 'js',
assets: ['Main.elm', 'index.js', 'esmodule-helpers.js'],
},
]);
const output = await run(b);
const Elm = output.default();
assert.equal(typeof Elm.Main.init, 'function');
assert.equal(typeof Elm.MainB.init, 'function');
assert.equal(typeof Elm.MainC.init, 'function');
});
});