Browserify with require('fs')

前端 未结 5 1126
感动是毒
感动是毒 2020-11-28 11:24

I was trying to use browserify on a file that uses the fs object. When I browserify it, the call to require(\'fs\') doesn\'t get transformed and require

5条回答
  •  萌比男神i
    2020-11-28 11:42

    For anyone on teh Google's I had much better luck with the stringify transform.

    https://github.com/JohnPostlethwait/stringify

    The answers here were frustrating (though not unwelcome) I'm importing templates as strings into my components to save on the HTTP requests bought about by templateUrl and keep them out of the Javascript files.

    For some reason brfs does not play nicely with babel and has a lot of caveats to get working at all.

    I couldn't get browserify-fs to work at all.

    However, after finding the stringify transform it was as simple as.

    import template from '../template.html'
    
    const definition = { template }
    
    component.directive('myDirective', () => definition)
    

    Translated for ES5 users:

    var template = require('../template.html')
    
    component.directive('myDirective', function() {
        return {
            template: template
        }
    })
    

提交回复
热议问题