2

我在使用 typescript 和 webpack 2 配置文件的语法时遇到问题:

等效的javascript是:

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 需要一个 TS 配置文件,所以我尝试将此部分更改为:

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';
}

我收到错误消息:“导出声明只能在模块中使用”。但我不清楚这意味着什么。我怎样才能在打字稿中更正这个?或者这不是在 webpack 2 中构建配置的方式吗?

4

1 回答 1

5

Typescript 仅支持顶级导入​​/导出。

尝试

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;
于 2017-03-13T18:26:12.457 回答