override built-in json-loader in webpack 4

╄→гoц情女王★ 提交于 2019-12-22 18:48:08

问题


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

回答1:


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"
         }
   ]
}

Changelog for Webpack 4



来源:https://stackoverflow.com/questions/49052935/override-built-in-json-loader-in-webpack-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!