Currently, I am working on a TypeScript project where I need to generate an output file based on certain conditions (dependent on the environment). Below is a simplified example of the code snippet. I am utilizing webpack for building the project.
testComponent.ts
export const someComponent = () => {
console.log("This is Some Component");
};
index.ts
import { someComponent } from "./components/testComponent";
let goTo = 1;
if (goTo === 1) {
console.log("Will go to this");
} else {
someComponent();
}
In the scenario above, since the compiler will never reach the `else` block, the code from `testComponent.ts` should not be compiled into the output file. However, the current output generated is as follows:
(() => {
"use strict";
var o = {
310: (o, e) => {
Object.defineProperty(e, "__esModule", { value: !0 }),
(e.someComponent = void 0),
(e.someComponent = function () {
console.log("This is Some Component");
});
},
},
e = {};
function t(n) {
var r = e[n];
if (void 0 !== r) return r.exports;
var s = (e[n] = { exports: {} });
return o[n](s, s.exports, t), s.exports;
}
t(310), console.log("Will go to this");
})();
For reference, here is the webpack configuration being used:
const path = require("path");
const bundleOutputDir = "./dist";
module.exports = (env) => {
return {
entry: "./src/index.ts",
output: {
filename: "output.js",
path: path.resolve(bundleOutputDir),
},
devServer: {
contentBase: bundleOutputDir,
},
plugins: [],
module: {
rules: [
{
test: /\.ts?$/,
use: "ts-loader",
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [".ts", ".js"],
alias: {
"@": path.resolve(__dirname, "src"),
},
},
mode: "production",
};
};
If anyone could provide assistance with resolving this issue, it would be greatly appreciated. Thank you in advance.
I am hoping to remove the code from `testComponent.ts` from the output file.