Skip to content

Added support for the handlebars-loader #301

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

Merged
merged 1 commit into from
Apr 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fixtures/js/handlebars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var template = require('../templates/template.hbs');

document.getElementById('app').innerHTML = template({
title: 'Welcome to Your Handlebars App'
});
1 change: 1 addition & 0 deletions fixtures/templates/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>{{ title }}</h1>
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,27 @@ class Encore {
return this;
}

/**
* Call this if you plan on loading Handlebars files.
*
* Encore.enableHandlebarsLoader();
*
* Or pass options to the loader
*
* Encore.enableHandlebarsLoader(function(options) {
* // https://github.com/pcardune/handlebars-loader
* // options.debug = true;
* });
*
* @param {function} callback
* @returns {Encore}
*/
enableHandlebarsLoader(callback = () => {}) {
webpackConfig.enableHandlebarsLoader(callback);

return this;
}

/**
* Call this if you wish to disable the default
* images loader.
Expand Down
12 changes: 12 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class WebpackConfig {
this.useCoffeeScriptLoader = false;
this.useForkedTypeScriptTypeChecking = false;
this.useWebpackNotifier = false;
this.useHandlebarsLoader = false;

// Features/Loaders options
this.sassOptions = {
Expand All @@ -87,6 +88,7 @@ class WebpackConfig {
this.vueLoaderOptionsCallback = () => {};
this.tsConfigurationCallback = () => {};
this.coffeeScriptConfigurationCallback = () => {};
this.handlebarsConfigurationCallback = () => {};

// Plugins options
this.cleanWebpackPluginPaths = ['**/*'];
Expand Down Expand Up @@ -446,6 +448,16 @@ class WebpackConfig {
this.notifierPluginOptionsCallback = notifierPluginOptionsCallback;
}

enableHandlebarsLoader(callback = () => {}) {
this.useHandlebarsLoader = true;

if (typeof callback !== 'function') {
throw new Error('Argument 1 to enableHandlebarsLoader() must be a callback function.');
}

this.handlebarsConfigurationCallback = callback;
}

disableImagesLoader() {
this.useImagesLoader = false;
}
Expand Down
8 changes: 8 additions & 0 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const babelLoaderUtil = require('./loaders/babel');
const tsLoaderUtil = require('./loaders/typescript');
const coffeeScriptLoaderUtil = require('./loaders/coffee-script');
const vueLoaderUtil = require('./loaders/vue');
const handlebarsLoaderUtil = require('./loaders/handlebars');
// plugins utils
const extractTextPluginUtil = require('./plugins/extract-text');
const deleteUnusedEntriesPluginUtil = require('./plugins/delete-unused-entries');
Expand Down Expand Up @@ -242,6 +243,13 @@ class ConfigGenerator {
});
}

if (this.webpackConfig.useHandlebarsLoader) {
rules.push({
test: /\.(handlebars|hbs)$/,
use: handlebarsLoaderUtil.getLoaders(this.webpackConfig)
});
}

this.webpackConfig.loaders.forEach((loader) => {
rules.push(loader);
});
Expand Down
5 changes: 5 additions & 0 deletions lib/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const features = {
method: 'configureUrlLoader()',
packages: ['url-loader'],
description: 'use the url-loader'
},
handlebars: {
method: 'enableHandlebarsLoader()',
packages: ['handlebars', 'handlebars-loader'],
description: 'load Handlebars files'
}
};

Expand Down
32 changes: 32 additions & 0 deletions lib/loaders/handlebars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

const loaderFeatures = require('../features');
const applyOptionsCallback = require('../utils/apply-options-callback');

/**
* @param {WebpackConfig} webpackConfig
* @return {Array} of loaders to use for Handlebars
*/
module.exports = {
getLoaders(webpackConfig) {
loaderFeatures.ensurePackagesExist('handlebars');

const options = {};

return [
{
loader: 'handlebars-loader',
options: applyOptionsCallback(webpackConfig.handlebarsConfigurationCallback, options)
}
];
}
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
"eslint-plugin-header": "^1.0.0",
"eslint-plugin-node": "^4.2.2",
"fork-ts-checker-webpack-plugin": "^0.2.7",
"handlebars": "^4.0.11",
"handlebars-loader": "^1.7.0",
"http-server": "^0.9.0",
"less": "^2.7.2",
"less-loader": "^4.0.2",
Expand Down
22 changes: 22 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,28 @@ describe('WebpackConfig object', () => {
});
});

describe('enableHandlebarsLoader', () => {

it('Call with no config', () => {
const config = createConfig();
config.enableHandlebarsLoader();

expect(config.useHandlebarsLoader).to.be.true;
});

it('Pass config', () => {
const config = createConfig();
const callback = (options) => {
options.debug = true;
};
config.enableHandlebarsLoader(callback);

expect(config.useHandlebarsLoader).to.be.true;
expect(config.handlebarsConfigurationCallback).to.equal(callback);
});

});

describe('addPlugin', () => {
it('extends the current registered plugins', () => {
const config = createConfig();
Expand Down
26 changes: 26 additions & 0 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,32 @@ describe('The config-generator function', () => {
});
});

describe('enableHandlebarsLoader() adds the handlebars-loader', () => {

it('without enableHandlebarsLoader()', () => {
const config = createConfig();
config.outputPath = '/tmp/output/public-path';
config.publicPath = '/public-path';
config.addEntry('main', './main');
const actualConfig = configGenerator(config);

expect(JSON.stringify(actualConfig.module.rules)).to.not.contain('handlebars-loader');
});

it('enableHandlebarsLoader()', () => {
const config = createConfig();
config.outputPath = '/tmp/output/public-path';
config.publicPath = '/public-path';
config.addEntry('main', './main');
config.enableHandlebarsLoader();

const actualConfig = configGenerator(config);

expect(JSON.stringify(actualConfig.module.rules)).to.contain('handlebars-loader');
});

});

describe('addLoader() adds a custom loader', () => {
it('addLoader()', () => {
const config = createConfig();
Expand Down
26 changes: 26 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,32 @@ module.exports = {
});
});

it('When configured, Handlebars is compiled', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
config.addEntry('main', ['./js/handlebars.js']);
const testCallback = () => {};
config.enableHandlebarsLoader(testCallback);

testSetup.runWebpack(config, () => {
expect(config.outputPath).to.be.a.directory().with.deep.files([
'main.js',
'manifest.json'
]);

testSetup.requestTestPage(
path.join(config.getContext(), 'www'),
[
'build/main.js'
],
(browser) => {
browser.assert.text('#app h1', 'Welcome to Your Handlebars App');
done();
}
);
});
});

it('The output directory is cleaned between builds', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ describe('Public API', () => {

});

describe('enableHandlebarsLoader', () => {

it('must return the API object', () => {
const returnedValue = api.enableHandlebarsLoader();
expect(returnedValue).to.equal(api);
});

});

describe('disableImagesLoader', () => {

it('must return the API object', () => {
Expand Down
59 changes: 59 additions & 0 deletions test/loaders/handlebars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

const expect = require('chai').expect;
const WebpackConfig = require('../../lib/WebpackConfig');
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
const handlebarsLoader = require('../../lib/loaders/handlebars');

function createConfig() {
const runtimeConfig = new RuntimeConfig();
runtimeConfig.context = __dirname;
runtimeConfig.babelRcFileExists = false;

return new WebpackConfig(runtimeConfig);
}

describe('loaders/handlebars', () => {
it('getLoaders() basic usage', () => {
const config = createConfig();
config.enableHandlebarsLoader();

const actualLoaders = handlebarsLoader.getLoaders(config);
expect(actualLoaders).to.have.lengthOf(1);
expect(actualLoaders[0].options).to.be.empty;
});

it('getLoaders() with options callback', () => {
const config = createConfig();
config.enableHandlebarsLoader((options) => {
options.debug = true;
});

const actualLoaders = handlebarsLoader.getLoaders(config);
expect(actualLoaders).to.have.lengthOf(1);
expect(actualLoaders[0].options.debug).to.be.true;
});

it('getLoaders() with options callback that returns an object', () => {
const config = createConfig();
config.enableHandlebarsLoader((options) => {
options.debug = true;

// This should override the original config
return { foo: true };
});

const actualLoaders = handlebarsLoader.getLoaders(config);
expect(actualLoaders).to.have.lengthOf(1);
expect(actualLoaders[0].options).to.deep.equal({ foo: true });
});
});
Loading