Help with inline import json-schema-ref-loader
ValYouW opened this issue · 5 comments
Hi,
I have an issue when importing json using json-schema-ref-loader, this is most likely NOT json-schema-ref-loader issue but will appreciate if anyone can see what I'm missing...
So my import is like
import MySchema from '@cloudflare/json-schema-ref-loader!./my-schema.json';
And I get the following error when running webpack:
Module parse failed: Unexpected token m in JSON at position 0 while parsing near 'module.exports ...
I assumed this is because there is some other loader who tries to parse the json after json-schema-ref-loader loaded it, so I prefixed the loader with !!
which should disable all configured loaders:
import MySchema from '!!@cloudflare/json-schema-ref-loader!./my-schema.json';
But still no luck :(
If I change the file extension from json -> jjj
the following works:
import MySchema from '@cloudflare/json-schema-ref-loader!./my-schema.jjj';
Any ideas how I can resolve this?
Thx!
@ValYouW json-schema-ref-loader
understands several formats. If there is a .json
extension it will just try to parse it as JSON. I don't remember offhand what the default behavior is if the extension is unrecognized, but it's probably either YAML or JavaScript, both of which are more forgiving.
Given that you have module.exports
, it looks like your file is probably JavaScript and not plain JSON.
@handrews Hi, I don't have "module.exports", I have a regular json file:
{
"a": 123,
"b": ""B"
...
}
As I wrote, I think that json-schema-ref-loader is working OK, converting the json to a module (here) but since WebPack works in a loader-chain fashion there is probably a loader that comes after json-schema-ref-loader that expects to get a json file but gets the json-schema-ref-loader output, which is a module... this is my assumptions, I am trying to see if I can "disable" all the loaders that comes after json-schema-ref-loader in the chain...
OK, found that in WebPack 4 changelog:
JSON
webpack now handles JSON natively
You may need to add type: "javascript/auto" when transforming JSON via loader to JS
So I need to "tell" webpack that json-schema-ref-loader emits javascript, so it will know that the json file is no longer a json and won't try to parse it (webpack 4 parse json files natively)... the problem is that I can't do it for "inline loaders"...
Oh, I see- it's outputting JavaScript which is now a problem. One day it will be possible to do things in JS without stacking a bajillion constantly changing frameworks on top of each other. Maybe.
OK, so to close this issue, this is what I ended up with:
- I removed the "inline loader" style when importing the schema
- Renamed all schema files to end with
.schema.json
- Defined the following module rule in my webpack config:
{
test: /\.schema\.json$/,
loader: '@cloudflare/json-schema-ref-loader',
type: 'javascript/auto',
exclude: /node_modules/
}
This way json-schema-ref-loader
will only handle my schema files and webpack will keep handling regular json
files...
Thx :)