This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (44 loc) · 1.56 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
'use strict';
const loaderUtils = require('loader-utils');
const glob = require('glob');
const temp = require('temp');
const fs = require('fs');
const spawnSync = require('child_process').spawnSync;
function addDependencies(loader) {
// get dependencies from elm-packages.json
// TODO: specify location of elm-package.json as option
var depsGlob;
var elmPackage = JSON.parse(fs.readFileSync('./elm-package.json', 'utf8'));
if (elmPackage['source-directories'].length > 1) {
depsGlob = '{' + elmPackage['source-directories'].join(',') + '}/**/*.elm'
} else {
depsGlob = elmPackage['source-directories'][0] + '/**/*.elm'
}
glob.sync(depsGlob).forEach(loader.addDependency);
}
function compile(input) {
const outputFile = temp.path({
suffix: '.js'
});
const elmMakeOptions = ['--yes', '--output', outputFile, input];
console.log("Compiling: ", input);
const res = spawnSync('elm-make', elmMakeOptions);
if (res.error) {
throw res.error;
}
if (res.status !== 0) {
throw new Error('elm-make exited with ' + res.status + '\n' + res.output);
}
const data = fs.readFileSync(outputFile);
fs.unlinkSync(outputFile);
console.log(res.stdout.toString('utf8'));
return data;
}
module.exports = function() {
var loader = this;
var input = loader.resourcePath;
// NOTE: currently no options are allowed. Just keeping this here for future development
var options = loaderUtils.getOptions(loader);
addDependencies(loader);
return compile(input);
}