san-xui 是基于 san 开发的一套UI组件库,在百度云的控制台中得到了广泛的应用。
NPM:
npm i --save san-xui
这里使用的是 esl 作为 AMD Loader,使用之前需要配置一下 paths 和 packages,如下:
<script src="https://cdn.bdstatic.com/ecom/esl/2.2.0-rc.2/esl.js"></script>
<script>
require.config({
paths: {
san: "https://cdn.bdstatic.com/san/3.5.1-rc.1/san",
jquery: "https://unpkg.com/jquery@3.3.1/dist/jquery",
humanize: "https://unpkg.com/humanize@0.0.9/humanize",
lodash: "https://unpkg.com/lodash@4.17.5/lodash",
moment: "https://unpkg.com/moment@2.21.0/moment",
axios: "https://unpkg.com/axios@0.18.0/dist/axios",
clipboard: "https://unpkg.com/clipboard@2.0.0/dist/clipboard",
"async-validator": "https://cdn.bdstatic.com/console/async-validator/0.0.0/async-validator.bundle",
"big.js": "https://unpkg.com/big.js@5.0.3/big"
},
packages: [
{
name: "san-xui",
location: "https://unpkg.com/san-xui@0.2.0/lib",
main: "index"
}
]
});
</script>
引入所需要的样式代码:
<link rel="stylesheet" type="text/css" href="https://cdn.bdstatic.com/iconfont/iconfont.css" />
<link rel="stylesheet" type="text/css" href="https://unpkg.com/san-xui@0.2.0/dist/xui.css" />
最后是应用的代码:
define(function(require) {
const san = require("san");
const { Button, alert } = require("san-xui");
const App = san.defineComponent({
template: `<template><ui-button on-click="onBtnClick">Hello san-xui</ui-button></template>`,
components: {
"ui-button": Button
},
onBtnClick() {
alert("Button clicked");
}
});
function start() {
const app = new App();
app.attach(document.getElementById("root"));
}
return { start };
});
最终的效果如下:https://codepen.io/leeight/pen/QmMabe
通过 named import 导入所需要使用的组件
import 'san-xui/dist/xui.css';
import {defineComponent} from 'san';
import {Button} from 'san-xui';
// 引入单个的组件
import Button from 'san-xui/lib/x/components/Button';
const App = defineComponent({
template: `<template><ui-button>Hello san-xui</ui-button></template>`,
components: {
'ui-button': Button
}
});
const app = new App();
app.attach(document.body);
需要安装必要的一些插件
npm i --save-dev babel-loader css-loader style-loader less-loader less file-loader babel-preset-stage-0 babel-preset-env
然后补充上一些相关的配置
const path = require('path');
function alias(name) {
return path.dirname(require.resolve(name));
}
module.exports = {
...
resolve: {
mainFiles: ['index', 'main']
},
module: {
rules: [
{
test: /\.(png|gif|jpe?g|svg)$/,
use: [
{
loader: 'file-loader',
options: {
name(file) {
return 'assets/images/[hash].[ext]';
}
}
}
]
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.less$/,
use: [
{loader: 'style-loader'},
{loader: 'css-loader'},
{
loader: 'less-loader',
options: {
relativeUrls: true,
paths: []
}
}
]
},
{
test: /\.js$/,
exclude: /(node_modules|dist)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env', 'stage-0']
}
}
}
]
},
...
}