wrapper-webpack-plugin
Advanced tools
Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "wrapper-webpack-plugin", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Wraps output files (chunks) with custom text or code.", | ||
@@ -9,3 +9,3 @@ "main": "wrapper-webpack-plugin.js", | ||
"dependencies": { | ||
"webpack-sources": "^1.0.1" | ||
"webpack-sources": "^1.1.0" | ||
}, | ||
@@ -12,0 +12,0 @@ "files": [ |
@@ -10,3 +10,4 @@ # A webpack plugin that wraps output files (chunks) with custom text or code. | ||
Works with *all* current versions of webpack, including 1, 2, and 3. | ||
Newest version works only with webpack 4.\ | ||
For webpack 3, 2, or 1 use [version 1](https://github.com/levp/wrapper-webpack-plugin/tree/v1) of the plugin with `npm i -D wrapper-webpack-plugin@1` | ||
@@ -37,3 +38,3 @@ ## Usage | ||
```javascript | ||
var WrapperPlugin = require('wrapper-webpack-plugin'); | ||
const WrapperPlugin = require('wrapper-webpack-plugin'); | ||
@@ -59,3 +60,3 @@ module.exports = { | ||
```javascript | ||
var WrapperPlugin = require('wrapper-webpack-plugin'); | ||
const WrapperPlugin = require('wrapper-webpack-plugin'); | ||
@@ -77,2 +78,28 @@ module.exports = { | ||
Accessing file name, build hash, and chunk hash at runtime. | ||
```javascript | ||
const WrapperPlugin = require('wrapper-webpack-plugin'); | ||
module.exports = { | ||
// other webpack config here | ||
output: { | ||
filename: '[name].[chunkhash].js' | ||
}, | ||
plugins: [ | ||
new WrapperPlugin({ | ||
header: `(function (FILE_NAME, BUILD_HASH, CHUNK_HASH) {`, | ||
footer(fileName, args) { | ||
return `})('${fileName}', '${args.hash}', '${args.chunkhash}');`; | ||
// note: args.hash and args.chunkhash correspond to the [hash] and [chunkhash] | ||
// placeholders you can specify in the output.filename option. | ||
} | ||
}) | ||
] | ||
}; | ||
``` | ||
## Example configuration #4 | ||
Keeping header in a separate file: | ||
@@ -89,6 +116,6 @@ | ||
```javascript | ||
var fs = require('fs'); | ||
var WrapperPlugin = require('wrapper-webpack-plugin'); | ||
const fs = require('fs'); | ||
WrapperPlugin = require('wrapper-webpack-plugin'); | ||
var headerDoc = fs.readFileSync('./header.js', 'utf8'); | ||
const headerDoc = fs.readFileSync('./header.js', 'utf8'); | ||
@@ -106,3 +133,3 @@ module.exports = { | ||
## Example configuration #4 | ||
## Example configuration #5 | ||
@@ -112,7 +139,7 @@ A slightly more complex example using `lodash` templates: | ||
```javascript | ||
var WrapperPlugin = require('wrapper-webpack-plugin'); | ||
var template = require('lodash.template'); | ||
var pkg = require('./package.json'); | ||
const WrapperPlugin = require('wrapper-webpack-plugin'); | ||
const template = require('lodash.template'); | ||
const pkg = require('./package.json'); | ||
var tpl = '/*! <%= name %> v<%= version %> | <%= author %> */\n'; | ||
const tpl = '/*! <%= name %> v<%= version %> | <%= author %> */\n'; | ||
@@ -119,0 +146,0 @@ module.exports = { |
'use strict'; | ||
var ConcatSource = require("webpack-sources").ConcatSource; | ||
var ModuleFilenameHelpers = require("webpack/lib/ModuleFilenameHelpers"); | ||
const ConcatSource = require("webpack-sources").ConcatSource; | ||
const ModuleFilenameHelpers = require("webpack/lib/ModuleFilenameHelpers"); | ||
/** | ||
* @param args | ||
* @param {string|function} [args.header] | ||
* @param {string|function} [args.footer] | ||
* @param {string|RegExp} [args.test] | ||
* @constructor | ||
*/ | ||
function WrapperPlugin(args) { | ||
if (typeof args !== 'object') { | ||
throw new TypeError('Argument "args" must be an object.'); | ||
class WrapperPlugin { | ||
constructor(args) { | ||
if (typeof args !== 'object') { | ||
throw new TypeError('Argument "args" must be an object.'); | ||
} | ||
this.header = args.hasOwnProperty('header') ? args.header : ''; | ||
this.footer = args.hasOwnProperty('footer') ? args.footer : ''; | ||
this.test = args.hasOwnProperty('test') ? args.test : ''; | ||
} | ||
this.header = args.hasOwnProperty('header') ? args.header : ''; | ||
this.footer = args.hasOwnProperty('footer') ? args.footer : ''; | ||
this.test = args.hasOwnProperty('test') ? args.test : ''; | ||
} | ||
apply(compiler) { | ||
const header = this.header; | ||
const footer = this.footer; | ||
const tester = {test: this.test}; | ||
function apply(compiler) { | ||
var header = this.header; | ||
var footer = this.footer; | ||
var tester = {test: this.test}; | ||
compiler.hooks.compilation.tap('WrapperPlugin', (compilation) => { | ||
compilation.hooks.optimizeChunkAssets.tapAsync('WrapperPlugin', (chunks, done) => { | ||
wrapChunks(compilation, chunks, footer, header); | ||
done(); | ||
}) | ||
}); | ||
compiler.plugin('compilation', function (compilation) { | ||
compilation.plugin('optimize-chunk-assets', function (chunks, done) { | ||
wrapChunks(compilation, chunks, footer, header); | ||
done(); | ||
}) | ||
}); | ||
function wrapFile(compilation, fileName, chunkHash) { | ||
const headerContent = (typeof header === 'function') ? header(fileName, chunkHash) : header; | ||
const footerContent = (typeof footer === 'function') ? footer(fileName, chunkHash) : footer; | ||
function wrapFile(compilation, fileName) { | ||
var headerContent = (typeof header === 'function') ? header(fileName) : header; | ||
var footerContent = (typeof footer === 'function') ? footer(fileName) : footer; | ||
compilation.assets[fileName] = new ConcatSource( | ||
String(headerContent), | ||
compilation.assets[fileName], | ||
String(footerContent)); | ||
} | ||
compilation.assets[fileName] = new ConcatSource( | ||
String(headerContent), | ||
compilation.assets[fileName], | ||
String(footerContent)); | ||
} | ||
function wrapChunks(compilation, chunks) { | ||
chunks.forEach(function (chunk) { | ||
chunk.files.forEach(function (fileName) { | ||
if (ModuleFilenameHelpers.matchObject(tester, fileName)) { | ||
wrapFile(compilation, fileName); | ||
} | ||
function wrapChunks(compilation, chunks) { | ||
chunks.forEach(chunk => { | ||
const args = { | ||
hash: compilation.hash, | ||
chunkhash: chunk.hash | ||
}; | ||
chunk.files.forEach(fileName => { | ||
if (ModuleFilenameHelpers.matchObject(tester, fileName)) { | ||
wrapFile(compilation, fileName, args); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
Object.defineProperty(WrapperPlugin.prototype, 'apply', { | ||
value: apply, | ||
enumerable: false | ||
}); | ||
module.exports = WrapperPlugin; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6272
159
46
Updatedwebpack-sources@^1.1.0