从虚拟目录提供Webpack服务

3
我正在将所有文件构建到一个名为“dist”的文件夹中。
Dist

->index.html

->bundle.js

我的配置已经设置为使用我需要的特定端口运行。

{   
    entry: './src/index.js',
    devtool: 'source-map',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    devServer: {
        port: 9004,
        contentBase: path.join(__dirname, "dist")
    },
    watch: true,
}

我已经尝试添加


proxy: {
    "/virtual-directory-name-here": {
        "target": {
            "host": "localhost",
            "port": 9004
        }
    }
}

在devServer下,但是没有成功。我知道你可以在output下添加publicPath,但我不需要在虚拟目录中提供文件。我只想让它像IIS虚拟目录一样运行。
我的最终目标是从dist文件夹中的http://localhost:9004/virtual-directory-name-here提供所有文件。
有人能指点我吗?
2个回答

1

我也一直在寻找这个问题的解决方法。 通过使用devServer的代理选项,找到了一种解决方案:

proxy: {
    "/virtual-folder": {
        target: "http://127.0.0.1:9004",
        pathRewrite: {"^/virtual-folder" : ""},
    }
},

此外,如果有人在本地主机上使用https,可能需要将其添加到配置中:
secure: false 

0

我使用以下配置成功完成了它:

export = {
    devServer: {
        publicPath: "/virtual-directory-name-here/",
        openPage: "virtual-directory-name-here/",
    },
};

请注意,publicPath 前面有一个斜杠,但 openPage 没有。这似乎很重要。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接