【发布时间】:2018-10-30 19:32:46
【问题描述】:
案例是有主文件夹'Projects' 并在那里安装了所有带有配置的 webpack 4 文件。在“项目”文件夹中有许多文件夹,例如 'Project1', 'Project2', etc...我想从 webpack.config.js 所在的 'node_modules' 入口点开始,并在那里自定义更多入口点,如 ./Project1/js/app.jsx,输出如 ./Project1/dist/js/main.js,与 scss 和索引相同.html,全部在'dist' 文件夹中生成。问题是始终只在主 'Projects' 文件夹中安装一次 node_modules。
package.json:
{
"name": "webpack4_example",
"version": "1.0.0",
"main": "index.js",
"browserslist": ["last 2 versions"],
"scripts": {
"build": "webpack --mode production",
"watch": "webpack --watch --mode development",
"start": "webpack-dev-server --open --mode development"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"css-loader": "^0.28.11",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.4.0",
"node-sass": "^4.9.0",
"postcss-loader": "^2.1.5",
"prop-types": "^15.6.1",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"sass-loader": "^7.0.1",
"webpack": "^4.8.3",
"webpack-cli": "^2.1.3",
"webpack-dev-server": "^3.1.4"
}
}
webpack.config.js:
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: './Project1/js/app.jsx', // customized entry point
output: {
path: path.resolve(__dirname, 'dist'),
filename: './js/out.js' // generated ./Project1/dist/js/out.js
},
watch: true,
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: { minimize: true }
}
]
},
{
test: /\.(png|jpe?g)/i,
use: [
{
loader: 'url-loader',
options: {
name: './img/[name].[ext]',
limit: 10000
}
},
{
loader: 'img-loader'
}
]
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader'
]
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: './Project1/index.html', // not sure how to set these sections
filename: 'index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
})
]
};
.babelrc:
{
"presets": ["env", "react", "stage-2"]
}
据我了解,没有自定义入口点的 webpack 默认会搜索 'Projects/src/...' 以查找所有文件,如 scss、js、img、index.html、index.js 以对它们进行操作,并将生成 'Projects/dist' 文件夹生成的所有文件。
上面的配置表单编译没有错误,但不会在任何地方生成“dist”文件夹,所以我不确定编译过程是否正确完成。
不胜感激任何建议和解决方案
【问题讨论】:
-
好的,似乎 package.json 中的 'scripts' 部分可能有助于覆盖默认输入/输出:“scripts”:{“dev”:“webpack --mode development ./foo/ src/js/index.js --output ./foo/main.js", "build": "webpack --mode production ./foo/src/js/index.js --output ./foo/main.js " }
标签: javascript node.js configuration webpack-4 webpack.config.js