如何通过webpack在tsconfig.json动态创建sourceMap选项?

时间:2017-01-16 08:37:20

标签: typescript webpack babeljs source-maps tsconfig

我正在使用webpack来编译我的typescript相关.tsx文件,这些文件也使用jsxES2015/stage-0语法。

我的webpack.config.js文件如下:

var PATHS = {
    "build": path.join(__dirname, 'build'),
    "myModule1": path.join(__dirname, 'js', 'module1'),
    "myModule2": path.join(__dirname, 'js', 'module2')
}

var scriptIncludes = [PATHS.myModule1, 
                      PATHS.myModule2]

module.exports = {
    entry: {
        "my-module1": path.join(PATHS.myModule1, 'index.jsx'),
        "my-module2": path.join(PATHS.myModule2, 'index.tsx')
    },
    output: {
        filename: '[name].js',
        sourceMapFilename: '[name].js.map',
        path: PATHS.build
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // resolvable extensions.
        // Files with the following extensions are fair game for webpack to process.
        extensions: ['', ".webpack.js", ".web.js", ".ts", ".tsx", ".js", ".jsx"],
        alias: {
            'ie': 'component-ie'
        }
    },
    plugins: [], //plugins,
    module: {
        loaders: [
        {
            test: /\.js*/,
            include: scriptIncludes,
            loader: "babel-loader", query: { presets: ['react', 'es2015', 'stage-0'] } 
        },
        {
            // The loader that handles ts and tsx files. These are compiled
            // with the awesome-typescript-loader and the output is then passed through to the
            // babel-loader. The babel-loader uses the es2015, react and stage-0 presets
            // in order that jsx and es6 are processed.
            // Note that order of loader processing is from right to left.
            test: /\.ts(x?)$/,
            include: scriptIncludes,
            loader: 'babel-loader?presets[]=es2015&presets[]=react&presets[]=stage-0!awesome-typescript-loader'
        }],
        preLoaders: [
          // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
          { test: /\.js$/, loader: "source-map-loader" }
        ]
    },

    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },
};

我的tsconfig.json文件如下:

{
  "compilerOptions": {
    "outDir": "./build/",
    "module": "commonjs",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": true,
    "jsx": "react"
  },
  "exclude": [
    "node_modules"
  ]
}

现在:

  1. 如果我在sourceMap中设置tsconfig.json选项为true,则只会生成源地图。我想基于一些命令行参数使其动态化,而不是每次都在tsconfig.json文件中对其进行硬编码。 我怎样才能实现这个目标?

  2. 另外,如果我在webpack配置中评论preLoaders选项,会有什么不同吗?

1 个答案:

答案 0 :(得分:2)

您可以在加载器查询字符串

中传递编译器选项

e.g

  

真棒-打字稿装载机?sourceMap =假