-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
103 lines (82 loc) · 2.83 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
"use strict";
var fs = require('fs');
var loaderUtils = require("loader-utils");
var assign = require("object-assign");
var path = require('path');
var childProcess = require('child_process');
var binPath = './node_modules/.bin/mermaid';
var tmpFolder = '.tmpMermaid';
// default option
var options = {
};
module.exports = function (mermaidGraph) {
var callback = this.async();
// merge params and default config
var query = loaderUtils.parseQuery(this.query);
var configKey = query.config || "mermaidLoader";
var options = this.options[configKey] || {};
var config = {
publicPath: false,
name: "[name].png"
};
// options takes precedence over config
Object.keys(options).forEach(function(attr) {
config[attr] = parseInt(options[attr]) || options[attr];
});
// query takes precedence over config and options
Object.keys(query).forEach(function(attr) {
config[attr] = query[attr];
});
var self = this;
var fileName = loaderUtils.interpolateName(this, '[name].mmd.png', {
context: this.options.context
});
var filePath = loaderUtils.interpolateName(this, '[path][name].[ext]', {
context: this.options.context
});
var url = loaderUtils.interpolateName(this, config.name, {
context: config.context || this.options.context,
content: mermaidGraph,
regExp: config.regExp
});
this.cacheable && this.cacheable();
var publicPath = "__webpack_public_path__ + " + JSON.stringify(url);
if (config.publicPath) {
// support functions as publicPath to generate them dynamically
publicPath = JSON.stringify(
typeof config.publicPath === "function"
? config.publicPath(url)
: config.publicPath + url
);
}
var seqDiagConfig = assign(
{},
query,
options);
Object.keys(seqDiagConfig).forEach(function(attr) {
seqDiagConfig[attr] = parseInt(seqDiagConfig[attr]) || seqDiagConfig[attr];
});
if (!fs.existsSync(tmpFolder)){
fs.mkdirSync(tmpFolder);
}
var configDiagPath = './' + tmpFolder + '/seqDiagConfig.json';
fs.writeFileSync(configDiagPath, JSON.stringify(seqDiagConfig), 'utf-8');
var childArgs = [
'-o=' + tmpFolder,
'-c=' + configDiagPath,
filePath
];
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
if (err) {
return callback(err);
}
var imgPath = './' + tmpFolder + '/' + fileName;
var content = fs.readFileSync(imgPath);
self.emitFile(url, content);
fs.unlinkSync(imgPath);
fs.unlinkSync(configDiagPath);
fs.rmdirSync('./' + tmpFolder);
callback(null, "module.exports = " + publicPath + ";")
});
};
module.exports.raw = true;