Skip to content

Commit

Permalink
feat: configurable file system via options.fs (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinell authored and evilebottnawi committed Feb 19, 2019
1 parent 8e54af2 commit 1762cb3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ please see the [webpack documentation](https://webpack.js.org/configuration/watc
Type: `Boolean|Function`
Default: `false`

If true, the option will instruct the module to write files to the configured
If `true`, the option will instruct the module to write files to the configured
location on disk as specified in your `webpack` config file. _Setting
`writeToDisk: true` won't change the behavior of the `webpack-dev-middleware`,
and bundle files accessed through the browser will still be served from memory._
Expand All @@ -231,6 +231,17 @@ of `true` _will_ write the file to disk. eg.
}
```

### fs
Type: `Object`
Default: `MemoryFileSystem`

Set the default file system which will be used by webpack as primary destination of generated files. Default is set to webpack's default file system: [memory-fs](https://github.com/webpack/memory-fs). This option isn't affected by the [writeToDisk](#writeToDisk) option.

**Note:** As of 3.5.x version of the middleware you have to provide `.join()` method to the `fs` instance manually. This can be done simply by using `path.join`:
```js
fs.join = path.join // no need to bind
```

## API

`webpack-dev-middleware` also provides convenience methods that can be use to
Expand Down
16 changes: 14 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,21 @@ module.exports = {

let fileSystem;
// store our files in memory
const isMemoryFs = !compiler.compilers && compiler.outputFileSystem instanceof MemoryFileSystem;
const isConfiguredFs = context.options.fs;
const isMemoryFs = !isConfiguredFs
&& !compiler.compilers
&& compiler.outputFileSystem instanceof MemoryFileSystem;

if (isMemoryFs) {
if (isConfiguredFs) {
const { fs } = context.options;
if (typeof fs.join !== 'function') {
// very shallow check
throw new Error('Invalid options: options.fs.join() method is expected');
}

compiler.outputFileSystem = fs;
fileSystem = fs;
} else if (isMemoryFs) {
fileSystem = compiler.outputFileSystem;
} else {
fileSystem = new MemoryFileSystem();
Expand Down
35 changes: 35 additions & 0 deletions test/tests/file-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ describe('FileSystem', () => {
assert.equal(firstFs, secondFs);
});

describe('options.fs', () => {
// lightweight compiler mock
const hook = { tap() {} };
const compiler = {
outputPath: '/output',
watch() {},
hooks: { done: hook, invalid: hook, run: hook, watchRun: hook }
};

const fs = { join() {} };

it('should throw on invalid fs', (done) => {
assert.throws(() => {
middleware(compiler, { fs: {} });
});
done();
});

it('should assign fs to the compiler.outputFileSystem', (done) => {
const instance = middleware(compiler, { fs });

assert.equal(compiler.outputFileSystem, fs);
instance.close(done);
});

it('should go safely when compiler.outputFileSystem is assigned by fs externally', (done) => {
const cmplr = Object.create(compiler);
cmplr.outputFileSystem = fs;
const instance = middleware(cmplr, { fs });

assert.equal(cmplr.outputFileSystem, fs);
instance.close(done);
});
});

it('should throw on invalid outputPath config', () => {
const compiler = fakeWebpack();
compiler.outputPath = './dist';
Expand Down

0 comments on commit 1762cb3

Please sign in to comment.