pugjs / pug-cli

Pug's CLI interface
MIT License
259 stars 63 forks source link

Preload file and/or content to source file #48

Closed arxpoetica closed 7 years ago

arxpoetica commented 7 years ago

I'm using npm (actually yarn) scripts, and would like to prepend a file of mixins to a series of pug files in a directory. I could just manually include in each file, but I'd rather automate this if possible.

I tried the following (for example), but it's not working as expected:

echo 'include /pug/helpers/mixins' | pug source/*.pug -b $PWD

Any ideas?

TimothyGu commented 7 years ago

Some shell magic can go a long way :)

for f in source/*.pug; do echo 'include /pug/helpers/mixins' | cat - $f | pug --filename $f -b $PWD; done

EDIT: you can also use find and xargs, though since the find regex is a bit weird IMO I used globs above instead.

arxpoetica commented 7 years ago

That's some sweet bash logix above. I was trying to do something similar, but my shell chops aren't up to snuff. Thx for the help. ;)

arxpoetica commented 7 years ago

What if I wanted to include (this is a little more complicated) some files (note the plural) like mixins, vars, etc...how could I preload that for each separate file?

I think this isn't a common use case, but I'm compiling multiple pug files SEPARATE of one another, but I want to include lots of files without manually doing it at the top of each file. (I can always just be manual, too...but who wants to do THAT :P )

TimothyGu commented 7 years ago

You can have a common include

//- /pug/mixins-and-vars.pug
include /pug/mixins/a.pug
include /pug/mixins/b.pug
include /pug/mixins/c.pug
include /pug/mixins/d.pug
include /pug/vars/a.pug
include /pug/vars/b.pug
include /pug/vars/c.pug

Generally though I'd recommend just write them manually, for the same reason why modular JS is better than dumping everything to window or globals.