0

Webpack: Localhost refused to connect

A webpack windows opens when I generate a yeoman excel add-in. After the process finishes, I close the window. But then all my add-ins stop working. Leaving the window open lets my add-ins work but is not what the customer expects. I’ve searched the web for several days without finding a solution. I'd appreciate a response.


webpack -version: System: OS: Windows 8 6.2.9200 CPU: (4) x64 Intel(R) Core(TM) i5-7400 CPU @ 3.00GHz Memory: 8.69 GB / 15.97 GB Binaries: Node: 18.16.0 - C:\Program Files\nodejs\node.EXE npm: 9.5.1 - C:\Program Files\nodejs\npm.CMD Browsers: Internet Explorer: 11.0.19041.1566 Packages: webpack: ^5.82.1 => 5.82.1 webpack-cli: ^5.0.2 => 5.0.2 webpack-dev-server: ^4.15.0 => 4.15.0


Webpack code:
const devCerts = require("office-addin-dev-certs");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const urlDev = "https://localhost:3000/";
const urlProd = "https://www.contoso.com/"; // CHANGE THIS TO YOUR PRODUCTION DEPLOYMENT     LOCATION

async function getHttpsOptions() {
const httpsOptions = await devCerts.getHttpsServerOptions();
return { ca: httpsOptions.ca, key: httpsOptions.key, cert: httpsOptions.cert };
}

module.exports = async (env, options) => {
const dev = options.mode === "development";
const config = {
devtool: "source-map",
entry: {
polyfill: ["core-js/stable", "regenerator-runtime/runtime"],
taskpane: ["./src/taskpane/taskpane.js", "./src/taskpane/taskpane.html"],
commands: "./src/commands/commands.js",
},
output: {
clean: true,
},
resolve: {
extensions: [".html", ".js"],
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
},
},
},
{
test: /\.html$/,
exclude: /node_modules/,
use: "html-loader",
},
{
test: /\.(png|jpg|jpeg|gif|ico)$/,
type: "asset/resource",
generator: {
 filename: "assets/[name][ext][query]",
},
},
],
},
plugins: [
new HtmlWebpackPlugin({
filename: "taskpane.html",
template: "./src/taskpane/taskpane.html",
chunks: ["polyfill", "taskpane"],
}),
new CopyWebpackPlugin({
patterns: [
{
from: "assets/*",
to: "assets/[name][ext][query]",
},
{
from: "manifest*.xml",
to: "[name]" + "[ext]",
transform(content) {
if (dev) {
return content;
} else {
return content.toString().replace(new RegExp(urlDev, "g"), urlProd);
}
},
},
],
}),
new HtmlWebpackPlugin({
filename: "commands.html",
template: "./src/commands/commands.html",
chunks: ["polyfill", "commands"],
 }),
],
devServer: {
headers: {
"Access-Control-Allow-Origin": "*",
 },
server: {
type: "https",
options: env.WEBPACK_BUILD || options.https !== undefined ? options.https : await getHttpsOptions(),
},
port: process.env.npm_package_config_dev_server_port || 3000,
},
};

return config;
};
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45

1 Answers1

1

Webpack runs a web server locally for serving your add-in files. In general, you must put your add-in to any web server to make the add-in files hosted anywhere. See webpack-dev-server API for more information.

See Host an Office Add-in on Microsoft Azure if you want to deploy your add-in to customers.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45