webpack-css

1.说明

npm+webpack是本篇的基础,本篇需要的两个模块是 lodash style-loader css-loader,使用命令如下。

2.环境准备和命令

npm init -y
npm install --save-dev webpack
npm install --save-dev webpack-cli
npm install --save lodash
npm install --save-dev style-loader css-loader
npm  run build

3.代码目录

4.代码

 dist/index.html

<!doctype html>
<html>
  <head>
    <title>起步</title>
  </head>
  <body>
    <script src="bundle.js"></script>
  </body>
</html>

src/index.js 

import _ from 'lodash';
import './style.css';

function component() {
  var element = document.createElement('div');

  // Lodash(目前通过一个 script 脚本引入)对于执行这一行是必需的
  element.innerHTML = _.join(['Hello', 'webpack'], ' ');
  element.classList.add('hello');

  return element;
}

document.body.appendChild(component());

 src/style.css

.hello {
    color: red;
  }

 package.json

{
  "name": "qibu",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.15"
  },
  "devDependencies": {
    "css-loader": "^3.4.2",
    "style-loader": "^1.1.2"
  }
}

 webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
      rules: [
        {
          test: /\.css$/,
          use: [
            'style-loader',
            'css-loader'
          ]
        }
      ]
    }
};

5.运行效果

6.代码分析 

 

发布了472 篇原创文章 · 获赞 38 · 访问量 6万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览