Skip to content

Commit

Permalink
添加 06-加载CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
hao-kuai committed Jul 17, 2019
1 parent 98074ab commit 90b5527
Show file tree
Hide file tree
Showing 9 changed files with 3,114 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 06-加载 CSS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
使用以下命令运行本地webpack不是特别方便,

```
npx webpack
//或者
npx webpack --config webpack.config.js
```
可以设置使用快捷方式:[npm scripts](https://docs.npmjs.com/misc/scripts)

## 一、 添加 npm scripts
编辑 package.json 文件,添加在 npm scripts 中添加一个 npm 命令:
```
{
"name": "demo02",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"webpack": "^4.35.3",
"webpack-cli": "^3.3.6"
},
"scripts": {
"build": "webpack",
"goujian": "webpack",
"build_config": "webpack --config webpack.config.js"
}
}
```

其中 build 是 指令名称,根据需要自定义即可;webpack 是重要的指令,不能写错。

## 二、 使用 npm scripts
在控制台中输入指令,即可生成 main.js 文件。

```
npm run build
//或者
yarn build
```

打开index.html文件即可看到 Hello webpack 字样。
> 每次执行指令前,需要手动删除 dist 路径下的 main.js 文件
> 参考链接
- [webpack起步](https://webpack.docschina.org/guides/getting-started/)
- [示例代码](https://github.com/1071942338/WebpackStudyNotes/tree/master/05-%E4%BD%BF%E7%94%A8%20npm%20scripts)
1 change: 1 addition & 0 deletions 06-加载 CSS/demo06/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
进入package.json所在目录执行 npm i 或者yarn 命令即可安装依赖
1 change: 1 addition & 0 deletions 06-加载 CSS/demo06/dist/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions 06-加载 CSS/demo06/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>06-加载CSS</title>
</head>
<body>
<script src="bundle.js"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions 06-加载 CSS/demo06/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "demo02",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"css-loader": "^3.0.0",
"style-loader": "^0.23.1",
"webpack": "^4.35.3",
"webpack-cli": "^3.3.6"
},
"scripts": {
"build": "webpack"
}
}
11 changes: 11 additions & 0 deletions 06-加载 CSS/demo06/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "./style.css"
//生成一个内容为Hello webpack !的div标签
function component() {
let element = document.createElement('div');
element.innerHTML = "Hello webpack !";
//添加class
element.classList.add("hello");
return element;
}
//将生成的div标签添加到body中去
document.body.appendChild(component());
3 changes: 3 additions & 0 deletions 06-加载 CSS/demo06/src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hello{
color: red;
}
20 changes: 20 additions & 0 deletions 06-加载 CSS/demo06/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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"
]
}
]
}
};
Loading

0 comments on commit 90b5527

Please sign in to comment.