Mr_Green
Mr_Green

Reputation: 41840

Issue in brfs plugin of browserify

I am trying to convert all .html template files in to a single bundle.js file by reading file content from each .html file.

I wrote following code in main.js

var fs = require('fs');
var templates = {
    'header': fs.readFileSync('app/templates/header.html', 'utf8'),
    'heading': fs.readFileSync('app/templates/heading.html', 'utf8')
}

This is working but adding some unnecessary wrapper functions in bundle.js when I run browserify -t brfs main.js > bundle.js in cmd:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

var templates = {
    'header': "<div class=\"headerSection\">\r\n\t<div class=\"headerSectionWrapper\">\r\n\t\t<div class=\"logo\">{{name}}</div>\r\n\t\t<div class=\"searchBarSection\">\r\n\t\t\t<div class=\"searchBar\">\r\n\t\t\t\t\r\n\t\t\t</div>\r\n\t\t\t<div class=\"searchTextHolder\">\r\n\t\t\t\t<form name=\"searchform\">\r\n\t\t\t\t\t<input type=\"text\" name=\"searchbox\"></input>\r\n\t\t\t\t</form>\r\n\t\t\t</div>\r\n\t\t</div>\r\n\t</div>\r\n</div>",
    'heading': "<!-- HTML Template -->\n<h3>heading</h3>\n"
}
},{}]},{},[1]);

How to remove this wrapper functions?

Upvotes: 1

Views: 659

Answers (2)

substack
substack

Reputation: 3386

Just run brfs directly:

$ brfs main.js
var templates = {
    'header': "<div class=\"headerSection\">\r\n\t<div class=\"headerSectionWrapper\">\r\n\t\t<div class=\"logo\">{{name}}</div>\r\n\t\t<div class=\"searchBarSection\">\r\n\t\t\t<div class=\"searchBar\">\r\n\t\t\t\t\r\n\t\t\t</div>\r\n\t\t\t<div class=\"searchTextHolder\">\r\n\t\t\t\t<form name=\"searchform\">\r\n\t\t\t\t\t<input type=\"text\" name=\"searchbox\"></input>\r\n\t\t\t\t</form>\r\n\t\t\t</div>\r\n\t\t</div>\r\n\t</div>\r\n</div>",
    'heading': "<!-- HTML Template -->\n<h3>heading</h3>\n"
}

The brfs command ships with the brfs package.

Upvotes: 4

Peter Lyons
Peter Lyons

Reputation: 146094

That's the browserify prelude code. It's the "runtime" that makes browserify work. You get it even with a tiny example:

echo 'console.log("foo")' | browserify
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({},{},[]);

It is unrelated to brfs, just browserify, and it's the magic that makes browserify work in browsers and node.

Upvotes: 1

Related Questions