项目原始demo,不改动
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.

README.md 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. ## Configuration:
  14. The plugin can receive the following options:
  15. * assetProcessors: An Array of objects that describe asset processors:
  16. * regExp: A regular expression to match the asset name that the processor handles.
  17. * 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.
  18. * 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:
  19. * `compilation.optimize-chunk-assets`
  20. * `compilation.optimize-assets`
  21. * `emit`
  22. * onStart: A function with the signature of ``` function(assets, assetsAndProcessors, webpackCompilationObject) ``` that will be called before the plugin starts calling the assets processors.
  23. * onEnd: A function with the signature of ``` function(error) ``` that will be called after the plugin calls all the assets processors. If no errors occurred the ``` error ``` parameter will be undefined.
  24. * canPrint: A boolean indicating if the plugin can print messages to the console, defaults to `true`.
  25. Note: An environment supporting Promises or a Promise polyfill is needed for this plugin to be used.
  26. ## Example:
  27. ``` javascript
  28. var cssnano = require('cssnano');
  29. var LastCallWebpackPlugin = require('last-call-webpack-plugin');
  30. module.exports = {
  31. module: {
  32. loaders: [
  33. { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }
  34. ]
  35. },
  36. plugins: [
  37. new ExtractTextPlugin("styles.css"),
  38. new LastCallWebpackPlugin({
  39. assetProcessors: [{
  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. onStart: () => console.log('Starting to process assets.'),
  48. onEnd: (err) => console.log(err ? 'Error: ' + err : 'Finished processing assets.'),
  49. canPrint: true
  50. })
  51. ]
  52. }
  53. ```
  54. ## Assets manipulation
  55. 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).
  56. Example:
  57. ``` javascript
  58. var cssnano = require('cssnano');
  59. var LastCallWebpackPlugin = require('last-call-webpack-plugin');
  60. module.exports = {
  61. module: {
  62. loaders: [
  63. { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }
  64. ]
  65. },
  66. plugins: [
  67. new ExtractTextPlugin("styles.css"),
  68. new LastCallWebpackPlugin({
  69. assetProcessors: [{
  70. regExp: /\.css$/,
  71. processor: (assetName, asset, assets) => {
  72. assets.setAsset(assetName + '.map', null); // Delete the <assetName>.map asset.
  73. assets.setAsset(assetName + '.log', 'All OK'); // Add the <assetName>.log asset with the content 'All OK'.
  74. return cssnano
  75. .process(asset.source())
  76. .then(r => r.css)
  77. }
  78. }],
  79. onStart: () => console.log('Starting to process assets.'),
  80. onEnd: (err) => console.log(err ? 'Error: ' + err : 'Finished processing assets.'),
  81. canPrint: true
  82. })
  83. ]
  84. }
  85. ```
  86. 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).
  87. ## License
  88. MIT (http://www.opensource.org/licenses/mit-license.php)