Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`<br>
Type: `String | Array(String) | Object`<br>
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`<br>
Expand Down
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand Down
32 changes: 28 additions & 4 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
22 changes: 18 additions & 4 deletions test/snapshots/plugin.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Binary file modified test/snapshots/plugin.test.js.snap
Binary file not shown.
Binary file modified test/snapshots/proxy.test.js.snap
Binary file not shown.