From 4cb1f4efe3fca1eea3b906543f7dd04f3a6740fc Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Sun, 13 Oct 2019 01:21:16 +0100 Subject: [PATCH] Treat created files as compilation assets --- index.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index e869e1d..a325dad 100644 --- a/index.js +++ b/index.js @@ -23,21 +23,18 @@ var CreateFilePlugin = (function () { this.options = options; } - function _createFile(filePath, fileName, content) { - return () => { - const fullPath = path.join(filePath, fileName); - write.sync(fullPath, content); - } - } - CreateFilePlugin.prototype.apply = function (compiler) { - const createFile = () => _createFile(this.options.path, this.options.fileName, this.options.content); + compiler.hooks.emit.tap({ name: 'CreateFileWebpack' }, compilation => { + const relativePath = path.join(this.options.path, this.options.fileName); + const targetPath = path.join(compiler.options.output.path, relativePath); - if (!!compiler.hooks) { - compiler.hooks.done.tap('CreateFileWebpack', createFile()); - } else { - compiler.plugin('done', createFile()); - } + write.sync(targetPath, this.options.content); + + compilation.assets[relativePath] = { + source: () => this.options.content, + size: () => this.options.content.length + }; + }); }; return CreateFilePlugin;