Vue+thinkJs博客網站(一)之vue多頁面應用的webpack3配置

一.項目簡介css

    本項目使用vue做爲前端框架,thinkJs做爲後端框架,構建我的博客網站,頁面分爲博客展現和後臺管理,主要目的是學習使用thinkJs。如今只完成了主要的博客增刪改功能,發現webpack的配置遇到了一些坑,這裏先記錄下。項目目錄結構以下:
圖片描述html

其中system文件夾是前端vue項目的代碼,博客展現頁面與後臺管理頁面都在這裏,打包成兩個頁面,具體webpack配置是本文重點。項目地址在這前端

二.搭建過程vue

安裝nodejs後,首先安裝thinkjsnode

npm install -g think-cli;
thinkjs new self_blog;
npm install;
npm start;

這樣thinkJs項目初步搭建完成了,接下來建立vue項目。項目根目錄下新建文件夾system。webpack

npm install -g vue-cli
vue init webpack self_blog
cd self_blog
npm install
npm run dev

這樣vue項目初步搭建完畢,接下來說下這裏vue的具體webpack配置。git

三.webpack配置github

項目前端頁面分爲博客的展現和後臺管理,這裏的思路是將sytem的src下面放後臺管理頁面,在sytem下新建blog文件夾,放前臺展現頁面,打包時生成兩個html,實現webpack打包多頁面應用。目錄以下:
圖片描述web

1.webpack.base.conf.js的配置vue-router

使用vue-cli建立項目,npm run dev運行開發環境,會使用webpack-dev-server做爲前端服務器,從而實現熱加載。這樣打包的文件只會在內存裏,這裏個人思路是直接生成到thinkJs項目的view目錄下,經過服務端的路由返回給前端。這樣只用啓動後端的服務就好了,由於文件已經打包到服務端,前端也不存在跨域訪問後端的問題了。因此這裏就不須要配置devServer了,而是改變html與js等靜態資源的生成目錄。首先看下webpack.base.conf.js的代碼:

"use strict";
const path = require("path");
const utils = require("./utils");
const config = require("../config");
const vueLoaderConfig = require("./vue-loader.conf");
const webpack = require("webpack");
const baseFileName = require("../package.json").name;
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const cleanWebpackPlugin = require("clean-webpack-plugin");
const AssetsPlugin = require("assets-webpack-plugin");
function resolve(dir) {
  return path.join(__dirname, "..", dir);
}

module.exports = {
  context: path.resolve(__dirname, "../"),
  entry: {
    app: "./src/main.js",
    blog: "./blog/index.js"
  },
  output: {
    path: config.build.assetsRoot,
    filename: "[name].js",
    publicPath:
      process.env.NODE_ENV === "production"
        ? config.build.assetsPublicPath
        : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: [".js", ".vue", ".json"],
    alias: {
      vue$: "vue/dist/vue.esm.js",
      "@": resolve("src")
    }
  },
  externals: {
    vue: "Vue",
    "vue-router": "VueRouter",
    echarts: "echarts",
    ElementUI: "element-ui"
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: "vue-loader",
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: "babel-loader",
        include: [
          resolve("src"),
          resolve("test"),
          resolve("node_modules/webpack-dev-server/client")
        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: "url-loader",
        exclude: [resolve("src/icons")],
        options: {
          limit: 10000,
          name: utils.assetsPath(baseFileName + "/img/[name].[hash:7].[ext]")
        }
      },
      {
        test: /\.svg$/,
        loader: "svg-sprite-loader",
        include: [resolve("src")],
        options: {
          symbolId: "icon-[name]"
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: "url-loader",
        options: {
          limit: 10000,
          name: utils.assetsPath(baseFileName + "/media/[name].[hash:7].[ext]")
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: "url-loader",
        options: {
          limit: 10000,
          name: utils.assetsPath(baseFileName + "/fonts/[name].[hash:7].[ext]")
        }
      }
    ]
  },
  plugins: [
    new AssetsPlugin({
      filename: "build/webpack.assets.js",
      processOutput: function(assets) {
        return "window.WEBPACK_ASSETS=" + JSON.stringify(assets);
      }
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: "vendor",
      minChunks: function(module) {
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          module.resource.indexOf(path.join(__dirname, "../node_modules")) === 0
        );
      }
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: "manifest",
      minChunks: Infinity
    }),
    new ExtractTextPlugin({
      filename: utils.assetsPath(
        baseFileName + "/css/[name].[contenthash].css"
      ),
      allChunks: true
    }),
    // 編譯前刪除以前編譯生成的靜態資源
    new cleanWebpackPlugin(["www/static/self_blog", "view/blog"], {
      root: resolve("../")
    })
  ],
  node: {
    setImmediate: false,
    dgram: "empty",
    fs: "empty",
    net: "empty",
    tls: "empty",
    child_process: "empty"
  }
};

首先須要改的是入口文件,由於是多頁面應用,須要多個入口文件來保證打包成不一樣的chunk。咱們知道正常vue-cli建立的但頁面應用,會打包成三個chunk,分別是vendor.js(第三方依賴),manifest.js(異步加載的實現),app.js(業務代碼)。這裏新增一個入口文件,打包時就會生成名爲blog.js的chunk,含有前臺展現頁面的js業務代碼。

這裏新增使用的clean-webpack-plugin,是爲了每次編譯後,刪除以前生成的js,css等靜態資源,不然由於這些資源不重名,就會一直堆在你生成的目錄下。

此外一些第三方依賴,vue,elementUI什麼的,我使用了cdn引入,這樣打包時這些依賴不會打進去,進而減少的vendor.js的體積。具體作法就是配置裏的externals定義這些依賴,而後在生成html的模板裏用script標籤直接引入cdn裏的js。

注意一下,若是你開發時想用vue-devtool,須要引入vue.js,若是不用就引入vue.min.js。

2.webpack.dev.conf.js的配置

先看下代碼:

"use strict";
const utils = require("./utils");
const webpack = require("webpack");
const config = require("../config");
const merge = require("webpack-merge");
const path = require("path");
const baseWebpackConfig = require("./webpack.base.conf");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const FriendlyErrorsPlugin = require("friendly-errors-webpack-plugin");
const portfinder = require("portfinder");

const HOST = process.env.HOST;
const PORT = process.env.PORT && Number(process.env.PORT);
const baseFileName = require("../package.json").name;
function resolve(dir) {
  return path.join(__dirname, "..", dir);
}
const devWebpackConfig = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({
      sourceMap: config.dev.cssSourceMap,
      extract: true,
      usePostCSS: true
    })
  },
  // cheap-module-eval-source-map is faster for development
  devtool: config.dev.devtool,
  output: {
    path: resolve(config.dev.assetsRoot),
    filename: "static/" + baseFileName + "/js/[name]-[hash:5].js",
    chunkFilename: "static/" + baseFileName + "/js/[name]-[id:5].js"
  },

  plugins: [
    new webpack.DefinePlugin({
      "process.env": require("../config/dev.env")
    }),
    // new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
    new webpack.NoEmitOnErrorsPlugin(),
    // https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: resolve(`../view/blog/index_index.html`),
      template: "./view/index.html",
      title: "博客管理系統",
      favicon: resolve("favicon.ico"),
      inject: true,
      chunks: ["manifest", "vendor", "app"]
    }),
    new HtmlWebpackPlugin({
      filename: resolve(`../view/blog/blog_index.html`),
      template: "./view/blog.html",
      title: "博客展現",
      inject: true,
      chunks: ["manifest", "vendor", "blog"]
    }),
    // copy custom static assets
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, "../static"),
        to: config.dev.assetsSubDirectory,
        ignore: [".*"]
      }
    ])
  ]
});
module.exports = devWebpackConfig;

這裏我刪掉了以前默認配置的devSever,以及module.exports直接導出devWebpackConfig。此外,由於打包生成目錄的變化,我修改了output裏面的path,filename,chunkFilename。使他們生成到self_blog根目錄的www/static下面,這也是thinkJs靜態資源的默認目錄。須要注意下,filename指的是你的出口文件(app),以及經過codespliting生成的js(vendor,manifest)的文件名;chunkFilename定義的是一些懶加載組件打包後的js文件名。

