Support source map generation for UMD bundle output!
MODIFY
package-lock.json
package-lock.json
"concat-map": "0.0.1"
}
},
+ "buffer-crc32": {
+ "version": "0.2.13",
+ "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz",
+ "integrity": "sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI="
+ },
"builtin-modules": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
},
+ "concat-with-sourcemaps": {
+ "version": "1.0.5",
+ "resolved": "https://registry.npmjs.org/concat-with-sourcemaps/-/concat-with-sourcemaps-1.0.5.tgz",
+ "integrity": "sha512-YtnS0VEY+e2Khzsey/6mra9EoM6h/5gxaC0e3mcHpA5yfDxafhygytNmcJWodvUgyXzSiL5MSkPO6bQGgfliHw==",
+ "requires": {
+ "source-map": "0.6.1"
+ },
+ "dependencies": {
+ "source-map": {
+ "version": "0.6.1",
+ "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
+ "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="
+ }
+ }
+ },
"concurrently": {
"version": "3.5.1",
"resolved": "https://registry.npmjs.org/concurrently/-/concurrently-3.5.1.tgz",
}
},
"ecstatic": {
- "version": "3.1.1",
- "resolved": "https://registry.npmjs.org/ecstatic/-/ecstatic-3.1.1.tgz",
- "integrity": "sha512-D9UcjcxDMMqjaQxC0mSsFh/IjJSdiZVPnHrhjHuKXlhLByk5QGGPX1GUIDIjRzhTq4UDCPYwWblw79VBEh3r1w==",
+ "version": "3.2.0",
+ "resolved": "https://registry.npmjs.org/ecstatic/-/ecstatic-3.2.0.tgz",
+ "integrity": "sha512-Goilx/2cfU9vvfQjgtNgc2VmJAD8CasQ6rZDqCd2u4Hsyd/qFET6nBf60jiHodevR3nl3IGzNKtrzPXWP88utQ==",
"requires": {
"he": "1.1.1",
"mime": "1.6.0",
"resolved": "https://registry.npmjs.org/es6-collections/-/es6-collections-0.5.6.tgz",
"integrity": "sha1-VVLoAK0SwYIM2ivUp5rn27A9iaI="
},
+ "es6-promise": {
+ "version": "3.3.1",
+ "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz",
+ "integrity": "sha1-oIzd6EzNvzTQJ6FFG8kdS80ophM="
+ },
"escape-html": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
"requires": {
"colors": "1.0.3",
"corser": "2.0.1",
- "ecstatic": "3.1.1",
+ "ecstatic": "3.2.0",
"http-proxy": "1.16.2",
"opener": "1.4.3",
"optimist": "0.6.1",
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz",
"integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8="
+ },
+ "wordwrap": {
+ "version": "0.0.3",
+ "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz",
+ "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="
}
}
},
"prelude-ls": "1.1.2",
"type-check": "0.3.2",
"wordwrap": "1.0.0"
- },
- "dependencies": {
- "wordwrap": {
- "version": "1.0.0",
- "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz",
- "integrity": "sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus="
- }
}
},
"parse-json": {
"graceful-fs": "4.1.11"
}
},
+ "sander": {
+ "version": "0.5.1",
+ "resolved": "https://registry.npmjs.org/sander/-/sander-0.5.1.tgz",
+ "integrity": "sha1-dB4kXiMfB8r7b98PEzrfohalAq0=",
+ "requires": {
+ "es6-promise": "3.3.1",
+ "graceful-fs": "4.1.11",
+ "mkdirp": "0.5.1",
+ "rimraf": "2.6.2"
+ }
+ },
"semver": {
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
"hoek": "0.9.1"
}
},
+ "sorcery": {
+ "version": "0.10.0",
+ "resolved": "https://registry.npmjs.org/sorcery/-/sorcery-0.10.0.tgz",
+ "integrity": "sha1-iukK19fLBfxZ8asMY3hF1cFaUrc=",
+ "requires": {
+ "buffer-crc32": "0.2.13",
+ "minimist": "1.2.0",
+ "sander": "0.5.1",
+ "sourcemap-codec": "1.3.1"
+ }
+ },
"source-map": {
"version": "0.5.7",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz",
"integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=",
"optional": true
},
+ "sourcemap-codec": {
+ "version": "1.3.1",
+ "resolved": "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.3.1.tgz",
+ "integrity": "sha1-mtb5vb1pGTEBbjCTnbyGhnMyMUY=",
+ "requires": {
+ "vlq": "0.2.3"
+ }
+ },
"sourcemap-to-ast": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/sourcemap-to-ast/-/sourcemap-to-ast-0.0.2.tgz",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
},
+ "vlq": {
+ "version": "0.2.3",
+ "resolved": "https://registry.npmjs.org/vlq/-/vlq-0.2.3.tgz",
+ "integrity": "sha512-DRibZL6DsNhIgYQ+wNdWDL2SL3bKPlVrRiBqV5yuMm++op8W4kGFtaQfCs4KEJn0wBZcHVHJ3eoywX8983k1ow=="
+ },
"watch": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/watch/-/watch-1.0.2.tgz",
}
},
"wordwrap": {
- "version": "0.0.3",
- "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz",
- "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz",
+ "integrity": "sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus="
},
"wrappy": {
"version": "1.0.2",

MODIFY
package.cson
package.cson
@@ -163,8 +163,13 @@ devDependencies:
"yuidocjs": '^0.10.2'
+ # UMD bundle generation
"amdclean": 'github:JCCR/amdclean#master'
+ "concat-with-sourcemaps": '^1.0.5'
+
+ "sorcery": '^0.10.0'
+
#####################################################################
scripts:

MODIFY
package.json
package.json
"jshint": "^2.9.5",
"glob": "^7.1.2",
"yuidocjs": "^0.10.2",
- "amdclean": "github:JCCR/amdclean#master"
+ "amdclean": "github:JCCR/amdclean#master",
+ "concat-with-sourcemaps": "^1.0.5",
+ "sorcery": "^0.10.0"
},
"scripts": {
"cson2json": "node ./node_modules/cson/bin/cson2json package.cson > package.json && node readium-build-tools/optimizePackageJsonScripts.js",
"http:watch": "echo > open_webbrowser.js && run-p \"dowatch\" \"httpServe\"",
"http": "npm run genPluginsConfig && npm run httpServe",
"dowatch": "node ./node_modules/watch/cli.js \"npm run build && node readium-build-tools/webbrowser.js && node ./node_modules/rimraf/bin.js open_webbrowser.js\" dev/ js/ plugins/",
- "httpServe": "node readium-build-tools/http-server.js -LOGFILE http.log -OPEN /dev/ -a 127.0.0.1 -p 9090 --cors ."
+ "watch": "watch \"npm run build\" dev/ js/ plugins/",
+ "httpServe": "node readium-build-tools/http-server.js -LOGFILE http.log -OPEN /dev/ -c-1 -a 127.0.0.1 -p 9090 --cors ."
}
}
\ No newline at end of file

