Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing/aliasing a file using Webpack

Tags:

webpack

I am trying to use a very new third-party module in my project, which I installed using npm. The module was apparently developed on an OS with a case-insensitive file system, so it requires a file injectable.js whereas the actual file name is Injectable.js. This breaks the bundling process.

The developers of the module know of this issue. Meanwhile, I am trying to figure out how to use the module as it is. I am using Webpack for bundling my project.

The tree structure of the relevant part of my project is roughly this:

├──config
│     ├──webpack
│          ├──webpack.js
│
├──src
│   ├──client
│        ├──index.js
│
│
├──node_modules
│    ├──the module in question
│          ├── dist
│          │   ├── decorators
│          │   │   ├── providers
│          │   │         ├── Injectable.js
│          │   │
│          │   ├── index.js
|
├──gulpfile.js

The index.js file in the node_modules/the module in question/dist folder is requiring the file Injectable.js from the providers folder, but does so with the command require('./decorators/providers/injectable');

I would like to alias ./decorators/providers/injectable in that index.js with ./decorators/providers/Injectable.

Here is what I am doing in my webpack.js file:

resolve: {
        alias: {
            './decorators/providers/injectable': './decorators/providers/Injectable.js',
        },
}

But this still does not work; I am getting the error Module not found: Error: Cannot resolve 'file' or 'directory' ./decorators/providers/injectable

Could you please suggest how to replace one file name in a node module with another while bundling with Webpack?

UPDATE:

Here's webpack's config file:

var path = require('path');
var rucksack = require('rucksack-css')({
    fallbacks: true,
    autoprefixer: true
});
var precss = require('precss');
var csswring = require('csswring');
var webpack = require('webpack');

module.exports = {
    context: path.join(__dirname, '../../'),
    debug: false,
    entry: [
        './src/client/'
    ],
    output: {
        path: path.join(__dirname, '../../public/assets/js'),
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel?stage=1'
            },
            {
                test: /\.css$/,
                loader: 'style-loader!css-loader!postcss-loader'
            },
            {test: /\.png$/, loader: 'file-loader'},
            {test: /\.gif$/, loader: 'file-loader'},
            {test: /\.jpe?g$/, loader: 'file-loader'},
            {test: /\.eot$/, loader: 'file-loader'},
            {test: /\.woff2?$/, loader: 'file-loader'},
            {test: /\.ttf$/, loader: 'file-loader'},
            {test: /\.svg$/, loader: 'file-loader'}
        ]
    },
    postcss: function () {
        return [rucksack, precss, csswring];
    },
    plugins: [
        new webpack.NormalModuleReplacementPlugin(
            /ng-forward\/dist\/decorators\/providers\/injectable\.js/,
            require.resolve('ng-forward/dist/decorators/providers/Injectable')
        )
    ],
    resolve: {
        extensions: ['', '.js']
    }
};
like image 903
azangru Avatar asked Nov 02 '15 16:11

azangru


1 Answers

Use webpack.NormalModuleReplacementPlugin:

plugins: [
  new webpack.NormalModuleReplacementPlugin(
    /ng-forward\/dist\/decorators\/providers\/injectable\.js/,
    require.resolve('ng-forward/dist/decorators/providers/Injectable')
  ),
],
like image 92
Alexandre Kirszenberg Avatar answered Oct 02 '22 18:10

Alexandre Kirszenberg