assets-webpack-plugin
Advanced tools
Comparing version 4.0.0 to 5.0.0
88
index.js
@@ -1,2 +0,4 @@ | ||
var merge = require('lodash.merge') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var _ = require('lodash') | ||
@@ -11,8 +13,13 @@ var getAssetKind = require('./lib/getAssetKind') | ||
function AssetsWebpackPlugin (options) { | ||
this.options = merge({}, { | ||
path: '.', | ||
this.options = _.merge({}, { | ||
filename: 'webpack-assets.json', | ||
prettyPrint: false, | ||
update: false, | ||
fullPath: true | ||
fullPath: true, | ||
manifestFirst: true, | ||
useCompilerPath: false, | ||
fileTypes: ['js', 'css'], | ||
includeAllFileTypes: true, | ||
keepInMemory: false, | ||
integrity: false | ||
}, options) | ||
@@ -23,3 +30,2 @@ this.writer = createQueuedWriter(createOutputWriter(this.options)) | ||
AssetsWebpackPlugin.prototype = { | ||
constructor: AssetsWebpackPlugin, | ||
@@ -30,2 +36,5 @@ | ||
self.options.path = path.resolve(self.options.useCompilerPath | ||
? (compiler.options.output.path || '.') : (self.options.path || '.')) | ||
var afterEmit = (compilation, callback) => { | ||
@@ -54,11 +63,29 @@ var options = compiler.options | ||
// } | ||
var assetsByChunkName = stats.assetsByChunkName | ||
var output = Object.keys(assetsByChunkName).reduce(function (chunkMap, chunkName) { | ||
var assets = assetsByChunkName[chunkName] | ||
var seenAssets = {} | ||
var chunks | ||
if (self.options.entrypoints) { | ||
chunks = Object.keys(stats.entrypoints) | ||
} else { | ||
chunks = Object.keys(stats.assetsByChunkName) | ||
chunks.push('') // push "unamed" chunk | ||
} | ||
var output = chunks.reduce(function (chunkMap, chunkName) { | ||
var assets | ||
if (self.options.entrypoints) { | ||
assets = stats.entrypoints[chunkName].assets | ||
} else { | ||
assets = chunkName ? stats.assetsByChunkName[chunkName] : stats.assets | ||
} | ||
if (!Array.isArray(assets)) { | ||
assets = [assets] | ||
} | ||
chunkMap[chunkName] = assets.reduce(function (typeMap, asset) { | ||
if (isHMRUpdate(options, asset) || isSourceMap(options, asset)) { | ||
var added = false | ||
var typeMap = assets.reduce(function (typeMap, obj) { | ||
var asset = obj.name || obj | ||
if (isHMRUpdate(options, asset) || isSourceMap(options, asset) || (!chunkName && seenAssets[asset])) { | ||
return typeMap | ||
@@ -68,7 +95,30 @@ } | ||
var typeName = getAssetKind(options, asset) | ||
typeMap[typeName] = assetPath + asset | ||
if (self.options.includeAllFileTypes || self.options.fileTypes.includes(typeName)) { | ||
var combinedPath = assetPath && assetPath.slice(-1) !== '/' ? `${assetPath}/${asset}` : assetPath + asset | ||
var type = typeof typeMap[typeName] | ||
var compilationAsset = compilation.assets[asset] | ||
var integrity = compilationAsset && compilationAsset.integrity | ||
if (type === 'undefined') { | ||
typeMap[typeName] = combinedPath | ||
if (self.options.integrity && integrity) { | ||
typeMap[typeName + 'Integrity'] = integrity | ||
} | ||
} else { | ||
if (type === 'string') { | ||
typeMap[typeName] = [typeMap[typeName]] | ||
} | ||
typeMap[typeName].push(combinedPath) | ||
} | ||
added = true | ||
seenAssets[asset] = true | ||
} | ||
return typeMap | ||
}, {}) | ||
if (added) { | ||
chunkMap[chunkName] = typeMap | ||
} | ||
return chunkMap | ||
@@ -81,3 +131,7 @@ }, {}) | ||
if (manifestEntry) { | ||
var manifestAssetKey = manifestEntry.js.substr(assetPath.length) | ||
var js = manifestEntry.js | ||
if (!Array.isArray(js)) { | ||
js = [js] | ||
} | ||
var manifestAssetKey = js[js.length - 1].substr(assetPath.length) | ||
var parentSource = compilation.assets[manifestAssetKey] | ||
@@ -88,3 +142,2 @@ var entryText = parentSource.source() | ||
} | ||
// use _value if the uglify plugin was applied | ||
manifestEntry.text = entryText | ||
@@ -98,3 +151,8 @@ } | ||
self.writer(output, function (err) { | ||
if (!compiler.outputFileSystem.readFile) { | ||
compiler.outputFileSystem.readFile = fs.readFile.bind(fs) | ||
compiler.outputFileSystem.join = path.join.bind(path) | ||
} | ||
self.writer(compiler.outputFileSystem, output, function (err) { | ||
if (err) { | ||
@@ -108,3 +166,3 @@ compilation.errors.push(err) | ||
if (compiler.hooks) { | ||
var plugin = {name: 'AssetsWebpackPlugin'} | ||
var plugin = { name: 'AssetsWebpackPlugin' } | ||
@@ -111,0 +169,0 @@ compiler.hooks.afterEmit.tapAsync(plugin, afterEmit) |
{ | ||
"name": "assets-webpack-plugin", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Emits a json file with assets paths", | ||
"main": "index.js", | ||
"main": "dist/index.js", | ||
"engines": { | ||
"node": ">=10.x.x" | ||
}, | ||
"scripts": { | ||
"lint": "standard --verbose | snazzy", | ||
"test": "mocha test" | ||
"fix-lint": "standard --fix", | ||
"test": "mocha test", | ||
"build": "babel index.js --out-dir dist && babel lib --out-dir dist/lib", | ||
"prepublish": "npm run build" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/sporto/assets-webpack-plugin.git" | ||
"url": "https://github.com/ztoben/assets-webpack-plugin.git" | ||
}, | ||
@@ -24,23 +30,29 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/sporto/assets-webpack-plugin/issues" | ||
"url": "https://github.com/ztoben/assets-webpack-plugin/issues" | ||
}, | ||
"homepage": "https://github.com/sporto/assets-webpack-plugin", | ||
"homepage": "https://github.com/ztoben/assets-webpack-plugin", | ||
"files": [ | ||
"index.js", | ||
"dist/" | ||
], | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"css-loader": "^0.28.11", | ||
"lodash": "^3.9.3", | ||
"mocha": "^5.2.0", | ||
"rimraf": "^2.6.2", | ||
"snazzy": "^7.1.1", | ||
"standard": "^11.0.1", | ||
"style-loader": "^0.21.0", | ||
"webpack": "^4.8.3" | ||
"babel-cli": "6.26.0", | ||
"babel-core": "6.26.3", | ||
"babel-preset-env": "1.7.0", | ||
"chai": "4.2.0", | ||
"css-loader": "3.5.3", | ||
"mini-css-extract-plugin": "0.9.0", | ||
"mocha": "8.0.1", | ||
"rimraf": "3.0.2", | ||
"snazzy": "8.0.0", | ||
"standard": "14.3.4", | ||
"style-loader": "1.2.1", | ||
"webpack": "4.43.0" | ||
}, | ||
"dependencies": { | ||
"camelcase": "^5.0.0", | ||
"escape-string-regexp": "^1.0.3", | ||
"lodash.assign": "^4.2.0", | ||
"lodash.merge": "^4.6.1", | ||
"mkdirp": "^0.5.1" | ||
"camelcase": "6.0.0", | ||
"escape-string-regexp": "4.0.0", | ||
"lodash": "4.17.15", | ||
"mkdirp": "1.0.4" | ||
} | ||
} |
103
readme.md
# assets-webpack-plugin | ||
[![Build Status](https://travis-ci.org/kossnocorp/assets-webpack-plugin.svg?branch=master)](https://travis-ci.org/kossnocorp/assets-webpack-plugin) [![Build status](https://ci.appveyor.com/api/projects/status/qmvi3h6lk0xu8833?svg=true)](https://ci.appveyor.com/project/kossnocorp/assets-webpack-plugin) | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | ||
[![Build Status](https://travis-ci.org/ztoben/assets-webpack-plugin.svg?branch=master)](https://travis-ci.org/ztoben/assets-webpack-plugin) | ||
[![Build status](https://ci.appveyor.com/api/projects/status/sbchndv12vk45mo3?svg=true)](https://ci.appveyor.com/project/ztoben/assets-webpack-plugin) | ||
[![NPM](https://nodei.co/npm/assets-webpack-plugin.png)](https://www.npmjs.com/package/assets-webpack-plugin) | ||
Webpack plugin that emits a json file with assets paths. | ||
@@ -16,5 +21,5 @@ | ||
- [Change Log](./CHANGELOG.md) | ||
- [Change Log](./changelog.md) | ||
- [Contributing Guidelines](./CONTRIBUTING.md) | ||
- [Contributing Guidelines](./contributing.md) | ||
@@ -152,2 +157,13 @@ ## Why Is This Useful? | ||
#### `manifestFirst` | ||
Optional. `false` by default. | ||
Orders the assets output so that manifest is the first entry. This is useful for cases where script tags are generated | ||
from the assets json output, and order of import is important. | ||
```js | ||
new AssetsPlugin({manifestFirst: true}) | ||
``` | ||
#### `path` | ||
@@ -157,3 +173,3 @@ | ||
Path where to save the created JSON file. | ||
Path where to save the created JSON file. Will default to the highest level of the project unless useCompilerPath is specified. | ||
@@ -164,2 +180,10 @@ ```js | ||
#### `useCompilerPath` | ||
```js | ||
new AssetsPlugin({useCompilerPath: true}) | ||
``` | ||
Will override the path to use the compiler output path set in your webpack config. | ||
#### `prettyPrint` | ||
@@ -211,2 +235,69 @@ | ||
#### `includeAllFileTypes` | ||
Optional. `true` by default. | ||
When set false, falls back to the `fileTypes` option array to decide which file types to include in the assets file. | ||
```js | ||
new AssetsPlugin({includeAllFileTypes: false}) | ||
``` | ||
#### `fileTypes` | ||
Optional. ```['js', 'css']``` by default. | ||
When set and `includeAllFileTypes` is set false, only assets matching these types will be included in the assets file. | ||
```js | ||
new AssetsPlugin({fileTypes: ['js', 'jpg']}) | ||
``` | ||
#### `keepInMemory` | ||
Optional. `false` by default. | ||
When set the assets file will only be generated in memory while running `webpack-dev-server` and not written to disk. | ||
```js | ||
new AssetsPlugin({keepInMemory: true}) | ||
``` | ||
#### `integrity` | ||
Optional. `false` by default. | ||
When set the output from [webpack-subresource-integrity](https://github.com/waysact/webpack-subresource-integrity) is included in the assets file. | ||
Please make sure you have `webpack-subresource-integrity` installed and included in your webpack plugins. | ||
```js | ||
new AssetsPlugin({integrity: true}) | ||
``` | ||
Output will now look like this: | ||
```json | ||
{ | ||
"main": { | ||
"js": "/bundle.js", | ||
"jsIntegrity": "sha256-ANGwtktWN96nvBI/cjekdTvd0Dwf7SciIFTQ2lpTxGc= sha384-Ly439pF3K+J8hnhk1BEcjKnv1R9BApFYVIVJvr64PcgBjdT4N7hfPzQynItHwcaO" | ||
}, | ||
"vendors~main": { | ||
"js": "/1.bundle.js", | ||
"jsIntegrity": "sha256-yqNi1hgeAdkXVOORgmVMeX+cbuXikoj6I8qWZjPegsA= sha384-4X75tnsGDwnwL5kBUPsx2ko9DeWy0xM8BcDQdoR185yho+OnxjjPXl2wCdebLWTG" | ||
} | ||
} | ||
``` | ||
#### `entrypoints` | ||
Optional. `false` by default. | ||
If the 'entrypoints' option is given, the output will be limited to the entrypoints and the chunks associated with them. | ||
```js | ||
new AssetsPlugin({entrypoints: true}) | ||
``` | ||
### Using in multi-compiler mode | ||
@@ -269,2 +360,6 @@ | ||
### Using this with ASP.NET Core | ||
You can use this with ASP.NET Core via the [WebpackTag](https://d.sb/webpacktag) library. | ||
## Test | ||
@@ -271,0 +366,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
4
364
35753
12
14
553
3
1
+ Addedlodash@4.17.15
+ Addedcamelcase@6.0.0(transitive)
+ Addedescape-string-regexp@4.0.0(transitive)
+ Addedlodash@4.17.15(transitive)
+ Addedmkdirp@1.0.4(transitive)
- Removedlodash.assign@^4.2.0
- Removedlodash.merge@^4.6.1
- Removedcamelcase@5.3.1(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedlodash.assign@4.2.0(transitive)
- Removedlodash.merge@4.6.2(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
Updatedcamelcase@6.0.0
Updatedescape-string-regexp@4.0.0
Updatedmkdirp@1.0.4