提交 21e07093 编写于 作者: Y yangyiliang

chameleon-loader prepareParseUsingComponents

上级 91b4d0ac
......@@ -15,6 +15,7 @@ var jsonHandler = require('./cml-compile/json-handle.js');
const { getScriptCode } = require('./interface-check/getScriptCode.js');
const cmlUtils = require('chameleon-tool-utils');
const prehandle = require('./utils/prehandle.js');
const loaderMethods = require('./loaderMethods');
let jsonObject = {};
module.exports = function (content) {
......@@ -442,24 +443,13 @@ module.exports = function (content) {
* }]
*/
function prepareParseUsingComponents(originObj) {
return Object.keys(originObj).map(key=>{
let value = originObj[key];
let {filePath, refUrl} = cmlUtils.handleComponentUrl(context, self.resourcePath, value, cmlType);
// 如果是node_modules中的refUrl中会变成npm,替换成node_modules后再查找组件
if(~value.indexOf('/npm') && filePath === '') {
value = value.replace('/npm/','/node_modules/');
filePath = cmlUtils.handleComponentUrl(context, self.resourcePath, value, cmlType).filePath;
}
return {
tagName: key,
refUrl,
filePath,
isNative: !filePath.endsWith('.cml')
}
return loaderMethods.prepareParseUsingComponents({
loaderContext: self,
context,
originObj,
cmlType
})
}
// done
return output
}
......
const cmlUtils = require('chameleon-tool-utils');
exports.prepareParseUsingComponents = function({loaderContext, context, originObj, cmlType}) {
return Object.keys(originObj).map(key => {
let value = originObj[key];
let {filePath, refUrl} = cmlUtils.handleComponentUrl(context, loaderContext.resourcePath, value, cmlType);
// 如果是node_modules中的refUrl中会变成npm,替换成/node_modules/后再查找组件
if (~value.indexOf('/npm') && filePath === '') {
value = value.replace(/(.*?)npm\//g, '/node_modules/');
filePath = cmlUtils.handleComponentUrl(context, loaderContext.resourcePath, value, cmlType).filePath;
}
return {
tagName: key,
refUrl,
filePath,
isNative: !filePath.endsWith('.cml')
}
})
}
const loaderMethods = require('../../src/loaderMethods.js');
const expect = require('chai').expect;
const path = require('path');
describe('prepareParseUsingComponents', function(){
it('/npm/', function() {
let originObj = {
scroller: '/npm/cml-ui/scroller/scroller'
}
let loaderContext = {
resourcePath: path.join(__dirname, './project/src/pages/page1.cml')
}
let context = path.join(__dirname, './project');
let result = loaderMethods.prepareParseUsingComponents({
loaderContext,
context,
originObj,
cmlType: 'wx'
});
let expectPath = path.join(__dirname, './project/node_modules/cml-ui/scroller/scroller.cml')
console.log(result)
result.forEach(item=>{
if (item.tagName === 'scroller') {
expect(item.filePath).to.be.equal(expectPath);
expect(item.isNative).to.be.equal(false);
}
})
})
it('../npm/', function() {
let originObj = {
scroller: './../npm/cml-ui/scroller/scroller'
}
let loaderContext = {
resourcePath: path.join(__dirname, './project/src/pages/page1.cml')
}
let context = path.join(__dirname, './project');
let result = loaderMethods.prepareParseUsingComponents({
loaderContext,
context,
originObj,
cmlType: 'wx'
});
let expectPath = path.join(__dirname, './project/node_modules/cml-ui/scroller/scroller.cml')
result.forEach(item=>{
if (item.tagName === 'scroller') {
expect(item.filePath).to.be.equal(expectPath);
expect(item.isNative).to.be.equal(false);
}
})
})
})
......@@ -732,7 +732,7 @@ _.findInterfaceFile = function(context, cmlFilePath, comPath) {
_.npmComponentRefPath = function (componentAbsolutePath, context) {
let refUrl = '';
refUrl = path.relative(context, componentAbsolutePath);
refUrl = refUrl.replace('node_modules', 'npm');
refUrl = refUrl.replace(/node_modules/g, 'npm');
refUrl = _.handleWinPath(refUrl);
if (refUrl[0] !== '/') {
refUrl = '/' + refUrl
......@@ -893,7 +893,7 @@ _.getEntryPath = function (filePath, context) {
let entryName;
if (~filePath.indexOf('node_modules')) {
entryName = path.relative(root, filePath);
entryName = entryName.replace('node_modules', 'npm');
entryName = entryName.replace(/node_modules/g, 'npm');
} else {
entryName = path.relative(projectPath, filePath);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册