javascript - Webpack url 不正确

标签 javascript node.js webpack build-tools

我在 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/

相关文章:

javascript - JSON 循环对象类型 Reviver

javascript - 无法使用 Array.from 将 FileList 转换为 Array 并在 typescript 中传播语法

javascript - 如何在保持库模块化的同时在 Node 和浏览器中使用相同的库?

javascript - 语法错误 : Unexpected end of input error using Gulp and main-bower-files package

jquery - angular 2 wepack元素添加jquery(Bootstrap的JavaScript需要jQuery)

javascript - 使用javascript查找多个按钮的值

javascript - 检查 child_process 是否已在 node.js 中结束?

css - 'npm run dev' 和 'npm run build prod' 没有产生相同的输出

javascript - 将 Intro.JS 库添加到 Vue-Cli/Webpack 项目

JavaScript - 修改函数以便在以后的实例中可重用?