forked from parcel-bundler/parcel
/
ElmTransformer.js
184 lines (161 loc) Β· 4.68 KB
/
ElmTransformer.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
// @flow strict-local
import {Transformer} from '@parcel/plugin';
import spawn from 'cross-spawn';
import path from 'path';
import {minify} from 'terser';
import ThrowableDiagnostic, {md} from '@parcel/diagnostic';
// $FlowFixMe
import elm from 'node-elm-compiler';
// $FlowFixMe
import elmHMR from 'elm-hot';
import {load, elmBinaryPath} from './loadConfig';
let isWorker;
try {
let worker_threads = require('worker_threads');
isWorker = worker_threads.threadId > 0;
} catch (_) {
isWorker = false;
}
export default (new Transformer({
loadConfig({config}) {
return load({config});
},
async transform({asset, options, logger}) {
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,
report: 'json',
};
asset.invalidateOnEnvChange('PARCEL_ELM_NO_DEBUG');
const extraSources = resolveExtraSources({asset, logger});
extraSources.forEach(filePath => {
asset.invalidateOnFileChange(filePath);
});
const sources = [asset.filePath, ...extraSources];
const dependencies = await Promise.all(
sources.map(source => elm.findAllDependencies(source)),
);
const uniqueDeps = new Set(dependencies.flat());
Array.from(uniqueDeps).forEach(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, sources, compilerConfig);
} catch (e) {
let compilerJson = e.message.split('\n')[1];
let compilerDiagnostics = JSON.parse(compilerJson);
if (compilerDiagnostics.type === 'compile-errors') {
throw new ThrowableDiagnostic({
diagnostic: compilerDiagnostics.errors.flatMap(
elmCompileErrorToParcelDiagnostics,
),
});
}
// compilerDiagnostics.type === "error"
// happens for example when compiled in prod mode with Debug.log in code
throw new ThrowableDiagnostic({
diagnostic: formatElmError(compilerDiagnostics, ''),
});
}
if (options.hmrOptions) {
code = elmHMR.inject(code);
}
if (compilerConfig.optimize) code = await minifyElmOutput(code);
asset.type = 'js';
asset.setCode(code);
return [asset];
},
}): Transformer);
// gather extra modules that should be added to the compilation process
function resolveExtraSources({asset, logger}) {
const dirname = path.dirname(asset.filePath);
const relativePaths = asset.query.getAll('with');
if (relativePaths.length > 0) {
logger.info({
message: md`Compiling elm with additional sources: ${md.bold(
JSON.stringify(relativePaths),
)}`,
});
}
return relativePaths.map(relPath => path.join(dirname, relPath));
}
function compileToString(elm, elmBinary, sources, config) {
return elm.compileToString(sources, {
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, {
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;
}
function formatMessagePiece(piece) {
if (piece.string) {
if (piece.underline) {
return md`${md.underline(piece.string)}`;
}
return md`${md.bold(piece.string)}`;
}
return md`${piece}`;
}
function elmCompileErrorToParcelDiagnostics(error) {
const relativePath = path.relative(process.cwd(), error.path);
return error.problems.map(problem => formatElmError(problem, relativePath));
}
function formatElmError(problem, relativePath) {
const padLength = 80 - 5 - problem.title.length - relativePath.length;
const dashes = '-'.repeat(padLength);
const message = [
'',
`-- ${problem.title} ${dashes} ${relativePath}`,
'',
problem.message.map(formatMessagePiece).join(''),
].join('\n');
return {
message,
origin: '@parcel/elm-transformer',
stack: '', // set stack to empty since it is not useful
};
}