Skip to content

Instantly share code, notes, and snippets.

@danielhusar
Last active August 9, 2018 00:27
Show Gist options
  • Save danielhusar/cab8ce230b186960c198526adf87a324 to your computer and use it in GitHub Desktop.
Save danielhusar/cab8ce230b186960c198526adf87a324 to your computer and use it in GitHub Desktop.
var SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
var utils = require('loader-utils');
module.exports = function() {
// ...
};
module.exports.pitch = function(request) {
this.addDependency(request);
var query = utils.parseQuery(this.query);
var compiler = createCompiler(this, request, {
filename: query.name,
});
runCompiler(compiler, this.async());
};
function runCompiler(compiler, callback) {
compiler.runAsChild(function(error, entries) {
if (error) {
callback(error);
} else if (entries[0]) {
var url = entries[0].files[0];
callback(null, getSource(url));
} else {
callback(null, null);
}
});
}
function createCompiler(loader, request, options) {
loader.options.resolve.alias['react'] =
'/Users/danielhusar/src/intercom-js/node_modules/react-16';
loader.options.resolve.alias['react-dom'] =
'/Users/danielhusar/src/intercom-js/node_modules/react-dom-16';
var compiler = getCompilation(loader).createChildCompiler('entry', options);
var plugin = new SingleEntryPlugin(loader.context, '!!' + request, 'main');
compiler.parentCompilation.compiler.options.resolve.alias['react'] =
'/Users/danielhusar/src/intercom-js/node_modules/react-16';
compiler.parentCompilation.compiler.options.resolve.alias['react-dom'] =
'/Users/danielhusar/src/intercom-js/node_modules/react-dom-16';
compiler.options.resolve.alias['react'] =
'/Users/danielhusar/src/intercom-js/node_modules/react-16';
compiler.options.resolve.alias['react-dom'] =
'/Users/danielhusar/src/intercom-js/node_modules/react-dom-16';
//console.log(options);
compiler.apply(plugin);
var subCache = 'subcache ' + __dirname + ' ' + request;
compiler.plugin('compilation', function(compilation) {
compilation.compiler.options.resolve = {
alias: {
react: '/Users/danielhusar/src/intercom-js/node_modules/react-16',
'react-dom': '/Users/danielhusar/src/intercom-js/node_modules/react-dom-16',
},
};
compilation.options.resolve = {
alias: {
react: '/Users/danielhusar/src/intercom-js/node_modules/react-16',
'react-dom': '/Users/danielhusar/src/intercom-js/node_modules/react-dom-16',
},
};
if (!compilation.cache) {
return;
}
if (!compilation.cache[subCache]) {
compilation.cache[subCache] = {};
}
compilation.cache = compilation.cache[subCache];
});
return compiler;
}
function getSource(url) {
return 'module.exports = __webpack_public_path__ + ' + JSON.stringify(url);
}
function getCompilation(loader) {
return loader._compilation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment