Skip to content

Commit 6e2c0d9

Browse files
committed
Initial commit.
0 parents  commit 6e2c0d9

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# requirejs-resolver
2+
3+
Allows using `import` to find modules based on requirejs config path mapping.
4+
5+
This is specifically useful for Magento 2, which extensively uses requirejs.
6+
7+
## Config parsing
8+
9+
The config file is executed to find all calls to `require.config()`.
10+
11+
## Usage
12+
13+
const RequireJsResolverPlugin = require('@sdinteractive/requirejs-resolver');
14+
15+
module.exports = {
16+
resolve: {
17+
plugins: [
18+
new RequireJsResolverPlugin({
19+
configPath: path.resolve(__dirname, 'requirejs-config.js'),
20+
}),
21+
],
22+
},
23+
};

index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
const vm = require('vm');
4+
5+
function RequireJsResolverPlugin(options) {
6+
this.configPath = options.configPath;
7+
}
8+
9+
RequireJsResolverPlugin.prototype.getConfig = function(fs) {
10+
return new Promise((resolve, reject) => {
11+
if (this.configData) {
12+
return resolve(this.configData);
13+
}
14+
15+
fs.readFile(this.configPath, function(err, buffer) {
16+
if (err) {
17+
reject(err);
18+
} else {
19+
resolve(buffer.toString('utf8'));
20+
}
21+
});
22+
}).then(code => {
23+
if (this.configData) {
24+
return this.configData;
25+
}
26+
27+
var sandbox = {
28+
paths: {},
29+
require: function() {
30+
},
31+
};
32+
sandbox.require.addPaths = function(paths) {
33+
for (var path in paths) {
34+
sandbox.paths[path] = paths[path];
35+
}
36+
};
37+
sandbox.require.config = function(config) {
38+
if (config.paths) {
39+
this.addPaths(config.paths);
40+
}
41+
42+
// Used by Magento.
43+
if (config.map && config.map['*']) {
44+
this.addPaths(config.map['*']);
45+
}
46+
};
47+
vm.runInNewContext(code, sandbox, {
48+
filename: this.configPath,
49+
displayErrors: true,
50+
});
51+
52+
this.configData = sandbox.paths;
53+
return this.configData;
54+
});
55+
};
56+
57+
RequireJsResolverPlugin.prototype.apply = function(resolver) {
58+
resolver.plugin('module', (request, callback) => {
59+
this.getConfig(resolver.fileSystem).then(config => {
60+
if (config[request.request]) {
61+
var nextRequest = Object.assign({}, request, { request: config[request.request] });
62+
return resolver.doResolve('resolve', nextRequest, 'mapping via requirejs-config', function(err, result) {
63+
callback(err, result);
64+
});
65+
} else {
66+
callback();
67+
}
68+
})
69+
});
70+
};
71+
72+
module.exports = RequireJsResolverPlugin;

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@sdinteractive/requirejs-resolver",
3+
"version": "1.0.0",
4+
"description": "webpack resolver to find requirejs modules via requirejs config.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/sdinteractive/webpack-requirejs-resolver.git"
12+
},
13+
"keywords": [
14+
"webpack",
15+
"requirejs"
16+
],
17+
"author": "Something Digital",
18+
"license": "ISC",
19+
"bugs": {
20+
"url": "https://github.com/sdinteractive/webpack-requirejs-resolver/issues"
21+
},
22+
"homepage": "https://github.com/sdinteractive/webpack-requirejs-resolver#readme"
23+
}

0 commit comments

Comments
 (0)