webpack單獨打包類庫文件


https://www.imooc.com/video/16410學習視頻

打包vue或其他第三方包

配置如下:

const path = require('path');
const isDev = process.env.NODE_ENV === 'development';
const HTMLPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const ExtractPlugin = require('extract-text-webpack-plugin'); //把非js代碼打包成單獨的資源文件,提高效率
const webpack = require('webpack');
const config = {
  target: 'web',
  entry: path.join(__dirname, 'src/index.js'), //__dirname 當前文件所在的目錄地址
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
      },
      {
        test: /\.jsx$/,
        loader: 'babel-loader',
      },
      {
        test: /\.(gif|jpg|jpeg|png|svg)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 1024,
              name: '[name]-aaa.[ext]',
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: isDev ? '"development"' : '"production"',
      },
    }),
    new VueLoaderPlugin(),
    new HTMLPlugin(),
  ],
  mode: 'development',
};
//判斷開發環境和正式環境   cross-env 運行跨平台設置和用環境變量的腳本
if (isDev) {
  config.module.rules.push({
    test: /\.styl/,
    use: [
      'style-loader',
      'css-loader',
      {
        loader: 'postcss-loader',
        options: {
          sourceMap: true,
        },
      },
      'stylus-loader',
    ],
  });
  config.devtool = '#cheap-module-eval-source-map';
  config.devServer = {
    port: 8000,
    host: '0.0.0.0',
    overlay: {
      errors: true,
    },
    hot: true,
    // open: true
  };
  config.plugins.push(
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  );
} else {
    //正式環境
  config.enter={
    app:path.join(__dirname,'src/index.js'),
    vendor:['vue']//可以添加相關的第三方庫
 }
  config.output.filename = '[name].[chunkhash:8].js';
  config.module.rules.push({
    test: /\.styl/,
    use: ExtractPlugin.extract({
      fallback: 'style-loader',
      use: [
        'css-loader',
        {
          loader: 'postcss-loader',
          options: {
            sourceMap: true,
          },
        },
        'stylus-loader',
      ],
    }),
  });
  config.plugins.push(
    new ExtractPlugin('styles.[contentHash:hex:8].css'),
    new webpack.optimize.CommonsChunkPlugin({
      name:'vendor' })
    );
}
module.exports = config;

2npm run build

會報錯難過(ಥ﹏ಥ),我們來看一下

 

 看來還是版本問題

3修改配置,解決以上問題

const path = require('path');
const isDev = process.env.NODE_ENV === 'development';
const HTMLPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const ExtractPlugin = require('extract-text-webpack-plugin'); //把非js代碼打包成單獨的資源文件,提高效率
const webpack = require('webpack');
const config = {
  target: 'web',
  entry: path.join(__dirname, 'src/index.js'), //__dirname 當前文件所在的目錄地址
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
      },
      {
        test: /\.jsx$/,
        loader: 'babel-loader',
      },
      {
        test: /\.(gif|jpg|jpeg|png|svg)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 1024,
              name: '[name]-aaa.[ext]',
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: isDev ? '"development"' : '"production"',
      },
    }),
    new VueLoaderPlugin(),
    new HTMLPlugin(),
  ],
  mode: 'development',
};
//判斷開發環境和正式環境   cross-env 運行跨平台設置和用環境變量的腳本
if (isDev) {
  config.module.rules.push({
    test: /\.styl/,
    use: [
      'style-loader',
      'css-loader',
      {
        loader: 'postcss-loader',
        options: {
          sourceMap: true,
        },
      },
      'stylus-loader',
    ],
  });
  config.devtool = '#cheap-module-eval-source-map';
  config.devServer = {
    port: 8000,
    host: '0.0.0.0',
    overlay: {
      errors: true,
    },
    hot: true,
    // open: true
  };
  config.plugins.push(
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  );
} else {
    //正式環境
  // config.enter={
  //   app:path.join(__dirname,'src/index.js'),
  //   vendor:['vue']//可以添加相關的第三方庫
  // }
  config.output.filename = '[name].[chunkhash:8].js';
  config.module.rules.push({
    test: /\.styl/,
    use: ExtractPlugin.extract({
      fallback: 'style-loader',
      use: [
        'css-loader',
        {
          loader: 'postcss-loader',
          options: {
            sourceMap: true,
          },
        },
        'stylus-loader',
      ],
    }),
  });
  config.plugins.push(
    new ExtractPlugin('styles.[contentHash:hex:8].css'),
    // new webpack.optimize.CommonsChunkPlugin({
    //   name:'vendor'
    // })
    );
    config.optimization={
      splitChunks:{
        cacheGroups:{
          commons:{
            name:'vendor',
          }
        }
      },
      runtimeChunk:true }
}
module.exports = config;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM