You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.7 KiB

4 years ago
  1. # Last Call Webpack Plugin
  2. A Webpack plugin that allows you to transform \ modify assets just before Webpack emits them.
  3. ## What does the plugin do?
  4. It allows you to transform \ modify Webpack assets just before Webpack emits them (writes them to files or memory in case you are using something like Webpack dev server).
  5. It can be used for example to:
  6. * Prefix a ``` /* Author: John Doe */ ``` comment on all the .js files Webpack generates.
  7. * Run some final optimization on all .css files Webpack generates.
  8. ## Installation:
  9. Using npm:
  10. ```shell
  11. $ npm install --save-dev last-call-webpack-plugin
  12. ```
  13. > :warning: For webpack v3 or below please use `last-call-webpack-plugin@v2.1.2`. The `last-call-webpack-plugin@v3.0.0` version and above supports webpack v4.
  14. ## Configuration:
  15. The plugin can receive the following options:
  16. * assetProcessors: An Array of objects that describe asset processors:
  17. * regExp: A regular expression to match the asset name that the processor handles.
  18. * processor: A function with the signature of ``` function(assetName, webpackAssetObject, assets) ``` that returns a Promise. If the Promise returns a result this result will replace the assets content.
  19. * phase: The webpack compilation phase that at which the processor should be called. Default value is `compilation.optimize-assets`. Can be one of the following values:
  20. * `compilation.optimize-chunk-assets`
  21. * `compilation.optimize-assets`
  22. * `emit`
  23. * canPrint: A boolean indicating if the plugin can print messages to the console, defaults to `true`.
  24. Note: An environment supporting Promises or a Promise polyfill is needed for this plugin to be used.
  25. ## Example:
  26. ``` javascript
  27. var cssnano = require('cssnano');
  28. var LastCallWebpackPlugin = require('last-call-webpack-plugin');
  29. module.exports = {
  30. module: {
  31. loaders: [
  32. { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }
  33. ]
  34. },
  35. plugins: [
  36. new ExtractTextPlugin("styles.css"),
  37. new LastCallWebpackPlugin({
  38. assetProcessors: [
  39. {
  40. regExp: /\.js$/,
  41. processor: (assetName, asset) => Promise.resolve('// Author: John Doe \n' + asset.source())
  42. }, {
  43. regExp: /\.css$/,
  44. processor: (assetName, asset) => cssnano.process(asset.source())
  45. .then(r => r.css)
  46. }
  47. ],
  48. canPrint: true
  49. })
  50. ]
  51. }
  52. ```
  53. ## Assets manipulation
  54. The `processor` method is supplied an `assets` object that allows asset manipulation via the `setAsset(assetName, assetValue)` method. If `assetValue` is null the asset will be deleted. This object can be used to generate aditional assets (like source maps) or rename the an asset (create a new asset and delete the current one).
  55. Example:
  56. ``` javascript
  57. var cssnano = require('cssnano');
  58. var LastCallWebpackPlugin = require('last-call-webpack-plugin');
  59. module.exports = {
  60. module: {
  61. loaders: [
  62. { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }
  63. ]
  64. },
  65. plugins: [
  66. new ExtractTextPlugin("styles.css"),
  67. new LastCallWebpackPlugin({
  68. assetProcessors: [{
  69. regExp: /\.css$/,
  70. processor: (assetName, asset, assets) => {
  71. assets.setAsset(assetName + '.map', null); // Delete the <assetName>.map asset.
  72. assets.setAsset(assetName + '.log', 'All OK'); // Add the <assetName>.log asset with the content 'All OK'.
  73. return cssnano
  74. .process(asset.source())
  75. .then(r => r.css)
  76. }
  77. }],
  78. canPrint: true
  79. })
  80. ]
  81. }
  82. ```
  83. The `assets` object also has a `getAsset(assetName)` method to get the content of an asset (returns undefined in case the asset does not exist).
  84. ## License
  85. MIT (http://www.opensource.org/licenses/mit-license.php)