weex-loader
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -9,2 +9,4 @@ 'use strict'; | ||
var _util = require('./util'); | ||
var _config = require('./config'); | ||
@@ -18,2 +20,4 @@ | ||
var _map = require('./map'); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
@@ -23,3 +27,3 @@ | ||
function partedLoader(type, loader, params, source) { | ||
function partedLoader(type, loader, params, source, map) { | ||
var promise = void 0; | ||
@@ -43,3 +47,4 @@ switch (type) { | ||
default: | ||
promise = (0, _parser.parseWeexFile)(loader, params, source); | ||
map.enable(); | ||
promise = (0, _parser.parseWeex)(loader, params, source, map); | ||
break; | ||
@@ -62,10 +67,19 @@ } | ||
var type = params.loaderQuery.type || 'we'; | ||
var promise = partedLoader(type, this, params, source); | ||
var resourcePath = params.resourcePath; | ||
var filename = (0, _util.getFilenameByPath)(resourcePath); | ||
var map = new _map.ScriptMap(filename, source); | ||
var promise = partedLoader(type, this, params, source, map); | ||
promise.then(function (result) { | ||
if (map.enabled) { | ||
map.parse(); | ||
} | ||
if (type === 'style' || type === 'css' || type === 'html' || type === 'tpl' || type === 'template') { | ||
result = 'module.exports=' + result; | ||
} | ||
callback(null, result); | ||
callback(null, result, map.toJSON()); | ||
}).catch(function (err) { | ||
// console.error(err.stack) | ||
_this.emitError(err.toString()); | ||
@@ -72,0 +86,0 @@ callback(err.toString(), ''); |
@@ -50,2 +50,3 @@ 'use strict'; | ||
// re-order the elements and scripts into history | ||
var length = current.elements.length; | ||
@@ -123,3 +124,3 @@ if (length > 0) { | ||
var scriptLength = info.length; | ||
_this.add(original.line + (elInfo.line || 1) - 2, scriptLength, generated.line + startLine + (line || 1) + externalOffset); | ||
_this.add(original.line + (elInfo.line || 1) - 1, scriptLength, generated.line + startLine + (line || 1) - 1 + externalOffset); | ||
}); | ||
@@ -126,0 +127,0 @@ |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.parseWeexFile = parseWeexFile; | ||
exports.parseWeex = parseWeex; | ||
exports.parseTemplate = parseTemplate; | ||
@@ -38,3 +38,3 @@ exports.parseStyle = parseStyle; | ||
function parseWeexFile(loader, params, source, deps, elementName) { | ||
function parseWeex(loader, params, source, map, deps, elementName) { | ||
return new Promise( | ||
@@ -44,5 +44,5 @@ // separate source into <element>s, <template>, <style>s and <script>s | ||
// pre-parse non-javascript parts | ||
.then(preParseBlocks(loader, params, elementName)) | ||
.then(preParseBlocks(loader, params, map)) | ||
// join blocks together and parse as javascript finally | ||
.then(parseBlocks(loader, params, elementName)); | ||
.then(parseBlocks(loader, params, map, elementName)); | ||
} | ||
@@ -63,3 +63,3 @@ | ||
function preParseBlocks(loader, params) { | ||
function preParseBlocks(loader, params, map) { | ||
return function (blocks) { | ||
@@ -82,3 +82,5 @@ var deps = blocks.deps; | ||
var el = elements[key]; | ||
elPromises.push(parseWeexFile(loader, params, el.content, deps, el.name)); | ||
// record original positions of each <element> | ||
map.setElementPosition(el.name, el.line, el.column); | ||
elPromises.push(parseWeex(loader, params, el.content, map, deps, el.name)); | ||
}); | ||
@@ -104,3 +106,3 @@ promises[0] = Promise.all(elPromises); | ||
function parseBlocks(loader, params, elementName) { | ||
function parseBlocks(loader, params, map, elementName) { | ||
return function (results) { | ||
@@ -119,5 +121,22 @@ var elements = results[0] || []; | ||
var mapOffset = { basic: 0, subs: [] }; | ||
if (scripts) { | ||
content += scripts.reduce(function (pre, cur) { | ||
return pre + '\n;' + cur.content; | ||
// record original and generated position of each <script> | ||
// the generated content is begin with empty string | ||
// so later the template, styles and elements will be appended/prepended | ||
// and mapOffset.basic will record lines of prepended *required* content | ||
content += scripts.reduce(function (prev, next, i) { | ||
// length of previous content | ||
var line = prev.split(/\r?\n/g).length + 1; | ||
var column = 1; | ||
var oriLine = next.line; | ||
var oriColumn = next.column; | ||
mapOffset.subs.push({ | ||
original: { line: oriLine, column: oriColumn }, | ||
generated: { line: line, column: column }, | ||
// length of next content | ||
length: next.content.split(/\r?\n/g).length | ||
}); | ||
return prev + '\n;' + next.content; | ||
}, ''); | ||
@@ -131,6 +150,11 @@ } | ||
}).join('\n'); | ||
content = requireContent + '\n' + content; | ||
if (requireContent) { | ||
// length of implicitly requires | ||
mapOffset.basic = requireContent.split(/\r?\n/g).length; | ||
content = requireContent + '\n' + content; | ||
} | ||
} | ||
if (template) { | ||
// append template content, not impact sourcemap | ||
content += '\n;module.exports.template = module.exports.template || {}' + '\n;Object.assign(module.exports.template, ' + template + ')'; | ||
@@ -140,5 +164,7 @@ } | ||
if (style) { | ||
// append style content, not impact sourcemap | ||
content += '\n;module.exports.style = module.exports.style || {}' + '\n;Object.assign(module.exports.style, ' + style + ')'; | ||
} | ||
// prepare entry config | ||
if (configResult) { | ||
@@ -150,2 +176,3 @@ config = new Function('return ' + configResult.content.replace(/\n/g, ''))(); | ||
// prepare entry data | ||
if (dataResult) { | ||
@@ -156,3 +183,3 @@ data = new Function('return ' + dataResult.content.replace(/\n/g, ''))(); | ||
return parseScript(loader, params, content, { config: config, data: data, elementName: elementName, elements: elements }); | ||
return parseScript(loader, params, content, { config: config, data: data, elementName: elementName, elements: elements, map: map, mapOffset: mapOffset }); | ||
}; | ||
@@ -205,2 +232,4 @@ } | ||
var elements = env.elements; | ||
var map = env.map; | ||
var mapOffset = env.mapOffset; | ||
@@ -214,2 +243,13 @@ // the entry component has a special resource query and not a sub element tag | ||
// join with elements deps | ||
// 2 more lines between each element and the end | ||
map && map.start(); | ||
var prefix = (elements || []).reduce(function (prev, next, index) { | ||
var prevLength = prev.split(/\r?\n/g).length; | ||
var nextLength = next.split(/\r?\n/g).length; | ||
// record generated positions of each <element> | ||
map && map.addElement(name, index, prevLength, nextLength); | ||
return prev + next + ';\n\n'; | ||
}, ''); | ||
// fix data option from an object to a function | ||
@@ -219,6 +259,20 @@ var target = _weexScripter2.default.fix(source); | ||
// wrap with __weex_define__(name, [], (r, e, m) {...}) | ||
// 1 more line at start, 1 more line at end | ||
target = target.replace(_util.MODULE_EXPORTS_REG, '__weex_module__.exports').replace(_util.REQUIRE_REG, '__weex_require__($1$2$1)'); | ||
target = ';__weex_define__("@weex-component/' + name + '", [], ' + 'function(__weex_require__, __weex_exports__, __weex_module__)' + '{\n' + target + '\n})'; | ||
// record mapOffset into sourcemap | ||
if (mapOffset) { | ||
(function () { | ||
// length of generated prefix (elements) and basic (implicitly requires) | ||
var preLines = prefix.split(/\r?\n/g).length + mapOffset.basic; | ||
mapOffset.subs.forEach(function (info) { | ||
map.addScript(elementName || name, info, preLines); | ||
}); | ||
})(); | ||
} | ||
map && map.end(); | ||
// append __weex_bootstrap__ for entry component | ||
// not impact sourcemap | ||
if (isEntry) { | ||
@@ -228,6 +282,3 @@ target += '\n;__weex_bootstrap__("@weex-component/' + name + '", ' + String(config) + ',' + String(data) + ')'; | ||
// join with elements deps | ||
target = (elements || []).concat(target).join(';\n\n'); | ||
return Promise.resolve(target); | ||
return Promise.resolve(prefix + target); | ||
} |
@@ -8,2 +8,3 @@ 'use strict'; | ||
exports.getNameByPath = getNameByPath; | ||
exports.getFilenameByPath = getFilenameByPath; | ||
exports.stringifyFunction = stringifyFunction; | ||
@@ -37,2 +38,6 @@ exports.appendToWarn = appendToWarn; | ||
function getFilenameByPath(filepath) { | ||
return _path2.default.relative('.', filepath); | ||
} | ||
var FUNC_START = exports.FUNC_START = '#####FUN_S#####'; | ||
@@ -39,0 +44,0 @@ var FUNC_START_REG = exports.FUNC_START_REG = new RegExp('["\']' + FUNC_START, 'g'); |
{ | ||
"name": "weex-loader", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "a webpack loader for weex", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
63765
517
2