prismic-archive / baked.js

A static website generator in JS, powerered by prismic.io
https://prismicio.github.io/baked.js
91 stars 14 forks source link

How to require() node modules #29

Open kevinsproles opened 9 years ago

kevinsproles commented 9 years ago

In your example here: https://github.com/prismicio/baked.js/blob/master/examples/vanilla/index.html it's great how you can require libraries, however I cannot figure out how to require a node_module. It's strange to me even in your example that require("myLib.js") looks in the current folder, when the proper behavior of require() is to look in the node_modules folder first and if you want to look in the current directory it should be require("./myLib.js").

I've even tried "../../../" and no matter what I can't get back further than to_generate, yet the node_modules folder is one more back than that.

Any ideas?

dohzya commented 9 years ago

One important feature of baked.js is that it can render content dynamically as well (in order to preview unpublished released for instance). If we allow to look in the node_module directory when rendering server-side, we will need to copy it entirely in the generated directory then to find a way to make AMD's require work in the browser (which is not a trivial problem). That's why we don't allow to do this.

I propose to start with the blank example and to add a gulp task (for instance genlib) which uses browserify (or equivalent) to generate (in the to_generate directory) a javascript file. Then you will only have to update the default tasks to use this new one and it should work well.

Here is an untested example which should be a good start:

var gulp = require('gulp');
var baked = require('baked/gulp');
var browserify = require('gulp-browserify');
var path = require('path');

gulp.task('genlib', ['baked:init'], function () {
  gulp.src('mylib.js')
    .pipe(browserify())
    .pipe(gulp.dest(path.join(baked.config.options.srcDir, 'mylib.js')))
});

gulp.task('serve', ['genlib'], function () {
  gulp.start('baked:serve');
});

gulp.task('default', ['genlib'], function () {
  gulp.start('baked:default');
});

gulp.task('clean', ['baked:clean']);