From a1b851f69432d0bc60269ccb7eee2bfe494315f2 Mon Sep 17 00:00:00 2001 From: shellscape Date: Sat, 4 May 2019 23:29:14 -0600 Subject: [PATCH 1/2] fix: static regression when passing array --- README.md | 4 ++-- lib/index.js | 5 +++-- lib/validate.js | 6 +++++- test/plugin.test.js | 27 ++++++++++++++++++++++++--- test/snapshots/plugin.test.js.md | 27 +++++++++++++++++++++++---- test/snapshots/plugin.test.js.snap | Bin 631 -> 762 bytes test/snapshots/proxy.test.js.snap | Bin 140 -> 140 bytes 7 files changed, 57 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 42fc615..e95cefe 100644 --- a/README.md +++ b/README.md @@ -222,12 +222,12 @@ If [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), the modul If a value of `'minimal'` is set, the progress indicator will render as a small, colored bar at the top of the window. This can be useful when the default fancy progress indicator interferes with elements in the page. ### `static` -Type: `String | Array(String)`
+Type: `String | Array(String) | Object`
Default: `compiler.context` Sets the directory(s) from which static files will be served from the root of the application. Bundles will be served from the `output` config setting. For specifying options for static file directories, please see [`middleware > static`](#middleware). For a in-depth example, check out the [Static HTML File](./recipes/static-html-files.md) recipe. -The `static` option supports [glob patterns](https://github.com/sindresorhus/globby#globbypatterns-options) when an `Array` of `String` is passed. This is useful for targeting only specific files within a directory. +The `static` option supports [glob patterns](https://github.com/sindresorhus/globby#globbypatterns-options) when an `Object` is passed with a `glob` property. eg. `{ glob: [ ... ], options: { ... } }`. This is useful for targeting only specific directories in a complex tree. Users may also provide an `options` property which supports [globby options](https://github.com/sindresorhus/globby#options). ### `status` Type: `boolean`
diff --git a/lib/index.js b/lib/index.js index 9aa5655..8901aed 100644 --- a/lib/index.js +++ b/lib/index.js @@ -101,8 +101,9 @@ class WebpackPluginServe extends EventEmitter { if (!options.static) { options.static = []; - } else if (Array.isArray(options.static)) { - options.static = globby.sync(options.static); + } else if (options.static.glob) { + const { glob, options: globOptions = {} } = options.static; + options.static = globby.sync(glob, globOptions); } this.app = new Koa(); diff --git a/lib/validate.js b/lib/validate.js index fa21a57..d7f0a7d 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -53,7 +53,11 @@ module.exports = { progress: [boolean(), string().valid('minimal')], secure: any().forbidden(), // prettier-ignore - static: [string().allow(null), array().items(string())], + static: [ + string().allow(null), + array().items(string()), + object().keys({ glob: array().items(string()), options: object() }) + ], status: boolean(), waitForBuild: boolean() }; diff --git a/test/plugin.test.js b/test/plugin.test.js index eec71dd..caf4283 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.js @@ -4,17 +4,38 @@ const test = require('ava'); const { WebpackPluginServe } = require('../lib'); +const reCleanDir = /^.+(serve|project)\//g; +const fixturePath = join(__dirname, 'fixtures').replace(reCleanDir, ''); + test('defaults', (t) => { const plugin = new WebpackPluginServe(); t.snapshot(plugin.options); }); +test('static → string', (t) => { + const { options } = new WebpackPluginServe({ + allowMany: true, + static: fixturePath + }); + t.snapshot(options.static); +}); + +test('static → array(string)', (t) => { + const { options } = new WebpackPluginServe({ + allowMany: true, + static: [fixturePath] + }); + t.snapshot(options.static); +}); + test('static → glob', (t) => { - const basePath = join(__dirname, 'fixtures'); const { options } = new WebpackPluginServe({ allowMany: true, - static: [join(basePath, '/**/app.js'), '!**/temp*/*'] + static: { + glob: [join(__dirname, 'fixtures')], + options: { onlyDirectories: true } + } }); - options.static = options.static.map((p) => p.replace(/^.+(serve|project)\//, '')); + options.static = options.static.map((p) => p.replace(reCleanDir, '')); t.snapshot(options.static); }); diff --git a/test/snapshots/plugin.test.js.md b/test/snapshots/plugin.test.js.md index f821278..d8d0489 100644 --- a/test/snapshots/plugin.test.js.md +++ b/test/snapshots/plugin.test.js.md @@ -34,13 +34,32 @@ Generated by [AVA](https://ava.li). status: true, } +## static → array(string) + +> Snapshot 1 + + [ + 'test/fixtures', + ] + ## static → glob > Snapshot 1 [ - 'test/fixtures/multi/app.js', - 'test/fixtures/proxy/app.js', - 'test/fixtures/simple/app.js', - 'test/fixtures/wait-for-build/app.js', + 'test/fixtures/multi', + 'test/fixtures/proxy', + 'test/fixtures/simple', + 'test/fixtures/temp-multi-hmr', + 'test/fixtures/temp-single-hmr', + 'test/fixtures/wait-for-build', + 'test/fixtures/proxy/output', + 'test/fixtures/simple/output', + 'test/fixtures/wait-for-build/output', ] + +## static → string + +> Snapshot 1 + + 'test/fixtures' diff --git a/test/snapshots/plugin.test.js.snap b/test/snapshots/plugin.test.js.snap index cf715dfc65b5214b0062f5b40f56b40d5daff4f1..ca90b11654dbffbad68b98f28f3c8913a9dd7662 100644 GIT binary patch literal 762 zcmVO$O@nR%T_%@@hbw59FBD(F%K zp;Ax>2tN$x%OoOkbgi9-lc$oj|A>(ft$ z$Fr~ARyMb0cY(C8eVLGZgS*qY@|WwkUc4SVSQ}Xd9_>>IdGPY)ugz_5f9l5YVc*ny z03Lma7+Lu=!u~vJeBS$U_-uUVF7W78l@OU^xiV;=SMlCNJV$IGwh=o#i3*5ohzi0+ zJVks!d`0|341ziU>YeH=tucW3u~;k;C=lo;pd&{b0+~Q&0JNHGdp->U9*_${I@_d1 zo%%hpdeaP;>n}_gmQ^)sk9k}w3M{#4`<;v~WR#{GuVRq#T|~T^5AZ zw6NC6W^Vhwu>e~1r0Y6AHL}n~#-c51#lth58CRUPW7sr$8JeS256?%*v%-eylmhCv zD4*CRIWaLjhco6pHw2!YN@Nw*q)xjZc?qK{h&N(Sa@S$;LSal3L&ZOfz(^b literal 631 zcmV--0*L)VRzV%YhA2X{e7zdMB3q@h{hf*0U3LOemdL3}|R=p-s2 zt|GdK81V@48u1bF4RIFg%TT`+3~8tUiq}@FMPQM@5&;i6)(Y4JYRlk;B2FdEG99oN zjdUZTevisivR=e8CFEqo=REMk2Rd%G43-_ma+dLoj1!4Woh;qtY(%@13%^&WE&=?( z2@72=20FNA9R-^)>-9Jt`;wZ?L`svg*1b}hlM1O!Yf>o&g*C?xU(aZmOIlXkE-BVC zw?na)3iit!Yd;Xy-T(A~!!#vv% zu2+TKS+;glN`C_G{LS+`y*0KmMvc=E<%M}H=?l~C#E7337v@a%iDv0S!={F56SlN;%l%Tz*_7s1 zC>p2SSH*A(TU(m9vs1L36&gLAYR}UDy><3&PF|~$$39c-zL4!8 RXS{b<^dF`LEjl3u001T$CTaix diff --git a/test/snapshots/proxy.test.js.snap b/test/snapshots/proxy.test.js.snap index 144c5c641618888af544b91eb345683e42bc61a3..ffe22f9566a82f1df4796070deedbd18ef1691a9 100644 GIT binary patch literal 140 zcmZ<^b5sb-&d$z%{7v5ke#&=qFn|H`=A?v#grphI z3*H+0U;oSG?S9kmeargOl9CP%QXR(r_y3%YKeq3eMg7(%ys>F^ZW~sZ3K$!2Xbv^p sWW-_g>6N-ziq?{6OGTcfKbyM1gR^nT7w@2M?+2e4R=@eXx)f*y0Nq_Y`~Uy| literal 140 zcmV;70CWFARzVegYHF$cZK$RCRc00000000BC zVqjokV#qvw^EZDP`|9KT)si=KnfVwQKtKVA=Q&*a%+qzhz|v8+(>vkoaz?PIH4rm1 uu!B`GvN8xV3h5^nWa^is7MCcb=A{(mXXcfl$n%2a;ZguY|96me0000IH9kfF From aca8e51ffabc7d8e42ee9e36fddd148ea87396e1 Mon Sep 17 00:00:00 2001 From: shellscape Date: Sat, 4 May 2019 23:41:10 -0600 Subject: [PATCH 2/2] chore: clearer readme example, fix tests --- README.md | 9 ++++++++- test/plugin.test.js | 7 +++++-- test/snapshots/plugin.test.js.md | 5 ----- test/snapshots/plugin.test.js.snap | Bin 762 -> 692 bytes 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e95cefe..8648f69 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,14 @@ Default: `compiler.context` Sets the directory(s) from which static files will be served from the root of the application. Bundles will be served from the `output` config setting. For specifying options for static file directories, please see [`middleware > static`](#middleware). For a in-depth example, check out the [Static HTML File](./recipes/static-html-files.md) recipe. -The `static` option supports [glob patterns](https://github.com/sindresorhus/globby#globbypatterns-options) when an `Object` is passed with a `glob` property. eg. `{ glob: [ ... ], options: { ... } }`. This is useful for targeting only specific directories in a complex tree. Users may also provide an `options` property which supports [globby options](https://github.com/sindresorhus/globby#options). +The `static` option supports [glob patterns](https://github.com/sindresorhus/globby#globbypatterns-options) when an `Object` is passed with a `glob` property. This is useful for targeting only specific directories in a complex tree. Users may also provide an `options` property which supports [globby options](https://github.com/sindresorhus/globby#options). For example: + +```js +static: { + glob: [path.join(__dirname, 'dist/**/public')], + options: { onlyDirectories: true } +} +``` ### `status` Type: `boolean`
diff --git a/test/plugin.test.js b/test/plugin.test.js index caf4283..59fff19 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.js @@ -36,6 +36,9 @@ test('static → glob', (t) => { options: { onlyDirectories: true } } }); - options.static = options.static.map((p) => p.replace(reCleanDir, '')); - t.snapshot(options.static); + const res = options.static + .map((p) => p.replace(reCleanDir, '')) + .filter((p) => !/temp|output/.test(p)) + .sort(); + t.snapshot(res); }); diff --git a/test/snapshots/plugin.test.js.md b/test/snapshots/plugin.test.js.md index d8d0489..98b55ea 100644 --- a/test/snapshots/plugin.test.js.md +++ b/test/snapshots/plugin.test.js.md @@ -50,12 +50,7 @@ Generated by [AVA](https://ava.li). 'test/fixtures/multi', 'test/fixtures/proxy', 'test/fixtures/simple', - 'test/fixtures/temp-multi-hmr', - 'test/fixtures/temp-single-hmr', 'test/fixtures/wait-for-build', - 'test/fixtures/proxy/output', - 'test/fixtures/simple/output', - 'test/fixtures/wait-for-build/output', ] ## static → string diff --git a/test/snapshots/plugin.test.js.snap b/test/snapshots/plugin.test.js.snap index ca90b11654dbffbad68b98f28f3c8913a9dd7662..e9523aab15edc99946b59e668b2e74643f60890a 100644 GIT binary patch literal 692 zcmV;l0!#ftRzVK@^|2AIT;)-Gqdq;3bIQ!4U9JR8X;pUQMGO#EZMhB-y%O>`sz~wg;=AM-haI zs37&=!QKT?p~oIP`WN(8u|f}C3cgI5?e4U}g&(u~K7McBZ{Dm+2vNw!$I}}Nk7nl5 zFW%HQw-$GS44*xjkUPcQg-rG9l^f4qUf6$KUI%WSQV6;C{MxV0ZGW$Jb>?8I_6~qs z?<2}4`GZNBR^nB}>A+v5+Op@EdasnlhGIF%&U zMqc%15Eui{#&5c=^Qn=AHdLGTs2z!Cxh+pr+cg|I{wVdS>EnDiGoaNQ6?-Y@seAylA|ugsbJLc zZG%P4WtNwD-0Esna+E04RFXBL`>v7#fyD7-xmu@& zm7|?cdL3aN{$4))k6RJBiwW+k)3up(7y~dN2yfETU>p=lSxN?l779zpQSvzI aY%;2ESgg|Wf{NL-?B+kXJnZkJ1polb!&Bn` literal 762 zcmVO$O@nR%T_%@@hbw59FBD(F%K zp;Ax>2tN$x%OoOkbgi9-lc$oj|A>(ft$ z$Fr~ARyMb0cY(C8eVLGZgS*qY@|WwkUc4SVSQ}Xd9_>>IdGPY)ugz_5f9l5YVc*ny z03Lma7+Lu=!u~vJeBS$U_-uUVF7W78l@OU^xiV;=SMlCNJV$IGwh=o#i3*5ohzi0+ zJVks!d`0|341ziU>YeH=tucW3u~;k;C=lo;pd&{b0+~Q&0JNHGdp->U9*_${I@_d1 zo%%hpdeaP;>n}_gmQ^)sk9k}w3M{#4`<;v~WR#{GuVRq#T|~T^5AZ zw6NC6W^Vhwu>e~1r0Y6AHL}n~#-c51#lth58CRUPW7sr$8JeS256?%*v%-eylmhCv zD4*CRIWaLjhco6pHw2!YN@Nw*q)xjZc?qK{h&N(Sa@S$;LSal3L&ZOfz(^b