Skip to content

Commit

Permalink
feat: core
Browse files Browse the repository at this point in the history
  • Loading branch information
komomoo committed Mar 13, 2019
1 parent 54c500b commit 4a3fbef
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* crossorigin-webpack-plugin
*/

module.exports = class CrossoriginWebpackPlugin {
constructor (options) {
this.options = options || { crossorigin: 'anonymous' }
}

apply (compiler) {
const HtmlWebpackPlugin = require('html-webpack-plugin')

// Hook into the html-webpack-plugin processing
if (HtmlWebpackPlugin && HtmlWebpackPlugin.getHooks) {
// HtmlWebpackPlugin 4
HtmlWebpackPlugin.getHooks(compiler).alterAssetTags.tapAsync('CrossoriginWebpackPlugin', this._addAttributeToScripts.bind(this))
} else if (compiler.hooks) {
// HtmlWebpackPlugin 3
compiler.hooks.compilation.tap('CrossoriginWebpackPlugin', compilation => {
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('CrossoriginWebpackPlugin', this._addAttributeToScripts.bind(this))
})
} else {
// HtmlWebpackPlugin 2
compiler.plugin('compilation', compilation => {
compilation.plugin(
'html-webpack-plugin-alter-asset-tags',
this._addAttributeToScripts.bind(this)
)
})
}
}

_addAttributeToScripts (htmlPluginData, callback) {
htmlPluginData.head.filter(tag => tag.tagName === 'script').forEach(script => (script.attributes.crossorigin = this.options.crossorigin))
htmlPluginData.body.filter(tag => tag.tagName === 'script').forEach(script => (script.attributes.crossorigin = this.options.crossorigin))
callback(null, htmlPluginData)
}
}

0 comments on commit 4a3fbef

Please sign in to comment.