简体   繁体   中英

How do I make gulp-jade add 'export' to the generated function?

I'm using Jade to write all HTML, and have them in individual files, including some tiny bits that get injected with jQuery as a response to events. These bits must have locals inserted clientside, so I use gulp-jade with client: true and then try to use those functions directly in Webpack.

The problem is that I have no way to import them. I mainly use ES6 (converted with babel-loader), but of course requireJS would work just as well.

The way I solve this currently is to never automatically run gulp-jade, but only run it manually whenever I make a change, and then manually add 'export' at the beginning of the file.

This works, of course, but first of all I never remember to run gulp-jade until I see the errors that get generated, and secondly whenever I do run gulp-jade it overwrites every single file (even with gulp-changed) so that I have to add 'export' to all of them.

I have no experience with Vinyl streams outside of Gulp, so I have no idea how to do it, but I am aware that I could probably run something like stream =+ 'export ' + stream , but it would be far better to have a gulp-plugin which identified all exportable elements in source-files, and exported them according to ES6 or RequireJS standards depending on options.

And it seems like such a thing should exist, being all round useful, but I can't find it!

Does it exist? Is there a simple hack I could use while waiting for someone smarter than me to create it?

This ended up being the solution, straight from my gulpfile:

gulp.task('jade: fix', ['jade: compile'], plugins.shell.task([
  'for file in *.js;do { printf \'export \'; cat $file; } >$file.new;mv $file.new $file;done'
], {cwd: process.cwd() + '/src/lib/nodes/templates'}));

plugins.shell.task = gulp-shell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM