An multi-page website starter based on https://github.com/lingobus/mpa-vue-template.
- webpack@4.2.0
- babel 7 with preset-env(using browserslist for browser compatibility)
- pug(for vue template and html template)
- vue@2.5.7
- stylus(stylus -> postcss)
- postcss with autoprefixer,preset-env(using browserslist for browser compatibility)
- multiple pages
- element-ui@2.4.8
- axios@0.18.0
- mock data
- a bunch of solutions......
using the best practice of multi-page webpack config is the principle of this project. you can:
- use
require('./some-img.png')
in pug entry. - write pug with hmr support(entry png, vue pug and their included or extended dependences ).
- js/css injection support for pug entry.
- put all dependences(pug,css,js,img...) of one page in one folder.
- specify page num and port name when running dev server(PAGE=lazyload PORT=8081 npm start)
# download repo
git clone https://github.com/lingobus/mpa-starter.git
cd mpa-starter
# install dependencies
yarn or npm install
npm run clone
# serve all pages with hot reload
npm start
# build all pages for production
npm run release
# specify one page
PAGE=login npm start
PAGE=login npm run release
# you can specify multiple pages with ',' as seperator
PAGE=index,login,register npm start # see https://github.com/lingobus/mpa-starter#known-issues
PAGE=index,login,register npm run release
# you can run dev-server with a different port number in development(usefull when develop multiple pages in parallel)
PAGE=lazyload PORT=8081 npm start
- pages/[name]/index.[contenthash].js: main js bundle
- pages/[name]/venders.[contenthash].js: node_modules dependences
- pages/[name]/index.[contenthash].css: imported .styl,.css and
<style></style>
in vue <style scoped></style>
in vue is injected to html because html element attribute likedata-v-2a8bbda4
generated by vue-loader changes by every build and we don't want it to change css file's [contenthash]- common/lib/* is imported with
<script src='*' />
in html template, so they has no relation with webpack. So puting large dependence in common/lib/ can speed up webpack compilation but may bring in compatibility problems. (Using webpack with browserslist's ecosystem for everything seems to be best practice.) - pages/[name]/[module-name].[contenthash].js:
import(/* webpackChunkName: "pages/dynamic-import/async-component" */'./async-component.vue')
will generate pages/dynamic-import/async-component.js
https://webpack.js.org/guides/code-splitting/#bundle-analysis
# generate build/prod/compilation-stats.json
PAGE=register npm run release
# analysis with https://github.com/webpack-contrib/webpack-bundle-analyzer
npm run analyzer
- common components and assets are placed under
src/commom
directory,page related code are placed undersrc/pages
directory,page entry must besrc/pages/[name]/index.js
orsrc/pages/[name]/${locale}/index.js
, page template must besrc/pages/[name]/index.pug
orsrc/pages/[name]/${locale}/index.pug
. /src/js/common/lib
hold external assets for sharing with multiple pages.
in styl
background: url('~@/img/common@2x.png')
background: url('./img/banner@2x.png')
in vue
img(src='@/img/common@2x.png')
img(src='./img/banner@2x.png')
background: url('~@/img/common@2x.png')
background: url('./img/banner@2x.png')
in pug
img(src=require('@/img/common/@2x.png'))
img(src=require('./img/banner@2x.png'))
- When you run
npm start
with multiple pages, all bundles will be rebuilded when you change code in one because webpack-dev-middleware seems don't support multi-compiler. So always specify one page likePAGE=* npm start
in development. If you want to develop multiple pages in parallel, run another dev-server with a different port number likePAGE=lazyload PORT=8081 npm start
.