电商_后台管理[4]

2019-12-19  本文已影响0人  Mark同学

可以完美运行的demo

安装 Node.js

安装 Yarn@1.21.1(官网默认版本)

安装 webpack

配置 webpack

webpack.config.js

const path = require('path')
module.exports = {
  entry: './src/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.js'
  }
}

src/app.js

console.log('hello')

处理 html

const HtmlWebpackPlugin = require('html-webpack-plugin')
// ...
plugins: [new HtmlWebpackPlugin()]

打包指定的 html

  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    })
  ]

src/index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="author" content="Mark">
  <title>HAPPYMMALL ADMIN</title>
</head>
<body>
  <div id="app">test</div>
</body>
</html>

处理 js

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env']
          }
        }
      }
    ]
  }

src/app.js

let a = 123
let test = (value) => {
  return value * 2
}
test(a)

处理 react

presets: ['env', 'react']

安装 react

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
  <h1>Hello, world!</h1>,
  document.getElementById('app')
);

处理 css

      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      }

src/index.css

#app {
  color: red;
}

src/app.js

import './index.css'

css 独立打包

const path = require('path')
var HtmlWebpackPlugin = require('html-webpack-plugin');
var { CleanWebpackPlugin } = require('clean-webpack-plugin')
+var MiniCssExtractPlugin = require("mini-css-extract-plugin")
var webpack = require('webpack')

module.exports = {
  mode: 'development',
  devtool: 'cheap-module-eval-source-map',
  entry: {
    main: './src/index.js',
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js'
  },
  module: {
    rules: [{ 
      test: /\.js$/, 
      exclude: /node_modules/, 
      loader: 'babel-loader'
    },{
      test: /\.(png|jpg|gif)$/,
      use: {
        loader: 'url-loader',
        options: {
          name: '[name].[ext]',
          outputPath: 'images/',
          limit: 2048
        }
      }
    },{
      test: /\.css$/,
      use: [
        'style-loader',
+        {
+          loader: MiniCssExtractPlugin.loader
+        },
        "css-loader",
        'postcss-loader'
      ]
    },{
      test: /\.scss$/,
      use: [
        'style-loader',
        {
          loader: 'css-loader', 
          options: {
            importLoaders: 2
          }
        },
        'sass-loader',
        'postcss-loader'
      ]
    },{
      test: /\.(eot|ttf|svg|woff)$/,
      use: {
        loader: 'file-loader',
      }
    }]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'src/index.html'
    }), 
    new CleanWebpackPlugin(),
    new webpack.HotModuleReplacementPlugin(),
+    new MiniCssExtractPlugin({
+      filename: "index.css"
+    })
  ],
  devServer: {
    contentBase: './dist',
    open: true,
    port: 8080,
    hot: true,
    hotOnly: true
  }
}

处理 sass

webpack.config.js

{
      test: /\.scss$/,
      use: [
        'style-loader',
+        {
+          loader: MiniCssExtractPlugin.loader
+        },
        {
          loader: 'css-loader', 
          options: {
            importLoaders: 2
          }
        },
        'sass-loader',
        'postcss-loader'
      ]
    }

src/index.scss

body {
  background: #ccc;
  #app {
    font-size: 100px;
  }
}

src/app.js

import './index.scss'

处理 font-awesome

import 'font-awesome/css/font-awesome.min.css'
<i className='fa fa-address-booke'></I>

提出公共模块

代码提交

代码提交
上一篇 下一篇

猜你喜欢

热点阅读