简体   繁体   中英

Handlebars with Webpack returns Javascript code instead of rendering the HTML

I have a basic Laravel project, A todo app, And I'm using Laravel Elixir to compile down assets, And I'm using handlebars-loader for Webpack to compile down my assets.

Here's my gulpfile.js:

const elixir = require('laravel-elixir');


elixir((mix) => {
    mix.webpack('app.js', null, null, require('./webpack.config.js'));
});

And I'm requiring webpack.config.js because I want to load the handlebars-loader

Here's my webpack.config.js:

module.exports = {
    module: {
        loaders: [
            { test: /\.handlebars$/, loader: "handlebars-loader" }
        ]
    },
};

And for making things simpler for this question I'm gonna put all my code in app.js:

'use strict';

var _token = $('meta[name="csrf-token"]').attr('content');

var renderErrorsTemplate = require('./../templates/errors.handlebars');
var renderTasksTemplate  = require('./../templates/task.handlebars');

$('#taskForm').submit(function(event) {
    var form = $(this);    
    var request = $.ajax({
        type: form.attr('method'),
        url: form.attr('action'),
        data: form.serialize()
    });
    request.done(function(response) { 
        renderTasks(response);
    });
    request.fail(function(response) {
        renderErrors(response.responseText);
    });
    event.preventDefault();
});


function renderTasks(response) {
    var response = {task: response, token: _token};
    $('#tasks').append(renderTasksTemplate(response));
    console.log(renderTasksTemplate);
}

function renderErrors(response) {
    $('#errors').html(renderErrorsTemplate(JSON.parse(response)))
    console.log(renderErrorsTemplate(response));
}   

I'm using require('template.js') to require the needed templates, Without using Laravel Elixir it works on another plain App without using any frameworks, but If I run gulp in the terminal, it will compile it down, so later when I trigger the functions using click events, it just returns some Javascript code instead of actually running that Javascript code and render the templates.

This is what it returns:

var Handlebars = require("/home/akar/Code/laravel-app/node_modules/handlebars/runtime.js"); function __default(obj) { return obj && (obj.__esModule ? obj["default"] : obj); } module.exports = (Handlebars["default"] || Handlebars).template({"1":function(container,depth0,helpers,partials,data) { return "
" + container.escapeExpression(container.lambda((depth0 != null ? depth0["0"] : depth0), depth0)) + "
\n"; },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { var stack1; return "
\n
\n" + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},depth0,{"name":"each","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
\n
"; },"useData":true});

instead of the proccesed version of this template:

<div class="alert alert-danger">
    <ul>
        {{#each this}}   
            <li>{{ this.[0] }}</li>
       {{/each}}
    </ul>

So at the end I changed my gulpfile.js to this:

const elixir = require('laravel-elixir');


elixir((mix) => {
    mix.sass('app.scss')
       .webpack('app.js');
});

So apparently, if you just run mix.webpack by itself it will check automatically for any webpack.config.js in your directory, and then read configurations from there.

and I changed my webpack.config.js file to this:

var webpack = require('webpack');

module.exports = {
    devtool: 'source-map',
    module: {   
        loaders: [
            { test: /\.js$/, loader: 'buble' },
            { test: /\.handlebars$/, loader: "handlebars-loader" }
        ]
    },
    watch: true,
};

And also I removed node_modules and did npm install again.

Hope this helps someone, I don't know what was exactly causing this mess but I fixed it anyways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM