"的WebPack" vs" webpack --watch"创造不同的输出

时间:2017-10-14 14:52:25

标签: javascript typescript webpack

我使用此设置作为我项目的基础:https://www.typescriptlang.org/docs/handbook/react-&-webpack.html

当我运行webpack时,它会编译一个在浏览器中运行的包。

当我运行webpack --watch时,它会重新编译文件更改,但会在浏览器中导致此错误:

Uncaught ReferenceError: exports is not defined

我查看了两者的输出,看起来webpack --watch 不包含webpack引导代码或我的模块 - 只有条目文件已编译

webpack

  • 在单个文件中包含我的所有模块,以及使用webpacks自己的模块要求。
  • 例如:var io = __webpack_require__(20);

webpack --watch

  • 仅包含我的输入模块 - 没有其他模块,没有__webpack_require__
  • E.g。 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"
}

1 个答案:

答案 0 :(得分:0)

修复方法是从outDir删除tsconfig.json

{
    "compilerOptions": {
        "outDir": "./dist/"
}
}

tsconfig.json文件有一个冲突的输出目录。使用webpack --watch

时,文件仅由打字稿写入