覆盖 webpack 4 中的内置 json-loader [英] override built-in json-loader in webpack 4

查看:50
本文介绍了覆盖 webpack 4 中的内置 json-loader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的 json-loader,我想用它来代替内置的 loader.这曾经在 webpack-3 中工作;在 webpack-4 中,我的加载器被调用但是结果会传递给内置加载器,然后会出错,因为它提供的是 JS 源,而不是 json.如何防止调用内置 json-loader?我的 webpack.cofig.ts 看起来像这样:

I have my own json-loader which I want to use instead of the built-in loader. This used to work in webpack-3; in webpack-4 my loader gets called but the results get passed to the built-in loader, which then errors out because what it's being fed is JS source, not json. How can I prevent the buit-in json-loader being called? My webpack.cofig.ts looks like this:

import * as webpack from 'webpack'
import * as path from 'path'

const config = {
  mode: 'production',
  node: { fs: 'empty' },
  resolveLoader: {
    alias: { 'custom-json-loader': 'zotero-plugin/loader/json' },
  },
  module: {
    rules: [ { test: /\.json$/, use: [ 'custom-json-loader' ] } ],
  },

  // ...
}

export default config

推荐答案

你必须告诉 webpack 你的加载器发出的是 javascript 而不是 json.为此,您必须将 type: "javascript/auto" 添加到您的加载程序配置中:

You have to tell webpack that your loader emits javascript and not json. To do so you must add type: "javascript/auto" to your loader configuration:

module: {
    rules: [ 
         { 
            test: /\.json$/, 
            use: [ 'custom-json-loader' ] ,
            type: "javascript/auto"
         }
   ]
}

Webpack 4 的更新日志

这篇关于覆盖 webpack 4 中的内置 json-loader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