├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jorik Tangelder 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | template-html-loader 2 | ==================== 3 | 4 | Parse templates to html loader for webpack. It parses any template language supported 5 | by [consolidate.js](https://github.com/visionmedia/consolidate.js), and returns the html. 6 | 7 | You will still need to install the template engine. The script will try to detect 8 | the template engine by the file extension. You can overwrite this by setting the engine parameter. 9 | 10 | ````js 11 | loaders: [ 12 | { test: /\.jade/, loader: "template-html-loader" }, 13 | { test: /\.ejs/, loader: "template-html-loader" } 14 | { test: /\.mustache/, loader: "template-html-loader?engine=hogan" } 15 | ] 16 | ```` 17 | 18 | Available on NPM, `template-html-loader`. 19 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var cons = require('consolidate'); 2 | var utils = require('loader-utils'); 3 | var path = require('path'); 4 | var fs = require('fs'); 5 | 6 | 7 | module.exports = function(content) { 8 | this.cacheable && this.cacheable(); 9 | 10 | var callback = this.async(); 11 | var opt = utils.getOptions(this); 12 | 13 | function exportContent(content) { 14 | if (opt.raw) { 15 | callback(null, content); 16 | } else { 17 | callback(null, "module.exports = " + JSON.stringify(content)); 18 | } 19 | } 20 | 21 | // with no engine given, use the file extension as engine 22 | if(!opt.engine) { 23 | opt.engine = path.extname(this.request).substr(1).toLowerCase(); 24 | } 25 | 26 | if(!cons[opt.engine]) { 27 | throw new Error("Engine '"+ opt.engine +"' isn't available in Consolidate.js"); 28 | } 29 | 30 | // for relative includes 31 | opt.filename = this.resourcePath; 32 | opt.dirname = path.dirname(this.resourcePath); 33 | 34 | const self = this; 35 | if(opt.dataFiles instanceof Array) { 36 | opt.dataFiles.reverse().forEach(function(file) { 37 | file = path.join(opt.dirname, file); 38 | if(!fs.existsSync(file)) { // if the given name doesn't exist, check if it needs an extension 39 | file += '.json'; 40 | if(!fs.existsSync(file)) { 41 | throw new Error("Data file '"+ file +"' does not exist"); 42 | } 43 | } 44 | opt = Object.assign(JSON.parse(fs.readFileSync(file)), opt); // ensure that opt takes precedence 45 | self.addDependency(file); 46 | }) 47 | delete opt.dataFiles; 48 | } 49 | 50 | if(opt.partialsFiles instanceof Object) { 51 | Object.keys(opt.partialsFiles).forEach(function(key) { 52 | var file = path.join(opt.dirname, opt.partialsFiles[key]); 53 | if(!fs.existsSync(file)) { 54 | throw new Error("Partials file '"+ file +"' does not exist"); 55 | } 56 | opt.partials = opt.partials || {}; 57 | opt.partials[key] = fs.readFileSync(file).toString(); 58 | self.addDependency(file); 59 | }) 60 | delete opt.extraFiles; 61 | } 62 | 63 | cons[opt.engine].render(content, opt, function(err, html) { 64 | if(err) { 65 | throw err; 66 | } 67 | exportContent(html); 68 | }); 69 | }; 70 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "template-html-loader", 3 | "version": "1.0.0", 4 | "description": "Parse templates to html - loader for webpack", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/jtangelder/template-html-loader" 9 | }, 10 | "author": "Jorik Tangelder", 11 | "license": "MIT", 12 | "dependencies": { 13 | "loader-utils": "~1.1.0" 14 | }, 15 | "peerDependencies": { 16 | "consolidate": "^0.14.0" 17 | } 18 | } 19 | --------------------------------------------------------------------------------