[](https://opensource.org/licenses/MIT)
npm install write-file-webpackThis is a simple webpack plugin for writing data to file. And there is a similar plugin for webpack v4.
npm install --save-dev write-file-webpack``javascript
const WriteToFilePlugin = require('write-file-webpack');
module.exports = {
...
plugins: [
new WriteToFilePlugin({
filename: 'path/to/write/file',
data: 'console.log("write to file")'
})
]
...
}
`
`javascript
const WriteToFilePlugin = require('write-file-webpack');
module.exports = {
...
plugins: [
new WriteToFilePlugin({
filename: 'path/to/write/file',
data: function () {
return "console.log('write to file')"
}
})
]
...
}
`
If the data is provided as a function, we can do more operations than just simply returning the data to be written. For example, if we wanna write to a file parts of an exsited file say package.json, and more specifically, removing the dependencies and devDependencies items, with write-file-webpack we can do this:
`javascript
const WriteToFilePlugin = require('write-file-webpack');
const config = require('./package.json');
module.exports = {
...
plugins: [
new WriteToFilePlugin({
filename: 'path/to/write/package.json',
data: function () {
return JSON.stringify({
...config,
dependencies: undefined,
devDependencies: undefined,
});
}
})
]
...
}
`copy-webpack-plugin
Of course, if we want to copy the whole content of a existed file, there is webpack plugin called .
and webpack v3Options
- filename (required)
- data (required)
- override
, if set to false, no data will be written to an exsited file
- encoding
- mode
- flag
For more information about
encoding, mode, and flag` please refer to node writeFile.