tslint-loader与webpack 2.1.0-beta.25的集成

17

我有一个使用webpack进行压缩/编译的Angular2项目。

我在webpack中使用tslink loader,因此在webpack.config.js中有与tslint相关的配置。

module.exports = {
...
tslint: {
    configuration: {
        rules: {
            quotemark: [true, "double"]
        }
    },

    // tslint errors are displayed by default as warnings
    // set emitErrors to true to display them as errors
    emitErrors: false,

    // tslint does not interrupt the compilation by default
    // if you want any file with tslint errors to fail
    // set failOnHint to true
    failOnHint: true,

    // name of your formatter (optional)
    formatter: "",

    // path to directory containing formatter (optional)
    formattersDirectory: "node_modules/tslint-loader/formatters/",

    // These options are useful if you want to save output to files
    // for your continuous integration server
    fileOutput: {
        // The directory where each file"s report is saved
        dir: "./webpack-log/",

        // The extension to use for each report"s filename. Defaults to "txt"
        ext: "xml",

        // If true, all files are removed from the report directory at the beginning of run
        clean: true,

        // A string to include at the top of every report file.
        // Useful for some report formats.
        header: "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<checkstyle version=\"5.7\">",

        // A string to include at the bottom of every report file.
        // Useful for some report formats.
        footer: "</checkstyle>"
    }
},
...
preLoaders: [
        {
            test: /\.ts$/,
            loader: "tslint"
        }
    ],
}
}

我将webpack从1.13.1更新到2.1.0-beta.25,但tslint配置破坏了npm run build的编译过程。

我将preLoaders指令更改为loaders

module: {
        ....
        {
            test: /\.ts$/,
            loader: 'tslint',
            exclude: /(node_modules)/,
            enforce: 'pre'
        },
    ],
}

那还不够,因为我还是收到了错误提示

For loader options: webpack 2 no longer allows custom properties in configuration.
 Loaders should be updated to allow passing options via loader options in module.rules.

所以我应该将tslint配置移动到其他地方。有点迷失在这里。所以任何关于此问题的信息都将不胜感激。

谢谢!

3个回答

58

对于其他在webpack 2中遇到预加载器问题的用户,在beta v2.1-beta.23中有关于 pre/postLoaders 的破坏性变更。

首先,"loaders" 部分应该改名为 "rules"。同时,pre/postLoaders 现在定义在 rules 下。

在我的例子中,我使用tslint作为preLoader。要将pre/postLoader添加到rules中,请使用具有值为prepostenforce 属性。

module: {
    rules: [
        {
            enforce: 'pre',
            test: /\.tsx?$/,
            loader: 'tslint',
            exclude: /(node_modules)/,
        },
        {
            test: /\.tsx?$/,
            loaders: ['awesome-typescript-loader'],
            exclude: /(node_modules)/
        }
    ]
}

更多信息可在github发布版本中查看:Webpack v2.1.0-beta.23

发布信息中还有一个指向pull request的链接,该链接展示了从v2.1.0-beta.22升级到v2.1.0-beta.23需要进行的webpack配置文件更改。在那里您可以看到,您还需要LoaderOptionsPlugin。

plugins: [
    new webpack.LoaderOptionsPlugin({
        options: {
            tslint: {
                emitErrors: true,
                failOnHint: true
            }
        }
    })
]

非常好的答案。我可以不用括号使用node_modules吗?谢谢。 - skiabox

2

好的,所以我只需要把tslint定义移到以下位置:

plugins: [
    new LoaderOptionsPlugin({
        options: {
           tslint: {
             ...

并声明
const LoaderOptionsPlugin = require("webpack/lib/LoaderOptionsPlugin");

1
如果您不想添加插件,可以像这样操作:
module: {
  rules: [
    {
      enforce: 'pre',
      test: /\.ts$/,
      loader: 'tslint-loader?' + JSON.stringify({
        emitErrors: true,
        failOnHint: true
      })
    }
  ]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接