Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Set module.path in Module constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Mar 29, 2019
1 parent 0749eb8 commit 0799513
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SafeObject from "./safe/object.js"

import assign from "./util/assign.js"
import builtinIds from "./builtin-ids.js"
import getModuleDirname from "./util/get-module-dirname.js"
import isExtNode from "./path/is-ext-node.js"
import isObjectLike from "./util/is-object-like.js"
import maskFunction from "./util/mask-function.js"
Expand All @@ -31,14 +32,15 @@ const {
ELECTRON
} = ENV

const Module = maskFunction(function (id, parent) {
const Module = maskFunction(function (id = "", parent) {
this.children = GenericArray.of()
this.exports = GenericObject.create()
this.filename = null
this.id = id
this.loaded = false
this.parent = parent
this.paths = void 0
this.path = getModuleDirname(this)

const children = parent == null
? null
Expand Down
25 changes: 22 additions & 3 deletions src/util/get-module-dirname.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import builtinLookup from "../builtin-lookup.js"
import { dirname } from "../safe/path.js"
import getFilePathfromURL from "./get-file-path-from-url.js"
import isFileOrigin from "./is-file-origin.js"
import isObject from "./is-object.js"
import shared from "../shared.js"

function init() {
function getModuleDirname(mod) {
if (isObject(mod)) {
const { filename } = mod
const { path } = mod

if (builtinLookup.has(mod.id)) {
if (typeof path === "string") {
return path
}

const { id } = mod

if (builtinLookup.has(id)) {
return ""
} else if (typeof filename === "string") {
}

let { filename } = mod

if (filename === null &&
typeof id === "string") {
filename = isFileOrigin(id)
? getFilePathfromURL(id)
: id
}

if (typeof filename === "string") {
return dirname(filename)
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/cjs/intercept/dynamic-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const content = [

const filename = path.resolve(__dirname, "virtual.js")
const intercepted = []
const mod = new Module(".")
const mod = new Module(filename)
const oldRequire = Module.prototype.require

Module.prototype.require = function (request) {
Expand Down
2 changes: 1 addition & 1 deletion test/cjs/intercept/static-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const content = [

const filename = path.resolve(__dirname, "virtual.js")
const intercepted = []
const mod = new Module(".")
const mod = new Module(filename)
const oldRequire = Module.prototype.require

Module.prototype.require = function (request) {
Expand Down

0 comments on commit 0799513

Please sign in to comment.