我使用此设置作为我项目的基础:https://www.typescriptlang.org/docs/handbook/react-&-webpack.html
当我运行webpack
时,它会编译一个在浏览器中运行的包。
当我运行webpack --watch
时,它会重新编译文件更改,但会在浏览器中导致此错误:
Uncaught ReferenceError: exports is not defined
我查看了两者的输出,看起来webpack --watch
不包含webpack引导代码或我的模块 - 只有条目文件已编译。
webpack
var io = __webpack_require__(20);
webpack --watch
__webpack_require__
。var io = require("socket.io-client");
版本: - webpack:3.7.1 - tsc:1.8.10
module.exports = {
entry: "./src/index.tsx",
output: {
filename: "bundle.js",
path: __dirname + "/dist"
},
// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
"react": "React",
"react-dom": "ReactDOM"
}
答案 0 :(得分:0)
修复方法是从outDir
删除tsconfig.json
:
{
"compilerOptions": {
"outDir": "./dist/"
}
}
tsconfig.json文件有一个冲突的输出目录。使用webpack --watch