diff --git a/README.md b/README.md index 42fc615..8648f69 100644 --- a/README.md +++ b/README.md @@ -222,12 +222,19 @@ 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. 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/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..59fff19 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.js @@ -4,17 +4,41 @@ 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 → glob', (t) => { - const basePath = join(__dirname, 'fixtures'); +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: [join(basePath, '/**/app.js'), '!**/temp*/*'] + static: [fixturePath] }); - options.static = options.static.map((p) => p.replace(/^.+(serve|project)\//, '')); t.snapshot(options.static); }); + +test('static → glob', (t) => { + const { options } = new WebpackPluginServe({ + allowMany: true, + static: { + glob: [join(__dirname, 'fixtures')], + options: { onlyDirectories: true } + } + }); + 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 f821278..98b55ea 100644 --- a/test/snapshots/plugin.test.js.md +++ b/test/snapshots/plugin.test.js.md @@ -34,13 +34,27 @@ 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/wait-for-build', ] + +## static → string + +> Snapshot 1 + + 'test/fixtures' diff --git a/test/snapshots/plugin.test.js.snap b/test/snapshots/plugin.test.js.snap index cf715df..e9523aa 100644 Binary files a/test/snapshots/plugin.test.js.snap and b/test/snapshots/plugin.test.js.snap differ diff --git a/test/snapshots/proxy.test.js.snap b/test/snapshots/proxy.test.js.snap index 144c5c6..ffe22f9 100644 Binary files a/test/snapshots/proxy.test.js.snap and b/test/snapshots/proxy.test.js.snap differ