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)
- Module Federation - Some chunks are not loaded when navigating from the Remote module to the Host module
- failed to import ffmpeg-core.js HOT 1
- Webpack 5 doesnt generate the chunks of imports HOT 1
- Error: Cannot find module 'webpack-cli/package.json' HOT 2
- Unable to bust cache of module federation remotes....
- Re-exported Ant Design components don't have same style with the original components
- failed to import ffmpeg-core.js HOT 3
- DllPlugin: When processing multiple chunks, does the `path` option must have a string template?
- Webpack BannerPlugin with `raw` option should put `use strict` on the very first line HOT 10
- After webpack 5 migration not able to use local packages in a mono repo
- 95% emitting emit phase takes much longer in Node >=16
- Plugin apply not working for multi compiler HOT 2
- packages/tailwindcss/src/compile.ts HOT 1
- Current error handling mainly involves calling the `onInvalidCandidate` callback function to handle invalid candidates, and printing corresponding error messages to the console.
- [5.91.0] "Fixed fs types" breaks `outputFileSystem` compatibility with `memfs` HOT 6
- Using `[name]` within `output.library`, the correct value isn't utilized in the source map namespace HOT 1
- 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. HOT 1
- how to make webpack recompile on dev mode with cache on? HOT 1
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.