UNPKG

csg-html-plugin-insert

Version:

webpack-html-plugin

105 lines (97 loc) 4.15 kB
// html-webpack-inject-attributes-plugin var assign = require('lodash.assign'); var forEach = require('lodash.foreach'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); function MyPlugin(options) { this.options = options; } MyPlugin.prototype.apply = function (compiler) { var self = this; compiler.plugin('compilation', function (compilation) { function addAttr(tags, key, val) { if (!tags || !tags.length) { return; } forEach(tags, function (tag, index) { var value = val; if (typeof val === 'function') { value = val(tag, compilation, index); } !tag.attributes && (tag.attributes = {}); tag.attributes[key] = value; }); } // console.log(process.env) // * `html-webpack-plugin-before-html-generation` // * `html-webpack-plugin-before-html-processing` // * `html-webpack-plugin-alter-asset-tags` // * `html-webpack-plugin-after-html-processing` // * `html-webpack-plugin-after-emit` compilation.plugin('html-webpack-plugin-alter-asset-tags', function (htmlPluginData, callback) { // console.log(htmlPluginData); // add sentry cdn script to first position of script tags htmlPluginData.body.unshift({ attributes: { type: "text/javascript", }, closeTag: true, tagName: "script", innerHTML: 'Sentry.init({dsn: "'+ process.env.dsn +'",release: "' + process.env.release + '"})' // +'",' // +'integrations: [new Sentry.Integrations.BrowserTracing()],' // + 'tracesSampleRate: 1.0})' }) htmlPluginData.body.unshift({ attributes: { src: "https://browser.sentry-cdn.com/5.27.2/bundle.tracing.min.js", type: "text/javascript", integrity: "sha384-rpbR8aO7ZS39FLWobN304qdytlzvMSYnMFEJgiCC2dUEWmaD1W9bSWfy20JHFU6Q", crossorigin: "anonymous", }, closeTag: true, tagName: "script" }); htmlPluginData.body.unshift({ attributes: { src: "https://browser.sentry-cdn.com/5.27.2/bundle.min.js", type: "text/javascript", integrity: "sha384-+69fdGw+g5z0JJXjw46U9Ls/d9Y4Zi6KUlCcub+qIWsUoIlyimCujtv+EnTTHPTD", crossorigin: "anonymous", }, closeTag: true, tagName: "script" }); var options = assign({}, null, htmlPluginData.plugin.options && htmlPluginData.plugin.options.attributes); forEach(options, function (val, key) { if (typeof val !== 'string' && typeof val !== 'function') { return; } addAttr(htmlPluginData.head, key, val); addAttr(htmlPluginData.body, key, val); }); if (typeof callback === 'function') { callback(null, htmlPluginData); } else { // return new Promise(resolve => resolve(htmlPluginData)); return htmlPluginData; } }); }); //delete sourcemap compiler.plugin('done', function (stats) { const DEFAULT_DELETE_REGEX = /\.map$/; let mapObjs = Object.keys(stats.compilation.assets).filter(function (name) { return DEFAULT_DELETE_REGEX.test(name); }) forEach(mapObjs,function (name) { var existsAt = stats.compilation.assets[name].existsAt; if(_fs.existsSync(existsAt)){ _fs2.default.unlinkSync(existsAt); } }); }) }; module.exports = MyPlugin;