Socket
Socket
Sign inDemoInstall

@rollup/plugin-json

Package Overview
Dependencies
21
Maintainers
4
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @rollup/plugin-json

Convert .json files to ES6 modules


Version published
Weekly downloads
2.2M
increased by2.82%
Maintainers
4
Install size
213 kB
Created
Weekly downloads
 

Package description

What is @rollup/plugin-json?

The @rollup/plugin-json npm package allows users to import JSON files as modules in their Rollup bundles. It converts JSON files into ES6 modules, making it possible to include JSON data in the bundle as if they were regular JavaScript modules.

What are @rollup/plugin-json's main functionalities?

Import JSON files as modules

This feature allows you to import JSON files directly into your JavaScript code. The JSON data is parsed and transformed into an ES6 module, which can then be used like any other imported module.

import config from './config.json';
console.log(config);

Customize indentation

This feature allows you to specify the indentation for the generated default export, giving you control over the formatting of the output.

import json from '@rollup/plugin-json';

export default {
  plugins: [
    json({
      indent: '  '
    })
  ]
};

Filter which JSON files to include

This feature allows you to use a filter function to determine which JSON files should be processed by the plugin. This is useful when you want to include only certain JSON files based on a pattern or condition.

import json from '@rollup/plugin-json';
import { createFilter } from '@rollup/pluginutils';

const filter = createFilter('**/config/*.json');

export default {
  plugins: [
    json({
      include: filter
    })
  ]
};

Exclude JSON files

This feature allows you to exclude certain JSON files from being processed by the plugin. For example, you might want to exclude JSON files from the 'node_modules' directory.

import json from '@rollup/plugin-json';

export default {
  plugins: [
    json({
      exclude: ['node_modules/**']
    })
  ]
};

Other packages similar to @rollup/plugin-json

Readme

Source

npm size libera manifesto

@rollup/plugin-json

🍣 A Rollup plugin which Converts .json files to ES6 modules.

Requirements

This plugin requires an LTS Node version (v14.0.0+) and Rollup v1.20.0+.

Install

Using npm:

npm install @rollup/plugin-json --save-dev

Usage

Create a rollup.config.js configuration file and import the plugin:

import json from '@rollup/plugin-json';

export default {
  input: 'src/index.js',
  output: {
    dir: 'output',
    format: 'cjs'
  },
  plugins: [json()]
};

Then call rollup either via the CLI or the API.

With an accompanying file src/index.js, the local package.json file would now be importable as seen below:

// src/index.js
import { readFileSync } from 'fs';

const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
console.log(`running version ${pkg.version}`);

Options

compact

Type: Boolean
Default: false

If true, instructs the plugin to ignore indent and generates the smallest code.

exclude

Type: String | Array[...String]
Default: null

A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default no files are ignored.

include

Type: String | Array[...String]
Default: null

A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

includeArbitraryNames

Type: Boolean
Default: false

If true and namedExports is true, generates a named export for not a valid identifier properties of the JSON object by leveraging the "Arbitrary Module Namespace Identifier Names" feature.

indent

Type: String
Default: '\t'

Specifies the indentation for the generated default export.

namedExports

Type: Boolean
Default: true

If true, instructs the plugin to generate a named export for every property of the JSON object.

preferConst

Type: Boolean
Default: false

If true, instructs the plugin to declare properties as variables, using either var or const. This pertains to tree-shaking.

Meta

CONTRIBUTING

LICENSE (MIT)

Keywords

FAQs

Last updated on 12 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc