Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-import-loader

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-import-loader - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

24

lib/load-data.js

@@ -10,2 +10,4 @@ "use strict";

require("core-js/modules/es6.array.find");
require("core-js/modules/es7.array.includes");

@@ -19,2 +21,4 @@

var _fs = _interopRequireDefault(require("fs"));
function loadData(contentPath, options) {

@@ -38,5 +42,15 @@ if (options === void 0) {

function load(contentPath, options) {
var extension = _path.default.extname(contentPath).slice(1); // use normal require
var extension = _path.default.extname(contentPath).slice(1);
if (!extension) {
var resolvedPath = resolvePath(contentPath, options);
if (resolvedPath) {
// tslint:disable no-parameter-reassignment
contentPath = resolvedPath;
extension = _path.default.extname(contentPath).slice(1);
}
} // use normal require
if (['json', 'js'].includes(extension)) {

@@ -71,2 +85,10 @@ var result = require(contentPath); // if js export was a function, execute it and use the result

throw new Error("Extension \"" + extension + "\" for path \"" + contentPath + "\" is not supported, please configure a resolve function by setting \"options.resolvers." + extension + "\". It will receive a path and should return the content as a string.");
}
function resolvePath(contentPath, options) {
return ['json', 'js'].concat(options.resolvers && Object.keys(options.resolvers) || []).map(function (extension) {
return contentPath + "." + extension;
}).find(function (path) {
return _fs.default.existsSync(path);
});
}

2

package.json
{
"name": "json-import-loader",
"version": "1.0.0",
"version": "1.1.0",
"description": "Allows import files into a json file, contains webpack-loader and node API.",

@@ -5,0 +5,0 @@ "main": "./index.js",

@@ -7,3 +7,3 @@ # json-import-loader

This module also contains a util function that works in plain node, in case you need to use the json
or yaml file outside of webpack. Keep in mind that you use full and relative imports.
or yaml file outside of webpack. Keep in mind that you use full and relative imports.

@@ -20,3 +20,2 @@ [![Travis](https://img.shields.io/travis/mediamonks/json-import-loader.svg?maxAge=2592000)](https://travis-ci.org/mediamonks/json-import-loader)

## Usage

@@ -43,2 +42,3 @@

### Inline
```js

@@ -55,2 +55,3 @@ const json = require('json-import-loader!json-loader!./file.json');

**webpack.config.js**
```js

@@ -62,11 +63,8 @@ module.exports = {

test: /\.json$/,
type: "javascript/dynamic", // only for webpack 4+
use: [
{ loader: "json-import-loader" },
{ loader: "json-loader" }
]
type: 'javascript/dynamic', // only for webpack 4+
use: [{ loader: 'json-import-loader' }, { loader: 'json-loader' }],
},
]
}
}
],
},
};
```

@@ -77,9 +75,7 @@

**foo.json**
```json
{
"foo": "import!foo/bar.json",
"foos": [
"import!foo/foo1.json",
"import!foo/foo2.json"
]
"foos": ["import!foo/foo1.json", "import!foo/foo2.json"]
}

@@ -101,23 +97,21 @@ ```

test: /\.json$/,
type: "javascript/dynamic", // only for webpack 4+
use: [
{ loader: "json-partial-loader" },
{ loader: "json-loader" }
]
type: 'javascript/dynamic', // only for webpack 4+
use: [{ loader: 'json-partial-loader' }, { loader: 'json-loader' }],
},
{
test: /\.yaml$/,
type: "javascript/dynamic", // only for webpack 4+
type: 'javascript/dynamic', // only for webpack 4+
use: [
{ loader: "json-partial-loader" },
{ loader: "json-loader" },
{ loader: "yaml-loader" }
]
{ loader: 'json-partial-loader' },
{ loader: 'json-loader' },
{ loader: 'yaml-loader' },
],
},
]
}
}
],
},
};
```
**foo.yaml**
```yaml

@@ -146,18 +140,26 @@ foo: import!foo/bar.yaml

// additional extensions can be supported by providing resolvers for extensions
const yamlObj = loadData(
path.resolve(__dirname, './data/foo.yaml'),
{
resolvers: {
yaml: path => yaml.safeLoad(fs.readFileSync(path, 'utf8'))
},
const yamlObj = loadData(path.resolve(__dirname, './data/foo.yaml'), {
resolvers: {
yaml: path => yaml.safeLoad(fs.readFileSync(path, 'utf8')),
},
);
});
```
#### import js
If you import a JS file, it should either export an object or a function. When a function, it will
be called, and the result will be used.
#### resolver map
The resolver map expects the extension as the key, and a function as the value. The resolve function
will receive the path (from the import) as parameter, and should return a string or object.
#### importing without extension
When no file extension is given in the import directive (e.g. `"import!./foo"`), it will try to
resolve the file on disk with all the available extensions, starting with `json` and `js`, followed
up by the extensions passed in the resolve map. This is mainly to mimic webpack resolve logic, so
the same json files can be used with both the Webpack loader and the Node API.
## Building

@@ -169,2 +171,3 @@

Clone a copy of the repo:
```sh

@@ -175,2 +178,3 @@ git clone https://github.com/mediamonks/json-import-loader.git

Change to the json-import-loader directory:
```sh

@@ -181,2 +185,3 @@ cd json-import-loader

Install dev dependencies:
```sh

@@ -187,2 +192,3 @@ yarn

Use one of the following main scripts:
```sh

@@ -200,3 +206,2 @@ yarn build # build this project

## Contribute

@@ -206,3 +211,2 @@

## Changelog

@@ -212,3 +216,2 @@

## Authors

@@ -218,7 +221,4 @@

## LICENSE
[MIT](./LICENSE) © ThaNarie
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc