Skip to content

Commit

Permalink
fix: override externals resolution in Node.js v10
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Aug 8, 2018
1 parent 5cb9af3 commit 4e59857
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const aFrom = require("es5-ext/arra
, d = require("d")
, deferred = require("deferred")
, memoize = require("memoizee/plain")
, cjsResolve = require("cjs-module/resolve/sync")
, { stat } = require("fs")
, { basename, extname, dirname, join, resolve, sep } = require("path")
, commonPath = require("path2/common")
, nmSource = process.binding("natives")
, getRequire = require("next/module/get-require")
, findRoot = require("next/module/find-package-root")
, { readFile } = require("fs2/read-file")
Expand Down Expand Up @@ -109,7 +109,8 @@ const getMain = memoize(path =>
const { main } = parse(stripBOM(content));
if (!main) throw new Error("No main setting found");
return main;
}));
})
);

const readFileContent = function (code, filename, fileParser, localFilename) {
const ext = extname(filename);
Expand Down Expand Up @@ -223,7 +224,8 @@ const readFileContent = function (code, filename, fileParser, localFilename) {

const readFileData = function (filename, fileParser, localFilename) {
return readFile(filename, readFileOpts)(code =>
readFileContent(code, filename, fileParser, localFilename));
readFileContent(code, filename, fileParser, localFilename)
);
};

const readFileDataCached = (function () {
Expand Down Expand Up @@ -311,7 +313,8 @@ const parser = {
return deferred.map(
data.deps, this.resolve.bind(this, input, dirname(input), scope, [])
);
}));
})
);
})(() => path.slice(0, -extname(path).length).replace(/\\/gu, "/"));
},
readFile(filename, name, scope, tree) {
Expand Down Expand Up @@ -358,7 +361,8 @@ const parser = {
if (err.code !== "ENOENT") throw err;
return false;
}
))(found => {
)
)(found => {
if (!found) {
throw new Error(
`Module '${ orgFn }' not found, as required in '${ fromfile }'`
Expand All @@ -382,7 +386,10 @@ const parser = {
return resolveSpecificPath(path, ".js")(null, () =>
resolveSpecificPath(path, ".json")(null, () =>
resolveSpecificPath(path, ".css")(null, () =>
resolveSpecificPath(path, ".html"))));
resolveSpecificPath(path, ".html")
)
)
);
};

const resolvePath = (function () {
Expand All @@ -401,13 +408,15 @@ const parser = {
if (e.code !== "ENOENT") throw e;
throw err;
}
));
)
);
};
return function (path, forceIndex) {
const ext = extname(path).toLowerCase();
if (ext === ".js" || ext === ".json" || ext === ".css" || ext === ".html") {
return resolveSpecificPath(path.slice(0, -ext.length), ext)(null, () =>
currentResolvePath(path, forceIndex));
currentResolvePath(path, forceIndex)
);
}
return currentResolvePath(path, forceIndex);
};
Expand Down Expand Up @@ -464,7 +473,7 @@ const parser = {
resolveExternal(fromfile, fileDirname, scope, dep) {
// console.log("RE", dep.value);
const org = dep.value, lScope = scope;
let filename = join(dep.value), tree, currentRequire, main, path, cache, ext;
let filename = join(dep.value), tree, currentRequire, main, path, ext;

const [name] = filename.split(sep, 1);
return deferred.promisifySync(() => {
Expand Down Expand Up @@ -496,12 +505,8 @@ const parser = {
// Require of Node.js native package.
// Hack Node.js internals to get path to substitite
// eventually provided in node_modules
cache = nmSource[name];
delete nmSource[name];
try { path = main = currentRequire.resolve(name); }
catch (ignore) {}
nmSource[name] = cache;
if (main === name) {
path = main = cjsResolve(fileDirname, name);
if (!main) {
// No substitute found
if (isOptional(scope[basename(fromfile)], dep.point, dep.raw.length)) {
return null;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"url": "git://github.com/medikoo/modules-webmake.git"
},
"dependencies": {
"cjs-module": "^1.3.1",
"d": "1",
"deferred": "^0.7.9",
"es5-ext": "^0.10.45",
Expand Down

0 comments on commit 4e59857

Please sign in to comment.