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

Don't crash when used in non-web builds #87

Closed
inductor opened this issue Oct 2, 2018 · 4 comments
Closed

Don't crash when used in non-web builds #87

inductor opened this issue Oct 2, 2018 · 4 comments

Comments

@inductor
Copy link

inductor commented Oct 2, 2018

On Nuxt.js 2.0.0

import SriPlugin from 'webpack-subresource-integrity';
module.exports = {
  build: {
    extend(config, ctx) {
      config.output.crossOriginLoading = 'anonymous'
      config.plugins.push(new SriPlugin({
        hashFuncNames: ['sha256', 'sha384']
      }))
    }
  }
}

This setting triggers this error

✖ error TypeError: Cannot read property 'tap' of undefined
  at WebIntegrityJsonpMainTemplatePlugin.apply (/Users/inductor/vue/project/node_modules/webpack-subresource-integrity/jmtp.js:76:36)
  at SubresourceIntegrityPlugin.registerJMTP (/Users/inductor/vue/project/node_modules/webpack-subresource-integrity/index.js:302:12)
  at SubresourceIntegrityPlugin.thisCompilation (/Users/inductor/vue/project/node_modules/webpack-subresource-integrity/index.js:348:10)
  at SyncHook.eval [as call] (eval at create (/Users/inductor/vue/project/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:13:1)
  at SyncHook.lazyCompileHook (/Users/inductor/vue/project/node_modules/tapable/lib/Hook.js:154:20)
  at Compiler.newCompilation (/Users/inductor/vue/project/node_modules/webpack/lib/Compiler.js:497:30)
  at hooks.beforeCompile.callAsync.err (/Users/inductor/vue/project/node_modules/webpack/lib/Compiler.js:534:29)
  at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/inductor/vue/project/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:6:1)
  at AsyncSeriesHook.lazyCompileHook (/Users/inductor/vue/project/node_modules/tapable/lib/Hook.js:154:20)
  at Compiler.compile (/Users/inductor/vue/project/node_modules/webpack/lib/Compiler.js:529:28)
  at readRecords.err (/Users/inductor/vue/project/node_modules/webpack/lib/Compiler.js:268:11)
  at Compiler.readRecords (/Users/inductor/vue/project/node_modules/webpack/lib/Compiler.js:396:11)
  at hooks.run.callAsync.err (/Users/inductor/vue/project/node_modules/webpack/lib/Compiler.js:265:10)
  at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/inductor/vue/project/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:6:1)
  at AsyncSeriesHook.lazyCompileHook (/Users/inductor/vue/project/node_modules/tapable/lib/Hook.js:154:20)
  at hooks.beforeRun.callAsync.err (/Users/inductor/vue/project/node_modules/webpack/lib/Compiler.js:262:19)
@jscheid
Copy link
Collaborator

jscheid commented Oct 2, 2018

You can fix this by adding the plugin only for the client build, not for the server build:

    extend(config, ctx) {
      config.output.crossOriginLoading = "anonymous";
      if (ctx.isClient) {
        config.plugins.push(
          new SriPlugin({
            hashFuncNames: ["sha256", "sha384"]
          })
        );
      }
    }

We should handle this case more gracefully though, I'll change the issue title to reflect this.

@jscheid jscheid changed the title Error when using in Nuxt.js Don't crash when used in non-web builds Oct 2, 2018
@inductor
Copy link
Author

inductor commented Oct 2, 2018

Thanks! I tried and it got fixed. However, the generated html with nuxt generate and nuxt start does not have integrity in the script tag.
Any idea why?

@jscheid
Copy link
Collaborator

jscheid commented Oct 2, 2018

Disregard that, it seems `inject=false` is only used for the server-side build.

I'm not sure but it must have something to do with their custom template.

I might take another look at this but not sure yet when I'll get to it. If you can find out anything in the meantime please let me know.

@jscheid
Copy link
Collaborator

jscheid commented Oct 2, 2018

It seems they bypass html-webpack-plugin altogether and use their own template rendering:
https://github.com/vuejs/vue/blob/52719ccab8fccffbdf497b96d3731dc86f04c1ce/src/server/template-renderer/index.js#L209

If so there's not much this plugin can do. I suppose you could ask them to make script (and link) rendering more configurable, then somebody could write a plugin for Vue that adds the integrity attribute.

But again, I don't know much about Nuxt or Vue so this might be completely off base.

jscheid added a commit that referenced this issue Oct 2, 2018
jscheid added a commit that referenced this issue Oct 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants