-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (152 loc) · 4.09 KB
/
index.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
// @flow strict-local
import {Transformer} from '@parcel/plugin';
import commandExists from 'command-exists';
import spawn from 'cross-spawn';
import path from 'path';
import {minify} from "@swc/core";
import nullthrows from 'nullthrows';
import ThrowableDiagnostic from '@parcel/diagnostic';
// $FlowFixMe
import elm from 'node-elm-compiler';
// $FlowFixMe
import elmHMR from 'elm-hot';
let isWorker;
try {
let worker_threads = require('worker_threads');
isWorker = worker_threads.threadId > 0;
} catch (_) {
isWorker = false;
}
export default (new Transformer({
async loadConfig({config}) {
const elmConfig = await config.getConfig(['elm.json']);
if (!elmConfig) {
elmBinaryPath(); // Check if elm is even installed
throw new ThrowableDiagnostic({
diagnostic: {
message: "The 'elm.json' file is missing.",
hints: [
"Initialize your elm project by running 'elm init'",
"If you installed elm as project dependency then run 'yarn elm init' or 'npx elm init'",
],
},
});
}
return elmConfig.contents;
},
async transform({asset, options}) {
const elmBinary = elmBinaryPath();
const compilerConfig = {
spawn,
cwd: path.dirname(asset.filePath),
// $FlowFixMe[sketchy-null-string]
debug: !options.env.PARCEL_ELM_NO_DEBUG && options.mode !== 'production',
optimize: asset.env.shouldOptimize,
};
asset.invalidateOnEnvChange('PARCEL_ELM_NO_DEBUG');
for (const filePath of await elm.findAllDependencies(asset.filePath)) {
asset.invalidateOnFileChange(filePath);
}
// Workaround for `chdir` not working in workers
// this can be removed after https://github.com/isaacs/node-graceful-fs/pull/200 was mergend and used in parcel
// $FlowFixMe[method-unbinding]
process.chdir.disabled = isWorker;
let code;
try {
code = await compileToString(elm, elmBinary, asset, compilerConfig);
} catch (e) {
throw new ThrowableDiagnostic({
diagnostic: {
message: 'Compilation failed',
origin: '@parcel/elm-transformer',
stack: e.toString(),
},
});
}
if (options.hmrOptions) {
code = elmHMR.inject(code);
}
if (compilerConfig.optimize) code = await minifyElmOutput(code);
asset.type = 'js';
asset.setCode(code);
return [asset];
},
}): Transformer);
function elmBinaryPath() {
let elmBinary = resolveLocalElmBinary();
if (elmBinary == null && !commandExists.sync('elm')) {
throw new ThrowableDiagnostic({
diagnostic: {
message: "Can't find 'elm' binary.",
hints: [
"You can add it as an dependency for your project by running 'yarn add -D elm' or 'npm add -D elm'",
'If you want to install it globally then follow instructions on https://elm-lang.org/',
],
origin: '@parcel/elm-transformer',
},
});
}
return elmBinary;
}
function resolveLocalElmBinary() {
try {
let result = require.resolve('elm/package.json');
// $FlowFixMe
let pkg = require('elm/package.json');
let bin = nullthrows(pkg.bin);
return path.join(
path.dirname(result),
typeof bin === 'string' ? bin : bin.elm,
);
} catch (_) {
return null;
}
}
function compileToString(elm, elmBinary, asset, config) {
return elm.compileToString(asset.filePath, {
pathToElm: elmBinary,
...config,
});
}
let elmPureFuncs = [
'F2',
'F3',
'F4',
'F5',
'F6',
'F7',
'F8',
'F9',
'A2',
'A3',
'A4',
'A5',
'A6',
'A7',
'A8',
'A9',
];
async function minifyElmOutput(source) {
// Recommended minification
// Based on: http://elm-lang.org/0.19.0/optimize
let result = await minify(source, {
minify: true,
jsc: {
minify: {
compress: {
keep_fargs: false,
passes: 2,
pure_funcs: elmPureFuncs,
pure_getters: true,
unsafe: true,
unsafe_comps: true,
},
mangle: {
reserved: elmPureFuncs,
}
}
}
});
if (result.code != null) return result.code;
throw result.error;
}