This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
60 lines (55 loc) · 1.66 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
var loaderUtils = require("loader-utils");
var path = require("path");
module.exports = function(content) {
this.cacheable && this.cacheable();
// defaults
var config = {
name: "[hash].[ext]",
basePath: undefined,
rewritePath: undefined,
relativePath: false
};
// Parse query
var query = loaderUtils.getOptions(this) || {};
var options = this.options[query.config || "nodeAddonLoader"] || {};
// options takes precedence over config
Object.keys(options).forEach(function(attr) {
config[attr] = options[attr];
});
// query takes precedence over config and options
Object.keys(query).forEach(function(attr) {
config[attr] = query[attr];
});
// Build the output file name
var url = loaderUtils.interpolateName(this, config.name, {
context: this.options.context,
content: content
});
// write the file to the output dir
if (this.emitFile) {
this.emitFile(url, content, false);
this.addDependency(this.resourcePath);
}
// Build the javascript to load the .node at runtime
var finalUrl;
if (config.rewritePath) {
finalUrl = JSON.stringify(path.join(config.rewritePath, url));
} else if (config.basePath) {
var basePath = path.relative(config.basePath, this.options.output.path);
finalUrl = JSON.stringify(path.join(basePath, url));
} else {
finalUrl = "__webpack_public_path__ + " + JSON.stringify(url);
}
if (config.relativePath) {
finalUrl = "__dirname + \"/\" + " + finalUrl;
}
return (
"try { global.process.dlopen(module, " +
finalUrl +
"); } catch(e) {" +
"throw new Error('Cannot open ' + " +
finalUrl +
" + ': ' + e);}"
);
};
module.exports.raw = true;