下面須要添加的就是html-webpack-plugin,由於須要打包成兩個html,因此使用兩次這個插件。除了定義生成文件名(filename),html模板(template)等,還須要定義你這個html須要的chunk,這是跟單頁面配置的一個區別。

除此以外,若是你想要開發環境打包時就分離出css,那麼在使用utils.styleLoaders時,將extract置爲true。由於這裏的方法判斷是開發環境才使用extract-text-plugin抽離css。

3.webpack.prod.conf.js的配置

prod的配置與dev差很少,生成文件目錄也沒變,只不過多了一些js,css壓縮插件。

"use strict";
const path = require("path");
const utils = require("./utils");
const webpack = require("webpack");
const config = require("../config");
const merge = require("webpack-merge");
const baseWebpackConfig = require("./webpack.base.conf");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const OptimizeCSSPlugin = require("optimize-css-assets-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const baseFileName = require("../package.json").name;
const env = require("../config/prod.env");
function resolve(dir) {
  return path.join(__dirname, "..", dir);
}
const webpackConfig = merge(baseWebpackConfig, {
  entry: {
    app: "./entry/entry-client-index",
    blog: "./entry/entry-client-blog"
  },
  module: {
    rules: utils.styleLoaders({
      sourceMap: config.build.productionSourceMap,
      extract: true,
      usePostCSS: true
    })
  },
  devtool: config.build.productionSourceMap ? config.build.devtool : false,
  output: {
    path: resolve(config.dev.assetsRoot),
    filename: "static/" + baseFileName + "/js/[name]-[chunkhash:5].js",
    chunkFilename: "static/" + baseFileName + "/js/[name]-[chunkhash:5].js"
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env": require("../config/prod.env"),
      "process.env.VUE_ENV": '"client"'
    }),
    new UglifyJsPlugin({
      uglifyOptions: {
        compress: {
          warnings: false
        }
      },
      sourceMap: config.build.productionSourceMap,
      parallel: true
    }),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    }),
    // extract css into its own file
    new ExtractTextPlugin({
      filename: utils.assetsPath(
        baseFileName + "/css/[name].[contenthash].css"
      ),
      allChunks: true
    }),
    new HtmlWebpackPlugin({
      minify: {},
      chunksSortMode: "dependency",
      environment: process.env.NODE_ENV,
      filename: resolve(`../view/blog/index_index.html`),
      template: "./view/index.html",
      title: "博客管理系統",
      favicon: resolve("favicon.ico"),
      inject: true,
      chunks: ["manifest", "vendor", "app"]
    }),
    new HtmlWebpackPlugin({
      filename: resolve(`../view/blog/blog_index.html`),
      template: "./view/blog.html",
      title: "博客展現",
      favicon: resolve("favicon.ico"),
      inject: true,
      chunks: ["manifest", "vendor", "blog"]
    }),
    // Compress extracted CSS. We are using this plugin so that possible
    // duplicated CSS from different components can be deduped.
    new OptimizeCSSPlugin({
      cssProcessorOptions: config.build.productionSourceMap
        ? { safe: true, map: { inline: false } }
        : { safe: true }
    }),
   
  ]
});

if (config.build.productionGzip) {
  const CompressionWebpackPlugin = require("compression-webpack-plugin");

  webpackConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: "[path].gz[query]",
      algorithm: "gzip",
      test: new RegExp(
        "\\.(" + config.build.productionGzipExtensions.join("|") + ")$"
      ),
      threshold: 10240,
      minRatio: 0.8
    })
  );
}

if (config.build.bundleAnalyzerReport) {
  const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
    .BundleAnalyzerPlugin;
  webpackConfig.plugins.push(new BundleAnalyzerPlugin());
}

module.exports = webpackConfig;

四.總結

具體能夠看github上的代碼

本次項目主要目的是練習thinkJs,這裏先把前期webpack配置方面的東西簡單說下,下一篇將會仔細講下thinkJs的使用方法和特性。項目會繼續更新,很多東西還沒作完呢。

相關文章
相關標籤/搜索