johnsoftek / plugin-jade

Jade loader plugin for jspm
MIT License
13 stars 5 forks source link

Option for returning function already called #16

Closed jonricaurte closed 8 years ago

jonricaurte commented 8 years ago

In my code I always have to call the function for every import I do of a jade file; it would be nice to have an option to pass plugin-jade where it returns the already called function. An Angular 2 example is:

import {Component} from 'angular2/core';
import template from './app.component.jade';

@Component({
  selector: 'app',
  template: template()
})
export class AppComponent {
  constructor() { }
}

It would be nice if I just had to do this:

import {Component} from 'angular2/core';
import template from './app.component.jade';

@Component({
  selector: 'app',
  template: template
})
export class AppComponent {
  constructor() { }
}

Without the parens for every template I import. Maybe have an option in the jspm config for this.

Thanks.

johnsoftek commented 8 years ago

@jonricaurte The npm page for (server-side) Jade describes the following API:

var jade = require('jade');

// compile 
var fn = jade.compile('string of jade', options);
var html = fn(locals);

The jade plugin follows a similar convention. In order to allow bundling jade templates without having to bundle the jade compiler, the plugin invokes the compiler, wraps the resulting javascript function and returns it to the jspm bundler.

So the browser usage is then:

var fn = require('file.jade');
var html = fn(locals);

or

import fn from 'file.jade';
let html = fn(locals);

Note that this usage allows parameters to be passed to the jade template.

I am uncomfortable changing the standard usage, particularly using a jspm configuration setting.

I propose adding named exports fn and html. Usage:

import { html } from 'component.jade'

This would also work:

import fn from 'component.jade'
let template = fn()

and this:

import { fn } from 'component.jade'
let template = fn()

@jonricaurte What do you think?

jonricaurte commented 8 years ago

Sounds good to me. Thanks!