下面是我的webpack配置。目前文件加载了这个main.js入口点
import './resources/assets/js/app.js';
import './resources/assets/sass/main.scss';
我可以在public / js文件中获取这两个文件,但我想将css和js放在他们自己的文件夹中。这可能吗?
var webpack = require('webpack');
var path = require('path');
let ExtractTextPlugin = require("extract-text-webpack-plugin");
var WebpackNotifierPlugin = require('webpack-notifier');
module.exports = {
resolve: {
alias: {
'masonry': 'masonry-layout',
'isotope': 'isotope-layout'
}
},
entry: './main.js',
devtool: 'source-map',
output: {
path: path.resolve(__dirname, './public'),
filename: 'bundle.js'
},
module: {
rules: [
{ test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader?presets[]=es2015",
},
{
test:/\.scss$/,
use: ExtractTextPlugin.extract({
use: [{loader:'css-loader?sourceMap'}, {loader:'sass-loader', options: {
sourceMap: true
}}],
})
},
/*{
test : /\.(png|jpg|svg)$/,
include : path.join(__dirname, '/dist/'),
loader : 'url-loader?limit=30000&name=images/[name].[ext]'
}, */
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
}
// other vue-loader options go here
}
},
]
},
plugins: [
//new webpack.optimize.UglifyJsPlugin(),
new ExtractTextPlugin('app.css'),
new WebpackNotifierPlugin(),
]
};
答案 0 :(得分:23)
是的,你可以这样做,这里有一个例子,它不需要你在你的js文件中导入sass文件:
const config = {
entry: {
main: ['./assets/js/main.js', './assets/css/main.scss'],
},
module: {
rules: [
{test: /\.(css|scss)/, use: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])}
// ...
],
},
output: {
path: './assets/bundles/',
filename: "[name].min.js",
},
plugins: [
new ExtractTextPlugin({
filename: '[name].min.css',
}),
// ...
]
// ...
}
您最终应该使用./assets/bundles/main.min.js
和./assets/bundles/main.min.css
。你必须明显添加js规则。
答案 1 :(得分:18)
我们有多个入口点和输出,并按如下方式定义:
entry: {
'js/main.min.js': './resources/assets/js/app.js',
'css/main.min.scss': './resources/assets/sass/main.scss'
},
output: {
filename: path.resolve(__dirname, './public/assets/[name]')
},
webpack读取条目的键并将其替换为输出文件名中的[name]
标记。 See documentation for "output filename"
答案 2 :(得分:3)
我有类似的需求,不确定这是否是正确的做法,但它对我有用。
output: {
path: path.join(__dirname, './public/js'),
filename:'bundle.js'
},
然后是css:
plugins: [
new ExtractTextPlugin("../css/[name].css")
],
答案 3 :(得分:2)
ExtractTextPlugin
已弃用CSS,推荐的方法是使用MiniCssExtractPlugin
。
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: ['index.js', 'index.css'],
module: {
rules: [
{
test: /\.(scss|css)$/,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name]-[hash].css'
})
]
};
您还需要单独安装npm i node-sass
。
答案 4 :(得分:0)
我有一个管理面板(panel.js和panel.scss)和非管理内容页面(index.js和main.scss),我想编译为2个CSS文件和2个js文件 这就是我所做的:
<style>
div {
background-image: url('custom1.png')
}
</style>
结果如下:
module.exports = {
entry: {
'pages': ['./src/js/index.js', './src/css/main.scss'],
'admin': ['./src/js/admin-index.js', './src/css/admin.scss']
},
output: {
filename: "./js/[name]-bundle.js",
path: path.resolve(__dirname, "dist"),
},
plugins: [
new MiniCssExtractPlugin(
{
filename: "./css/[name].css"
}
),
]
}
运作良好,希望对您有帮助
答案 5 :(得分:0)
mini-css-extract-plugin
可以用 webpack 5 修复
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: ['./src/js/index.js', './src/scss/index.scss'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ['@babel/preset-env']
}
}
}, {
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name]-[hash].css'
}),
]
};