element/bin/build-entry.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

var Components = require('../components.json');
var fs = require('fs');
var render = require('json-templater/string');
var uppercamelcase = require('uppercamelcase');
var path = require('path');
var OUTPUT_PATH = path.join(__dirname, '../src/index.js');
var IMPORT_TEMPLATE = 'import {{name}} from \'../packages/{{package}}/index.js\';';
var ISNTALL_COMPONENT_TEMPLATE = ' Vue.component({{name}}.name, {{name}});';
2016-07-27 14:15:02 +08:00
var MAIN_TEMPLATE = `{{include}}
const install = function(Vue) {
2016-07-27 17:05:28 +08:00
if (install.installed) return;
2016-07-27 14:15:02 +08:00
{{install}}
2016-08-15 11:51:25 +08:00
Vue.use(Loading);
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$notify = Notification;
Vue.prototype.$message = Message;
2016-07-27 14:15:02 +08:00
};
// auto install
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
};
module.exports = {
install,
{{list}}
};
`;
2016-07-27 14:15:02 +08:00
delete Components.font;
2016-07-27 14:15:02 +08:00
var ComponentNames = Object.keys(Components);
2016-07-27 14:15:02 +08:00
var includeComponentTemplate = [];
var installTemplate = [];
var listTemplate = [];
2016-07-27 14:15:02 +08:00
ComponentNames.forEach(name => {
var componentName = uppercamelcase(name);
2016-07-27 14:15:02 +08:00
includeComponentTemplate.push(render(IMPORT_TEMPLATE, {
name: componentName,
package: name
}));
2016-07-27 14:15:02 +08:00
if (['Loading', 'MessageBox', 'Notification'].indexOf(componentName) === -1) {
installTemplate.push(render(ISNTALL_COMPONENT_TEMPLATE, {
name: componentName,
component: name
}));
2016-07-27 14:15:02 +08:00
}
listTemplate.push(` ${componentName}`);
});
2016-07-27 14:15:02 +08:00
var template = render(MAIN_TEMPLATE, {
include: includeComponentTemplate.join('\n'),
install: installTemplate.join('\n'),
list: listTemplate.join(',\n')
});
2016-07-27 14:15:02 +08:00
fs.writeFileSync(OUTPUT_PATH, template);
console.log('[build entry] DONE:', OUTPUT_PATH);
2016-07-27 14:15:02 +08:00