Home > Article > Web Front-end > Multi-entry project scaffolding implemented by webpack
A multi-entry project scaffolding based on webpack2, mainly using extract-text-webpack-plugin to implement js and css public code extraction, html-webpack-plugin to implement html multi-entry, and less-loader to implement less compiles, postcss-loader configures autoprefixer to automatically add browser-compatible prefixes, html-withimg-loader implements adding image version numbers and template functions into HTML, babel-loader implements ES6 transcoding function, and happypack multi-threading accelerates the construction speed.
├── dist # 构建后的目录 ├── config # 项目配置文件 │ ├── webpack.config.js # webpack 配置文件 │ └── postcss.config.js # postcss 配置文件 ├── src # 程序源文件 │ └── js # 入口 │ ├ └── index.js # 匹配 view/index.html │ ├ └── user │ ├ ├ ├── index.js # 匹配 view/user/index.html │ ├ ├ ├── list.js # 匹配 view/user/list.html │ ├ └── lib # JS 库等,不参与路由匹配 │ ├ ├── jquery.js │ └── view │ ├ └── index.html # 对应 js/index.js │ ├ └── user │ ├ ├── index.html # 对应 js/user/index.js │ ├ ├── list.html # 对应 js/user/list.js │ └── css # css 文件目录 │ ├ └── base.css │ ├ └── iconfont.css │ └── font # iconfont 文件目录 │ ├ └── iconfont.ttf │ ├ └── iconfont.css │ └── img # 图片文件目录 │ ├ └── pic1.jpg │ ├ └── pic2.png │ └── template # html 模板目录 │ └── head.html │ └── foot.html
Get the multi-entry array according to the
JS
directory
const ROOT = process.cwd(); // 根目录 let entryJs = getEntry('./src/js/**/*.js'); /** * 根据目录获取入口 * @param {[type]} globPath [description] * @return {[type]} [description] */ function getEntry (globPath) { let entries = {}; Glob.sync(globPath).forEach(function (entry) { let basename = path.basename(entry, path.extname(entry)), pathname = path.dirname(entry); // js/lib/*.js 不作为入口 if (!entry.match(/\/js\/lib\//)) { entries[pathname.split('/').splice(3).join('/') + '/' + basename] = pathname + '/' + basename; } }); return entries; } // webpack 配置 const config = { entry: entryJs, output: { filename: 'js/[name].js?[chunkhash:8]', chunkFilename: 'js/[name].js?[chunkhash:8]', path: path.resolve(ROOT, 'dist'), publicPath: '/' }, }
Use babel to implement ES2015 to ES5, less to css and use postcss to implement autoprefixer to automatically add browser compatibility, url-loader to implement css to reference images and fonts and add version numbers, html -withimg-loader implements html reference images, adds version numbers and implements template functions.
module: { rules: [ { test: /\.js$/, exclude: /(node_modules|bower_components)/, use: { loader: 'babel-loader?id=js', options: { presets: ['env'] } } }, { test: /\.(less|css)$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader?id=styles', use: [{ loader: 'css-loader?id=styles', options: { minimize: !IsDev } }, { loader: 'less-loader?id=styles' }, { loader: 'postcss-loader?id=styles', options: { config: { path: PostcssConfigPath } } } ] }) }, { test: /\.(png|jpg|gif)$/, use: [ { loader: 'url-loader', options: { limit: 100, publicPath: '', name: '/img/[name].[ext]?[hash:8]' } } ] }, { test: /\.(eot|svg|ttf|woff)$/, use: [ { loader: 'url-loader', options: { limit: 100, publicPath: '', name: '/font/[name].[ext]?[hash:8]' } } ] }, // @see { test: /\.(htm|html)$/i, loader: 'html-withimg-loader?min=false' } ] }, // postcss.config.js module.exports = { plugins: [ require('autoprefixer')({ browsers: ['> 1%', 'last 5 versions', 'not ie <= 9'], }) ] }
According to the directory correspondence, obtain the html entry corresponding to js
let entryHtml = getEntryHtml('./src/view/**/*.html'), configPlugins; entryHtml.forEach(function (v) { configPlugins.push(new HtmlWebpackPlugin(v)); }); // webpack 配置 resolve: { alias: { views: path.resolve(ROOT, './src/view') } }, /** * 根据目录获取 Html 入口 * @param {[type]} globPath [description] * @return {[type]} [description] */ function getEntryHtml (globPath) { let entries = []; Glob.sync(globPath).forEach(function (entry) { let basename = path.basename(entry, path.extname(entry)), pathname = path.dirname(entry), // @see minifyConfig = IsDev ? '' : { removeComments: true, collapseWhitespace: true, minifyCSS: true, minifyJS: true }; entries.push({ filename: entry.split('/').splice(2).join('/'), template: entry, chunks: ['common', pathname.split('/').splice(3).join('/') + '/' + basename], minify: minifyConfig }); }); return entries; }
Use happypack multi-threading to speed up the build, CommonsChunkPlugin implements extraction of public js into separate files, extract-text-webpack-plugin implements extraction of public css into separate files,
let configPlugins = [ new HappyPack({ id: 'js', // @see threadPool: HappyThreadPool, loaders: ['babel-loader'] }), new HappyPack({ id: 'styles', threadPool: HappyThreadPool, loaders: ['style-loader', 'css-loader', 'less-loader', 'postcss-loader'] }), new webpack.optimize.CommonsChunkPlugin({ name: 'common' }), // @see new ExtractTextPlugin({ filename: 'css/[name].css?[contenthash:8]', allChunks: true }) ]; entryHtml.forEach(function (v) { configPlugins.push(new HtmlWebpackPlugin(v)); }); // webpack 配置 plugins: configPlugins,
webpack-dev-server implements functions such as automatic refresh of the development environment
// webpack 配置 devServer: { contentBase: [ path.join(ROOT, 'src/') ], hot: false, host: '0.0.0.0', port: 8080 }
npm start
http://localhost:8080/view
cross-env realizes the distinction between development and production environment build
Command | Description |
---|---|
##npm run dev
| Development environment build, no code compression|
npm run build
| Production environment build, compressed code
Welcome star
Please indicate the source for reprinting:
The above is the detailed content of Multi-entry project scaffolding implemented by webpack. For more information, please follow other related articles on the PHP Chinese website!