webpack 初学指南

栏目: CSS · 发布时间: 6年前

内容简介:这边用到的 webpack 版本 4.0+,这边将接触到 TypeScript,ES6,ES7,sass 等 loader …不想解释了直接上代码—- dist|- css

这边用到的 webpack 版本 4.0+,这边将接触到 TypeScript,ES6,ES7,sass 等 loader …不想解释了直接上代码

目录结构

—- dist

|- css

|- js

|- images

|- fonts

|- index.html

—- src

|- scss

|- js

|- images

|- fonts

|- index.html

package.json

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "serve": "webpack-dev-server --compress --progress ",
    "dev": "webpack --mode development",
    "build": "webpack --mode production"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "@babel/core": "^7.1.5",
    "@babel/preset-env": "^7.1.5",
    "autoprefixer": "^9.3.1",
    "babel-cli": "^6.26.0",
    "babel-eslint": "^10.0.1",
    "babel-loader": "^8.0.4",
    "babel-preset-env": "^1.7.0",
    "clean-webpack-plugin": "^1.0.0",
    "compression-webpack-plugin": "^2.0.0",
    "css-loader": "^1.0.1",
    "eslint": "^5.9.0",
    "eslint-loader": "^2.1.1",
    "file-loader": "^2.0.0",
    "html-webpack-plugin": "^3.2.0",
    "html-withimg-loader": "^0.1.16",
    "mini-css-extract-plugin": "^0.4.4",
    "node-sass": "^4.10.0",
    "open-browser-webpack-plugin": "0.0.5",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "postcss-loader": "^3.0.0",
    "sass-loader": "^7.1.0",
    "standard": "^12.0.1",
    "style-loader": "^0.23.1",
    "ts-loader": "^5.3.0",
    "typescript": "^3.1.6",
    "url-loader": "^1.1.2",
    "webpack": "^4.25.1",
    "webpack-cli": "^3.1.2",
    "webpack-dev-server": "^3.1.10"
  }
}

.babelrc

{
  "presets": ["@babel/preset-env"]
}

.eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true,
    browser: true
  },
  extends: [
    'standard'
  ]
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules"
  ]
}

webpack.config.js

const path = require('path')
// 拆分 css 出来单独打包
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
// 添加不同浏览器的兼容
const autoprefixer = require('autoprefixer')
// 打包之前清掉原打包信息
const CleanWebpackPlugin = require('clean-webpack-plugin')
// 结合 postcss 来解决不同浏览器的兼容
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const OpenBrowserPlugin = require('open-browser-webpack-plugin')
const CompressionWebpackPlugin = require('compression-webpack-plugin')

const productionGzipExtensions = ['js', 'css']

module.exports = {
  mode: 'development',
  entry: path.join(__dirname, './src/js/main.ts'),
  output: {
    filename: 'js/[name]-[hash:32].js',
    path: path.resolve(__dirname, 'dist')
  },
  resolve: {
    extensions: ['.js', '.json', '.css', '.scss', '.sass', 'ts'],
    alias: {
      '@': path.join(__dirname, 'src')
    }
  },
  module: {
    rules: [

      {
        test: /\.ts$/,
        exclude: /node_modules/,
        use: 'ts-loader'
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [

          {
            loader: 'babel-loader'
          }

        ]

      },
      {
        test: /\.(css)$/,
        // exclude: /node_modules/,
        use: [{
          loader: 'css-loader',
          options: {
            importLoaders: 2
          }
        }]
      },
      {
        test: /\.(sass|scss)$/,
        // exclude: /node_modules/,
        use: [

          {
            loader: MiniCssExtractPlugin.loader
          },
          {
            loader: 'css-loader',
            options: {
              importLoaders: 2
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: () => [
                autoprefixer({
                  browsers: [
                    'ie >= 8',
                    'ie_mob >= 10',
                    'ff >= 26',
                    'chrome >= 20',
                    'safari >= 6',
                    'opera >= 12',
                    'Firefox >= 24',
                    'Explorer >= 8',
                    'ios >= 5',
                    'android >= 2.3',
                    'bb >= 10',
                    'last 1 version'
                  ]
                })
              ]
            }
          },
          {
            loader: 'sass-loader'
          }

        ]
      },
      {
        test: /\.(jpg|png|gif|jpeg|woff|woff2|svg|ttf|eot)$/,
        use: [

          {
            loader: 'url-loader',
            options: {
              limit: 8192,
              name: 'images/[name]-[hash:32].[ext]'
            }
          }

        ]
      },
      {
        test: /\.html$/,
        loader: 'html-withimg-loader'
      },
      {
        test: /\.(woff|woff2|svg|ttf|eot)$/,
        use: [

          {
            loader: 'file-loader',
            options: {
              name: 'fonts/[name].[hash:8].[ext]'
            }
          } // 项目设置打包到dist下的fonts文件夹下

        ]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      minify: {
        removeAttributeQuotes: true // 去掉属性值后的双引号
      },
      hash: true,
      template: './src/index.html',
      inject: 'body'
    }),
    new CleanWebpackPlugin(['dist']),
    new MiniCssExtractPlugin({
      filename: 'css/[name]-[hash:32].css'
    }),
    new OptimizeCssAssetsPlugin({
      assetNameRegExp: /\.css$/g,
      cssProcessor: require('cssnano'),
      cssProcessorOptions: {
        safe: true,
        discardComments: {
          removeAll: true
        }
      },
      canPrint: true
    }),
    new OpenBrowserPlugin({
      url: 'http://localhost:3000'
    }),
    new CompressionWebpackPlugin({
      test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
      threshold: 8192,
      minRatio: 0.8
    })
  ],
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 3000
  }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

疯狂XML讲义

疯狂XML讲义

李刚 / 电子工业出版社 / 2009-11 / 65.00元

《疯狂XML讲义》主要以XML为核心,深入地介绍了XML的各种相关知识。《疯狂XML讲义》作为疯狂Java体系图书之一,依然保持该体系图书系统、全面的特点:不仅详细介绍了XML,文档的各种知识,还通过案例示范了实际开发中如何应用XML知识。 《疯狂XML讲义》主要分为五个部分。第一部分介绍了XML、DTD、XML Schema等基础知识,这些知识主要教读者如何定义有效的XML文档,这部分内容......一起来看看 《疯狂XML讲义》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具