Parcel: Parcel require('./file.pug') integration not working

Created on 5 Aug 2018  路  5Comments  路  Source: parcel-bundler/parcel

馃檵鈥嶁檧锔廎eature

馃帥 Configuration (.babelrc, package.json, cli command)

npx parcel index.html

馃 Expected Behavior


The page should display "It works!" using the pre-compiled pug file and should also print "hello world" in the console

馃槸 Current Behavior


Nothing happens. Not even console.log. (Commenting out the .pug require fixes the console.log)

馃拋 Possible Solution


In PugAsset detect if the template is being required from a js file and if so call pug.compileClient instead of pug.compile and return compiled + ';module.exports=template;' as well as set type = 'js'

馃敠 Context


Instead of not doing anything/confusing me I expect require('./main.pug') to return a pre-compiled pug template function.

馃捇 Code Sample


Assets:
parcel-pug-not-working.tar.gz

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.9.7
| Node | v10.8.0
| npm/Yarn | 6.2.0
| Operating System | Ubuntu 16.04 amd64

Feature HTML Preprocessing

Most helpful comment

This is the one issue I currently having with parcel...

importing a pug file into a js source should provide the compiled template function, rather than the complete rendered template.

This makes pug templates actually usable in Ajax code that renders dynamic data into the page.

In short, this should be possible:

import template from './mytemplate.pug'

document.getElementById('foo').innerHTML = template(somedata);

Are there any plans to include this feature?

All 5 comments

This is not a bug, Pug resolves into html, so it should return the html in the next version as that feature has just been added.

Not sure what you mean with the js and templates and such, if that's something pug specific and you do not mean html than this is a feature request.

Otherwise this issue should be closed due to already being implemented.

Currently the built-in pug plugin only resolves to static HTML. I need a way of using a compiled version that can dynamically render the page.
I have implemented the feature I meant here: https://github.com/mkg20001/parcel-plugin-pug-client/blob/master/PugAsset.js although this one dosen't dynamically do both js and html output, only js.
This is how it gets used: https://github.com/mkg20001/apkmirror2fdroid/blob/fbff5fd5e40f6d93d870ebd0a2d630a48bde6949/public/main.js#L62-L65

Not sure what you mean with the js and templates and such

This is documented here https://pugjs.org/api/reference.html#pugcompileclientsource-options

How is it possible for someone to miss the point of templates this hard?

Any progress on making this available upstream? Is it even possible right now to make it work this way (so it's still possible to call parcel index.pug and use require('./some-other-file.pug') inside the JS at the same time)

This is the one issue I currently having with parcel...

importing a pug file into a js source should provide the compiled template function, rather than the complete rendered template.

This makes pug templates actually usable in Ajax code that renders dynamic data into the page.

In short, this should be possible:

import template from './mytemplate.pug'

document.getElementById('foo').innerHTML = template(somedata);

Are there any plans to include this feature?

Was this page helpful?
0 / 5 - 0 ratings