我是webpack的初學者用戶。我想寫一個webpack.config.js
來建立我的項目。但它有什麼問題!在webpack配置中上下文是什麼意思?
這裏是我的package.json
(所有dependenciens安裝):
{
"name": "webpack-101",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack --progress --colors",
"build": "webpack --progress --colors"
},
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^0.23.1",
"style-loader": "^0.13.0",
"webpack": "^1.12.14",
"webpack-dev-server": "^1.14.1"
}
}
我的項目目錄結構是:
--webpack-hello
---- dist
----src
--css
-- style.css
--js
-- entry.js
-- content.js
---- index.html
---- package.json
---- webpack.config.js
---- node_modules
entry.js
是:
// load css files
require("../css/style.css");
document.write("It works.");
document.write("<br/>");
document.write(require("./content.js"));
style.css
是:
body {
background: #f90;
}
的重要文件,webpack.config.js
是:
var path = require('path');
var webpack = require('webpack');
module.exports = {
context: __dirname + "/src",
entry: "./js/entry.js",
output: {
path: __dirname + "/dist",
filename: "bundle.js"
},
module: {
loaders: [{
test: /\.css$/,
loaders: ["style", "css"]
}]
}
};
當我運行npm run dev
,控制檯打印:
F:\my-workspace\codeTest\webpack\webpack-hello>npm run dev
> [email protected] dev F:\my-workspace\codeTest\webpack\webpack-hello
> webpack --progress --colors
▀ ╢░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Hash: 081ea611fafd2241cf14
Version: webpack 1.12.14
Time: 107ms
Asset Size Chunks Chunk Names
bundle.js 3.03 kB 0 [emitted] main
[0] ./js/entry.js 194 bytes {0} [built]
[2] ./js/content.js 93 bytes {0} [built]
+ 1 hidden modules
ERROR in ./css/style.css
Module not found: Error: Cannot resolve 'file' or 'directory' ./../../node_modules/css-loader/index.js in F:\my-workspace\codeTest\webpack\webpack-hello/src\css
@ ./css/style.css 4:14-79
ERROR in ./css/style.css
Module not found: Error: Cannot resolve 'file' or 'directory' ./../../node_modules/style-loader/addStyles.js in F:\my-workspace\codeTest\webpack\webpack-hello/src\css
@ ./css/style.css 7:13-71
▀ ╢░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
如果我修改webpack.config.js
(刪除context
):
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: "./src/js/entry.js",
output: {
path: __dirname + "/dist",
filename: "bundle.js"
},
module: {
loaders: [{
test: /\.css$/,
loaders: ["style", "css"]
}]
}
};
I牛逼效果很好:
F:\my-workspace\codeTest\webpack\webpack-hello>npm run dev
> [email protected] dev F:\my-workspace\codeTest\webpack\webpack-hello
> webpack --progress --colors
▀ ╢░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Hash: 798df3fe90bea39e31ad
Version: webpack 1.12.14
Time: 811ms
Asset Size Chunks Chunk Names
bundle.js 12 kB 0 [emitted] main
[0] ./src/js/entry.js 194 bytes {0} [built]
[5] ./src/js/content.js 93 bytes {0} [built]
+ 4 hidden modules
▀ ╢░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
我讀的WebPack的配置,它說的是context
:The base directory (absolute path!) for resolving the entry option.
。但爲什麼我添加它,CSS文件不能reslove?
什麼是管理webpack中的CSS文件的基本做法?
謝謝!