Steps to implement multi-page site development with webpack+express
This time I will bring you the steps to implement webpack express multi-page site development. What are the precautions for webpack express multi-page site development? The following is a practical case, let's take a look.
After studying the webpack entry-level tutorial, I feel that it may be specially designed for single-page applications, such as webpack react, webpack vue, etc., which can solve the dependency loading of various resources. Packaging issue. 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.rarInitialize 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 the express framework, others are based on 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. The content of index.html is as follows:nbsp;html> <meta> <title>首页</title> <!-- 描述:head中无需再引入css以及facicon,webpack将根据入口JS文件的要求自动实现按需加载或者生成style标签 --> <!-- 描述:body中同样无需单独引入JS文件,webpack会根据入口JS文件自动实现按需加载或者生成script标签,还可以生成对应的hash值 -->It is such a simple HTML template. Do not introduce any CSS and JS. It can be automatically introduced for us through webpack packaging. The content of index.js is as follows:
//引入css require("../../css/lib/base.css"); require("../../css/page/index.scss"); $('body').append('<p>index</p>');page1.html:
nbsp;html> <meta> <title>page1</title>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 modulesAt this time, go to the views directory to view the generated index.html file, as follows:
nbsp;html> <meta> <title>首页</title> <link><link><link> <script></script><script></script>You can see that in addition to retaining the content in the original template, the generated file also automatically adds the CSS and JS files that need to be introduced, as well as the favicon according to the definition of the entry file index.js, and also adds the corresponding hash value.
Two questions
- How webpack automatically discovers the entry file and configures the corresponding template How to directly handle the automatic introduction of styles and scripts
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
entries: { index: [ './src/template/index.js' ], page1: [ './src/template/page1.js' ] } pages: [ 'index', 'page1' ]Make corresponding changes according to the configuration of your personal computerI believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
How to synchronize multiple scroll bars with native JS
How to build the vue2-webpack2 framework
How mint-ui implements infinite scrolling loading function
The above is the detailed content of Steps to implement multi-page site development with webpack+express. For more information, please follow other related articles on the PHP Chinese website!

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.


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

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools