From 1da9c3639fd101880d07f498be8a6f6ea72422d2 Mon Sep 17 00:00:00 2001 From: hubcarl Date: Thu, 25 Jan 2018 17:47:51 +0800 Subject: [PATCH] feat: support config.template for html --- lib/builder.js | 2 +- lib/client.js | 5 ++- test/html.test.js | 95 +++++++++++++++++++++++++++++++++++++++++++++++ utils/utils.js | 5 +++ 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 test/html.test.js diff --git a/lib/builder.js b/lib/builder.js index a1fb8fd..eb7e121 100644 --- a/lib/builder.js +++ b/lib/builder.js @@ -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'); diff --git a/lib/client.js b/lib/client.js index 137bb87..1037517 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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)) { diff --git a/test/html.test.js b/test/html.test.js new file mode 100644 index 0000000..2272e2c --- /dev/null +++ b/test/html.test.js @@ -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); + }); + }); +}); \ No newline at end of file diff --git a/utils/utils.js b/utils/utils.js index 187d601..c1eadc2 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -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);