This article mainly introduces the detailed explanation of webpack express multi-page site development
After learning the webpack entry-level tutorial, I feel that it may be specially tailored for single-page applications, such as webpack react, webpack vue, etc., can solve the dependency loading and packaging problems of various resources. Even css is packaged in js and dynamically added to the dom document.
So if we want an ordinary web site with multiple pages, css is independent, and js loading requires modules?
Project address: webpackDemo_jb51.rar
Initialize the project, install dependencies
package.json
"devDependencies": { "css-loader": "^0.23.1", "extract-text-webpack-plugin": "^1.0.1", "file-loader": "^0.8.5", "html-loader": "^0.4.3", "html-webpack-plugin": "^2.9.0", "jquery": "^1.12.0", "less": "^2.6.0", "less-loader": "^2.2.2", "sass-loader": "^4.0.2", "style-loader": "^0.13.0", "url-loader": "^0.5.7", "webpack": "^1.12.13", "webpack-dev-server": "^1.14.1" }
Directory structure (I use express framework, others According to personal needs)
- webpackDemo - src #代码开发目录 - css #css目录,按照页面(模块)、通用、第三方三个级别进行组织 + page + common + lib - js #JS脚本,按照page、components进行组织 + page + components + template #HTML模板 - node_modules #所使用的nodejs模块 - public #express静态资源文件 - dist #webpack编译打包输出目录,无需建立目录可由webpack根据配置自动生成 + css + js + img #图片资源 + view #express静态资源文件(webpack编译打包输出view目录) package.json #项目配置 webpack.config.js #webpack配置
Development page
Create the index.js file in the src/js/page directory and the index.html file in the src/view directory . The entry js corresponds to the template file name.
index.html The content is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> <!-- 描述:head中无需再引入css以及facicon,webpack将根据入口JS文件的要求自动实现按需加载或者生成style标签 --> </head> <body> <!-- 描述:body中同样无需单独引入JS文件,webpack会根据入口JS文件自动实现按需加载或者生成script标签,还可以生成对应的hash值 --> </body> </html>
is such a simple HTML template. Do not introduce any CSS and JS. It can be automatically introduced for us through webpack packaging.
index.js content is as follows:
//引入css require("../../css/lib/base.css"); require("../../css/page/index.scss"); $('body').append('<p class="text">index</p>');
page1.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>page1</title> </head> <body> </body> </html>
page1.js:
//引入css require("../../css/lib/base.css"); require("../../css/page/page1.less"); $('body').html('page1');
webpack configuration (I use the express framework, Others are based on personal needs)
var path = require('path'); var webpack = require('webpack'); /* extract-text-webpack-plugin插件, 有了它就可以将你的样式提取到单独的css文件里, 妈妈再也不用担心样式会被打包到js文件里了。 */ var ExtractTextPlugin = require('extract-text-webpack-plugin'); /* html-webpack-plugin插件,重中之重,webpack中生成HTML的插件, 具体可以去这里查看https://www.npmjs.com/package/html-webpack-plugin */ var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: { //配置入口文件,有几个写几个 index: './src/js/page/index.js', page1: './src/js/page/page1.js' }, output: { path: path.join(__dirname, './public/dist/'), //输出目录的配置,模板、样式、脚本、图片等资源的路径配置都相对于它 publicPath: '/dist/', //模板、样式、脚本、图片等资源对应的server上的路径 filename: 'js/[name].js', //每个页面对应的主js的生成配置 chunkFilename: 'js/[id].chunk.js' //chunk生成的配置 }, module: { loaders: [ //加载器,关于各个加载器的参数配置,可自行搜索之。 { test: /\.css$/, //配置css的抽取器、加载器。'-loader'可以省去 loader: ExtractTextPlugin.extract('style-loader', 'css-loader') }, { test: /\.less$/, //配置less的抽取器、加载器。中间!有必要解释一下, //根据从右到左的顺序依次调用less、css加载器,前一个的输出是后一个的输入 //你也可以开发自己的loader哟。有关loader的写法可自行谷歌之。 loader: ExtractTextPlugin.extract('css!less') }, { test: /\.scss$/, //配置scss的抽取器、加载器。中间!有必要解释一下, //根据从右到左的顺序依次调用scss、css加载器,前一个的输出是后一个的输入 //你也可以开发自己的loader哟。有关loader的写法可自行谷歌之。 loader: ExtractTextPlugin.extract('css!scss') }, { //html模板加载器,可以处理引用的静态资源,默认配置参数attrs=img:src,处理图片的src引用的资源 //比如你配置,attrs=img:src img:src就可以一并处理src引用的资源了,就像下面这样 test: /\.html$/, loader: "html?attrs=img:src img:src" }, { //文件加载器,处理文件静态资源 test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file-loader?name=./fonts/[name].[ext]' }, { //图片加载器,雷同file-loader,更适合图片,可以将较小的图片转成base64,减少http请求 //如下配置,将小于8192byte的图片转成base64码 test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192&name=./img/[hash].[ext]' } ] }, plugins: [ new webpack.ProvidePlugin({ //加载jq $: 'jquery' }), new webpack.optimize.CommonsChunkPlugin({ name: 'commons', // 将公共模块提取,生成名为`commons`的chunk chunks: ['index','page1'], //提取哪些模块共有的部分 minChunks: 2 // 提取至少2个模块共有的部分 }), new ExtractTextPlugin('css/[name].css'), //单独使用link标签加载css并设置路径,相对于output配置中的publickPath //HtmlWebpackPlugin,模板生成相关的配置,每个对于一个页面的配置,有几个写几个 new HtmlWebpackPlugin({ //根据模板插入css/js等生成最终HTML favicon: './src/favicon.ico', //favicon路径,通过webpack引入同时可以生成hash值 filename: '../../views/index.html', //生成的html存放路径,相对于path template: './src/template/index.html', //html模板路径 inject: 'body', //js插入的位置,true/'head'/'body'/false hash: true, //为静态资源生成hash值 chunks: ['commons', 'index'],//需要引入的chunk,不配置就会引入所有页面的资源 minify: { //压缩HTML文件 removeComments: true, //移除HTML中的注释 collapseWhitespace: false //删除空白符与换行符 } }), new HtmlWebpackPlugin({ //根据模板插入css/js等生成最终HTML favicon: './src/favicon.ico', //favicon路径,通过webpack引入同时可以生成hash值 filename: '../../views/page1.html', //生成的html存放路径,相对于path template: './src/template/page1.html', //html模板路径 inject: true, //js插入的位置,true/'head'/'body'/false hash: true, //为静态资源生成hash值 chunks: ['commons', 'list'],//需要引入的chunk,不配置就会引入所有页面的资源 minify: { //压缩HTML文件 removeComments: true, //移除HTML中的注释 collapseWhitespace: false //删除空白符与换行符 } }) // new webpack.HotModuleReplacementPlugin() //热加载 ], //使用webpack-dev-server,提高开发效率 // devServer: { // contentBase: './', // host: 'localhost', // port: 9090, //默认8080 // inline: true, //可以监控js变化 // hot: true, //热启动 // } };
Okay, after completing the above configurations, execute the webpack
packaging command to complete the project packaging.
Hash: e6219853995506fd132a Version: webpack 1.14.0 Time: 1338ms Asset Size Chunks Chunk Names js/index.js 457 bytes 0 [emitted] index js/page1.js 392 bytes 1 [emitted] page1 js/commons.js 306 kB 2 [emitted] commons css/index.css 62 bytes 0 [emitted] index css/page1.css 62 bytes 1 [emitted] page1 css/commons.css 803 bytes 2 [emitted] commons favicon.ico 1.15 kB [emitted] ../../view/index.html 496 bytes [emitted] ../../view/page1.html 499 bytes [emitted] [0] ./src/js/page/index.js 170 bytes {0} [built] [0] ./src/js/page/page1.js 106 bytes {1} [built] + 7 hidden modules Child html-webpack-plugin for "../../view/page1.html": + 1 hidden modules Child html-webpack-plugin for "../../view/index.html": + 1 hidden modules Child extract-text-webpack-plugin: + 2 hidden modules Child extract-text-webpack-plugin: + 2 hidden modules Child extract-text-webpack-plugin: + 2 hidden modules
At this time, go to the views directory to view the generated index.html file, as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> <link rel="shortcut icon" href="/dist/favicon.ico" rel="external nofollow" ><link href="/dist/css/commons.css?e6219853995506fd132a" rel="external nofollow" rel="stylesheet"><link href="/dist/css/index.css?e6219853995506fd132a" rel="external nofollow" rel="stylesheet"></head> <body> <script type="text/javascript" src="/dist/js/commons.js?e6219853995506fd132a"></script><script type="text/javascript" src="/dist/js/index.js?e6219853995506fd132a"></script></body> </html>
You can see that in addition to retaining the content in the original template, the generated file also uses the entry file The definition of index.js, automatic addition requires the introduction of CSS and JS files, as well as favicon, and the corresponding hash value is also added.
Two questions
How does webpack automatically discover the entry file and configure the corresponding template
How to directly deal with styles and script automatic introduction issues
var path = require('path'); var webpack = require('webpack'); var glob = require('glob'); /* extract-text-webpack-plugin插件, 有了它就可以将你的样式提取到单独的css文件里, 妈妈再也不用担心样式会被打包到js文件里了。 */ var ExtractTextPlugin = require('extract-text-webpack-plugin'); /* html-webpack-plugin插件,重中之重,webpack中生成HTML的插件, 具体可以去这里查看https://www.npmjs.com/package/html-webpack-plugin */ var HtmlWebpackPlugin = require('html-webpack-plugin'); /** *将公共模块提取,生成名为`commons`的chunk */ var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin; //压缩 var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin; //判断开发模式 var debug = process.env.NODE_ENV !== 'production'; var getEntry = function(globPath, pathDir) { var files = glob.sync(globPath); var entries = {}, entry, dirname, basename, pathname, extname; for (var i = 0; i < files.length; i++) { entry = files[i]; dirname = path.dirname(entry); //文件目录 extname = path.extname(entry); //后缀名 basename = path.basename(entry, extname); //文件名 pathname = path.join(dirname, basename); pathname = pathDir ? pathname.replace(new RegExp('^' + pathDir), '') : pathname; entries[pathname] = ['./' + entry]; //这是在osx系统下这样写 win7 entries[basename] } console.log(entries); return entries; } //入口(通过getEntry方法得到所有的页面入口文件) var entries = getEntry('src/js/page/**/*.js', 'src/js/page/'); //提取哪些模块共有的部分从entries里面获得文件名称 var chunks = Object.keys(entries); //模板页面(通过getEntry方法得到所有的模板页面) var pages = Object.keys(getEntry('src/template/**/*.html', 'src/template/')); console.log(pages) var config = { entry: entries, output: { path: path.join(__dirname, './public/dist/'),//输出目录的配置,模板、样式、脚本、图片等资源的路径配置都相对于它 publicPath: '/dist/', //模板、样式、脚本、图片等资源对应的server上的路径 filename: 'js/[name].js', //每个页面对应的主js的生成配置 chunkFilename: 'js/[id].chunk.js?[chunkhash]' //chunk生成的配置 }, module: { loaders: [ //加载器 { test: /\.css$/, loader: ExtractTextPlugin.extract('style', 'css') }, { test: /\.less$/, loader: ExtractTextPlugin.extract('css!less') }, { test: /\.html$/, loader: "html?-minimize" //避免压缩html,https://github.com/webpack/html-loader/issues/50 }, { test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file-loader?name=fonts/[name].[ext]' }, { test: /\.(png|jpe?g|gif)$/, loader: 'url-loader?limit=8192&name=imgs/[name]-[hash].[ext]' } ] }, plugins: [ new webpack.ProvidePlugin({ //加载jq $: 'jquery' }), new CommonsChunkPlugin({ name: 'commons', // 将公共模块提取,生成名为`commons`的chunk chunks: chunks, minChunks: chunks.length // 提取所有entry共同依赖的模块 }), new ExtractTextPlugin('css/[name].css'), //单独使用link标签加载css并设置路径,相对于output配置中的publickPath debug ? function() {} : new UglifyJsPlugin({ //压缩代码 compress: { warnings: false }, except: ['$super', '$', 'exports', 'require'] //排除关键字 }), ] }; pages.forEach(function(pathname) { var conf = { filename: '../../views/' + pathname + '.html', //生成的html存放路径,相对于path template: 'src/template/' + pathname + '.html', //html模板路径 inject: false, //js插入的位置,true/'head'/'body'/false /* * 压缩这块,调用了html-minify,会导致压缩时候的很多html语法检查问题, * 如在html标签属性上使用{{...}}表达式,所以很多情况下并不需要在此配置压缩项, * 另外,UglifyJsPlugin会在压缩代码的时候连同html一起压缩。 * 为避免压缩html,需要在html-loader上配置'html?-minimize',见loaders中html-loader的配置。 */ // minify: { //压缩HTML文件 // removeComments: true, //移除HTML中的注释 // collapseWhitespace: false //删除空白符与换行符 // } }; if (pathname in config.entry) { favicon: './src/favicon.ico', //favicon路径,通过webpack引入同时可以生成hash值 conf.inject = 'body'; conf.chunks = ['commons', pathname]; conf.hash = true; } config.plugins.push(new HtmlWebpackPlugin(conf)); }); module.exports = config;
The following code is similar to the above. The essential difference is that all related files are put into one object through one method. This completes the effect of automatic introduction!
The above are all configurations in mac osx system, the win7 path may be different
glob: What is parsed here is different:
But the final requirement is
entries: { index: [ './src/template/index.js' ], page1: [ './src/template/page1.js' ] } pages: [ 'index', 'page1' ]
You need to change it accordingly according to the configuration of your personal computer
The above is the text I compiled, I hope it will be helpful to everyone
Related articles:
How to implement entry/leave animation in Vue
Detailed interpretation of the entry function run in webpack
Solution to the Bootstrap modal box submission BUG Solution
How to implement simple calculations in AngularJS
How to implement collection data traversal display in AngularJS
How to integrate the carousel chart in mint-ui in vue.js
The above is the detailed content of How to implement multi-page site development using webpack+express. For more information, please follow other related articles on the PHP Chinese website!

Vue是一款优秀的JavaScript框架,它可以帮助我们快速构建交互性强、高效性好的Web应用程序。Vue3是Vue的最新版本,它引入了很多新的特性和功能。Webpack是目前最流行的JavaScript模块打包器和构建工具之一,它可以帮助我们管理项目中的各种资源。本文就为大家介绍如何使用Webpack打包和构建Vue3应用程序。1.安装Webpack

区别:1、webpack服务器启动速度比vite慢;由于vite启动的时候不需要打包,也就无需分析模块依赖、编译,所以启动速度非常快。2、vite热更新比webpack快;vite在HRM方面,当某个模块内容改变时,让浏览器去重新请求该模块即可。3、vite用esbuild预构建依赖,而webpack基于node。4、vite的生态不及webpack,加载器、插件不够丰富。

随着Web开发技术的不断发展,前后端分离、模块化开发已经成为了一个广泛的趋势。PHP作为一种常用的后端语言,在进行模块化开发时,我们需要借助一些工具来实现模块的管理和打包,其中webpack是一个非常好用的模块化打包工具。本文将介绍如何使用PHP和webpack进行模块化开发。一、什么是模块化开发模块化开发是指将程序分解成不同的独立模块,每个模块都有自己的作

在vue中,webpack可以将js、css、图片、json等文件打包为合适的格式,以供浏览器使用;在webpack中js、css、图片、json等文件类型都可以被当做模块来使用。webpack中各种模块资源可打包合并成一个或多个包,并且在打包的过程中,可以对资源进行处理,如压缩图片、将scss转成css、将ES6语法转成ES5等可以被html识别的文件类型。

Webpack是一款模块打包工具。它为不同的依赖创建模块,将其整体打包成可管理的输出文件。这一点对于单页面应用(如今Web应用的事实标准)来说特别有用。

配置方法:1、用导入的方法把ES6代码放到打包的js代码文件中;2、利用npm工具安装babel-loader工具,语法“npm install -D babel-loader @babel/core @babel/preset-env”;3、创建babel工具的配置文件“.babelrc”并设定转码规则;4、在webpack.config.js文件中配置打包规则即可。

随着现代Web应用程序的复杂性不断增加,构建优秀的前端工程和插件系统变得越来越重要。随着SpringBoot和Webpack的流行,它们成为了一个构建前端工程和插件系统的完美组合。SpringBoot是一个Java框架,它以最小的配置要求来创建Java应用程序。它提供了很多有用的功能,比如自动配置,使开发人员可以更快、更容易地搭建和部署Web应用程序。W

Laravel开发:如何使用LaravelMix和Webpack优化文件大小?Laravel是一个非常流行的PHP框架,它提供了许多功能和工具,帮助开发者在构建Web应用程序时提高生产效率。其中,LaravelMix和Webpack是两个强大的工具,它们可以帮助您优化文件大小和提高性能。在本文中,我们将介绍如何使用LaravelMix和Webpack优


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
