Skip to content

ialpert/babel-plugin-inject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-inject

Build Status codecov.io Code Climate bitHound Overall Score

Proposal

Plugin is useful in any case that requires to provide external asset or resource (mostly like css, html or txt files) to be used in your code. Any embedding task like building standalone pages, email newsletters or injectable widgets for the page.

NPM Package: https://www.npmjs.com/package/babel-plugin-inject

Usage Example

import Widget from './widgetClass.js';


(function (template1Html, template2Html, style1Css, style2Css) {

  class Widget1 extends Widget {

    constructor(options) {
      super(options);
    }

    method1() {

    }
  }


  var w = new Widget1();

  w.method1();


})(
  inject('./static1.htm'),
  inject('./template2.tpl'),  // To be preprocessed
  inject('./style1.css'),
  inject('./style2.sass') // To be preprocessed
);
let injectCss = inject('injectCSS');
injectCSS('body{color:red}');

Plugins

  1. txt Simple plain text injection, used as a base for other content driven plugins
  2. htm, html Placeholder for HTML injection. At this moment do nothing more then txt Could be extended to minify/optimize html
  3. tpl Compile HTML to Lodash completable template and provides temperating function

Helpers

  1. injectCSS inserts provided CSS to document

Installation

$ npm install babel-plugin-inject --save

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["babel-plugin-inject"]
}

Via CLI

$ babel --plugins babel-plugin-inject script.js

Via Node API

require("babel-core").transform('source code', {
  plugins: ['babel-plugin-inject']
});

No Conflict

We use another name for our function not to mess with existing inject() if any:

require("babel-core").transform('source code', {
  plugins: [['babel-plugin-inject', {fn: 'inject2'}]]
});
let injectCss = inject2('injectCSS');
injectCSS('body{color:red}');

About

Babel plugin to inject JS objects or assets

Resources

Stars

Watchers

Forks

Packages

No packages published