UNPKG

@omnia/foundation

Version:

Provide omnia foundation typings and tooling work on client side for omnia extension

81 lines (71 loc) 3.05 kB
'use strict'; var ConcatSource = require('webpack-core/lib/ConcatSource'); function replaceFile(compilation, fileName, replacements) { var result = compilation.assets[fileName].source(); replacements.forEach(function (replacement) { if (!replacement.skip && replacement.replacement) { if (replacement.insertToEOF) { var insertData = ""; if (replacement.partten) { var matchs = result.match(replacement.partten); if (matchs !== null) { insertData = replacement.replacement(matchs[0]); } } else insertData = replacement.replacement; if (typeof insertData === "string") { result = result + '\r\n' + insertData } } else if (replacement.partten) { result = result.replace(replacement.partten, replacement.replacement); } } }); compilation.assets[fileName] = new ConcatSource(result); } function ReplaceBundlePlugin(args) { if (!(args instanceof Array)) { throw new TypeError('Argument "args" must be an array.'); } this.replacements = args; } ReplaceBundlePlugin.prototype.apply = function (compiler) { compiler.plugin("compilation", function (compilation) { compilation.plugin("optimize-chunk-assets", function (chunks, done) { chunks.forEach(function (chunk) { chunk.files.forEach(function (fileName) { replaceFile(compilation, fileName, this.replacements); }, this); }, this); done(); }.bind(this)); }.bind(this)); }; function NamedModulesPlugin(options) { this.options = options || {}; } NamedModulesPlugin.prototype.apply = function (compiler) { compiler.plugin("compilation", function (compilation) { compilation.plugin("before-module-ids", function (modules) { modules.forEach(function (module) { if (module.id === null && module.libIdent) { module.id = module.libIdent({ context:compiler.options.context }); //if (module.id === "./") { // console.log(JSON.stringify(compiler.options)) //} if (this.options.replacements !== undefined) { this.options.replacements.forEach(function (replacement) { module.id = module.id.replace(replacement.pattern, replacement.replace); }, this); } module.id = module.id.toLowerCase(); } }, this); }.bind(this)); }.bind(this)); }; module.exports = { NamedModulesPlugin: NamedModulesPlugin, ReplaceBundlePlugin: ReplaceBundlePlugin };