Giter Site home page Giter Site logo

rollup-plugin-codefilter's Introduction

rollup-plugin-codefilter

This is a plugin for rollup/vite that filter code by dotenv variables. It allows user to use // ifdef {env variable} // endifcomment to keep or skrip code. For example:

// ifdef DEV
if (process.env.DEV) {
  console.log('dev')
}
// endif

If a DEV variable in dotenv and the value of it is true string, the code between ifdef and endif comment will be preserved. Otherwise, it will be removed.

install

npm install rollup-plugin-codefilter --save-dev

quick start

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

import codeFilter from 'rollup-plugin-codefilter'

module.exports = {
  input: 'index.js',
  output: {
    file: 'bundle.js',
    format: 'iife',
    name: 'test'
  },
  plugins: [
    codeFilter()
  ]
}

create a .env configuration file and define some true or false variables.

keep = true
discard = false

create index.js:

function log () {
  // ifdef keep
  console.log('hello world!')
  // endif

  // ifdef discard
  console.log('it will be discarded.')
  // endif
}

export default log

Finally, run rollup -c rollup.config.js.
The file will be generated in folder dist:

var test = (function () {
  'use strict';

  function log () {
    console.log('hello world!');
  }

  return log;

}());

options

ext

Type: Array<string> Default: [js, json]

Only handle the module which match the provided extension. Currently only support js and json.

dotenv

Refer to dotenv's options

exclude

Type: String | Array<String>
Default: null

A minimatch 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 minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.


The options below are the same as @rollup/plugin-json's.

To be careful, you can't use @rollup/plugin-json when this plugin injected.

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.

rollup-plugin-codefilter's People

Contributors

cary-mao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.