Skip to content
This repository was archived by the owner on Nov 18, 2018. It is now read-only.

Commit abff053

Browse files
author
lukas.steiger@siemens.com
committed
refactor plugin and alllow dynamic naming and splitting into two files
1 parent 9ffce42 commit abff053

File tree

3 files changed

+85
-50
lines changed

3 files changed

+85
-50
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
3+
## Prerequisites
4+
This plugin requires a valid webpack configuration. It should contain the following information:
5+
```
6+
const DojoModuleWrapperPlugin = require('dojo-module-wrapper-webpack-plugin');
7+
8+
module.exports = {
9+
entry: {
10+
app: '<entry point of main JS class>',
11+
},
12+
output: {
13+
libraryTarget: 'amd',
14+
filename: '<bundle destination>',
15+
},
16+
module: {
17+
// module transformations
18+
},
19+
20+
plugins: [
21+
new DojoModuleWrapperPlugin({
22+
chunks: {
23+
app: {
24+
removeBefore: 'var installedModules'
25+
},
26+
},
27+
}),
28+
],
29+
30+
externals: [
31+
// exclude dojo, dijit and dojox from bundling
32+
(context, request, callback) => {
33+
if (/^dojo/.test(request) ||
34+
/^dojox/.test(request) ||
35+
/^dijit/.test(request)
36+
) {
37+
return callback(null, `amd ${request}`);
38+
}
39+
callback();
40+
},
41+
],
42+
};
43+
```

index.js

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,68 @@
11
"use strict";
22

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";
267

278
function DojoModuleWrapperPlugin(options) {
289
this.options = options || {};
29-
this.chunks = this.options.chunks || {};
3010
}
3111

3212
DojoModuleWrapperPlugin.prototype.apply = function(compiler) {
3313
compiler.plugin("emit", (compilation, callback) => {
34-
let chunkKey = Object.keys(this.chunks);
14+
let chunkKey = Object.keys(this.options);
3515
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 || '';
4118

4219
let source = compilation.assets[distChunk].source();
4320

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];
4623

47-
var moduleName = "com.siemens.bt.jazz.ui.WorkItemBulkMover.bundling.bundle";
24+
const dojoDeclareLoaderStatement = this.generateStartStatement(moduleName, depExtractions[1], depExtractions[2]);
4825

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, "]);");
5428

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;
6030

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+
}
6338
};
6439

65-
compilation.assets[distChunk] = new ConcatSource(beforeContent, compilation.assets[distChunk], afterContent);
40+
compilation.assets[distChunk].source = () => {
41+
return dojoDeclareLoaderStatement;
42+
};
6643
});
6744
callback();
6845
});
69-
7046
};
7147

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+
7266
DojoModuleWrapperPlugin.prototype.findAsset = function(compilation, chunk) {
7367
let chunks = compilation.chunks;
7468
for (let i = 0, len = chunks.length; i < len; i++) {

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
22
"name": "dojo-module-wrapper-webpack-plugin",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"author": "Lukas Steiger | @innerjoin",
55
"description": "Convert webpack's AMD output into a valid Dojo Module, invokable by a 'executeBundle()' method",
6-
"peerDependencies": {
7-
},
86
"license": "MIT",
97
"repository": {
108
"type": "git",

0 commit comments

Comments
 (0)