Comments (5)
enhanced-require creates a own request function, so the mocked one created by 'mockery' or 'sandboxed-module' will be overwritten.
I cannot support these modules because they will break if they want to resolve the parameter of the required function (i. e. require("loader!./file")
). So there is no support with enhanced-require 0.3.x.
But I'm working on enhanced-require 0.4.x for a while and that will change some stuff.
You do not longer need to add the (ugly) header into each file. You can enable it once (like sandboxed-module). You create a require function with some options and use it. All modules required will have it recursivly enabled. The options are similar to the webpack options so you can use the alias
option to mock modules. Or you can inject modules into the cache.
var enhancedRequire = require("enhanced-require"); // 0.4.x
var myRequire = enhancedRequire(module, {
recursive: true,
resolve: {
alias: {
// provide a mock by new resolving alias
"module": "module-mock"
}
}
});
// inject a mock into the cache
myRequire.cache[myRequire.resolve("../lib/file")] = {
exports: { abc: "abc" }
};
var subject = myRequire("../lib/subject");
It's not (yet) perfect, but I try to add some some stuff to the wip 0.4.x version to support this (with better syntax). Maybe something like this:
var er = require("enhanced-require");
it("should do something", function() {
var subject = er.recursive(module, {
replacements: {
"../lib/file": { exports: { abc: "abc" }},
"module": "module-mock" // this is not exactly the same as above
}
})("../lib/subject");
subject.should.have.property("file").be.eql("abc");
}
NOTE: keep in mind that enhanced-require 0.4.x is still beta and API may change.
You can file more issue at http://github.com/webpack/enhanced-require
from webpack.
Thanks for the quick response … looks very promising ...
If I understand correctly, enhanced-require 0.4.x would then completely replace the sandboxed-module, correct ?
I quickly prototyped what you are suggesting into 0.3.x and indeed, this seems to work well, provided, as you already mentioned, enhanced-require can be applied as a global config instead of calling it in every source file.
The prototype adds a sandbox config to the enhanced-require options.
The sandbox config object is a sandboxed-module config (only implements requires), see example:
require = require("enhanced-require")(module, {
sandbox: {
requires: {
'./append': '',
'raw!../assets/hello.txt': 'Hello',
'raw!../assets/webPack.txt': 'WebPack',
'raw!../assets/world.txt': 'World'
}
}
});
Has been tested with the following small test example:
module.exports = function (done) {
require = require("enhanced-require")(module, { sandbox: {
requires: {
'./append': '',
'raw!../assets/hello.txt': 'Hello',
'raw!../assets/webPack.txt': 'WebPack',
'raw!../assets/world.txt': 'World'
}
}});
var append = require('./append');
var hello = require('raw!../assets/hello.txt');
var webPack = require('raw!../assets/webPack.txt');
var helloWebPack = append(hello, webPack);
require.ensure(['raw!../assets/world.txt'], function (require) {
var world = require('raw!../assets/world.txt');
var helloWebPackWorld = append(helloWebPack, world);
done(helloWebPackWorld);
});
return helloWebPack;
}
The implementation of the prototype only requires a few small changes to RequiredContent.js.
If you are interested to get the source, just let me know ...
(Didn't know how to attach the source files to this post)
In what state is 0.4.x, is it stable enough to try it out ?
Does it already recursively apply enhanced-require to all modules ?
from webpack.
Yes it apply it recursivly if you add recursive: true
to the options. So you can add it into the test case and remove it from the module.
You find wip-0.4
on enhanced-require and enhanced-resolve and wip-0.8
on webpack. They currently work well, but I think the HCR API is not yet stable enougth.
You can put it into a fork and I'll take a look.
{
substitutions: { // define the exports of a module
'raw!../assets/hello.txt': 'Hello',
'raw!../assets/webPack.txt': 'WebPack',
'raw!../assets/world.txt': 'World'
// the key is resolved with require.resolve,
// so any require matching this file will be replaced
}
}
from webpack.
Apparently I was already using wip-0.4 and not 0.3 :-).
Forked enhanced-require to: https://github.com/kavaro/enhanced-require.git
Made following changes to wip-0.4: (all changes are marked with // Kavaro comment)
- lib/require.js: add sandbox default options
- lib/RequireContext.js: add sandbox capability
- examples/simple-app: directory with a simple test app, used by test/simple_app.js
- test/simple_app.js: test case
Note: to run tests you need to first npm install enhanced-resolve and raw-loader
from webpack.
Here is the documentation.
from webpack.
Related Issues (20)
- Not found node_modules/@angular/common/bundles/common-http.umd.js
- webpack5.90.0 Modifying files in the node_modules directory (local component library) cannot trigger hot updates HOT 1
- Delete this.
- how to make webpack recompile on dev mode with cache on?
- Webpack build error for second time HOT 1
- `exportsPresence` does not work for unused imports HOT 3
- Implement ability to use "node:" prefixes for Node.js core modules HOT 1
- mangled exports breaks with destructuring assignment of JSON imports HOT 7
- there are confusing JDocs code
- Potential bug issues
- Webpack is not resolving node_modules specified in `resolve.modules` property.
- Dynamically loading chunks
- [Code implementation issues] ArrayQueue
- [feature] create cli HOT 3
- Not able to build node script
- bootstrap:27 Uncaught TypeError: __webpack_require__.nmd is not a function
- Self-reference dependency has unused export name when imported inside of a web worker
- support `import.meta.dirname` and `import.meta.filename` HOT 2
- <!-- identifier: admin-actions -->
- Remote into Web Component from React giving an error saying " Module does not exist in container"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webpack.