最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webpack conditional build based on configuration values - Stack Overflow

programmeradmin1浏览0评论

I'm looking for what ultimately would be a Webpack equivalent to Require.JS's has.js integration.

In Require.JS you can pile different code paths based on a variable in has.js. Is there an equivalent loader/plugin for Webpack to do the same thing?

Example:

config.js

module.exports = {
  option1: 'foo' // other option is 'bar'
}

then in code somewhere have something like the following

code.js

var a;

if (option1 === 'foo') {
  a = require('fooModule');
} else if (option1 === 'bar') {
  a = require('barModule');
}

and have a build only output the code in the condition that evaluates as true?

Alternatively, something like this would work, as well:

import MyLibrary from './modules/' + config.option1 + 'Module.js';

would this stackoverflow answer be the current best practice to achieve what i'm looking for? ultimately, the goal is to have a single set of source that can pile down to specific(ish) targets based on values in a configuration, while not sending non-applicable code down the wire as well in the final build.

I'm looking for what ultimately would be a Webpack equivalent to Require.JS's has.js integration.

In Require.JS you can pile different code paths based on a variable in has.js. Is there an equivalent loader/plugin for Webpack to do the same thing?

Example:

config.js

module.exports = {
  option1: 'foo' // other option is 'bar'
}

then in code somewhere have something like the following

code.js

var a;

if (option1 === 'foo') {
  a = require('fooModule');
} else if (option1 === 'bar') {
  a = require('barModule');
}

and have a build only output the code in the condition that evaluates as true?

Alternatively, something like this would work, as well:

import MyLibrary from './modules/' + config.option1 + 'Module.js';

would this stackoverflow answer be the current best practice to achieve what i'm looking for? ultimately, the goal is to have a single set of source that can pile down to specific(ish) targets based on values in a configuration, while not sending non-applicable code down the wire as well in the final build.

Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Aug 19, 2015 at 17:35 mindpivotmindpivot 3413 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You could use the webpack DefinePlugin to do this. Pete Hunt's Webpack Howto has a good example of implementation.

It seems the best, most webpack idiomatic way to handle this is through the

Webpack DefinePlugin

The webpack-hasjs-plugin should do what you want.

发布评论

评论列表(0)

  1. 暂无评论