Skip to content

Commit 6d3af66

Browse files
authored
Merge pull request #41 from waysact/eslint-config-airbnb/legacy
Upgrade to eslint-config-airbnb/legacy
2 parents c3afcbd + ae0fff6 commit 6d3af66

File tree

8 files changed

+196
-113
lines changed

8 files changed

+196
-113
lines changed

.codeclimate.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
engines:
22
eslint:
33
enabled: true
4-
channel: "eslint-2"

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": "eslint-config-airbnb-es5",
2+
"extends": "eslint-config-airbnb/legacy",
33
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# webpack-subresource-integrity
22

3-
[![npm version](https://badge.fury.io/js/webpack-subresource-integrity.svg)](https://badge.fury.io/js/webpack-subresource-integrity) [![Travis Build Status](https://travis-ci.org/waysact/webpack-subresource-integrity.svg?branch=master)](https://travis-ci.org/waysact/webpack-subresource-integrity) [![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/63bydfph00sghg18/branch/master?svg=true)](https://ci.appveyor.com/project/jscheid/webpack-subresource-integrity) [![Coverage Status](https://coveralls.io/repos/github/waysact/webpack-subresource-integrity/badge.svg)](https://coveralls.io/github/waysact/webpack-subresource-integrity) [![Dependency Status](https://www.versioneye.com/user/projects/587755bd3c8039003a1c358f/badge.svg?style=flat-square)](https://www.versioneye.com/user/projects/587755bd3c8039003a1c358f) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/waysact/webpack-subresource-integrity/master/LICENSE)
3+
[![npm version](https://badge.fury.io/js/webpack-subresource-integrity.svg)](https://badge.fury.io/js/webpack-subresource-integrity) [![Travis Build Status](https://travis-ci.org/waysact/webpack-subresource-integrity.svg?branch=master)](https://travis-ci.org/waysact/webpack-subresource-integrity) [![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/63bydfph00sghg18/branch/master?svg=true)](https://ci.appveyor.com/project/jscheid/webpack-subresource-integrity) [![Coverage Status](https://coveralls.io/repos/github/waysact/webpack-subresource-integrity/badge.svg)](https://coveralls.io/github/waysact/webpack-subresource-integrity) [![Code Climate](https://codeclimate.com/github/waysact/webpack-subresource-integrity/badges/gpa.svg)](https://codeclimate.com/github/waysact/webpack-subresource-integrity) [![Dependency Status](https://www.versioneye.com/user/projects/587755bd3c8039003a1c358f/badge.svg?style=flat-square)](https://www.versioneye.com/user/projects/587755bd3c8039003a1c358f) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/waysact/webpack-subresource-integrity/master/LICENSE)
44

55
Webpack plugin for enabling Subresource Integrity.
66

index.js

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@ function makePlaceholder(id) {
1212
return '*-*-*-CHUNK-SRI-HASH-' + id + '-*-*-*';
1313
}
1414

15-
function findDepChunks(chunk, allDepChunkIds) {
16-
chunk.chunks.forEach(function forEachChunk(depChunk) {
17-
if (!allDepChunkIds[depChunk.id]) {
18-
allDepChunkIds[depChunk.id] = true;
19-
findDepChunks(depChunk, allDepChunkIds);
20-
}
21-
});
22-
}
23-
2415
function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) {
2516
this.sriPlugin = sriPlugin;
2617
this.compilation = compilation;
2718
}
2819

2920
WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply(mainTemplate) {
3021
var self = this;
22+
var allDepChunkIds = {};
23+
24+
function findDepChunks(chunk) {
25+
chunk.chunks.forEach(function forEachChunk(depChunk) {
26+
if (!allDepChunkIds[depChunk.id]) {
27+
allDepChunkIds[depChunk.id] = true;
28+
findDepChunks(depChunk);
29+
}
30+
});
31+
}
3132

3233
/*
3334
* Patch jsonp-script code to add the integrity attribute.
@@ -53,8 +54,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply(mainTemplat
5354
*/
5455
mainTemplate.plugin('local-vars', function localVarsPlugin(source, chunk) {
5556
if (chunk.chunks.length > 0) {
56-
var allDepChunkIds = {};
57-
findDepChunks(chunk, allDepChunkIds);
57+
findDepChunks(chunk);
5858

5959
return this.asString([
6060
source,
@@ -95,11 +95,7 @@ function SubresourceIntegrityPlugin(options) {
9595
enabled: true
9696
};
9797

98-
for (var key in useOptions) {
99-
if (useOptions.hasOwnProperty(key)) {
100-
this.options[key] = useOptions[key];
101-
}
102-
}
98+
Object.assign(this.options, useOptions);
10399

104100
this.emittedWarnings = {};
105101
}
@@ -120,6 +116,10 @@ SubresourceIntegrityPlugin.prototype.error = function error(compilation, message
120116
};
121117

122118
SubresourceIntegrityPlugin.prototype.validateOptions = function validateOptions(compilation) {
119+
var foundStandardHashFunc = false;
120+
var hashFuncName;
121+
var i;
122+
123123
if (this.optionsValidated) {
124124
return;
125125
}
@@ -144,9 +144,8 @@ SubresourceIntegrityPlugin.prototype.validateOptions = function validateOptions(
144144
'instead got \'' + this.options.hashFuncNames + '\'.');
145145
this.options.enabled = false;
146146
} else {
147-
var foundStandardHashFunc = false;
148-
for (var i = 0; i < this.options.hashFuncNames.length; i++) {
149-
var hashFuncName = this.options.hashFuncNames[i];
147+
for (i = 0; i < this.options.hashFuncNames.length; i += 1) {
148+
hashFuncName = this.options.hashFuncNames[i];
150149
if (typeof hashFuncName !== 'string' &&
151150
!(hashFuncName instanceof String)) {
152151
this.error(
@@ -246,6 +245,14 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
246245
compilation.plugin('after-optimize-assets', function optimizeAssetsPlugin(assets) {
247246
var hashByChunkId = {};
248247
var visitedByChunkId = {};
248+
var chunkFile;
249+
var oldSource;
250+
var magicMarker;
251+
var magicMarkerPos;
252+
var newAsset;
253+
var asset;
254+
var newSource;
255+
249256
function processChunkRecursive(chunk) {
250257
var depChunkIds = [];
251258

@@ -259,9 +266,9 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
259266
});
260267

261268
if (chunk.files.length > 0) {
262-
var chunkFile = chunk.files[0];
269+
chunkFile = chunk.files[0];
263270

264-
var oldSource = assets[chunkFile].source();
271+
oldSource = assets[chunkFile].source();
265272

266273
if (oldSource.indexOf('webpackHotUpdate') >= 0) {
267274
self.warnOnce(
@@ -271,42 +278,43 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
271278
);
272279
}
273280

274-
var newAsset = new ReplaceSource(assets[chunkFile]);
281+
newAsset = new ReplaceSource(assets[chunkFile]);
275282

276283
depChunkIds.forEach(function forEachChunk(depChunkId) {
277-
var magicMarker = makePlaceholder(depChunkId);
278-
var magicMarkerPos = oldSource.indexOf(magicMarker);
284+
magicMarker = makePlaceholder(depChunkId);
285+
magicMarkerPos = oldSource.indexOf(magicMarker);
279286
if (magicMarkerPos >= 0) {
280287
newAsset.replace(
281288
magicMarkerPos,
282-
magicMarkerPos + magicMarker.length - 1,
289+
(magicMarkerPos + magicMarker.length) - 1,
283290
hashByChunkId[depChunkId]);
284291
}
285292
});
286293

294+
// eslint-disable-next-line no-param-reassign
287295
assets[chunkFile] = newAsset;
288296

289-
var newSource = newAsset.source();
290-
hashByChunkId[chunk.id] = newAsset.integrity = computeIntegrity(newSource);
297+
newSource = newAsset.source();
298+
newAsset.integrity = computeIntegrity(newSource);
299+
hashByChunkId[chunk.id] = newAsset.integrity;
291300
}
292-
return [ chunk.id ].concat(depChunkIds);
301+
return [chunk.id].concat(depChunkIds);
293302
}
294303

295304
compilation.chunks.forEach(function forEachChunk(chunk) {
296-
// chunk.entry was removed in Webpack 2. Use hasRuntime() for this check instead (if it exists)
305+
// chunk.entry was removed in Webpack 2. Use hasRuntime()
306+
// for this check instead (if it exists)
297307
if (('hasRuntime' in chunk) ? chunk.hasRuntime() : chunk.entry) {
298308
processChunkRecursive(chunk);
299309
}
300310
});
301311

302-
for (var key in assets) {
303-
if (assets.hasOwnProperty(key)) {
304-
var asset = assets[key];
305-
if (!asset.integrity) {
306-
asset.integrity = computeIntegrity(asset.source());
307-
}
312+
Object.keys(assets).forEach(function loop(assetKey) {
313+
asset = assets[assetKey];
314+
if (!asset.integrity) {
315+
asset.integrity = computeIntegrity(asset.source());
308316
}
309-
}
317+
});
310318
});
311319

312320
function getTagSrc(tag) {
@@ -339,8 +347,11 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
339347
return;
340348
}
341349
// Add integrity check sums
350+
351+
/* eslint-disable no-param-reassign */
342352
tag.attributes.integrity = checksum;
343353
tag.attributes.crossorigin = self.options.crossorigin;
354+
/* eslint-enable no-param-reassign */
344355
}
345356

346357
pluginArgs.head.filter(filterTag).forEach(processTag);
@@ -355,6 +366,7 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
355366
function beforeHtmlGeneration(pluginArgs, callback) {
356367
self.hwpPublicPath = pluginArgs.assets.publicPath;
357368
['js', 'css'].forEach(function addIntegrity(fileType) {
369+
// eslint-disable-next-line no-param-reassign
358370
pluginArgs.assets[fileType + 'Integrity'] =
359371
pluginArgs.assets[fileType].map(function assetIntegrity(filePath) {
360372
var src = self.hwpAssetPath(filePath);

karma.conf.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var karmaMiddleware = require('karma/lib/middleware/karma');
44
var toplevelScriptIntegrity;
55
var stylesheetIntegrity;
66

7+
var prevCreate = karmaMiddleware.create;
8+
79
/*
810
* Simple webpack plugin that records the top-level chunk's integrity
911
* attribute value.
@@ -27,18 +29,35 @@ GetIntegrityPlugin.prototype.apply = function apply(compiler) {
2729
* Hack Karma to add the integrity attribute to the script tag
2830
* loading the top-level chunk.
2931
*/
30-
var prevCreate = karmaMiddleware.create;
31-
function nextCreate(filesPromise, serveStaticFile, serveFile, injector, basePath, urlRoot, upstreamProxy) {
32-
var prevMiddleware = prevCreate(filesPromise, serveStaticFile, serveFile, injector, basePath, urlRoot, upstreamProxy);
32+
function nextCreate(
33+
filesPromise,
34+
serveStaticFile,
35+
serveFile,
36+
injector,
37+
basePath,
38+
urlRoot,
39+
upstreamProxy
40+
) {
41+
var prevMiddleware = prevCreate(
42+
filesPromise,
43+
serveStaticFile,
44+
serveFile,
45+
injector,
46+
basePath,
47+
urlRoot,
48+
upstreamProxy
49+
);
3350
return function nextMiddleware(request, response, next) {
3451
var requestUrl = request.normalizedUrl.replace(/\?.*/, '');
52+
var prevWrite;
3553
requestUrl = requestUrl.substr(urlRoot.length - 1);
3654
if (requestUrl === '/context.html' &&
3755
toplevelScriptIntegrity &&
3856
toplevelScriptIntegrity.startsWith('sha') &&
3957
stylesheetIntegrity &&
4058
stylesheetIntegrity.startsWith('sha')) {
41-
var prevWrite = response.write;
59+
prevWrite = response.write;
60+
// eslint-disable-next-line no-param-reassign
4261
response.write = function nextWrite(chunk, encoding) {
4362
var nextChunk = chunk.replace(
4463
'src="/base/test/test.js',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"coveralls": "^2.11.15",
3838
"css-loader": "^0.26.0",
3939
"eslint": "^3.10.0",
40-
"eslint-config-airbnb-es5": "^1.0.9",
40+
"eslint-config-airbnb": "^14.1.0",
4141
"eslint-plugin-react": "^6.6.0",
4242
"expect": "^1.13.4",
4343
"extract-text-webpack-plugin": "^1.0.1",

test/chunk2.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
var expect = require('expect');
22

33
module.exports = function chunk2(callback) {
4+
var resourcesWithIntegrity;
45
function forEachElement(el) {
56
var src = el.getAttribute('src') || el.getAttribute('href');
67
var integrity = el.getAttribute('integrity');
78
var crossorigin = el.getAttribute('crossOrigin');
9+
var match;
810
if (src) {
9-
var match = src.match(/[^\/]+\.(js|css)/);
11+
match = src.match(/[^/]+\.(js|css)/);
1012
if (match && crossorigin && integrity && integrity.match(/^sha\d+-/)) {
1113
resourcesWithIntegrity.push(match[0].toString());
1214
}
1315
}
1416
}
1517
try {
16-
var resourcesWithIntegrity = [];
18+
resourcesWithIntegrity = [];
1719
Array.prototype.slice.call(document.getElementsByTagName('script')).forEach(forEachElement);
1820
Array.prototype.slice.call(document.getElementsByTagName('link')).forEach(forEachElement);
1921
expect(resourcesWithIntegrity).toInclude('stylesheet.css');
2022
expect(resourcesWithIntegrity).toInclude('test.js');
21-
expect(resourcesWithIntegrity.filter(function filter(item) { return item.match(/^\d+\.(chunk|bundle).js$/); }).length).toBe(2);
23+
expect(resourcesWithIntegrity.filter(function filter(item) {
24+
return item.match(/^\d+\.(chunk|bundle).js$/);
25+
}).length).toBe(2);
2226
expect(window.getComputedStyle(document.getElementsByTagName('body')[0]).backgroundColor).toEqual('rgb(200, 201, 202)');
2327
callback();
2428
} catch (e) {

0 commit comments

Comments
 (0)