-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import JSON data into pug #3012
Comments
No, there's not currently any way of getting that information. A plugin would probably be the best way to do this. You could use a |
Thanks for the advice. I will try. |
@Matthias-Hermsdorf you might want to take a look at the way I configure locals in this file; the project is a pug-as-middleware approach using browsersync itself as the server (which in turn uses connnect): https://github.com/lunelson/penny/blob/master/lib/util/pug-locals.js#L55-L60 The important part is that this module exports a function which binds |
@lunelson penny is interessting and I've learned something reading your code, for example delete require.cache[absFile]; was new for me. But you can access with relFile only the Path relative to the entry site, not relative to the mixin. @ForbesLindesay I've digged through pug and now I have an plugin
and can write
But then I spoke to a teammate. He is refactoring our styleguide he builds the pages on startup. And later on he observes file changes and uses the dependency tree to rebuild only the neccessary files. Looking on the code he said: How should I observe this? We brainstormed to a more general solution and found out we need a new ast.type, thinking of
If we build this, would you merge the pullrequest? |
Some similar things have actually been requested a few times (#2804, #2772, #2604). What I'd like to do is add an mixin example
import exampleJson from './example.json'
- exampleJson.keyInJson should be here
h1="and here too: "+exampleJson.key This is not a small amount of work, but if you are willing to do that, I will definitely be happy to review/merge pull requests, and offer help where I can. The work breaks down as:
If it transpiles to a require call:This makes it relatively easy to support all of JavaScript and JSON and anything else you might want to import in one go. It also ensures that imported code doesn't need to be loaded again for every template in the application. It may make this feature unusable for people who use the output of pug client side directly without any bundling step though. It also means that
The question here is, do we try to figure out the correct relative path (relative to the output file) or just use absolute paths for when people use We'll also need to make sure some sort of If it inlines the imported code:This is probably more straightforward for JSON files, but harder for everything else. We could support other things via some kind of plugin system though.
|
Hi, I'm one of @Matthias-Hermsdorf teammates ✌️ 😄 Our new approach to get data from JSON files is this:
As Matthias mentioned, we wrote a npm script which manually compiles our pages and uses the pug-dependency package to build an Currently there is no possibility to integrate JSON files in the @ForbesLindesay according to your approach of having some kind of
IMO this comes a bit to close to the current ES6 imports, which might end up confusing a lot of people, if they try to import other javascript functionalities with it. Another approach to this would be the extention of the
Instead of inlining the include content, it would assign the content string to a scoped variable. |
FWIW I would suggest a more neutral word than
|
That's exactly the point. This should work like ES6 imports if you import a JavaScript file, and let you import JSON too (just like node.js + babel would let you do currently) |
Sorry, I assumed we wouldn't want to reimplement ES6 If / When this happens, wouldn't it then be possible to do something like this?
|
ES6 Modules in Node will be experimental in 10.1, see https://nodejs.org/api/esm.html which is pretty soon. I'm cautious with the name "import" which would imply for me that this is the JS import, but it isn't. The pug language addon to import things seemed neccesary for me for the dependency resolution. but now I think it would be better to extenc the pug-dependency plugin. I could parse the JS blocks there and search for imports and requires, without modifing the pug. Doubling the import has a bad smell for me. There is less benefit, but many side-effects. |
@chlorophyllkid That won't work for a few of reasons:
For these reasons, I would like to replicate what babel does by compiling the |
I guess you're right, a pug construct would work much better. But I'd still rather implement the solution that inlines the imported code. Mainly because it fits our usecase well enough and seems to be easier done. |
That's fine. I would suggest that for now, we only support: |
I would close this issue. I could solve my problem with the pug plugin to resolve long file path and then readFile or require them in an code block. Thanks for the discussion and help. |
Does someone found a way to properly import json files in pug? ES6
node 16.13.2
All workaround mentioned here doesn't work either. Can we re-open this issue? The only solution I found with gulp is the following: in import gulp from 'gulp';
const { series, parallel, src, dest, task } = gulp;
import gulpPug from 'gulp-pug';
import gulpData from 'gulp-data';
// compile pug templates into HTML and pass data in argument
function pug_src() {
return src('pug/*.pug')
.pipe(gulpData(function() {
return JSON.parse(fs.readFileSync('temp/data.json'));
}))
.pipe(gulpData(function() {
return JSON.parse(fs.readFileSync('pug/mapping.json'));
}))
.pipe(gulpPug({
pretty: true,
}))
.pipe(dest('build/'));
}; if the {
"mapping": {
"Tools": [
"A A",
"B B",
"C C"
]
} Then I can do that in pug: each category in mapping.Tools
+category(category) But I have to load each JSON file globally and there are referenced directly by the their root nodes so if there is the same node name in two json files there will be a conflict since I can't import them and give them an alias. |
**Pug Version:** 2.0.1
**Node Version:** 8.x
Hi there,
we are building a design system and styleguide with pug. When developing we have a express server generating the html. Some gulp tasks watch over all the files, generate the css, js and browsersyncs.
Often texts are coming from jsons and in the first iterations die jsons got required in the express part and given as locale variables to the pug.
But that meens you have a lot of ghost like global variables in every page. I don’t know the available names of the variables and the jsons are located far away from the rest of the component.
We changed the process. Now the only object we make available for the pug pages is require. just give {require: require} as a local and you have the might of everything in the template.
At the beginning of a mixin I readFile the json. I readFile them because require is cached for the server runtime and I modify the Files on runtime. The performance drawback is acceptable.
I can’t just include the json, because I need the json in the JS part to select and modify the strings.
My main discomfort is the long path to the json. At the moment the data fetching looks like:
It works, but it could be shorter, like:
My main Problem is, that readJson doesn’t know the location of the calling mixin. Every call is relative to the project root.
Is there a way to get to know the current position? A variable, a function or an entry in this would be nice. And I can write the readJson myself.
The text was updated successfully, but these errors were encountered: