Skip to content

Support newer webpack versions #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The config file is executed to find all calls to `require.config()`.

## Usage

In webpack.config.js:

const RequireJsResolverPlugin = require('@sdinteractive/requirejs-resolver');

module.exports = {
Expand All @@ -21,3 +23,6 @@ The config file is executed to find all calls to `require.config()`.
],
},
};

Normally for Magento 2, this should be the requirejs-config.js (or requirejs-config.min.js) generated by
the static content process. This will map aliases in the requirejs-config when imported.
34 changes: 22 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ function RequireJsResolverPlugin(options) {
this.sandbox = options.sandbox || {};
}

RequireJsResolverPlugin.prototype.getConfig = function(fs) {
RequireJsResolverPlugin.prototype.getConfig = function (fs) {
return new Promise((resolve, reject) => {
if (this.configData) {
return resolve(this.configData);
}

fs.readFile(this.configPath, function(err, buffer) {
fs.readFile(this.configPath, (err, buffer) => {
if (err) {
reject(err);
} else {
Expand All @@ -27,15 +27,15 @@ RequireJsResolverPlugin.prototype.getConfig = function(fs) {

let sandbox = Object.assign({
paths: {},
require: function() {
require: () => {
},
}, this.sandbox);
sandbox.require.addPaths = function(paths) {
for (var path in paths) {
sandbox.require.addPaths = function (paths) {
for (let path in paths) {
sandbox.paths[path] = paths[path];
}
};
sandbox.require.config = function(config) {
sandbox.require.config = function (config) {
if (config.paths) {
this.addPaths(config.paths);
}
Expand All @@ -55,14 +55,24 @@ RequireJsResolverPlugin.prototype.getConfig = function(fs) {
});
};

RequireJsResolverPlugin.prototype.apply = function(resolver) {
resolver.plugin('module', (request, callback) => {
function registerHook(object, oldName, newName, cb) {
if (object.hooks) {
object.hooks[newName].tapAsync('RequireJsResolverPlugin', cb);
} else {
object.plugin(oldName, cb);
}
}

RequireJsResolverPlugin.prototype.apply = function (resolver) {
const target = resolver.ensureHook('resolve');
registerHook(resolver, 'module', 'module', (request, resolveContext, callback) => {
if (!callback) {
callback = resolveContext;
}
this.getConfig(resolver.fileSystem).then(config => {
if (config[request.request]) {
var nextRequest = Object.assign({}, request, { request: config[request.request] });
return resolver.doResolve('resolve', nextRequest, 'mapping via requirejs-config', function(err, result) {
callback(err, result);
});
const nextRequest = Object.assign({}, request, { request: config[request.request] });
return resolver.doResolve(target, nextRequest, 'mapping via requirejs-config', callback);
} else {
callback();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sdinteractive/requirejs-resolver",
"version": "1.1.0",
"version": "2.0.0",
"description": "webpack resolver to find requirejs modules via requirejs config.",
"main": "index.js",
"scripts": {
Expand Down