在webpack配置中提供模块定义

时间:2015-06-20 17:32:42

标签: javascript dependency-management webpack

我以编程方式调用webpack。在我调用它的时候,我有一个设置对象,我想将其作为webpack中的模块包含在内。这可能吗?

我正在寻找类似于DefinePlugin的东西,但我想定义一个模块,而不是自由变量。

我的应用代码- (void)getGrades: callback:(SARequestCallback)callback; 如下所示:

if(appDelegate.user.community){
    [[SSDataProvider instance] getGrades: callback:^(SARequestResult *result) {
        [self stopSync];

        if(result.status == SARequestStatusOK){
            NSLog(@"Sync successful");
            [self loadWithData:result.data];
            if (deltaTime == 0) {
                [self writeDict:result.data file:@"GradesCache-0"];
            }
            [self writeDict:result.data file:[NSString stringWithFormat:@"GradesCache-%@", time]];
        }else if(result.error.code > 900){
            // Show login dialog, but only if the error is a Scholica error, not a network error
            NSLog(@"Scholica error, present login view: %@", result.error.errorDescription);
            [self login:YES];
        }else{
            // Network error, so try again in a couple of seconds
            NSLog(@"Network error, will try again soon.");
            [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(synchronize) userInfo:nil repeats:NO];
        }
    }];
}else{
    syncing = NO;
    [appDelegate getUser];
}

我的网站运营商app.js

var settings = require('settings');
console.log('Build number is', settings.buildNumber);

目前,我发现这样做的唯一方法是将我的设置保存到文件,然后将别名设置为文件的路径。但我想避免保存文件只是为了让webpack稍后打开它。

1 个答案:

答案 0 :(得分:-1)

是的,只需要在您的设置文件中。示例如下。

// settings.js
module.exports = {
  buildNumber: 100
};

// webpack.config.js
var settings = require('./settings'); // settings.buildNumber => 100

var config = {
  entry: './entry.js',
  output: {
    path: __dirname,
    filename: 'build.js'
  }
};

webpack(config, function( err, stats ) {
  console.log(stats.toString({
    colors: true,
    modules: true,
    chunkModules: true
  }));
});