-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile-elm-to-bundle.js
36 lines (32 loc) · 999 Bytes
/
compile-elm-to-bundle.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
const fs = require('fs').promises
const { compileToString } = require('node-elm-compiler');
/**
* Note: We save the compilation into the `src` directory to avoid unusual
* behavior from the `tabris` server. Attempting to move the bundle file may
* silently break the server, causing the app not to update.
*/
compileToString(
['./src/Main.elm'],
{ pathToElm: './node_modules/.bin/elm' }
).then(data => {
const wrappedCode = wrapElmCode(data);
return fs.writeFile('./src/elm-dist/elm-bundle.js', wrappedCode);
}).then(() => {
console.log('Done compiling Elm code');
}).catch(() => {
console.log('Oops cannot compile Elm code');
});
/**
* Note: We needed to create a function wrapper around the compiled JavaScript
* to avoid executing the source when importing the module.
*/
function wrapElmCode (code) {
return `
function wrapper() {
let output = {};
(function () { ${code} }).call(output);
return output.Elm;
}
export default wrapper;
`;
}