Giter Site home page Giter Site logo

Trouble with __dirname about mocha-webpack HOT 3 CLOSED

neelance avatar neelance commented on August 18, 2024
Trouble with __dirname

from mocha-webpack.

Comments (3)

0xR avatar 0xR commented on August 18, 2024 3

This works indeed!

Can this be part of mocha-webpack itself? That way I don't have to add this plugin I don't quite understand to my codebase.

from mocha-webpack.

zinserjan avatar zinserjan commented on August 18, 2024

Seems that there is no way to get this behavior with webpack without doing something special. Webpack replaces the __dirname and __filename with constant strings.

Let's do the same. I modified webpack's NodeStuffPlugin plugin to get the desired effect.

NodePathReplacePlugin.js

/**
 * Modified NodeStuffPlugin to replace __filename and __dirname with absolute path
 * @see https://github.com/webpack/webpack/blob/ca8b693c2c17bd06778476381fae23b3b21c0475/lib/NodeStuffPlugin.js
 */
function NodePathReplacePlugin() {}
module.exports = NodePathReplacePlugin;
NodePathReplacePlugin.prototype.apply = function(compiler) {

    function setModuleConstant(expressionName, fn) {
        compiler.parser.plugin("expression " + expressionName, function() {
            this.state.current.addVariable(expressionName, JSON.stringify(fn(this.state.module)));
            return true;
        });
    }

    setModuleConstant("__filename", function(module) {
        return module.resource;
    });

    setModuleConstant("__dirname", function(module) {
        return module.context;
    });

};

Just require it in your webpack config and add it the plugins sections
webpack-config.js

var NodePathReplacePlugin = require('./NodePathReplacePlugin');

module.exports = {
....
  plugins: [
    new NodePathReplacePlugin()
  ]
....
}

from mocha-webpack.

neelance avatar neelance commented on August 18, 2024

Cool, thanks!

from mocha-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.