提交 6d5927c2 编写于 作者: S sushuang

Support source map in requireES.

上级 b1e3be1a
(function () { (function () {
var baseUrl = window.AMD_BASE_URL || '../'; var baseUrl = window.AMD_BASE_URL || '../';
var sourceMap = window.AMD_ENABLE_SOURCE_MAP;
// `true` by default for debugging.
sourceMap == null && (sourceMap === true);
if (typeof require !== 'undefined') { if (typeof require !== 'undefined') {
require.config({ require.config({
...@@ -30,7 +33,8 @@ ...@@ -30,7 +33,8 @@
'map': 'map', 'map': 'map',
'extension': 'extension' 'extension': 'extension'
}, },
urlArgs: '_v_=' + (+new Date()) urlArgs: '_v_=' + (+new Date()),
sourceMap: sourceMap
}); });
} }
......
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
* packages: [ * packages: [
* {...}, ... * {...}, ...
* ], * ],
* urlArgs: +new Date() * urlArgs: +new Date(),
* sourceMap: true // Enable sourceMap for debugging. `false` by default.
* }); * });
* *
* requireES([ * requireES([
...@@ -56,7 +57,10 @@ ...@@ -56,7 +57,10 @@
* *
* @param {Object} cfg { * @param {Object} cfg {
* @param {string} [cfg.baseUrl='.'] * @param {string} [cfg.baseUrl='.']
* @param {Object} [cfg.paths] * @param {Object} [cfg.paths={}]
* @param {Array.<Object>} [cfg.packages=[]]
* @param {string} [cfg.urlArgs='']
* @param {boolean} [cfg.sourceMap=false]
*/ */
function amdConfig(cfg) { function amdConfig(cfg) {
if (cfg.baseUrl != null) { if (cfg.baseUrl != null) {
...@@ -74,6 +78,9 @@ ...@@ -74,6 +78,9 @@
if (cfg.urlArgs != null) { if (cfg.urlArgs != null) {
amdCfg.urlArgs = cfg.urlArgs; amdCfg.urlArgs = cfg.urlArgs;
} }
if (cfg.sourceMap != null) {
amdCfg.sourceMap = cfg.sourceMap;
}
} }
/** /**
...@@ -121,12 +128,21 @@ ...@@ -121,12 +128,21 @@
return bundle.generate({ return bundle.generate({
format: 'iife', format: 'iife',
legacy: true, legacy: true,
// But only bundle.write support generating inline source map.
sourcemap: 'inline',
name: TOP_MODULE_NAME name: TOP_MODULE_NAME
}); });
}).then(function (result) { }).then(function (result) {
var code = result.code;
if (amdCfg.sourceMap) {
code = addSourceMap(code, result.map);
}
var modules = (new Function( var modules = (new Function(
'var __DEV__ = true; ' 'var __DEV__ = true; '
+ result.code + code
+ '\n return ' + TOP_MODULE_NAME + '\n return ' + TOP_MODULE_NAME
))(); ))();
...@@ -296,6 +312,20 @@ ...@@ -296,6 +312,20 @@
return res; return res;
} }
function addSourceMap(code, map) {
// Use unescape(encodeURIComponent) to avoid the error on Chrome:
// Uncaught (in promise) DOMException: Failed to execute 'btoa' on 'Window':
// The string to be encoded contains characters outside of the Latin1 range
var dataURI = btoa(unescape(encodeURIComponent(map.toString()))); // jshint ignore:line
dataURI = 'data:application/json;charset=utf-8;base64,' + dataURI;
// Split the string to prevent sourcemap tooling from mistaking
// this for an actual sourceMappingURL.
code += '//# ' + 'sourceMa' + 'ppingURL' + '=' + dataURI + '\n';
return code;
}
function cwd() { function cwd() {
// Only support that works in browser. // Only support that works in browser.
return dir(location.pathname); return dir(location.pathname);
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
<script src="../lib/requireES.js"></script> <script src="../lib/requireES.js"></script>
<script> <script>
window.AMD_BASE_URL = '../../'; window.AMD_BASE_URL = '../../';
window.AMD_ENABLE_SOURCE_MAP = false;
</script> </script>
<script src="../lib/config.js"></script> <script src="../lib/config.js"></script>
</head> </head>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册