element/scripts/cooking.demo.js

122 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-07-27 14:15:02 +08:00
var cooking = require('cooking');
var path = require('path');
2016-08-20 00:38:59 +08:00
var md = require('markdown-it')();
2016-07-27 14:15:02 +08:00
var Components = require('../components.json');
2016-09-20 11:29:35 +08:00
var striptags = require('../examples/utils/strip-tags');
2016-07-27 14:15:02 +08:00
function convert(str) {
str = str.replace(/(&#x)(\w{4});/gi, function($0) {
return String.fromCharCode(parseInt(encodeURIComponent($0).replace(/(%26%23x)(\w{4})(%3B)/g, '$2'), 16));
2016-08-23 16:57:58 +08:00
});
return str;
}
2016-07-27 14:15:02 +08:00
cooking.set({
2016-09-21 14:38:07 +08:00
entry: './examples/entry.js',
2016-07-27 14:15:02 +08:00
dist: './examples/element-ui/',
2016-09-20 18:51:35 +08:00
template: './examples/index.tpl',
publicPath: '/',
2016-07-27 14:15:02 +08:00
hash: true,
devServer: {
port: 8085,
log: false,
publicPath: '/'
},
2016-09-06 11:51:08 +08:00
minimize: true,
2016-09-21 14:38:07 +08:00
chunk: true,
2016-07-27 14:15:02 +08:00
extractCSS: true,
2016-09-21 14:38:07 +08:00
sourceMap: true,
2016-09-06 11:51:08 +08:00
extends: ['vue2', 'lint'],
postcss: function(webapck) {
return [
require('postcss-salad')({
browser: ['ie > 8', 'last 2 version'],
features: {
'partialImport': {
addDependencyTo: webapck
2016-07-27 14:15:02 +08:00
},
2016-09-06 11:51:08 +08:00
'bem': {
'shortcuts': {
'component': 'b',
'modifier': 'm',
'descendent': 'e'
},
'separators': {
'descendent': '__',
'modifier': '--'
}
2016-07-27 14:15:02 +08:00
}
}
2016-09-06 11:51:08 +08:00
})
];
2016-07-27 14:15:02 +08:00
}
});
cooking.add('loader.md', {
test: /\.md$/,
loader: 'vue-markdown-loader'
});
cooking.add('vueMarkdown', {
use: [
2016-08-22 14:04:03 +08:00
[require('markdown-it-container'), 'demo', {
2016-08-20 00:38:59 +08:00
validate: function(params) {
2016-08-23 16:57:58 +08:00
return params.trim().match(/^demo\s*(.*)$/);
2016-08-20 00:38:59 +08:00
},
render: function(tokens, idx) {
2016-08-23 16:57:58 +08:00
var m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);
2016-08-20 00:38:59 +08:00
if (tokens[idx].nesting === 1) {
2016-08-23 13:46:51 +08:00
var description = (m && m.length > 1) ? m[1] : '';
2016-08-23 16:57:58 +08:00
var html = convert(striptags(tokens[idx + 1].content, 'script'));
2016-08-23 13:46:51 +08:00
var descriptionHTML = description
? '<div class="description">' + md.render(description) + '</div>'
: '';
2016-08-23 16:57:58 +08:00
return `<demo-block class="demo-box">
2016-08-22 14:04:03 +08:00
<div class="source">${html}</div>
<div class="meta">
2016-08-23 13:46:51 +08:00
${descriptionHTML}
2016-08-22 14:04:03 +08:00
<div class="highlight">`;
2016-08-20 00:38:59 +08:00
}
2016-08-23 16:57:58 +08:00
return '</div></div></demo-block>\n';
2016-08-20 00:38:59 +08:00
}
2016-07-27 14:15:02 +08:00
}]
],
preprocess: function(MarkdownIt, source) {
MarkdownIt.renderer.rules.table_open = function() {
2016-07-27 14:15:02 +08:00
return '<table class="table">';
};
MarkdownIt.renderer.rules.fence = wrap(MarkdownIt.renderer.rules.fence);
return source;
}
});
var wrap = function(render) {
return function() {
2016-07-27 14:15:02 +08:00
return render.apply(this, arguments)
.replace('<code class="', '<code class="hljs ')
.replace('<code>', '<code class="hljs">');
2016-07-27 14:15:02 +08:00
};
};
cooking.add('resolve.alias', {
'main': path.join(__dirname, '../src'),
'packages': path.join(__dirname, '../packages'),
'examples': path.join(__dirname, '../examples')
});
var externals = {};
Object.keys(Components).forEach(function(key) {
2016-07-27 14:15:02 +08:00
externals[`packages/${key}/style.css`] = 'null';
});
// 开发模式不需要将不存在的 style.css 打包进去
cooking.add('externals', externals);
2016-09-21 14:38:07 +08:00
if (process.env.NODE_ENV === 'production') {
cooking.add('externals.vue', 'Vue');
cooking.add('externals.vue-router', 'VueRouter');
}
2016-07-27 14:15:02 +08:00
module.exports = cooking.resolve();