2017-01-16 130 views
0

我使用webpack来编译我的typescript相关的.tsx文件,这些文件也使用jsxES2015/stage-0语法。如何通过webpack使tsconfig.json中的sourceMap选项变为动态?

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. 如果我在tsconfig.json真正的设置sourceMap选项,只有源图才会生成。我想根据一些命令行参数使其动态化,而不是每次都在tsconfig.json文件中对其进行硬编码。 我该如何做到这一点?

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

回答

2

您可以通过加载查询字符串中的编译器选项

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

+0

对于命令行支持,你可以做类似 > awesome-typecript-loader?sourceMap = process.env.TS_SOURCEMAPS 并使用 > [export | set] TS_SOURCEMAPS = true && webpack –

相关问题