Skip to content

Commit

Permalink
Merge pull request #150 from waysact/do-not-import-webpack
Browse files Browse the repository at this point in the history
Do not import webpack
  • Loading branch information
jscheid committed Feb 10, 2021
2 parents c7ef9d6 + 862832b commit cc0bc1a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```ts

import { Compiler } from 'webpack';
import type { Compiler } from 'webpack';

// @public
export class SubresourceIntegrityPlugin {
Expand Down
29 changes: 15 additions & 14 deletions webpack-subresource-integrity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
*/

import { createHash } from "crypto";
import webpack, {
Chunk,
Compiler,
Compilation,
Template,
sources,
optimize,
} from "webpack";
import type { Chunk, Compiler, Compilation, sources } from "webpack";
import { relative, sep, join } from "path";
import { readFileSync } from "fs";
import * as assert from "typed-assert";
Expand Down Expand Up @@ -260,13 +253,14 @@ export class SubresourceIntegrityPlugin {
* @internal
*/
private replaceAsset = (
compiler: Compiler,
assets: Record<string, sources.Source>,
hashByChunkId: Map<string | number, string>,
chunkFile: string
): sources.Source => {
const oldSource = assets[chunkFile].source();
const hashFuncNames = this.options.hashFuncNames;
const newAsset = new webpack.sources.ReplaceSource(
const newAsset = new compiler.webpack.sources.ReplaceSource(
assets[chunkFile],
chunkFile
);
Expand Down Expand Up @@ -311,7 +305,12 @@ export class SubresourceIntegrityPlugin {

if (assets[sourcePath]) {
this.warnIfHotUpdate(compilation, assets[sourcePath].source());
const newAsset = this.replaceAsset(assets, hashByChunkId, sourcePath);
const newAsset = this.replaceAsset(
compilation.compiler,
assets,
hashByChunkId,
sourcePath
);
const integrity = computeIntegrity(
this.options.hashFuncNames,
newAsset.source()
Expand Down Expand Up @@ -360,7 +359,7 @@ export class SubresourceIntegrityPlugin {
);
}

return Template.asString([
return compilation.compiler.webpack.Template.asString([
source,
elName + ".integrity = __webpack_require__.sriHashes[chunkId];",
elName +
Expand Down Expand Up @@ -496,7 +495,9 @@ export class SubresourceIntegrityPlugin {
compilation.hooks.processAssets.tap(
{
name: thisPluginName,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
stage:
compilation.compiler.webpack.Compilation
.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
},
(records: Record<string, sources.Source>) => {
return this.processAssets(compilation, records);
Expand All @@ -520,7 +521,7 @@ export class SubresourceIntegrityPlugin {
}
);

optimize.RealContentHashPlugin.getCompilationHooks(
compilation.compiler.webpack.optimize.RealContentHashPlugin.getCompilationHooks(
compilation
).updateHash.tap(thisPluginName, (input, oldHash) => {
const assetKey = this.inverseAssetIntegrity.get(oldHash);
Expand Down Expand Up @@ -593,7 +594,7 @@ export class SubresourceIntegrityPlugin {
const includedChunks = chunk.getChunkMaps(false).hash;

if (Object.keys(includedChunks).length > 0) {
return Template.asString([
return compilation.compiler.webpack.Template.asString([
source,
"__webpack_require__.sriHashes = " +
JSON.stringify(
Expand Down
2 changes: 1 addition & 1 deletion webpack-subresource-integrity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-subresource-integrity",
"version": "5.0.0-alpha.2",
"version": "5.0.0-alpha.3",
"description": "Webpack plugin for enabling Subresource Integrity",
"author": "Julian Scheid <julian@evergiving.com>",
"license": "MIT",
Expand Down

0 comments on commit cc0bc1a

Please sign in to comment.