提交 efecd2f6 编写于 作者: Y yylgit

find component

上级 b9d4e395
......@@ -617,6 +617,22 @@ _.getOnePackageComponents = function (npmName, packageFilePath, cmlType, context
})
}
})
// 多态组件之外 还有普通的cml组件 怎么判断 文件名中.cml 用.分隔后数组长度是2 后面是cml
let cmlGlobPath = path.join(context, 'node_modules', npmName, main, '/**/*.cml');
glob.sync(cmlGlobPath).forEach(cmlFilePath => {
// 其他端的多态cml组件排除在外
let paths = path.basename(cmlFilePath).split('.');
if (paths.length === 2 && paths[1] === 'cml') {
if (_.isFile(cmlFilePath)) {
let comKey = path.basename(cmlFilePath).replace(cmlExtReg, '');
components.push({
name: comKey,
filePath: cmlFilePath,
refPath: _.npmComponentRefPath(cmlFilePath, context)
})
}
}
})
}
return components;
......@@ -684,6 +700,17 @@ _.handleComponentUrl = function (context, cmlFilePath, comPath, cmlType) {
}
}
if (!findFile) {
if (_.isCli()) {
const result = {
filePath: '',
refUrl
};
cml.event.emit('find-component', {context, cmlFilePath, comPath, cmlType}, result);
// 通过扩展找到文件
if (result.filePath && _.isFile(result.filePath)) {
return result;
}
}
return {
filePath: '',
refUrl
......
......@@ -21,7 +21,6 @@ module.exports = async function (options) {
await utils.setFreePort();
let {type, media} = options;
let webpackConfig;
debugger
if (cml.config.get().extPlatform && ~Object.keys(cml.config.get().extPlatform).indexOf(type)) {
webpackConfig = getExtendConfig(options);
} else {
......
class CMLNode {
constructor(options = {}) {
this.ext;
this.realPath; // 文件物理地址
this.nodeType; // App/Page/Component/Module // 节点类型 CML文件分为App/Page/Component 其他的为Module CML文件中的每一个部分也是一个Node节点
this.moduleType; // template/style/script/json/asset CML为CML文件
this.dependencies = []; // 该节点的直接依赖编译及诶点 app.cml依赖pages.cml pages.cml依赖components.cml js依赖js cmss依赖cmss
this.childrens = []; // 子模块 CML才有子模块
this.parent; // 父模块 CML文件中的子模块才有
this.source; // 模块源代码
this.convert; // AST JSON
this.output; // 模块输出 各种过程操作该字段
// this.compiled; // 是否经过编译
this.extra; // 用户可以额外添加的信息
this.identifier; // 节点唯一标识
this.modId; // 模块化的id
Object.keys(options).forEach(key => {
this[key] = options[key];
})
}
}
module.exports = CMLNode;
const CMLNode = require('./cmlNode.js');
const path = require('path');
class mvvmGraphPlugin {
constructor() {
this.moduleRule = [ // 文件后缀对应module信息
{
test: /\.css|\.less$/,
moduleType: 'style',
attrs: {
lang: 'less'
}
},
{
test: /\.stylus|\.styls$/,
moduleType: 'style',
attrs: {
lang: 'stylus'
}
},
{
test: /\.js|\.interface$/,
moduleType: 'script'
},
{
test: /\.json$/,
moduleType: 'json'
},
{
test: /\.(png|jpe?g|gif|svg|mp4|webm|ogg|mp3|wav|flac|aac|woff|woff2?|eot|ttf|otf)(\?.*)?$/,
moduleType: 'asset'
}
]
}
apply(compiler) {
let self = this;
compiler.plugin('should-emit', function(compilation) {
let outPutFile = {}; // 输出文件 key为文件路径 value为输出文件内容
let modules = compilation.modules;
let appModule;
for (let i = 0; i < modules.length; i++) {
if (modules[i]._nodeType === 'app') {
appModule = modules[i];
}
// 静态资源的写入
if (modules[i]._nodeType === 'module' && modules[i]._moduleType === 'asset') {
if (modules[i]._assetSource && modules[i]._outputPath) {
outPutFile[modules[i]._outputPath] = modules[i]._assetSource;
}
}
}
if (!appModule) {
throw new Error('not find app.cml node!')
}
let moduleNodeMap = new Map();
// appModule
let mvvmGraph = self.createGraph(appModule, null, moduleNodeMap);
// 返回false 不进入emit阶段
return false;
})
}
// 创建依赖图
createGraph(targetModule, currentNode, moduleNodeMap) {
// 第一个app节点
if (!currentNode) {
currentNode = this.createNode(targetModule);
moduleNodeMap.set(targetModule, currentNode);
}
targetModule.dependencies.forEach(item => {
if (item.module) {
if (moduleNodeMap.has(item.module)) {
let subNode = moduleNodeMap.get(item.module);
if (subNode.realPath === currentNode.realPath) {
subNode.parent = currentNode;
currentNode.childrens.push(subNode);
} else {
currentNode.dependencies.push(subNode);
}
} else {
let subNode = this.createNode(item.module);
moduleNodeMap.set(item.module, subNode);
if (subNode.realPath === currentNode.realPath) {
subNode.parent = currentNode;
currentNode.childrens.push(subNode);
} else {
currentNode.dependencies.push(subNode);
}
this.createGraph(item.module, subNode, moduleNodeMap)
}
}
})
return currentNode;
}
// 创建单个节点
createNode(module) {
let options = {};
options.realPath = module.resource;
options.ext = path.extname(module.resource);
options.nodeType = module._nodeType || 'module';
options.identifier = module.rawRequest;
options.modId = module.rawRequest; // 模块化的id 这里可以优化成hash
if (options.nodeType === 'module') {
if (module._moduleType) {
options.moduleType = module._moduleType;
} else {
this.moduleRule.forEach(rule => {
if (rule.test.test(module.resource)) {
options.moduleType = rule.moduleType;
}
})
options.moduleType = options.moduleType || 'other';
}
options.source = module._cmlSource || module._source && module._source._value;
}
return new CMLNode(options)
}
}
module.exports = mvvmGraphPlugin;
\ No newline at end of file
......@@ -3,6 +3,7 @@
module.exports = function(content) {
this._module._nodeType = 'module';
this._module._moduleType = 'style';
this._cmlSource = content;
this._module._cmlSource = content;
return `module.exports = ${JSON.stringify(content)}`
}
\ No newline at end of file
}
......@@ -81,7 +81,6 @@ function loader(content) {
} else if(typeof options.publicPath === 'string') {
publicPath = options.publicPath + outputPath;
}
debugger
this._module._nodeType = 'module';
this._module._moduleType = 'asset';
this._module._cmlSource = content;
......
......@@ -77,6 +77,7 @@ class Compiler {
throw new Error('not find app.cml node!')
}
// 记录已经创建的节点
let moduleNodeMap = new Map();
this.projectGraph = this.createGraph(appModule, null, moduleNodeMap);
......@@ -91,9 +92,11 @@ class Compiler {
}
targetModule.dependencies.forEach(item => {
if (item.module) {
// 如果已经创建了节点
if (moduleNodeMap.has(item.module)) {
let subNode = moduleNodeMap.get(item.module);
// 如果 子节点的文件路径和父节点相同 ze是CML文件 放入childrens
if (subNode.realPath === currentNode.realPath) {
subNode.parent = currentNode;
currentNode.childrens.push(subNode);
......@@ -102,6 +105,7 @@ class Compiler {
}
} else {
// 创建节点
let subNode = this.createNode(item.module);
moduleNodeMap.set(item.module, subNode);
if (subNode.realPath === currentNode.realPath) {
......@@ -110,6 +114,7 @@ class Compiler {
} else {
currentNode.dependencies.push(subNode);
}
// 递归创建
this.createGraph(item.module, subNode, moduleNodeMap)
}
}
......@@ -126,10 +131,11 @@ class Compiler {
options.identifier = module.rawRequest;
options.modId = module.rawRequest; // 模块化的id 这里可以优化成hash
if (options.nodeType === 'module') {
// loader中设置
if (module._moduleType) {
options.moduleType = module._moduleType;
} else {
// 根据后缀
this.moduleRule.forEach(rule => {
if (rule.test.test(module.resource)) {
options.moduleType = rule.moduleType;
......@@ -137,8 +143,15 @@ class Compiler {
})
options.moduleType = options.moduleType || 'other';
}
options.source = module._cmlSource || module._source && module._source._value;
}
// 可能出现module._cmlSource为空字符串的情况
if (module._cmlSource !== undefined) {
options.source = module._cmlSource;
} else {
options.source = module._source && module._source._value;
}
return new CMLNode(options)
}
......
......@@ -12,6 +12,9 @@ class mvvmGraphPlugin {
let PlatformPlugin = require(path.join(cml.projectRoot, 'node_modules', npmName)); // eslint-disable-line
let plugin = new PlatformPlugin(this.options);
let mvvmCompiler = new MvvmCompiler(compiler);
cml.event.on('find-component', function( {context, cmlFilePath, comPath, cmlType}, result) {
})
plugin.register(mvvmCompiler);
compiler.plugin('should-emit', function(compilation) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册