【问题标题】:Map files for TS with webpack使用 webpack 为 TS 映射文件
【发布时间】:2020-05-02 02:23:56
【问题描述】:

我开始将 webpack 用于 Angular 项目。捆绑包得到生成的文件,但我希望看到地图文件能够在开发模式下编译时从浏览器中调试 TS/JS 代码。

我的 tsconfig.json 是:

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": true,
    "noImplicitAny": true,
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "lib": [
      "es2015",
      "dom"
    ],
    "typeRoots": [
      "./node_modules/@types/"
    ]
  },
  "exclude": [
    "node_modules",
    "./angularApp/main-aot.ts"
  ],
  "awesomeTypescriptLoaderOptions": {
    "useWebpackText": true
  },
  "compileOnSave": false,
  "buildOnSave": false
}

我的 webpack.dev.js 看起来像这样:

const path = require('path');
const rxPaths = require('rxjs/_esm5/path-mapping');

const webpack = require('webpack');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const FilterWarningsPlugin = require('webpack-filter-warnings-plugin');

const helpers = require('./webpack.helpers');

const ROOT = path.resolve(__dirname, '..');

console.log('@@@@@@@@@ USING DEVELOPMENT @@@@@@@@@@@@@@@');

module.exports = {
  mode: 'development',
  devtool: 'inline-source-map',
  performance: {
    hints: false
  },
  entry: {
    polyfills: './angularApp/polyfills.ts',
    vendor: './angularApp/vendor.ts',
    app: './angularApp/main.ts'
  },

  output: {
    path: ROOT + '/wwwroot/',
    filename: 'dist/[name].bundle.js',
    chunkFilename: 'dist/[id].chunk.js',
    publicPath: '/'
  },

  resolve: {
    extensions: ['.ts', ".tsx",'.js', '.json'],
    alias: rxPaths()
  },

  devServer: {
    historyApiFallback: true,
    contentBase: path.join(ROOT, '/wwwroot/'),
    watchOptions: {
      aggregateTimeout: 300,
      poll: 1000
    }
  },

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          'awesome-typescript-loader',
          'angular-router-loader',
          'angular2-template-loader',
          'source-map-loader',
          'tslint-loader'
        ]
      },
      {
        test: /\.(png|jpg|gif|woff|woff2|ttf|svg|eot)$/,
        use: 'file-loader?name=assets/[name]-[hash:6].[ext]'
      },
      {
        test: /favicon.ico$/,
        use: 'file-loader?name=/[name].[ext]'
      },
      {
        test: /\.css$/,
        use: ['style-loader', { 
          loader: 'css-loader', 
          options: { 
              sourceMap: true 
          } 
      }]
      },
      {
        test: /\.scss$/,
        include: path.join(ROOT, 'angularApp/styles'),
        use: ['style-loader', { 
          loader: 'css-loader', 
          options: { 
              sourceMap: true 
          } 
      }, { 
        loader: 'sass-loader', 
        options: { 
            sourceMap: true 
        } 
    }]
      },
      {
        test: /\.scss$/,
        exclude: path.join(ROOT, 'angularApp/styles'),
        use: ['raw-loader', { 
          loader: 'sass-loader', 
          options: { 
              sourceMap: true 
          } 
      }]
      },
      {
        test: /\.html$/,
        use: 'raw-loader'
      }
    ],
    exprContextCritical: false
  },
  plugins: [
    function () {
      this.plugin('watch-run', function (watching, callback) {
        console.log(
          '\x1b[33m%s\x1b[0m',
          `Begin compile at ${new Date().toTimeString()}`
        );
        callback();
      });
    },

    new webpack.optimize.ModuleConcatenationPlugin(),

    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery'
    }),


    new CleanWebpackPlugin({
      cleanOnceBeforeBuildPatterns: ['./wwwroot/dist', './wwwroot/assets'],
      root: ROOT
    }),

    new HtmlWebpackPlugin({
      filename: 'index.html',
      inject: 'body',
      template: 'angularApp/index.html'
    }),

    new CopyWebpackPlugin([
      { from: './angularApp/images/*.*', to: 'assets/', flatten: true }
    ]),

    new FilterWarningsPlugin({
      exclude: /System.import/
    })
  ]
};

ts.config 中的 "sourceMap": true 和 devtool: 'inline-source-map' 以及在 ts 文件插件中使用 source-map-loader 应该会导致包含地图文件,但是他们无处可寻。 有什么想法吗?

【问题讨论】:

  • inline-source-map 表示“源地图代码”包含在原始 js 文件(生成的包)中,而不是单独的文件中。这似乎不是您想要实现的目标,对吗?
  • 正确。让它成为一个答案,我会接受它

标签: angular typescript webpack


【解决方案1】:

inline-source-map 表示“源映射代码”包含在原始 js 文件(生成的包)中,而不是单独的文件中。

好像不是你想要达到的,看看其他选项webpack-sourcemap

【讨论】:

    猜你喜欢
    • 2019-11-30
    • 2020-03-25
    • 2021-05-10
    • 2016-04-02
    • 2022-09-25
    • 2015-11-05
    • 1970-01-01
    • 2016-03-15
    • 2019-01-05
    相关资源
    最近更新 更多