Skip to content
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

Cannot use variables for attributes in include or require macros #28

Open
jamesehly opened this issue Oct 5, 2017 · 3 comments
Open

Comments

@jamesehly
Copy link

It would be cool if we could use variables with the require and include macros, but it looks like they can only accept string, number, boolean and objects at the moment. For instance, doing this:

@include(htmlWebpackPlugin.options.filename)

results in...

ERROR in   Error: Child compilation failed:
  Module not found: Error: Can't resolve './NaN' in 'C:\static\outkit-docs\src\templates\docs':
  Error: Can't resolve './NaN' in 'C:\static\outkit-docs\src\templates\docs'

Does anyone know of a workaround for this?

@idudinov
Copy link
Contributor

+1, the other possible desired use case is something like that – just use something JS evaluate-able :

@require('./header.html', {
    someValue: 2 + 3,
    title: (originalTitle && originalTitle.trim()) || 'no title',
})

This could allow to have more dynamic and complex templating, and generate more static htmls instead of having it in runtime.

@idudinov
Copy link
Contributor

OK, I've investigated the sources a bit, and have found out that object literal (used as second @require argument) is processed by JSON.parse and then by JSON.stringify inside the loader, so should be evaluated somehow before @require is parsed. But as for now, it doesn't seem feasible to me to swap the order of parsing steps or add additional one.
So I have no idea how that feature can be implemented.

@idudinov
Copy link
Contributor

A workaround came to me suddenly :) Tricky a bit, and uses portions of the loader's source code as example, but here it is:

<%
    const title = 'Some ' + 'title';
    const headFileName = htmlWebpackPlugin.options.headFileName || 'head.ejs';

    function objExtend(args, obj) {
        args = Array.prototype.slice.call(args);
        args[0] = Object.assign(obj, args[0]);
        return args;
    };
%>

<%= require(`./structure/${headFileName}`).apply(null, objExtend(arguments, { title })) %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants