Skip to content

Commit

Permalink
feat: support config.template for html
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Jan 25, 2018
1 parent 64c72ea commit 1da9c36
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports.getBuilderConfig = (config = {}, option = {}) => {
}
return { baseDir };
}
if (utils.isObject(config) && (config.entry || config.framework || config.egg || config.lib || config.dll)) {
if (utils.isObject(config) && (config.cli || config.entry || config.framework || config.egg || config.lib || config.dll)) {
return merge({ baseDir }, config);
}
const filepath = path.join(config.baseDir || baseDir, 'webpack.config.js');
Expand Down
5 changes: 3 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ class WebpackClientBuilder extends WebpackBaseBuilder {
}

createHTML() {
if (this.config.entry && (this.config.entry.template || this.config.entry.html || this.config.framework === 'html')) {
if ((this.config.entry || (this.webpackConfig.entry && Object.keys(this.webpackConfig.entry).length)) &&
(this.config.template || this.config.entry.template || this.config.entry.html || this.config.framework === 'html')) {
const pluginName = 'html';
const plugin = this.getPluginByName(pluginName);
if (this.isUse(plugin)) {
let globalTemplate = this.config.entry.template;
let globalTemplate = this.config.template || this.config.entry.template;
const config = this.utils.cloneDeep(this.config);
/* istanbul ignore next */
if (this.utils.isObject(config.entry.html)) {
Expand Down
95 changes: 95 additions & 0 deletions test/html.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';
const expect = require('chai').expect;
const WebpackTool = require('webpack-tool');
const webpack = WebpackTool.webpack;
const merge = WebpackTool.merge;
const WebpackClientBuilder = require('../lib/client');
const path = require('path').posix;
const fs = require('fs');
const utils = require('../utils/utils');
// http://chaijs.com/api/bdd/
function createBuilder(config) {
const builder = new WebpackClientBuilder(config);
if (config && config.type) {
builder.type = config.type;
}
builder.setBuildPath(path.join(__dirname, 'dist/client'));
builder.setPublicPath('/public');
return builder;
}

function getLoaderByName(name, rules) {
const loaderName = `${name}-loader`;
return rules.find(rule => {
return rule.use.some(loader => {
return loaderName === loader || (typeof loader === 'object' && loader.loader === loaderName);
});
});
}

function getPluginByLabel(label, plugins) {
return plugins.find(plugin => {
return plugin.__lable__ === label || plugin.__plugin__ === label;
});
}

function getAllPluginByLabel(label, plugins) {
return plugins.filter(plugin => {
return plugin.__lable__ === label || plugin.__plugin__ === label;
});
}

describe('html.test.js', () => {
before(() => {
});

after(() => {
});

beforeEach(() => {
});

afterEach(() => {
});

describe('#webpack html test', () => {
it('should config html template', () => {
const template = path.normalize(path.join(__dirname, 'layout.html'));
const builder = createBuilder({
entry:{
client: './test/client.test.js'
},
template
});
const webpackConfig = builder.create();
const htmlPlugins = getPluginByLabel('html-webpack-plugin', webpackConfig.plugins);
expect(webpackConfig.entry).to.include.keys(['client']);
expect(htmlPlugins.options.template).to.equal(template);
});

it('should entry string and html template test', () => {
const template = path.normalize(path.join(__dirname, 'layout.html'));
const builder = createBuilder({
entry:'./test/client.test.js',
template
});
const webpackConfig = builder.create();
const htmlPlugins = getPluginByLabel('html-webpack-plugin', webpackConfig.plugins);
expect(webpackConfig.entry).to.include.keys(['client.test']);
expect(htmlPlugins.options.template).to.equal(template);
});

it('should entry dir html template test', () => {
const template = path.normalize(path.join(__dirname, 'layout.html'));
const builder = createBuilder({
entry:'./test',
template
});
const webpackConfig = builder.create();
const htmlPlugins = getPluginByLabel('html-webpack-plugin', webpackConfig.plugins);
expect(webpackConfig.entry).to.include.keys(['base.test', 'client.test']);
expect(Object.keys(webpackConfig.entry).length).to.equal(getAllPluginByLabel('html-webpack-plugin',webpackConfig.plugins).length);
expect(htmlPlugins.options.template).to.equal(template);
});
});
});
5 changes: 5 additions & 0 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ utils.getEntry = (config, type) => {
if (fs.statSync(filepath).isDirectory()) {
const dirEntry = utils.walkFile(filepath, configEntry.exclude, extMatch, config.baseDir);
Object.assign(entries, utils.createEntry(config, entryLoader, dirEntry, true));
} else {
const entryName = path.basename(filepath, path.extname(filepath));
const singleEntry = {};
singleEntry[entryName] = filepath;
Object.assign(entries, utils.createEntry(config, entryLoader, singleEntry, true));
}
} else if (entry instanceof RegExp) {
const dirEntry = utils.walkFile(entry, configEntry.exclude, extMatch, config.baseDir);
Expand Down

0 comments on commit 1da9c36

Please sign in to comment.