gpt4 book ai didi

javascript - Webpack url 不正确

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:41 24 4
gpt4 key购买 nike

我在 webpack 正确解析 URL 时遇到问题。当在我的本地 Node 服务器上运行时(因为它很方便),它的工作方式就像一个魅力,但是当我将它上传到我的服务器时,一旦我将它放入子文件夹( http://hostname.com/folder ),如果我将它放在根目录中,它就会停止工作。

我通过 require break 在 JS 中加载的图像。它们位于我的项目根目录的“ Assets ”文件夹内。但它会在服务器的根目录中搜索。

Webpack 配置:

var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');

var config = require('./_config'); //paths config..

module.exports = {
entry: [
config.build('js', 'src'), //JavaScript entry point
config.build('css', 'src'), //CSS entry point
output: {
path: config.js.dest.path,
filename: config.js.dest.file //JavaScript end point
}, //quickest, webpack -d -p for production
devtool: 'eval',
module: {
//test: which filetype?,
//exclude: which folders to exclude
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel',
query: {
babelrc: path.join(__dirname, '.babelrc')
}
}, {
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint'
}, {
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css!postcss!sass?outputStyle=expanded')
}, {
test: /\.json$/,
loader: 'file?hash=sha512&digest=hex&name=../assets/[hash].[ext]'
}, {
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'file?hash=sha512&digest=hex&name=../assets/[hash].[ext]',
'image-webpack?{progressive:true, optimizationLevel: 7, interlaced: false, pngquant:{quality: "65-90", speed: 4}}'
]
}]
}, postcss: function(){
return [
require('postcss-will-change'),
require('postcss-cssnext')({
browsers: ['IE >= 10', 'last 2 version'],
features: {
autoprefixer: {
cascase: false
}
}
})
]
}, //webpack plugins
plugins: [
new webpack.optimize.DedupePlugin(),
//extract CSS into seperate file
new ExtractTextPlugin(
config.build('css', 'dest')
)
], eslint: {
configFile: path.join(__dirname, '.eslintrc'),
ignorePath: path.join(__dirname, '.eslintignore')
}, resolve: {
extensions: ['', '.json', '.js', '.css'],
fallback: path.join(__dirname, 'node_modules')
}, resolveLoader: {
fallback: path.join(__dirname, 'node_modules')
}
};

提前致谢!

最佳答案

设置publicPath指向您的 Assets 目录。示例:

output: {
path: config.js.dest.path,
filename: config.js.dest.file,
publicPath: 'assets'
}

此外,您需要像这样调整加载器定义:

loader: 'file?hash=sha512&digest=hex&name=./assets/[hash].[ext]'

完成这些调整后,您的应用程序可以正常运行,没有任何错误。

关于javascript - Webpack url 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34958208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com