Giter Site home page Giter Site logo

atcompileify's Introduction

atcompileify

fs.readFileSync() and fs.readFile() static asset browserify transform

build status

This module is a plugin for browserify that lets you run any expression at compile time, and get the result in your bundle. If you use it directly in node, it runs the expression at load time, without compiling.

This allows you to interact with the file system, or make build time network requests, without individual transforms for each. Commonly used transforms like atcompileify, envify, and more, can be replaced with this.

This is a slightly modified version of atcompileify's intro example. The atcompileify repo is a fork of substack/atcompileify.

var atcompile = require('atcompile');
var html = atcompile.runSync(function(){
    var fs = require('fs');
    return fs.readFileSync(__dirname + '/robot.html', 'utf8')
});
console.log(html);

and a robot.html:

<b>beep boop</b>

first npm install --save-dev atcompileify into your project, then:

on the command-line

$ browserify -t atcompileify example/main.js > bundle.js

which turns your code into:

var html = "<b>beep boop</b>\n";
console.log(html);

or with the api

var browserify = require('browserify');
var fs = require('fs');

var b = browserify('example/main.js');
b.transform('atcompileify');

b.bundle().pipe(fs.createWriteStream('bundle.js'));

async

You can also use, for example, fs.readFile():

var atcompile = require('atcompile');

atcompile.run(function(cb){
    var fs = require('fs');
    fs.readFile(__dirname + '/robot.html', cb, 'utf8')
}, function(err, result){
    console.log(result);
});

When you run this code through atcompileify, it turns into:

var fs = require('fs');
process.nextTick(function() {function (err, result) {
    console.log(result);
})(null, "<b>beep boop</b>\n")});

methods

usage

A tiny command-line program ships with this module to make debugging easier.

usage:

  atcompileify file
 
    Runs the transform `file`, printing the transformed file
    contents to stdout.

  atcompileify
  atcompileify -
 
    Runs the transform on the code from stdin, printing the transformed file
    contents to stdout.

install

With npm do:

npm install atcompileify

then use -t atcompileify with the browserify command or use .transform('atcompileify') from the browserify api.

license

MIT

credit

Original work on brfs done by substack. brfs is great, but I want a more general solution.

atcompileify's People

Contributors

brigand avatar

Stargazers

 avatar

Watchers

 avatar

atcompileify's Issues

NPM returns a 404 when installing.

Looks like atcompileify is (not/no longer) on npm, but it looks really awesome. I can download and use it myself, but I'd like to distribute my code without distributing node_modules, so I need a way to install it using npm install.

Thanks for making this tho.

require()'ing json from a relative path inside runSync

I have a function that looks as follows:

getConfig: function(key){
    return atcompile.runSync(function(){
        return require("./../../config/settings.json")[key];
    });
}

but it keeps telling me that the module ./../../config/settings.json doesn't exist.

In the source of runSync, I see that you're passing a require wrapper into the context, but I don't know how to make it resolve relative paths correctly.

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.