Search code examples
javascripttypescriptwebpack-2

How to make a switch case in Typescript for the webpack.config.ts?


I'm having trouble with the syntax with typescript and webpack 2 config file:

The javascript equivalent is:

switch (process.env.BUILD_ENV) {
    case 'live':
        module.exports = require('./config/webpack.live');
        break;
    case 'debug':
        module.exports = require('./config/webpack.debug');
        break;
    default:
        module.exports = require('./config/webpack.doesntexist');
}

Webpack 2 takes a TS config file, so I tried changing this part to this:

switch (process.env.BUILD_ENV) {
case 'live':
    export * from './config/webpack.live';
    break;
case 'debug':
    export * from './config/webpack.debug';
    break;
default:
    export * from './config/webpack.doesntexist';
}

I'm getting the error: "an export declaration can only be used in a module". But It's not clear to me what this means. How can I correct this in typescript? Or is this not the way to build your configs in webpack 2?


Solution

  • Typescript supports only top-level import/export.

    Try

    import * as liveConfig from "./config/webpack.live";
    import * as debugConfig from "./config/webpack.debug";
    import * as defaultConfig from "./config/webpack.doesntexist";
    
    let config;
    
    switch (process.env.BUILD_ENV) {
        case 'live':
            config = liveConfig;
            break;
        case 'debug':
            config = debugConfig;
            break;
        default:
            config = defaultConfig;
    }
    
    export = config;