import-assertions
v0.0.4
Published
Import Assertions loader for webpack
Downloads
9
Maintainers
Readme
Import Assertions
See https://github.com/tc39/proposal-import-assertions.
Requires Babel v7.12.0 or higher.
Ensures the type of the file at webpack build time. Currently supported types are:
- json
import json from "./data" assert { type: "json" };
webpack loader
Add the following rule to your webpack configuration:
module: {
rules: [
{
test: /\.jsx?$/,
loader: "import-assertions"
}
]
}
Example
See example in ./example.
It's a simple app that import a JSON file, or at least assumes so...
Build and run the example:
$ webpack && node dist/main.js
{ name: 'Sven' }
Try to uncomment the import to ./fake-data
(line 2) and build the example you will see a JSON parse failure. This is because the fake-data
file isn't actually JSON.
if you remove the assert { type: "json" }
assertion from the import, build and run the example, you will see:
$ webpack && node dist/main.js
lol
{ name: 'Sven' }