MODIFY
readium-build-tools/RequireJS_function_umd-bundle_complete.js
readium-build-tools/RequireJS_function_umd-bundle_complete.js
function (data) {
console.log("========> onModuleBundleComplete [amdclean]");
+
+ console.log('Generating AMD cleaned bundle...');
+
var fs = module.require('fs');
var path = module.require('path');
var rimraf = module.require('rimraf');
+ var sorcery = module.require('sorcery');
+ var Concat = module.require('concat-with-sourcemaps');
+ var amdclean = module.require('amdclean');
+
var outputPath = path.dirname(data.path);
var outputFileName = path.basename(data.path);
var rootPath = path.resolve(outputPath, '../../');
@@ -24,23 +31,70 @@ function (data) {
+ "/"
+ process._RJS_Path_RelCwd__ConfigDir;
- var amdclean = module.require('amdclean');
- var outputFile = data.path;
- var cleanedCode = amdclean.clean({
- 'wrap': true,
- 'filePath': outputFile,
+ var codePath = data.path;
+
+ var mapPath = codePath + '.map';
+ var map = fs.readFileSync(mapPath, 'utf8');
+ var mapJSON = JSON.parse(map);
+ mapJSON.sourceRoot = '../../';
+ map = JSON.stringify(mapJSON);
+ fs.writeFileSync(mapPath, map);
+ var code = fs.readFileSync(codePath, 'utf8');
+ var cleaned = amdclean.clean({
+ 'sourceMap': map,
+ 'code': fs.readFileSync(codePath, 'utf8'),
+ 'wrap': false, // do not use wrap together with escodegen.sourceMapWithCode since it breaks the logic
'prefixTransform': function (postNormalizedModuleName, preNormalizedModuleName) {
return '_' + postNormalizedModuleName;
+ },
+ 'esprima': {
+ 'source': outputFileName // name of your file to appear in sourcemap
+ },
+ 'escodegen': {
+ 'sourceMap': true,
+ 'sourceMapWithCode': true
}
});
- cleanedCode = fs.readFileSync(configDir + '/umd_wrapper_start.template') +
- '\n' + cleanedCode + '\nreturn _' + data.name.replace(/-/g, '_') + ';\n' +
- fs.readFileSync(configDir + '/umd_wrapper_end.template');
+ console.log("AMD cleaned bundle generated.");
+
var cleanedOutputPath = path.resolve(outputPath, '../_umd-bundle');
+ rimraf.sync(cleanedOutputPath);
fs.mkdirSync(cleanedOutputPath);
- fs.writeFileSync(path.join(cleanedOutputPath, outputFileName), cleanedCode);
- //fs.unlinkSync(outputFile + '.map'); // Delete the source map, as it is incompatible :(
+
+
+ var newCodePath = path.join(cleanedOutputPath, outputFileName);
+ var newMapPath = path.join(cleanedOutputPath, outputFileName + '.map');
+ var newCode = cleaned.code;
+ cleaned.map.sourceRoot = '../../';
+ var newMap = cleaned.map.toString();
+
+ var concat = new Concat(true, newCodePath, '\n');
+ concat.add(null, fs.readFileSync(configDir + '/umd_wrapper_start.template'));
+ concat.add(null, newCode, newMap);
+ concat.add(null, 'return _' + data.name.replace(/-/g,'_') + ';');
+ concat.add(null, fs.readFileSync(configDir + '/umd_wrapper_end.template'));
+ concat.add(null, '//# sourceMappingURL=' + outputFileName + '.map');
+
+ var concatenatedContent = concat.content.toString();
+ var sourceMapForContent = concat.sourceMap;
+
+ fs.writeFileSync(newCodePath, concatenatedContent);
+ fs.writeFileSync(newMapPath, sourceMapForContent);
+
rimraf.sync(outputPath); // Delete the original path as it was temporary
console.log("UMD bundle created.");
+ var pathTo = path.join(cleanedOutputPath, outputFileName);
+ var chain = sorcery.loadSync(pathTo, {
+ content: {
+ [newCodePath]: concatenatedContent,
+ [codePath]: code,
+ },
+ sourcemaps: {
+ [newMapPath]: sourceMapForContent,
+ [mapPath]: map
+ }
+ });
+ chain.writeSync(newCodePath);
+ console.log("UMD source map created.");
}

Support source map generation for UMD bundle output!