【发布时间】:2022-01-13 10:35:51
【问题描述】:
我遵循了打字稿指南:https://webpack.js.org/guides/typescript/ 当我以“生产”模式运行 webpack 时,它几乎没有发出任何东西。
这是我的 src/index.ts 文件的源代码:
export function foo() {
return 'foo'
}
export const bar = 'bar'
然后我运行命令:
webpack
我把'bundle.js'文件编译到'dist'文件夹中,代码是:
(()=>{"use strict"})();
但是如果去'src'文件夹并使用'tsc'命令编译,它会生成带有以下代码的javascript文件:
"use strict";
exports.__esModule = true;
exports.bar = exports.foo = void 0;
function foo() {
return 'foo';
}
exports.foo = foo;
exports.bar = 'bar';
我认为 'dist/bundle.js' 和 'src/index.js' 文件应该是相同的,至少功能应该相同。但是现在'dist/bundle.js'文件根本不起作用。
以下是其他相关文件的代码:
// package.json
{
"name": "webpack-ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-loader": "^9.2.6",
"typescript": "^4.5.2",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1"
}
}
// webpack.config.js
const path = require('path');
module.exports = {
mode: 'production',
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
};
// tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node"
}
}
谁能帮帮我?谢谢。
【问题讨论】:
标签: javascript typescript webpack webpack-5