From bd345861f5545a05778e988c9c0af9e220cf58dc Mon Sep 17 00:00:00 2001 From: Dawid Rusnak Date: Mon, 12 Jun 2017 14:53:45 +0200 Subject: [PATCH] Fix linting, handle asynchronous virtual files --- index.js | 17 +++++++++++---- .../cases/contents-async/expected/file.txt | 1 + .../integration/cases/contents-async/index.js | 4 ++++ .../cases/contents-async/webpack.config.js | 21 +++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 test/integration/cases/contents-async/expected/file.txt create mode 100644 test/integration/cases/contents-async/index.js create mode 100644 test/integration/cases/contents-async/webpack.config.js diff --git a/index.js b/index.js index 2c95795..4c4bf15 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ class VirtualModulePlugin { contents = JSON.stringify(this.options.contents); } if (typeof this.options.contents === 'function') { - //call then function must be return string + // call then function must be return string contents = this.options.contents(); } @@ -39,10 +39,19 @@ class VirtualModulePlugin { if (!modulePath) { modulePath = this.join(compiler.context, moduleName); } - VirtualModulePlugin.populateFilesystem({ fs, modulePath, contents, ctime }); - if (cb) { - cb(); + + const resolve = (data) => { + VirtualModulePlugin.populateFilesystem({ fs, modulePath, contents: data, ctime }); + }; + + if (!cb) { + resolve(contents); + return; } + + Promise.resolve(contents) + .then(resolve) + .then(cb); } if (!compiler.resolvers.normal) { diff --git a/test/integration/cases/contents-async/expected/file.txt b/test/integration/cases/contents-async/expected/file.txt new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/test/integration/cases/contents-async/expected/file.txt @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/test/integration/cases/contents-async/index.js b/test/integration/cases/contents-async/index.js new file mode 100644 index 0000000..f396679 --- /dev/null +++ b/test/integration/cases/contents-async/index.js @@ -0,0 +1,4 @@ +'use strict'; + +// eslint-disable-next-line import/no-unresolved +require('./a.txt'); diff --git a/test/integration/cases/contents-async/webpack.config.js b/test/integration/cases/contents-async/webpack.config.js new file mode 100644 index 0000000..5c364a1 --- /dev/null +++ b/test/integration/cases/contents-async/webpack.config.js @@ -0,0 +1,21 @@ +'use strict'; + +const ExtractTextPlugin = require('extract-text-webpack-plugin'); +const VirtualModulePlugin = require('../../../../index'); + +function contents() { + return Promise.resolve('a'); +} + +module.exports = function webpackConfig() { + return { + entry: './index', + plugins: [ + new VirtualModulePlugin({ + moduleName: './a.txt', + contents, + }), + new ExtractTextPlugin('file.txt'), + ], + }; +};