Search code examples
javascriptpugbrowserify

browserify, jadeify. requiring a jade from a module inside node_modules.. strange behaviour


I'm requiring a jade in my class. All is working fine if I put my modules into a libs folder (outside node_moduleS) but when I move into node_modules, the jadeify transform, is never called on my require('./views/index.jade'). I have debugged the source of jadeify to verify that this require() doesn't pass through it.

index.jade

p= data.name
p
  | console.log("urzuleibailadu")

child module

function Child () {
  console.log('Dirname: ' + __dirname);
  Child.super_(this);
  console.log('Called new child()');
  this.pages = {};
  this.settings = {
    templates: {
      //index: ''
      index: require('./views/index.jade')
    }
  };
}

This is the project structure (libs/child and node_module/child contains the very same files):

- browserify
-- libs
--- child
---- views
----- index.jade
---- index.js
-- node_modules
--- child
---- views
----- index.jade
---- index.js
-- main.js

With this edit, in main.js all is working fine:

Child = require('./libs/child')

This, brokes all, causing jade being parsed as js (browser throws data is undefined, from p= data.name ).

Child = require('child')

Any ideas? Many thanks.


Solution

  • Got an answer from substack:

    This behavior is intentional because it lets you make sweeping configuration changes in an application or library without worrying about how your changes might affect modules you depend on which were not written against the interfaces or assumptions that transforms might entail.

    You should configure your module with its own package.json to apply the transform:

    {
      "browserify": {
        "transform": [ "jadeify" ]
      }
    }
    

    or better still, your package can specify its own dependencies so that it will be easier to manage breaking incrementally changes over time in an isolated way:

    {
      "browserify": {
        "transform": [ "jadeify" ]
      },
      "dependencies": {
        "jadeify": "^2.3.0"
      }
    }