先裝好node和npm,由於webpack是一個基於node的項目。而後javascript
npm install -g webpack
建一個文件夾,而後新建一個package.json的文件在項目根目錄下css
mkdir testwebpack cd testwebpack npm init
package.json package name: (webpack-test) 包名(demo) version: (1.0.0) 版本 description: 描述 entry point: 入口程序(main.js) test command: 測試指令("dev": "webpack-dev-server","build": "webpack -p") git repository: node_modules keywords: 關鍵字 author: 做者 license: (ISC) MIT MIT Is this ok? (yes) yes
module.exports = { entry: './main.js', /*你要打包的js文件*/ output: { filename: 'bundle.js' /*打包後生成的文件*/ }, module: { rules:[ { test: /\.css$/, /*引入css文件配置*/ use: [ 'style-loader', 'css-loader' ] }, ] }, module: { rules:[ { test: /\.(png|jpg)$/, /*引入圖片文件配置*/ use: [ { loader: 'url-loader', options: { limit: 8192 } } ] } ] } };
document.write('<h1>Hello World</h1>'); require('./app.css'); var img = document.createElement("img"); img.src = require("./small.png"); document.body.appendChild(img);
<html> <body> <script type="text/javascript" src="bundle.js"></script> </body> </html>
npm i -g webpack webpack-dev-server
npm install
npm run dev