diff --git a/README.md b/README.md index 26f48dd16..3df506d47 100644 --- a/README.md +++ b/README.md @@ -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._ @@ -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 diff --git a/lib/fs.js b/lib/fs.js index 6bd5e9baa..90dda88a1 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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(); diff --git a/test/tests/file-system.js b/test/tests/file-system.js index 99ce8c731..649f92b00 100644 --- a/test/tests/file-system.js +++ b/test/tests/file-system.js @@ -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';