国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

webpack4+react多頁面架構的實現

2024-05-06 15:28:28
字體:
來源:轉載
供稿:網友

webpack在單頁面打包上應用廣泛,以create-react-app為首的腳手架眾多,單頁面打包通常是將業務js,css打包到同一個html文件中,整個項目只有一個html文件入口,但也有許多業務需要多個頁面不同的入口,比如不同的h5活動,或者需要支持seo的官方網站,都需要多個不同的html,webpack-react-multi-page架構讓你可以實現多頁面架構,在項目開發中保證每個頁面都可以熱更新并且打包后有清晰的文件層次結構。

Github地址

項目架構

技術使用

react16 webpack4 html-webpack-plugin 生成html文件 mini-css-extract-plugin css分離打包 uglifyjs-webpack-plugin js壓縮 optimize-css-assets-webpack-plugin css壓縮 es6 babel node opn 打開瀏覽器 compression 開啟gzip壓縮 express git

目錄結構github

|-- webpack-react-multi-page //項目  |-- dist //編譯生產目錄    |-- index      |-- index.css      |-- index.js    |-- about      |-- about.css      |-- about.js    |-- images    |-- index.html    |-- about.html  |-- node_modules //node包  |-- src //開發目錄    |-- index //index頁面打包入口      |-- images/      |-- app.js// index業務js      |-- index.scss      |-- index.js //index頁面js入口    |-- about //about頁面打包入口      |-- images/      |-- app.js// about業務js      |-- index.scss      |-- index.js //about頁面js入口    |-- template.html // html模板     |-- style.scss //公共scss  |-- webpackConfig //在webpack中使用    |-- getEntry.js //獲取入口    |-- getFilepath.js //遍歷文件夾    |-- htmlconfig.js //每個頁面html注入數據  |-- package.json  |-- .gitignore  |-- webpack.config.js //webpack配置文件  |-- www.js //生產啟動程序

wiki

webpack單頁面打包配置

webpack.config.js

module.exports = (env, argv) => ({  entry: ".src/index.js",  output: {    path: path.join(__dirname, "dist"),    filename: "bundle.js"  },  module: {    rules: [      ...    ],  },  plugins: [    new HtmlWebpackPlugin({      title: "首頁",      filename:"index.html",      favicon:"",       template: "./src/template.html",     })  ]});

這樣就可以在dist文件夾下打包出一個下面這樣的文件

<!DOCTYPE html><html lang="en">  <head>  <title>首頁</title>  <body>    <div id="root"></div>    <script type="text/javascript" src="bundle.js"></script>  </body></html>

webpack多頁面打包配置

webpack 的entry支持兩種種格式

打包單個文件

module.exports = {  entry: '.src/file.js',  output: {    path: path.resolve(__dirname, 'dist'),    filename: 'bundle.js'  }};

在dist下打包出一個bundle.js

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 西充县| 大石桥市| 天水市| 汪清县| 常州市| 老河口市| 玉环县| 临湘市| 枣强县| 正定县| 讷河市| 昌平区| 柯坪县| 上林县| 会泽县| 新丰县| 九台市| 宜宾市| 阿拉善右旗| 杭锦后旗| 油尖旺区| 两当县| 东兰县| 区。| 任丘市| 高阳县| 嘉义市| 霍城县| 美姑县| 娄烦县| 藁城市| 武山县| 淮阳县| 永康市| 元氏县| 工布江达县| 承德县| 大安市| 胶南市| 丰原市| 孟连|