|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -var deps1 = '"dojo/request/xhr"'; |
4 |
| -var deps2 = '__WEBPACK_EXTERNAL_MODULE_17__'; |
5 |
| -// var moduleName = "com.siemens.bt.jazz.ui.WorkItemBulkMover.bundling.bundle"; |
6 |
| - |
7 |
| -// var startStatement = 'define(["dojo/_base/declare", ' + deps1 + '],' |
8 |
| -// + 'function(declare, ' + deps2 + ') {' |
9 |
| -// + 'return declare("' + moduleName + '", null, {' |
10 |
| -// + 'executeBundle: function() {' |
11 |
| - |
12 |
| -var removeBefore = ''; |
13 |
| - |
14 |
| -var endStatement = ']); } }); });'; |
15 |
| - |
16 |
| -var findRegex = /(define\(\[.*\]\,\s?function\(.*\)\s?\{\s?return.*)\(function\([a-z]*\)\s?\{\s?.*/; |
17 |
| -var beforeRegex = /define\(\[(.*)\]\,\s?function\((.*)\)\s?\{\s?return.*(\(function\([a-z]*\)\s?\{)\s?.*/; |
18 |
| -var endRegex = /.*\]\)\}\);;$/; |
19 |
| - |
20 |
| -var ConcatSource; |
21 |
| -try { |
22 |
| - ConcatSource = require("webpack-core/lib/ConcatSource"); |
23 |
| -} catch(e) { |
24 |
| - ConcatSource = require("webpack-sources").ConcatSource; |
25 |
| -} |
| 3 | +const replacementExpr = /(define\(\[.*\]\,\s?function\(.*\)\s?\{\s?return.*)\(function\([a-z]*\)\s?\{\s?.*/; |
| 4 | +const dependencyExtractorExpr = /define\(\[(.*)\]\,\s?function\((.*)\)\s?\{\s?return.*(\(function\([a-z]*\)\s?\{)\s?.*/; |
| 5 | +const endBracketExpr = /.*\]\)\}\);;$/; |
| 6 | +const postfix = "Scripts.js"; |
26 | 7 |
|
27 | 8 | function DojoModuleWrapperPlugin(options) {
|
28 | 9 | this.options = options || {};
|
29 |
| - this.chunks = this.options.chunks || {}; |
30 | 10 | }
|
31 | 11 |
|
32 | 12 | DojoModuleWrapperPlugin.prototype.apply = function(compiler) {
|
33 | 13 | compiler.plugin("emit", (compilation, callback) => {
|
34 |
| - let chunkKey = Object.keys(this.chunks); |
| 14 | + let chunkKey = Object.keys(this.options); |
35 | 15 | chunkKey.map((chunk, key) => {
|
36 |
| - let distChunk = this.findAsset(compilation, chunk), |
37 |
| - beforeContent = this.chunks[chunk].beforeContent || '', |
38 |
| - afterContent = this.chunks[chunk].afterContent || '', |
39 |
| - removeBefore = this.chunks[chunk].removeBefore || '', |
40 |
| - removeAfter = this.chunks[chunk].removeAfter || ''; |
| 16 | + const distChunk = this.findAsset(compilation, chunk); |
| 17 | + const moduleName = this.options[chunk].moduleName || ''; |
41 | 18 |
|
42 | 19 | let source = compilation.assets[distChunk].source();
|
43 | 20 |
|
44 |
| - var ele = source.match(beforeRegex); |
45 |
| - var toReplace = source.match(findRegex)[1]; |
| 21 | + const depExtractions = source.match(dependencyExtractorExpr); |
| 22 | + const toReplace = source.match(replacementExpr)[1]; |
46 | 23 |
|
47 |
| - var moduleName = "com.siemens.bt.jazz.ui.WorkItemBulkMover.bundling.bundle"; |
| 24 | + const dojoDeclareLoaderStatement = this.generateStartStatement(moduleName, depExtractions[1], depExtractions[2]); |
48 | 25 |
|
49 |
| - var startStatement = 'define(["dojo/_base/declare", ' + ele[1] + '],' |
50 |
| - + 'function(declare, ' + ele[2] + ') {' |
51 |
| - + 'return declare("' + moduleName + '", null, {' |
52 |
| - + 'executeBundle: function() {'; |
53 |
| - //+ 'toreplace is : ---> ' + toReplace + '<----' |
| 26 | + source = source.replace(toReplace, ""); |
| 27 | + source = source.replace(endBracketExpr, "]);"); |
54 | 28 |
|
55 |
| - |
56 |
| - source = source.replace(toReplace, startStatement); |
57 |
| - source = source.replace(endRegex, endStatement); |
58 |
| - //source = (removeBefore) ? source.replace(new RegExp('^' + removeBefore), "") : source; |
59 |
| - //source = (removeAfter) ? source.replace(new RegExp(removeAfter + '$'), "") : source; |
| 29 | + const newName = distChunk.substring(0, distChunk.indexOf(".js")) + postfix; |
60 | 30 |
|
61 |
| - compilation.assets[distChunk].source = () => { |
62 |
| - return source; |
| 31 | + compilation.assets[newName] = { |
| 32 | + source: function() { |
| 33 | + return source; |
| 34 | + }, |
| 35 | + size: function() { |
| 36 | + return source.length; |
| 37 | + } |
63 | 38 | };
|
64 | 39 |
|
65 |
| - compilation.assets[distChunk] = new ConcatSource(beforeContent, compilation.assets[distChunk], afterContent); |
| 40 | + compilation.assets[distChunk].source = () => { |
| 41 | + return dojoDeclareLoaderStatement; |
| 42 | + }; |
66 | 43 | });
|
67 | 44 | callback();
|
68 | 45 | });
|
69 |
| - |
70 | 46 | };
|
71 | 47 |
|
| 48 | +DojoModuleWrapperPlugin.prototype.generateStartStatement = function(moduleName, dependencies, dependencyVariables) { |
| 49 | + var windowDeps = ""; |
| 50 | + var deps = dependencyVariables.split(","); |
| 51 | + for(var i = 0; i < deps.length; i++) { |
| 52 | + windowDeps += ' window.' + deps[i] + ' = ' + deps[i] + ';\n'; |
| 53 | + } |
| 54 | + |
| 55 | + return 'define(["dojo/_base/declare", "dojo/request/script", ' + dependencies + '],' |
| 56 | + + 'function(declare, script, ' + dependencyVariables + ') { \n' |
| 57 | + + ' return declare("' + moduleName.replace(/\//g, ".") + '", null, { \n' |
| 58 | + + ' executeBundle: function() {\n' |
| 59 | + + windowDeps + '\n' |
| 60 | + + ' script.get("./' + moduleName + postFix + '"); \n' |
| 61 | + + ' } \n' |
| 62 | + + ' }); \n' |
| 63 | + + '}); \n'; |
| 64 | +} |
| 65 | + |
72 | 66 | DojoModuleWrapperPlugin.prototype.findAsset = function(compilation, chunk) {
|
73 | 67 | let chunks = compilation.chunks;
|
74 | 68 | for (let i = 0, len = chunks.length; i < len; i++) {
|
|
0 commit comments