From e7005aee33243260fad11add5ebd8c8c3c715091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A1=E8=B0=B7?= Date: Mon, 25 Dec 2017 12:00:45 +0900 Subject: [PATCH] If loader.query is not defined in webpack.config.js, getOptions is return null and Object.keys(query) is cause error. I'm fix this pattern and write test case. --- index.js | 2 +- test/index.js | 14 +++++++++++--- test/webpack.config.js | 3 +-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index dfcc528..5b88374 100644 --- a/index.js +++ b/index.js @@ -34,7 +34,7 @@ module.exports = function(source) { const tags = [] // parse the user query - const query = getOptions(this) + const query = getOptions(this) || {} // normalise the query object in case of question marks const opts = Object.keys(query).reduce(function(acc, key) { diff --git a/test/index.js b/test/index.js index 53ab228..8f32001 100644 --- a/test/index.js +++ b/test/index.js @@ -13,7 +13,7 @@ function readFile(file) { return normalize(fs.readFileSync(path.join(EXPECT, file), 'utf8')) } -function compile(opts = {}) { +function compile(opts) { webpackConfig.module.loaders[0].query = opts const compiler = webpack(webpackConfig) return new Promise(resolve => { @@ -25,8 +25,15 @@ function compile(opts = {}) { } describe('riot-tag-loader unit test', () => { - it('riot loader default options', (done) => { - compile().then(content => { + it('riot loader undefined options', (done) => { + compile(undefined).then(content => { + assert.equal(content, readFile('bundle-normal.js')) + done() + }) + }) + + it('riot loader empty options', (done) => { + compile({}).then(content => { assert.equal(content, readFile('bundle-normal.js')) done() }) @@ -54,4 +61,5 @@ describe('riot-tag-loader unit test', () => { done() }) }) + }) diff --git a/test/webpack.config.js b/test/webpack.config.js index d424d9f..28a7348 100644 --- a/test/webpack.config.js +++ b/test/webpack.config.js @@ -15,8 +15,7 @@ module.exports = { loaders: [ { test: /\.tag$/, - loader: 'riot-tag-loader', - query: {} + loader: 'riot-tag-loader' } ] },