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
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
```

Comment thread
hinell marked this conversation as resolved.
## 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') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just questions, can you find place where we use fs.join? Looks like we should remove this/deprecated from code base.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not used by the webpack-middleware but rather by wepback instead. Here it is as a precaution to avoid digging up webpack's erros. If you attempt to provide fs without join() webpack will complain about it when accessing webpack.outputFileSystem.

I can switch to Russian to explain more if you like.

@alexander-akait alexander-akait Feb 18, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can switch to Russian to explain more if you like.

No need

It is not used by the webpack-middleware but rather by wepback instead. Here it is as a precaution to avoid digging up webpack's erros. If you attempt to provide fs without join() webpack will complain about it when accessing webpack.outputFileSystem.

I think we should open issue in webpack and remove all fs.join in webpack@5, join is not part of fs and looks very dirty and misleading

Can you open issue?

@hinell hinell Feb 18, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I got no time for that, sorry. May be next time.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hinell i mean just create issue in webpack repo and all, i am from mobile and it is hard for me right now

// 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) => {
Comment thread
hinell marked this conversation as resolved.
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