我有以下Webpack配置文件:
const path = require('path'),
MiniCSSExtractPlugin = require('mini-css-extract-plugin'),
CleanWebpackPlugin = require('clean-webpack-plugin'),
postCssPresetEnv = require('postcss-preset-env'),
postCssScss = require('postcss-scss'),
glob = require('glob'),
autoprefixer = require('autoprefixer');
module.exports = {
entry: {
'material': glob.sync("./src/js/**/*.js"),
'material-light': './src/sass/material.scss'
},
output: {
path: path.resolve(__dirname, '/'),
filename: process.env.NODE_ENV === 'development' ? './dist/js/[name].js' : './docs/dist/js/[name].js',
publicPath: 'docs/dist/'
},
module: {
rules: [
{
test: /\.js$/,
exclude: ['/node_modules/', './dist/', '/src/css', '/docs'],
use: [
'babel-loader',
{
loader: 'eslint-loader',
options: {
fix: true
}
}
]
},
{
test: /\.(sa|sc|c)ss$/,
exclude: ['/node_modules', './dist', '/src/js', '/docs'],
use: [
MiniCSSExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
minimize: process.env.NODE_ENV === 'production',
publicPath: 'docs/dist/'
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
syntax: postCssScss,
plugins: () => [
autoprefixer,
postCssPresetEnv({
stage: 0
}),
],
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
]
},
devServer: {
overlay: true,
contentBase: path.join(__dirname, './docs/'),
publicPath: '/dist/',
watchContentBase: true,
open: true,
},
devtool: process.env.NODE_ENV === 'development' ? 'cheap-module-eval-source-map' : 'source-map',
plugins: [
new MiniCSSExtractPlugin({
filename: process.env.NODE_ENV === 'development' ? './dist/css/[name].css' : './docs/dist/css/[name].css',
}),
new CleanWebpackPlugin(['./dist/', './docs/dist']),
]
};
具有以下文件结构:
/
/docs/
All html files
/src/
/js/
All js files
/sass/
All sass files
webpack.config.js
packages.json
我想做的是以下几点。为了进行开发,我使用了webpack开发服务器来提供/docs
Directroy中的内容。但是,我希望文档中的URL相对于/docs
URL,以便可以使用GitHub页面。在/docs/index.html
中,我想将material.js
文件引用为:./dist/js
。对于material-light.css
为:./dist/css
。我想这样做是因为在运行js时,css文件会保存到docs/dist/(css/js)
目录中。
但是我不能得到正确的参考。在上面的配置中,我有docs/dist/
作为公共路径,但是docs/dist/js
也没有用。该问题与js和(s)css文件有关。
所以我的问题是如何使publicPath
在webpack开发服务器和github页面上都能正常工作?
相关但无效:
答案 0 :(得分:0)
配置存在多个问题。在github上问了同样的问题之后,我得到了以下配置:
const
autoprefixer = require('autoprefixer'),
CleanWebpackPlugin = require('clean-webpack-plugin'),
glob = require('glob'),
MiniCSSExtractPlugin = require('mini-css-extract-plugin'),
path = require('path'),
postCssPresetEnv = require('postcss-preset-env'),
postCssScss = require('postcss-scss');
const OUTPUT_DIR = path.resolve(__dirname, 'docs');
module.exports = {
entry: {
'material': glob.sync("./src/js/**/*.js"),
'material-light': './src/sass/material.scss'
},
output: {
path: OUTPUT_DIR,
filename: 'dist/js/[name].js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: ['/node_modules/', './dist/', '/src/css', '/docs'],
use: [
'babel-loader',
{
loader: 'eslint-loader',
options: {
fix: true
}
}
]
},
{
test: /\.(sa|sc|c)ss$/,
exclude: ['/node_modules', './dist', '/src/js', '/docs'],
use: [
MiniCSSExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
minimize: process.env.NODE_ENV === 'production',
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
syntax: postCssScss,
plugins: () => [
autoprefixer,
postCssPresetEnv({
stage: 0
}),
],
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
]
},
devServer: {
overlay: true,
contentBase: OUTPUT_DIR,
watchContentBase: true,
},
devtool: process.env.NODE_ENV === 'development' ? 'cheap-module-eval-source-map' : 'source-map',
resolve: {
extensions: ['.js', '.css', '.styl', '.scss'],
alias: {
'js': path.resolve(__dirname, './src/js'),
'css': path.resolve(__dirname, './src/css')
}
},
plugins: [
new MiniCSSExtractPlugin({
filename: 'dist/css/[name].css',
}),
new CleanWebpackPlugin(['./dist/', './docs/dist']),
]
};