Giter Site home page Giter Site logo

includes-loader's Introduction

includes-loader

A webpack loader for any text file, and support nested includes with flexible options

Installation

npm install  includes-loader --save-dev

Usage

Documentation: Using loaders

var confStr = require('includes!./file.conf')'
// confStr is 'foo\nbar\nconf'

Details as follow

directory

├── file.conf
└── foo
    ├── part.conf
    └── bar
       └── part.conf

./file.conf

#include "./foo/part"
conf

./foo/part.conf

foo
#include "./bar/part"

./foo/bar/part.conf

bar

Configuration

includes options

default options

includes: {
  pattern: {
    re: /#include\s+?"(.+?)";*?/,
    index: 1
  },
  extensions: [] // the extension will be same to the including file
}

pattern

An object that should be used to specify how to match the include statement patten.re is the RegExp to match the include statement patten.index is the index of filepath in the matched results

// can be set to a function with filepath argument
pattern: function (filepath) {
  var pattern
  if (/\.html$/.test(filepath)) {
    pattern = {
      re: /<!--#\s*?include\s+?virtual=("|')(.+?)\1\s*?-->/,
      index: 2
    }
  }
  return pattern
}

extensions

An array of extensions that should be used to resolve includes, If you include file with their own extension (e.g. #include './somefile.ext'), you must add an empty string in the array.

// can be set to a function with filepath argument
extensions: function (filepath) {
  var extensions
  if (/\.html$/.test(filepath)) {
    extensions = ['', '.html', '.shtml', '.htm']
  } else if (/\.glsl$/.test(filepath)) {
    extensions = ['', '.glsl', '.vert', '.frag']
  }
  return extensions
}

expamle webpack.config

module.exports = {
  module: {
    module: {
      loaders: [{
        test: /\.html$/,
        loader: 'html!includes'
      }, {
        test: /\.glsl$/,
        loader: 'includes'
      }]
    }
  },
  includes: {
    extensions: function (filepath) {
      var extensions
      if (/\.html$/.test(filepath)) {
        extensions = ['', '.html', '.shtml', '.htm']
      } else if (/\.glsl$/.test(filepath)) {
        extensions = ['', '.glsl', '.vert', '.frag']
      }
      return extensions
    },
    pattern: function (filepath) {
      var pattern
      // only custom includes pattern for html
      if (/\.html$/.test(filepath)) {
        pattern = {
          re: /<!--#\s*?include\s+?virtual=("|')(.+?)\1\s*?-->/,
          index: 2
        }
      }
      return pattern
    }
  }
};

License

MIT (http://www.opensource.org/licenses/mit-license.php)

includes-loader's People

Contributors

ggd543 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.