From 9102f97e1c143e9ddaab3139acbb3e104af7081b Mon Sep 17 00:00:00 2001 From: Hinell Date: Sat, 16 Feb 2019 01:05:39 +0300 Subject: [PATCH 1/3] Configurable file system via options.fs, couple of tests Changes: * fs option which can be used to configure non-default file system (it is forced to memory-fs in case it is not the same instance) * adding two simple tests --- README.md | 11 ++++++++++- lib/fs.js | 16 ++++++++++++++-- test/tests/file-system.js | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 26f48dd16..dc3637be7 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ app.use(middleware(compiler, { app.listen(3000, () => console.log('Example app listening on port 3000!')) ``` + ## Options The middleware accepts an `options` Object. The following is a property reference @@ -210,7 +211,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 +232,14 @@ 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. This can be done simply by using `path.join`. + ## 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..6a4ccee40 100644 --- a/test/tests/file-system.js +++ b/test/tests/file-system.js @@ -37,6 +37,43 @@ 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 provide .join()', (done) => { + try { + middleware(compiler, { fs }); + } catch (error) { + done(error); + return; + } + done(); + }); + + it('should be assigned 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'; From 3ac83d1e7523e983e4122650c47ca45ada13b639 Mon Sep 17 00:00:00 2001 From: Hinell Date: Mon, 18 Feb 2019 13:45:18 +0300 Subject: [PATCH 2/3] Better testing --- test/tests/file-system.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/test/tests/file-system.js b/test/tests/file-system.js index 6a4ccee40..3ad20192e 100644 --- a/test/tests/file-system.js +++ b/test/tests/file-system.js @@ -48,17 +48,14 @@ describe('FileSystem', () => { const fs = { join() {} }; - it('should provide .join()', (done) => { - try { - middleware(compiler, { fs }); - } catch (error) { - done(error); - return; - } + it('should throw on invalid fs', (done) => { + assert.throws(() => { + middleware(compiler, { fs: {} }); + }); done(); }); - it('should be assigned to the compiler.outputFileSystem', (done) => { + it('should assign fs to the compiler.outputFileSystem', (done) => { const instance = middleware(compiler, { fs }); assert.equal(compiler.outputFileSystem, fs); From 8743f977e6b29a72c94e1c9333acdb7afaf2bf8c Mon Sep 17 00:00:00 2001 From: Hinell Date: Mon, 18 Feb 2019 21:46:14 +0300 Subject: [PATCH 3/3] Readme options.fs example of how to fix join() (wording, requests for changes) --- README.md | 6 ++++-- test/tests/file-system.js | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dc3637be7..3df506d47 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ app.use(middleware(compiler, { app.listen(3000, () => console.log('Example app listening on port 3000!')) ``` - ## Options The middleware accepts an `options` Object. The following is a property reference @@ -238,7 +237,10 @@ 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. This can be done simply by using `path.join`. +**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 diff --git a/test/tests/file-system.js b/test/tests/file-system.js index 3ad20192e..649f92b00 100644 --- a/test/tests/file-system.js +++ b/test/tests/file-system.js @@ -61,6 +61,7 @@ describe('FileSystem', () => { 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;