raw-loader icon indicating copy to clipboard operation
raw-loader copied to clipboard

[Bug Report] raw-loader failed to load *.json

Open ulivz opened this issue 5 years ago • 2 comments

  • Operating System: OSX
  • Node Version: v12.16.1
  • NPM Version: 6.13.4
  • webpack Version: 4.42.1
  • raw-loader Version: 4.0.1

Expected Behavior

*.json should be loaded successfully with raw-loader like the *.js;

Actual Behavior

*.json cannot be loaded successfully.

Code

import foo from 'raw-loader!./foo.json' // don't work
// import foo from 'raw-loader!./foo.js' // work

export default {
  foo,
}

How Do We Reproduce?

I just created a minimal reproduction repo: raw-loader-load-json-repro, and here're the reproduction steps:

  1. git clone https://github.com/ulivz/raw-loader-load-json-repro
  2. yarn
  3. npx webpack
  4. You'll got following error:

  1. Modify entry index.js,comment json import and uncomment js import:
- import foo from 'raw-loader!./foo.json' // don't work
+ // import foo from 'raw-loader!./foo.json' // don't work
- // import foo from 'raw-loader!./foo.js' // work
+ import foo from 'raw-loader!./foo.js'

export default {
  foo,
}

Ru-run npx webpack,the build will be success.

ulivz avatar Apr 19 '20 18:04 ulivz

Webpack has built-in json loader and it automatically works on json extension.

Ideally it should be written:

import foo from '!!raw-loader!./foo.json'; // Disable all loader

but it is not work as expected.

Workaround - setup loader in the configuration file:

{
  test: /\.json$/i,
  type: "javascript/auto",
  use: 'raw-loader'
}

alexander-akait avatar Apr 20 '20 12:04 alexander-akait

Another example of usage:

import myJSON from "./test.json.raw!=!raw-loader!./test.json"

Say to webpack do not use test.json as json resource and loading this using raw-loader. But I think we should improve it.

alexander-akait avatar Jun 24 '20 13:06 alexander-akait