diff options
Diffstat (limited to 'node_modules/last-call-webpack-plugin')
| -rw-r--r-- | node_modules/last-call-webpack-plugin/LICENSE | 21 | ||||
| -rw-r--r-- | node_modules/last-call-webpack-plugin/README.md | 104 | ||||
| -rw-r--r-- | node_modules/last-call-webpack-plugin/package.json | 27 | ||||
| -rw-r--r-- | node_modules/last-call-webpack-plugin/src/index.js | 207 |
4 files changed, 359 insertions, 0 deletions
diff --git a/node_modules/last-call-webpack-plugin/LICENSE b/node_modules/last-call-webpack-plugin/LICENSE new file mode 100644 index 00000000..1973738e --- /dev/null +++ b/node_modules/last-call-webpack-plugin/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Nuno Rodrigues + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/node_modules/last-call-webpack-plugin/README.md b/node_modules/last-call-webpack-plugin/README.md new file mode 100644 index 00000000..b462603f --- /dev/null +++ b/node_modules/last-call-webpack-plugin/README.md @@ -0,0 +1,104 @@ +# Last Call Webpack Plugin + +A Webpack plugin that allows you to transform \ modify assets just before Webpack emits them. + +## What does the plugin do? + +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). + +It can be used for example to: +* Prefix a ``` /* Author: John Doe */ ``` comment on all the .js files Webpack generates. +* Run some final optimization on all .css files Webpack generates. + +## Installation: + +Using npm: +```shell +$ npm install --save-dev last-call-webpack-plugin +``` + +> :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. + +## Configuration: + +The plugin can receive the following options: +* assetProcessors: An Array of objects that describe asset processors: + * regExp: A regular expression to match the asset name that the processor handles. + * 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. + * 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: + * `compilation.optimize-chunk-assets` + * `compilation.optimize-assets` + * `emit` +* canPrint: A boolean indicating if the plugin can print messages to the console, defaults to `true`. + +Note: An environment supporting Promises or a Promise polyfill is needed for this plugin to be used. + +## Example: + +``` javascript +var cssnano = require('cssnano'); +var LastCallWebpackPlugin = require('last-call-webpack-plugin'); +module.exports = { + module: { + loaders: [ + { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") } + ] + }, + plugins: [ + new ExtractTextPlugin("styles.css"), + new LastCallWebpackPlugin({ + assetProcessors: [ + { + regExp: /\.js$/, + processor: (assetName, asset) => Promise.resolve('// Author: John Doe \n' + asset.source()) + }, { + regExp: /\.css$/, + processor: (assetName, asset) => cssnano.process(asset.source()) + .then(r => r.css) + } + ], + canPrint: true + }) + ] +} +``` + +## Assets manipulation + +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). + +Example: + +``` javascript +var cssnano = require('cssnano'); +var LastCallWebpackPlugin = require('last-call-webpack-plugin'); +module.exports = { + module: { + loaders: [ + { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") } + ] + }, + plugins: [ + new ExtractTextPlugin("styles.css"), + new LastCallWebpackPlugin({ + assetProcessors: [{ + regExp: /\.css$/, + processor: (assetName, asset, assets) => { + assets.setAsset(assetName + '.map', null); // Delete the <assetName>.map asset. + assets.setAsset(assetName + '.log', 'All OK'); // Add the <assetName>.log asset with the content 'All OK'. + return cssnano + .process(asset.source()) + .then(r => r.css) + } + }], + canPrint: true + }) + ] +} +``` + +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). + +## License + +MIT (http://www.opensource.org/licenses/mit-license.php)
\ No newline at end of file diff --git a/node_modules/last-call-webpack-plugin/package.json b/node_modules/last-call-webpack-plugin/package.json new file mode 100644 index 00000000..ec4ccfdd --- /dev/null +++ b/node_modules/last-call-webpack-plugin/package.json @@ -0,0 +1,27 @@ +{ + "name": "last-call-webpack-plugin", + "version": "3.0.0", + "author": "Nuno Rodrigues", + "description": "A Webpack plugin that allows to transform \\ modify assets just before Webpack emits them.", + "dependencies": { + "lodash": "^4.17.5", + "webpack-sources": "^1.1.0" + }, + "main": "src/index.js", + "homepage": "http://github.com/NMFR/last-call-webpack-plugin", + "repository": { + "type": "git", + "url": "http://github.com/NMFR/last-call-webpack-plugin.git" + }, + "keywords": [ + "transform", + "modify", + "manipulate", + "optimize", + "prefix", + "sufix", + "webpack", + "assets" + ], + "license": "MIT" +} diff --git a/node_modules/last-call-webpack-plugin/src/index.js b/node_modules/last-call-webpack-plugin/src/index.js new file mode 100644 index 00000000..252f5e35 --- /dev/null +++ b/node_modules/last-call-webpack-plugin/src/index.js @@ -0,0 +1,207 @@ +const assign = require('lodash/assign'); +const each = require('lodash/each'); +const find = require('lodash/find'); +const isArray = require('lodash/isArray'); +const isFunction = require('lodash/isFunction'); +const isRegExp = require('lodash/isRegExp'); +const keys = require('lodash/keys'); +const values = require('lodash/values'); +const webpackSources = require('webpack-sources'); + +const PHASES = { + OPTIMIZE_CHUNK_ASSETS: 'compilation.optimize-chunk-assets', + OPTIMIZE_ASSETS: 'compilation.optimize-assets', + EMIT: 'emit' +}; +const PHASE_LIST = values(PHASES); + +function ensureAssetProcessor(processor, index) { + if (!processor) { + throw new Error('LastCallWebpackPlugin Error: invalid options.assetProcessors[' + String(index) + '] (must be an object).'); + } + if (!isRegExp(processor.regExp)) { + throw new Error('LastCallWebpackPlugin Error: invalid options.assetProcessors[' + String(index) + '].regExp (must be an regular expression).'); + } + if (!isFunction(processor.processor)) { + throw new Error('LastCallWebpackPlugin Error: invalid options.assetProcessors[' + String(index) + '].processor (must be a function).'); + } + if (processor.phase === undefined) { + processor.phase = PHASES.OPTIMIZE_ASSETS; + } + if (!find(PHASE_LIST, function(p) { return p === processor.phase; })) { + throw new Error('LastCallWebpackPlugin Error: invalid options.assetProcessors[' + String(index) + '].phase (must be on of: ' + PHASES.join(', ') + ').'); + } +} + +class LastCallWebpackPlugin { + constructor(options) { + this.pluginDescriptor = this.buildPluginDescriptor(); + + this.options = assign( + { + assetProcessors: [], + canPrint: true + }, + options || {} + ); + + this.phaseAssetProcessors = {}; + each(PHASE_LIST, (phase) => { + this.phaseAssetProcessors[phase] = []; + }); + + if (!isArray(this.options.assetProcessors)) { + throw new Error('LastCallWebpackPlugin Error: invalid options.assetProcessors (must be an Array).'); + } + each(this.options.assetProcessors, (processor, index) => { + ensureAssetProcessor(processor, index); + this.phaseAssetProcessors[processor.phase].push(processor); + }); + + this.resetInternalState(); + } + + buildPluginDescriptor() { + return { name: 'LastCallWebpackPlugin' }; + } + + resetInternalState() { + this.deleteAssetsMap = {}; + } + + setAsset(assetName, assetValue, immediate, compilation) { + if (assetName) { + if (assetValue === null) { + this.deleteAssetsMap[assetName] = true; + if (immediate) { + delete compilation.assets[assetName]; + } + } else { + if (assetValue !== undefined) { + compilation.assets[assetName] = this.createAsset(assetValue, compilation.assets[assetName]); + } + } + } + } + + deleteAssets(compilation) { + if (this.deleteAssetsMap && compilation) { + each(keys(this.deleteAssetsMap), (key) => { + delete compilation.assets[key]; + }); + } + } + + print() { + if (this.options.canPrint) { + console.log.apply(console, arguments); + } + } + + createAsset(content, originalAsset) { + return new webpackSources.RawSource(content); + } + + getAssetsAndProcessors(assets, phase) { + const assetProcessors = this.phaseAssetProcessors[phase]; + const assetNames = keys(assets); + const assetsAndProcessors = []; + + each(assetNames, (assetName) => { + each(assetProcessors, (assetProcessor) => { + const regExpResult = assetProcessor.regExp.exec(assetName); + assetProcessor.regExp.lastIndex = 0; + if (regExpResult) { + const assetAndProcessor = { + assetName: assetName, + regExp: assetProcessor.regExp, + processor: assetProcessor.processor, + regExpResult: regExpResult, + }; + assetsAndProcessors.push(assetAndProcessor); + } + }); + }); + + return assetsAndProcessors; + } + + process(compilation, phase) { + const assetsAndProcessors = this.getAssetsAndProcessors(compilation.assets, phase); + if (assetsAndProcessors.length <= 0) { + return Promise.resolve(undefined); + } + + const promises = []; + + const assetsManipulationObject = { + setAsset: (assetName, assetValue, immediate) => { + this.setAsset(assetName, assetValue, immediate, compilation); + }, + getAsset: (assetName) => { + var asset = assetName && compilation.assets[assetName] && compilation.assets[assetName].source(); + return asset || undefined; + } + }; + + each(assetsAndProcessors, (assetAndProcessor) => { + const asset = compilation.assets[assetAndProcessor.assetName]; + const promise = assetAndProcessor + .processor(assetAndProcessor.assetName, asset, assetsManipulationObject) + .then((result) => { + if (result !== undefined) { + this.setAsset(assetAndProcessor.assetName, result, false, compilation); + } + }); + promises.push(promise); + }); + + return Promise.all(promises); + } + + apply(compiler) { + const hasOptimizeChunkAssetsProcessors = + this.phaseAssetProcessors[PHASES.OPTIMIZE_CHUNK_ASSETS].length > 0; + const hasOptimizeAssetsProcessors = + this.phaseAssetProcessors[PHASES.OPTIMIZE_ASSETS].length > 0; + const hasEmitProcessors = + this.phaseAssetProcessors[PHASES.EMIT].length > 0; + + compiler.hooks.compilation.tap( + this.pluginDescriptor, + (compilation, params) => { + this.resetInternalState(); + + if (hasOptimizeChunkAssetsProcessors) { + compilation.hooks.optimizeChunkAssets.tapPromise( + this.pluginDescriptor, + chunks => this.process(compilation, PHASES.OPTIMIZE_CHUNK_ASSETS, { chunks: chunks }) + ); + } + + if (hasOptimizeAssetsProcessors) { + compilation.hooks.optimizeAssets.tapPromise( + this.pluginDescriptor, + assets => this.process(compilation, PHASES.OPTIMIZE_ASSETS, { assets: assets }) + ); + } + } + ); + compiler.hooks.emit.tapPromise( + this.pluginDescriptor, + compilation => + ( + hasEmitProcessors ? + this.process(compilation, PHASES.EMIT) : + Promise.resolve(undefined) + ) + .then((result) => { + this.deleteAssets(compilation); + return result; + }) + ); + } +} +LastCallWebpackPlugin.PHASES = PHASES; + +module.exports = LastCallWebpackPlugin; |
