Skip to content

Commit

Permalink
chameleon-loader prepareParseUsingComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyiliang committed Jan 25, 2019
1 parent 91b4d0a commit 21e0709
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 18 deletions.
22 changes: 6 additions & 16 deletions packages/chameleon-loader/src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions packages/chameleon-loader/src/loaderMethods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

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')
}
})
}
58 changes: 58 additions & 0 deletions packages/chameleon-loader/test/src/loaderMethods.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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);
}
})
})
})


Empty file.
Empty file.
4 changes: 2 additions & 2 deletions packages/chameleon-tool-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 21e0709

Please sign in to comment.