Jade support in webpack releases
Sorry for not providing template information but I only have my phone available (and there's no critical info either).
Is there some in-the-works support for jade? The related issues/PR 's were closed but no real solution was provided. What can be done to have support for this?
@ShadowManu: Yeah, under normal circumstances I suppose that would've been the straight-forward way. One complication I faced personally with my plugin was (1) adding configuration, and (2) having to ensure I could add Pug plugins as well (-> functions, therefore can't survive JSON serialization, so couldn't be passed through a CLI). I never really wanted to use a plugin, but that's how it ended up. I'd definitely rather not resort to Gulp either.
I just did manage that with help from @TaylorDennisLee though, so to answer @mbonski's earlier question of a guide on how to get this to work before it's merged:
git clone -b pug https://github.com/zoitravel/angular-cli.git
# alternative with plugin: git clone -b pug-html https://github.com/tycho01/angular-cli.git
cd angular-cli
npm link
cd PATH/TO/MY/PROJECT
npm link angular-cli
# in component: templateUrl: './my_pug_file.pug',
ng serve
(Now as to the real frontier, ditching pug gulp tasks in Ionic...)
Has anyone gotten variable interpolation to work with webpack1 and pug-ng-html-loader? For some reason its not being able to access my variables
Edit: Just figured out I was basically writing the angular templates using pug and rendering it so instead of using pug variables I should be using angular variables like such {{var}}
I found a way to use AngularCLI with Pug using pug-html-loader. After generate new projects using ng new. Install pug and pug-html-loader packages. npm install --save-dev pug pug-html-loader. Add pug-html-loader to your project \node_modules@angular\cli\models\webpack-configs\common.js
{ test: /.(pug|jade)$/, loader: 'pug-html-loader' }, under { test: /.html$/, loader: 'raw-loader' },
Use .jade template in component.
@Component({ selector: 'app', templateUrl: './app.template.jade' })
I'v made a demo template here https://github.com/CrystalzWind/AngularCLI-Pug *Note: We must add pug loader to common.js manually after install
Equivalent option to @CrystalzWind 's example is to use pug-loader with apply-loader.
npm install pug-loader apply-loader
and piping the output of the pug-loader through the apply loader which simply executes the template function.
{
test: /\.pug$/,
loader: "apply-loader!pug-loader?self"
}
in
\node_modules@angular\cli\models\webpack-configs\common.js
Which works with ng serve and ng build --aot for me now
Extending on the workaround presented by @CrystalzWind and @donmahallem, looking at
https://github.com/angular/angular-cli/blob/master/packages/%40angular/cli/models/webpack-configs/common.ts#L33
https://github.com/angular/angular-cli/blob/master/packages/%40angular/cli/models/webpack-configs/common.ts#L104-L112
it seems like giving configuration access to the extraRules variable (there seems to be some configuration options through buildOptions and appConfig already) might be enough to solve this issue.
Edit: Aides359@f3cfdb803c7a17d2d18707f296e6fa51efb8a172 shows how such a change might look like (it works but I am fully aware that this is not suited for PR yet).
This would allow "loaderRules": [{ "test": "\\.(pug|jade)$", "loader": "apply-loader!pug-loader" }] in .angular-cli.json to fix this issue.
I'm sorry to be the bearer of bad news, but jade/pug support will not make it to 1.0. It's an medium/big feature that we can't spare the time to make sure it work properly.
I know it looks like it would only be a small change in loaders but that's just not been the case with similar features like sass support.
We're looking at it for 2.0 instead, either as base functionality or as one of the main usecases for addons.
Thanks for the update. If we could help out in the testing department, I'd be glad to.
@filipesilva one of the things that would need some consideration for proper pug support would be how to handle the angular syntax with pug since pug has problems with it (would we want to trade equivalence to pug for syntax convenience).
For reference see tycho01/pug-plugin-ng
On that, fixing Pug to play nice with Angular amounted to one small addition if it'd made it into Pug core (-> consider '[' and '(' after whitespace as valid indication a new property has started).
While I feel this makes much sense in the Angular 2+ context, I don't consider it a blocker for initial support, in the sense that it's pretty much backward compatible.
In the Angular context I'd be in favor of doctype: 'html' as well, to allow #myVar / md-raised-button over #myVar='' / md-raised-button=''.
They could, yes. We'd still like to make it a first class citizen one day.
@MarkPieszak this was my current solution to this problem. Thanks for mentioning.
hope some day can support pug in angular-cli
@mbonski
Your example is really close.
First install pug and pug-loader and use this example code.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: require('pug-loader!./app.component.pug')(),
styleUrls: ['app.component.scss']
})
export class AppComponent {
title = 'app works!';
}
The only difference is
template: require('./app.component.pug')
To =>
template: require('pug-loader!./app.component.pug')()
You may also need to add the following into typings.d.ts
// telling the typescript compiler that there is a global function
// called require. And to not freak out about references to it.
declare var require: any;
This also works for any other loader in the webpack universe
So far I got it working with ng eject BUT this does not work with tests which again does desire an appropiate loader.
@Anthonyzou Your workaround does work for JIT but I don't seem to get it running with AOT. The error reads as follows:
Error encountered resolving symbol values statically. Calling function 'template', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol abc in a/b/c
Following the advice of the error and some sites regarding aot I tried: ` export function template() { return require("pug-loader!./test.component.pug")(); }
@Component({ template: template() })`
But it still errors with basicly the same message. Any ideas?
@donmahallem I actually have no idea, actually. This is adapted from a graphql angular2 example where they manually require a graphql file and it works with AOT. It must also depend on how the loader is built.
Heya all, this isn't a feature that we're looking at adding in the 1.x timeframe, but rather something that would happen as a third party addon using the upcoming addon system for v2+.
Guys, I made a pull request to allow tweaking webpack config with a file in the project root. For now, you can use the cli at https://github.com/cashfarm/angular-advanced-cli (branch 1.2.x)
Install pug, pug-ng-html-loader and add a webpack.config.js with this to root of your project:
module.exports = {
module: {
rules: [
{
test: /\.(pug|jade)$/,
use: ['pug-ng-html-loader']
}
]
}
}
Then your components can use pug
@Component({
selector: 'app-root',
templateUrl: './app.component.pug',
styleUrls: ['./app.component.styl']
})
But your index has to stay in html.
Any update?
Going along with some of the recommendations above, I created a little script that can be ran to automatically insert the pug rule into the node_modules folder. @Pexak
This can be used as a temporary solution until it can be available natively. Note this could need to be updated & changed if the CLI changes the location or formatting of this file. It's naively searching for rules: [ within this specific file.
npm i --D apply-loader pug-loader
pug-rule-insert.js
Now you can just create a simple js file with the code below (put it at your root project level).
const fs = require('fs');
const commonCliConfig = 'node_modules/@angular/cli/models/webpack-configs/common.js';
const pug_rule = `\n{ test: /\.pug$/, loader: "apply-loader!pug-loader?self" },`;
fs.readFile(commonCliConfig, (err, data) => {
if (err) { throw err; }
const configText = data.toString();
if (configText.indexOf(pug_rule) > -1) {
return;
}
console.log('-- Inserting .pug webpack rule -- ');
const position = configText.indexOf('rules: [') + 8;
const output = [configText.slice(0, position), pug_rule, configText.slice(position)].join('');
const file = fs.openSync(commonCliConfig, 'r+');
fs.writeFile(file, output);
fs.close(file);
});
Running it
Run it simply with: node pug-rule-insert.js or create a nice postinstall hook to do it for you on new installs.
"postinstall": "node pug-rule-insert.js"
Needed this for a client, hope this helps!
Works with ng serve (even live reloads pug file changes) and AoT ng prod builds, even Universal + CLI projects! 🎁
Tnks @MarkPieszak