doberkofler
doberkofler

Reputation: 10431

How not to resolve a require when using webpack

How would I force webpack not to resolve a require statement in a node module?

In the following unit test example, I would like webpack to resolve require('./element.js') but simply to skip require('chai').

How can this be done?

index.js:

'use strict';

const assert = require('chai').assert;
const element = require('./element.js');

describe('element.create', function () {
    jsdom();

    it('creates a div', function () {
        var div = document.createElement(element.create());
        assert.strictEqual(div.nodeName, 'DIV');
    });
});

Run:

webpack index.js index-bundle.js
mocha index-bundle.js

Upvotes: 0

Views: 206

Answers (2)

Rajeesh Madambat
Rajeesh Madambat

Reputation: 3751

You can use require.ensure to load it asynchronously when you needed. This will help you to load require('chai') when ever your specific condition is met or do not load.

require.ensure

In addition to that to remove the require('chai') from your bundle and to split in to a separate bundle go for vendor bundle eg:

entry: {
    app: 'entry.js',
    vendor: ['jquery', 'jquery.plugin1']
}

Upvotes: 1

semirturgay
semirturgay

Reputation: 4201

I think you may be looking for externals

from the docs :

Externals: Specify dependencies that shouldn’t be resolved by webpack, but should become dependencies of the resulting bundle. The kind of the dependency depends on output.libraryTarget.

Upvotes: 1

Related Questions