English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
客户需要一个具有树结构、带复选框的下拉选择控件;在网上找到了select2、autocomplete都不满足要求。于是自己用ztree加bootstrap dropdown组合开发了一个下拉树选择控件。决定使用webpack打包,开发一个完整的jQuery控件,顺便系统地学习一下webpack。
目录结构:
package.json配置:
{ "name": "select-tree", "version": "0.0.1", "description": "选择树形下拉,带复选框" "license": "MIT", "author": "kaikai", "repository": "https://gitee.com/hkgit/select-tree", "scripts": { "start": "webpack --watch", "build": "webpack --config webpack.config.js" }, "dependencies": { "jquery": "~1.12.4" "bootstrap": "^3.3.7", "jquery-slimscroll": "latest", "ztree": "latest" }, "devDependencies": { "css-loader": "^0.28.7", "html-webpack-plugin": "^2.30.1", "style-loader": "^0.19.1", "uglifyjs-webpack-plugin": "^1.1.4", "webpack": "^3.10.0" }, "bugs": { "url": "https://gitee.com/hkgit/select-tree/issues" }, "keywords": [ "javascript", "select", "tree", "checkbox" ] }
Spiegazione: jQuery utilizza la versione 1.12 per la compatibilità con il browser IE9, l'ambiente di sviluppo utilizza la modalità di monitoraggio di webpack, poiché il progetto è piccolo, la debug è fatta direttamente aprendo il file dist/select-tree.html con chrome.
webpack.config.js codice:
const path = require('path'); const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const UglifyJSPlugin = require('uglifyjs-webpack-plugin'); module.exports = { entry: { vendor: ['jquery'], // Separare i plugin da includere singolarmente, separati dal corpo del plugin main: './src/select-tree.js' }, output: { filename: 'select-tree-min.js', path: path.resolve(__dirname, './dist'), library: 'selectTree', // Nome del plugin libraryTarget: 'umd' // Plugin supporta CommonJS2, CommonJS, amd, var }, // resolve: { // Il jquery scaricato tramite npm non richiede specificare il percorso // modules: [path.join(__dirname, "node_modules")], // alias: { // jquery: 'jquery/dist/jquery.js' // } // }, module: { rules: [{ test: /\.css$/, use: ['style-loader', 'css-loader'] }] }, plugins: [ new HtmlWebpackPlugin({ // Genera automaticamente html template: './src/select-tree.html', filename: 'select-tree.html' }), new UglifyJSPlugin({ // Compatta il codice sourceMap: true }), new webpack.optimize.CommonsChunkPlugin({ // impacchetta separatamente i plugin jq, il suo repository di dipendenze viene estratto separatamente, senza influenzare lo sviluppo del plugin name: "vendor", filename: "vendor.min.js" }), new webpack.ProvidePlugin({ // Carica automaticamente jq $: 'jquery', jQuery: 'jquery' }) ], devtool: 'source-map' // Facilita la debug };
Spiegazione: l'attenzione è posta su output.library e output.libraryTarget
Ecco tutto il contenuto che abbiamo raccolto per questo articolo. Se avete domande, potete discuterle nella sezione dei commenti sottostante. Grazie per il supporto a Guida a Urla.