2

出于某种原因,我无法让 webpack 在更改时重建我的文件。我基本上遵循了Browsersync - Webpack + TypeScript Recipe

我的webpack.config.js

let path = require('path');
let webpack = require('webpack');

let config = {
  debug: true,
  devtool: 'eval',
  entry: './app/index.ts',
  output: {
    publicPath: '/',
    path: path.join(__dirname, 'wwwroot'),
    filename: 'bundle.js'
  },
  plugins: [],
  resolve: {
    extensions: ['', '.ts', '.js']
  },
  module: {
    loaders: [
      { test: /\.ts$/, loader: 'ts', include: path.join(__dirname, 'app') }
    ]
  }
};
module.exports = config;

server.js我从配方中复制的浏览器同步配置 ( ):

var browserSync          = require('browser-sync').create();
var webpack              = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var stripAnsi            = require('strip-ansi');
var webpackConfig = require('./webpack.config');
var bundler       = webpack(webpackConfig);

bundler.plugin('done', function (stats) {
    if (stats.hasErrors() || stats.hasWarnings()) {
        return browserSync.sockets.emit('fullscreen:message', {
            title: "Webpack Error:",
            body:  stripAnsi(stats.toString()),
            timeout: 100000
        });
    }
    browserSync.reload();
});

browserSync.init({
    server: 'wwwroot',
    open: false,
    logFileChanges: false,
    middleware: [
        webpackDevMiddleware(bundler, {
            publicPath: webpackConfig.output.publicPath,
            stats: {colors: true}
        })
    ],
    plugins: ['bs-fullscreen-message'],
    files: [
    ]
});

要开始这一切,我只需使用 npm 脚本部分:

"scripts": {
  "build": "node server"
},

每当我更改打字稿文件时,app/什么都没有发生。我在这里做错了什么?

4

1 回答 1

4

通过显式将 watch 部分添加到 webpack 开发中间件配置来修复它。

按照这里的要求是配置更改:

browserSync.init({
  ...
  middleware: [
    webpackDevMiddleware(bundler, {
      // Explicitly set watch options:
      watchOptions: {
        aggregateTimeout: 300,
        poll: true
      }
    })
  ]
});
于 2016-11-20T17:41:35.127 回答