如何忽略未被使用的文件进行编译



我正在构建一个TypeScript项目,我想根据某些条件生成输出文件(对于实际项目,它取决于env)。我在下面给出一个简化的代码示例。我正在使用webpack来构建项目。

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();
}

在上面的情况下,由于编译器永远不会去else块,testComponent.ts的代码应该永远不会被编译到输出文件中。下面是我得到的输出

(() => {
"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");
})();

供参考,webpack如下

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",
};
};
有人能帮我一下吗?

Thanks in advance

我希望testComponent.ts的代码不会出现在输出中。

goTois:

  • let,即可变
  • <
  • 变量/gh>

如果你想让它可摇树,它应该是const goToif (true)

相关内容

  • 没有找到相关文章

最新更新