From 35504caf2e9f1407098ba59cd0887eaedad33366 Mon Sep 17 00:00:00 2001 From: yuriymanakov <31983893+yuriymanakov@users.noreply.github.com> Date: Tue, 10 Sep 2019 18:02:41 +0300 Subject: [PATCH] Fix module loading for webpack v4 (#9) --- index.js | 3 +-- load-module-recursively.js | 53 -------------------------------------- 2 files changed, 1 insertion(+), 55 deletions(-) delete mode 100644 load-module-recursively.js diff --git a/index.js b/index.js index 643d433..6f98a2b 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,6 @@ const os = require('os'); const loaderUtils = require('loader-utils'); const { ExtractGQL } = require('persistgraphql/lib/src/ExtractGQL'); const queryTransformers = require('persistgraphql/lib/src/queryTransformers'); -const loadModuleRecursively = require('./load-module-recursively'); module.exports = function graphQLPersistedDocumentLoader(content) { const deps = []; @@ -15,7 +14,7 @@ module.exports = function graphQLPersistedDocumentLoader(content) { const sandbox = { require(file) { deps.push(new Promise((resolve, reject) => { - loadModuleRecursively(context, file, (err, source, sourceMap, module) => { + context.loadModule(file, (err, source, sourceMap, module) => { if (err) { reject(err); } else { diff --git a/load-module-recursively.js b/load-module-recursively.js deleted file mode 100644 index e168caa..0000000 --- a/load-module-recursively.js +++ /dev/null @@ -1,53 +0,0 @@ -/* eslint-disable */ -// This code is copied from Webpack's code and modified so that it can load modules -// recursively, otherwise the loader for persisted queries fails if there are -// queries that depend on other files. - -// There's an issue in Webpack that talks about this problem -// https://github.com/webpack/webpack/issues/4959 -// Once it's solved, we won't need this file. -// The file copied from is https://github.com/webpack/webpack/blob/master/lib/dependencies/LoaderPlugin.js -const LoaderDependency = require('webpack/lib/dependencies/LoaderDependency'); - -module.exports = function loadModuleRecursively(loaderContext, request, callback) { - const { - _compilation: compilation, - _module: module - } = loaderContext; - const dep = new LoaderDependency(request); - dep.loc = request; - compilation.addModuleDependencies(module, [ - [dep] - ], true, "lm", true /* This is _false_ in the original file */, (err) => { - if(err) return callback(err); - - if(!dep.module) return callback(new Error("Cannot load the module")); - if(dep.module.building) dep.module.building.push(next); - else next(); - - function next(err) { - if(err) return callback(err); - - if(dep.module.error) return callback(dep.module.error); - if(!dep.module._source) throw new Error("The module created for a LoaderDependency must have a property _source"); - let source, map; - const moduleSource = dep.module._source; - if(moduleSource.sourceAndMap) { - const sourceAndMap = moduleSource.sourceAndMap(); - map = sourceAndMap.map; - source = sourceAndMap.source; - } else { - map = moduleSource.map(); - source = moduleSource.source(); - } - if(dep.module.fileDependencies) { - dep.module.fileDependencies.forEach((dep) => loaderContext.addDependency(dep)); - } - if(dep.module.contextDependencies) { - dep.module.contextDependencies.forEach((dep) => loaderContext.addContextDependency(dep)); - } - return callback(null, source, map, dep.module); - } - }); -}; -/* eslint-enable */