Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not play well with the contenthash path variable #78

Closed
romeovs opened this issue Jun 6, 2018 · 2 comments
Closed

Does not play well with the contenthash path variable #78

romeovs opened this issue Jun 6, 2018 · 2 comments
Labels

Comments

@romeovs
Copy link

romeovs commented Jun 6, 2018

webpack-subresource-integrity does not play well with the [contenthash] path variable.

For instance with the following webpack.config.js:

var SRIPlugin = require("webpack-subresource-integrity")

module.exports = {
  entry: "./src/index.js",
  mode: "production",
  output: {
    filename: "[name].[contenthash].js",
    chunkFilename: "[name].[contenthash].js",
  },
  plugins: [
    new SRIPlugin({
      enabled: true, // set to false to stop breakage
      hashFuncNames: [
        "sha256",
        "sha384",
      ],
    }),
  ],
}

I get the following output:

$ webpack --config webpack.config.js
Unhandled rejection Error: Path variable [contenthash] not implemented in this context: [name].[contenthash].js
    at fn (/Users/romeo/code/breaking-sri/node_modules/webpack/lib/TemplatedPathPlugin.js:43:11)
    at fn (/Users/romeo/code/breaking-sri/node_modules/webpack/lib/TemplatedPathPlugin.js:31:16)
    at String.replace (<anonymous>)
    at replacePathVariables (/Users/romeo/code/breaking-sri/node_modules/webpack/lib/TemplatedPathPlugin.js:99:5)
    at SyncWaterfallHook.eval [as call] (eval at create (/Users/romeo/code/breaking-sri/node_modules/tapable/lib/HookCodeFactory.js:17:12), <anonymous>:7:16)
    at MainTemplate.getAssetPath (/Users/romeo/code/breaking-sri/node_modules/webpack/lib/MainTemplate.js:474:31)
    at Object.getChunkFilename (/Users/romeo/code/breaking-sri/node_modules/webpack-subresource-integrity/util.js:130:50)
    at Array.from.reverse.forEach.childChunk (/Users/romeo/code/breaking-sri/node_modules/webpack-subresource-integrity/index.js:191:23)
    at Array.forEach (<anonymous>)
    at SubresourceIntegrityPlugin.processChunk (/Users/romeo/code/breaking-sri/node_modules/webpack-subresource-integrity/index.js:185:48)
    at forEachChunk (/Users/romeo/code/breaking-sri/node_modules/webpack-subresource-integrity/index.js:223:12)
    at Array.forEach (<anonymous>)
    at SubresourceIntegrityPlugin.afterOptimizeAssets (/Users/romeo/code/breaking-sri/node_modules/webpack-subresource-integrity/index.js:222:52)
    at SyncHook.eval [as call] (eval at create (/Users/romeo/code/breaking-sri/node_modules/tapable/lib/HookCodeFactory.js:17:12), <anonymous>:7:1)
    at SyncHook.lazyCompileHook [as _call] (/Users/romeo/code/breaking-sri/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeAssets.callAsync.err (/Users/romeo/code/breaking-sri/node_modules/webpack/lib/Compilation.js:981:38)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/romeo/code/breaking-sri/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/romeo/code/breaking-sri/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeChunkAssets.callAsync.err (/Users/romeo/code/breaking-sri/node_modules/webpack/lib/Compilation.js:977:32)
    at _err0 (eval at create (/Users/romeo/code/breaking-sri/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:11:1)
    at /Users/romeo/code/breaking-sri/node_modules/uglifyjs-webpack-plugin/dist/index.js:334:11
    at step (/Users/romeo/code/breaking-sri/node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:90:11)
    at done (/Users/romeo/code/breaking-sri/node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:99:22)
    at tryCatcher (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:693:18)
    at Promise._fulfill (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:638:18)
    at Promise._resolveCallback (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:432:57)
    at Promise._settlePromiseFromHandler (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:524:17)
    at Promise._settlePromise (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:693:18)
    at Promise._fulfill (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:638:18)
    at Promise._resolveCallback (/Users/romeo/code/breaking-sri/node_modules/bluebird/js/release/promise.js:432:57)

My limited understanding of the issue is that webpack-subresource-integrity tries to interpolate the filenames in getChunkFilename and that fails because the [contenthash] is not calculated yet at the point webpack-subresource-integrity runs (in the after-optimize-assets hook).

I suggest suggest doing one of two things:

  • Do not try to interpolate the asset names at all
  • Move to a later hook that does have the contenthash

I've created a repo that reproduces this issue: https://github.com/romeovs/breaking-sri-contenthash

@romeovs romeovs changed the title Does not play well with the [contenthash] path variable Does not play well with the contenthash path variable Jun 6, 2018
@jscheid
Copy link
Collaborator

jscheid commented Jun 7, 2018

Thanks for your bug report and for taking the time to create a demo repository, much appreciated.

This appears to be related to #60

I'll try and take a look at this, but not sure yet how soon I can get to it.

@jscheid jscheid added the bug label Jun 7, 2018
jscheid added a commit that referenced this issue Sep 8, 2018
jscheid added a commit that referenced this issue Sep 8, 2018
@jscheid jscheid closed this as completed in 05e996f Sep 9, 2018
@jscheid
Copy link
Collaborator

jscheid commented Sep 10, 2018

Thanks for your patience, this should be fixed in 1.1.0-rc.5. When you get a chance, could you let me know how it works for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants