Comparing version 0.0.16 to 0.0.18
@@ -180,7 +180,25 @@ var assert = require("assert"); | ||
var dd = decl.declaration; | ||
if (dd.type === "FunctionDeclaration" || | ||
dd.type === "ClassDeclaration") { | ||
// If the exported default value is a function or class declaration, | ||
// it's important that the declaration be visible to the rest of the | ||
// code in the exporting module, so we must avoid compiling it to a | ||
// named function or class expression. | ||
var map = {}; | ||
map[dd.id.name] = "default"; | ||
return [ | ||
pad("", code, decl.start, dd.start), | ||
exportDeclarationHelper(code, dd, map), | ||
pad("", code, dd.end, decl.end) | ||
].join(""); | ||
} | ||
return [ | ||
pad("exports.__esModule=true;exports.default=", | ||
// Otherwise, since the exported value is an expression, it's | ||
// important that we wrap it with parentheses, in case it's something | ||
// like a comma-separated sequence expression. | ||
pad("exports.default=(", | ||
code, decl.start, dd.start), | ||
compile(code.slice(dd.start, dd.end)), | ||
pad(";", code, dd.end, decl.end) | ||
pad(");module.export();", code, dd.end, decl.end) | ||
].join(""); | ||
@@ -187,0 +205,0 @@ }; |
var assert = require("assert"); | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
var settersMap = Object.create(null); | ||
var entryMap = Object.create(null); | ||
var accessorUtils = require("./accessor-utils.js"); | ||
@@ -21,4 +21,8 @@ | ||
Entry.get = function (id) { | ||
return entryMap[id] || null; | ||
}; | ||
Entry.getOrCreate = function (id) { | ||
return settersMap[id] = settersMap[id] || new Entry(id); | ||
return entryMap[id] = entryMap[id] || new Entry(id); | ||
}; | ||
@@ -37,2 +41,7 @@ | ||
Ep.addSetter = function (name, setter) { | ||
if (name === "__esModule") { | ||
// Ignore any requests for the exports.__esModule property." | ||
return; | ||
} | ||
assert.strictEqual(typeof name, "string"); | ||
@@ -53,2 +62,7 @@ assert.strictEqual(typeof setter, "function"); | ||
Ep.addGetter = function (name, setter) { | ||
if (name === "__esModule") { | ||
// Ignore any requests for the exports.__esModule property." | ||
return; | ||
} | ||
assert.strictEqual(typeof name, "string"); | ||
@@ -61,8 +75,8 @@ assert.strictEqual(typeof setter, "function"); | ||
Entry.runModuleSetters = function (module) { | ||
var entry = settersMap[module.id]; | ||
function runModuleSetters(module) { | ||
var entry = entryMap[module.id]; | ||
if (entry) { | ||
entry.runModuleSetters(module); | ||
} | ||
}; | ||
} | ||
@@ -108,3 +122,3 @@ Ep.runModuleSetters = function (module) { | ||
Object.keys(parents).forEach(function (id) { | ||
Entry.runModuleSetters(parents[id]); | ||
runModuleSetters(parents[id]); | ||
}); | ||
@@ -111,0 +125,0 @@ }; |
113
lib/node.js
@@ -1,82 +0,42 @@ | ||
var assert = require("assert"); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var Module = require("module"); | ||
var Mp = Module.prototype; | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
if (typeof Mp.import === "function" && | ||
typeof Mp.export === "function") { | ||
// If the Module.prototype.{import,export} methods are already defined, | ||
// abandon reification immediately. | ||
return; | ||
} | ||
// Enable import and export statements in the default Node REPL. | ||
require("./repl"); | ||
var Entry = require("./entry.js").Entry; | ||
var compile = require("./compiler.js").compile; | ||
var Module = require("./module.js").Module; | ||
var Mp = Module.prototype; | ||
var isEnabledCache = Object.create(null); | ||
Mp.import = function (id, setters) { | ||
var module = this; | ||
id = Module._resolveFilename(id, module); | ||
if (setters && typeof setters === "object") { | ||
var entry = Entry.getOrCreate(id); | ||
entry.addSetters(setters); | ||
entry.addParent(module); | ||
} | ||
var countBefore = entry && entry.runCount; | ||
var exports = module.require(id); | ||
if (entry && entry.runCount === countBefore) { | ||
entry.runModuleSetters(Module._cache[id] || { | ||
id: id, | ||
exports: exports, | ||
getExportByName: Mp.getExportByName | ||
}); | ||
} | ||
return exports; | ||
// Define Module.prototype.resolve in a way that makes sense for Node. | ||
Mp.resolve = function (id) { | ||
return Module._resolveFilename(id, this); | ||
}; | ||
// Register a getter function for a local variable in the scope of an | ||
// export statement. | ||
Mp.export = function (id, getter) { | ||
var entry = Entry.getOrCreate(this.id); | ||
if (typeof id === "string") { | ||
entry.addGetter(id, getter); | ||
} else if (typeof id === "object") { | ||
entry.addGetters(id); | ||
} | ||
}; | ||
// Override Module.prototype.load to call Entry.runModuleSetters whenever | ||
// Override Module.prototype.load to call this.runModuleSetters() whenever | ||
// a module has loaded. | ||
var load = Mp.load; | ||
Mp.load = function () { | ||
var result = load.apply(this, arguments); | ||
Entry.runModuleSetters(this); | ||
return result; | ||
}; | ||
if (! load.reified && Mp.runModuleSetters) { | ||
(Mp.load = function () { | ||
var result = load.apply(this, arguments); | ||
this.runModuleSetters(); | ||
return result; | ||
}).reified = load; | ||
} | ||
// Override Module.prototype._compile to compile any code that will be | ||
// evaluated as a module. | ||
// TODO Does this work in the Node REPL? | ||
var _compile = Mp._compile; | ||
Mp._compile = function (content, filename) { | ||
return _compile.call( | ||
this, | ||
isEnabled(filename) | ||
? compile(content) | ||
: content, | ||
filename | ||
); | ||
}; | ||
if (! _compile.reified) { | ||
(Mp._compile = function (content, filename) { | ||
return _compile.call( | ||
this, | ||
// Don't touch the file unless reification is enabled for the | ||
// package that contains the file. | ||
isEnabled(filename) | ||
? compile(content) | ||
: content, | ||
filename | ||
); | ||
}).reified = _compile; | ||
} | ||
var isEnabledCache = Object.create(null); | ||
function isEnabled(filename) { | ||
@@ -146,18 +106,3 @@ if (hasOwn.call(isEnabledCache, filename)) { | ||
// This method can be overridden by client code to implement custom export | ||
// naming logic. The current implementation works well with Babel's | ||
// __esModule convention. | ||
Mp.getExportByName = function (name) { | ||
var exports = this.exports; | ||
if (name === "*") { | ||
return exports; | ||
} | ||
if (name === "default" && | ||
! (exports && exports.__esModule)) { | ||
return exports; | ||
} | ||
return exports && exports[name]; | ||
}; | ||
// Enable import and export statements in the default Node REPL. | ||
require("./repl"); |
{ | ||
"name": "reify", | ||
"version": "0.0.16", | ||
"version": "0.0.18", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "browser": "lib/empty.js", |
@@ -48,2 +48,39 @@ var assert = require("assert"); | ||
}); | ||
it("should support default declarations", function () { | ||
import g, { check } from "./default-function"; | ||
check(g); | ||
}); | ||
it("should support default expressions", function () { | ||
import count from "./default-expression"; | ||
assert.strictEqual(count, 1); | ||
}); | ||
it("should be able to invoke setters later", function (done) { | ||
import def, { | ||
val, | ||
exportAgain, | ||
exportYetAgain, | ||
oneLastExport | ||
} from "./export-later"; | ||
assert.strictEqual(def, "default-1"); | ||
assert.strictEqual(val, "value-1"); | ||
exportAgain(); | ||
assert.strictEqual(def, "default-2"); | ||
assert.strictEqual(val, "value-2"); | ||
exportYetAgain(); | ||
assert.strictEqual(def, "default-3"); | ||
assert.strictEqual(val, "value-2"); | ||
setImmediate(function () { | ||
oneLastExport(); | ||
assert.strictEqual(def, "default-3"); | ||
assert.strictEqual(val, "value-3"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -50,0 +87,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30483
27
825