-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
145 lines (129 loc) · 5.43 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
const cmd = require('node-cmd')
const fs = require('fs');
const { sep: slash } = require('path');
const path = require('path');
const loaderUtils = require('loader-utils');
const extend = require('extend');
const spawn = require('child_process').spawn;
const properName = name => name.replace(/^./, c => c.toUpperCase());
const listify = array => array.join(', ')
// make a comma-separated list ending with a '&' separator
.replace(/(, )[^,]*$/, s => ' & ' + s.split(', ')[1]);
module.exports = function (source) {
const compilers = {
transcrypt: {
switches: '-b -n -m',
folder: `.${slash}__javascript__`,
install: 'pip install transcrypt',
python_version: '3.x',
sourcemaps: true,
keep_compiled: false
},
jiphy: {
switches: '',
folder: `.${slash}`,
install: 'pip install jiphy',
python_version: '2.x',
sourcemaps: false,
keep_compiled: false
},
pj: {
switches: '--inline-map --source-name %f -s -',
folder: `.${slash}`,
install: 'pip install javascripthon',
python_version: '3.x',
streaming: true,
sourcemaps: true,
keep_compiled: false
}
};
const options = loaderUtils.getOptions(this);
const compilerName = options && options.compiler || 'transcrypt';
const compiler = extend(true, {}, compilers[compilerName], options || {});
if (!compiler) {
throw new Error(`py-loader only supports ${
listify(Object.keys(compilers).map(properName))
} compilers at present. See README.md for information on using it with others.`);
}
compiler.name = compilerName;
const entry = this._module.resource;
//console.log(`py-loader: compiling ${entry} with ${compilerName}...`);
const fileinfo = path.parse(entry);
const srcDir = fileinfo.dir; // path.dirname(entry, ".py");
var basename = fileinfo.name;
var delete_after = false;
// const basename = path.basename(entry, ".py");
const callback = this.async();
if (compiler.streaming) {
compiler.switches = compiler.switches.replace('%f', basename);
var child = spawn(compiler.name, compiler.switches.split(' '));
child.stdin.write(source);
var data = '';
var error = '';
var sourceMap = '';
child.stdout.on('data', function (js) {
data = data + js;
});
child.stderr.on('data', function (msg) {
error = error + msg;
});
child.on('exit', function () {
if (compiler.sourcemaps) {
sourcemapLine = data.split('\n').splice(-3,1)[0]; // Javascripthon specific?
sourceMap = new Buffer(sourcemapLine.substring(sourcemapLine.indexOf('base64,') + 7), 'base64').toString();
}
if (error != '') {
console.error(error.toString());
callback(null, error, sourceMap) }
else {
callback(null, data, sourceMap);
}
});
child.on('error', function(err) {
console.error(`Some error occurred on ${properName(compiler.name)} compiler execution. Have you installed ${properName(compiler.name)}? If not, please run \`${compiler.install}\` (requires Python ${compiler.python_version})`);
callback(err);
});
child.stdin.end();
}
else {
if (fileinfo.ext == ".vue") {
basename = `__${fileinfo.name}`
delete_after = true;
fs.writeFileSync(`${srcDir}${slash}__${fileinfo.name}.py`, source);
}else if(fileinfo.ext != ".py")
{
console.warn("This loader only handles .py files. This could be a problem with your webpack.config.js file. Please add a rule for .py files in your modules entry.");
callback(null, source);
return;
}
cmd.get(`${compiler.name} ${compiler.switches} '${srcDir}${slash}${basename}.py'`, function(err, data, stderr) {
if (!err) {
const filename = `${srcDir}${slash}${compiler.folder}${slash}${basename}.js`;
js = fs.readFileSync(filename, "utf8");
if (!compiler.keep_compiled) {
fs.unlinkSync(filename);
if (delete_after) {
fs.unlinkSync(`${srcDir}${slash}__${fileinfo.name}.py`);
}
}
if (compiler.sourcemaps) {
const sourceMapFile = `${srcDir}${slash}${compiler.folder}${slash}extra${slash}sourcemap${slash}${basename}.js`;
sourceMap = fs.readFileSync(sourceMapFile + ".map", "utf8")
if (!compiler.keep_compiled) {
fs.unlinkSync(sourceMapFile + ".map");
}
callback(null, js, sourceMap);
}
else {
callback(null, js);
}
}
else {
console.error(stderr)
console.error(data)
// console.error(`Some error occurred on ${properName(compiler.name)} compiler execution. Have you installed ${properName(compiler.name)}? If not, please run \`${compiler.install}\` (requires Python ${compiler.python_version})`);
callback(err);
}
});
}
}