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
  }
}

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

查看所有标签

猜你喜欢:

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

信息架构

信息架构

[美] 路易斯·罗森菲尔德、[美] 彼得·莫尔维莱、[美] 豪尔赫·阿朗戈 / 樊旺斌、师蓉 / 电子工业出版社 / 2016-5-1 / 128.00元

本书的前三个版本都是信息架构领域的开山著作。其中描述了信息组织的普遍和永恒原则,这一原则也适用于不断增长的移动世界。在第4版中,作者运用大量最新的插图和例子为这些原则提供了当前实践中的情境,验证了那些与技术和供应商无关的工具,以及那些经受住时间考验的技术。一起来看看 《信息架构》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试