thlorenz / browserify-shim

📩 Makes CommonJS incompatible files browserifyable.
MIT License
933 stars 87 forks source link

Nunjucks setup #207

Closed daslicht closed 8 years ago

daslicht commented 8 years ago

Hi, I I have the following setup:

test.js

var $ = require( 'jquery' )
var tmpl = require( './myWidget.nunj' );

console.log( tmpl.render( { menu : 'chorizo' } ) ); 

myWidget.nunj:

<div>{{ menu }}</div>

package.json:

"browserify": {
    "transform": [
      [

        "browserify-shim"
      ]
    ]
  },
  "browserify-shim": {
    "nunjucks": "nunjucks"
  },
  "browser": {
    "nunjucks": "nunjucks/browser/nunjucks-slim"
  }

bundle include:

app.get('/bundle.js', browserify('./app/test.js'));
...
<script src="/bundle.js"></script>

bundle.js returns the following:


/Users/daslicht/DEV/node/contentRange-ts-nunjuck/app/myWidget.nunj:1
<div>{{ menu }}</div>
^
ParseError: Unexpected token

What do I miss , please?

bendrucker commented 8 years ago

You can't just require a template like that: require only works with valid JavaScript unless you hack it with a transform.

daslicht commented 8 years ago

Ok , so how would look that complete example, please:

source: https://github.com/mozilla/nunjucks/issues/507

 "browserify": {
    "transform": [
      "browserify-shim"
    ]
  },
  "browserify-shim": {
    "nunjucks": "nunjucks"
  }

so I need another transform ?

I tried it like this, but that didnt work either : https://github.com/rotundasoftware/nunjucksify/issues/16

Do I need to combine browserify-shim and nunjucksify ? How please?

bendrucker commented 8 years ago

No idea, I prefer to not use transforms to allow invalid JS and instead use brfs

daslicht commented 8 years ago

hm but that wont precompile nunjucks templates no ?

bendrucker commented 8 years ago

Correct, I don't really know what the tooling looks like for Nunjucks and can't be of much help here

dgbeck commented 8 years ago

check out nunjucksify, i believe that is the transform you are looking for

daslicht commented 8 years ago

@dgbeck I tried it like this, but that didnt work either : rotundasoftware/nunjucksify#16

dgbeck commented 8 years ago

Hi @daslicht sorry to hear that. nunuckisfy is currently broken because recent versions of nunjucks are not common-js compatible, due to https://github.com/mozilla/nunjucks/issues/507 . I am currently working to try to get some movement on that issue which has been stalled for some time.

daslicht commented 8 years ago

any news on this ?

dgbeck commented 8 years ago

@daslicht thanks for the heads up. now that nunjucks 3 is out we can move forward. just merged #19 and published nunjuckify to npm @2.0.0.

very happy to close this issue after so long. thank you!