Giter Club home page Giter Club logo

Comments (5)

sokra avatar sokra commented on March 29, 2024

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.

kavaro avatar kavaro commented on March 29, 2024

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.

sokra avatar sokra commented on March 29, 2024

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.

kavaro avatar kavaro commented on March 29, 2024

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)

  1. lib/require.js: add sandbox default options
  2. lib/RequireContext.js: add sandbox capability
  3. examples/simple-app: directory with a simple test app, used by test/simple_app.js
  4. test/simple_app.js: test case

Note: to run tests you need to first npm install enhanced-resolve and raw-loader

from webpack.

sokra avatar sokra commented on March 29, 2024

Here is the documentation.

from webpack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.