3

我在我的服务器中使用 webpack-dev-middleware 来编译 javascript,如下所示:

if (development){                                                          
  app.use(webpackMiddleware(webpack({                                      
     // webpack options                                                   
     // webpackMiddleware takes a Compiler object as first parameter      
     // which is returned by webpack(...) without callback.               
    entry: {                                                               
      dashboard: path.join(__dirname, 'scripts/dashboard.jsx'),            
      tasks: path.join(__dirname, 'scripts/tasks.jsx')                     
    },                                                                     
       output: {                                                            
          path: __dirname + 'dist',                                        
          filename: '[name].bundle.js',                                    
          // no real path is required, just pass "/"                       
          // but it will work with other paths too.                        
      },                                                                   
      resolve: {                                                           
        extensions: ['', '.js', '.jsx']                                    
      },                                                                   
      module: {                                                            
        loaders: [                                                         
           { test: /\.jsx$/, loader: "jsx" }                            
      ]                                                                  
      }                                                                    
  }                                                                        
  ),                                                                       
  {                                                                        
    stats: {                                                               
     colors: true                                                         
    }                                                                      
  }));                                                                     
} 

在开发中一切正常,我可以在我的视图中包含捆绑包。但是在生产中我不能包含它们,因为它们没有构建到“dist”中。此文件夹始终为空。我做错了什么?有人有想法吗?

最好的问候

4

3 回答 3

7

webpack-dev-middleware不输出任何文件。如果要输出真实文件,需要使用webpackexpress之外的命令。你可以将你的 webpack 配置放到一个单独的文件中,并在你的webpack命令中引用它,并在你的 express 脚本中要求它来访问它。

于 2014-12-20T10:39:17.313 回答
2

如果你想使用内存包,你可以像这样使用流文件:

var express = require('express');
var app = express();
var webpack = require('webpack');
var path = require('path');

var compiler = webpack(require('./webpack.config.js'));

app.use(require('webpack-dev-middleware')(compiler, {
  noInfo: true,
  publicPath: '/'
}));

app.use('*', function (req, res, next) {
  var filename = path.join(compiler.outputPath,'index.html');
  compiler.outputFileSystem.readFile(filename, function(err, result){
    if (err) {
      return next(err);
    }
    res.set('content-type','text/html');
    res.send(result);
    res.end();
  });
});

app.listen(3000);
于 2016-10-09T08:58:19.473 回答
1

中有一个可用的选项webpack-dev-middleware

writeToFile可以是布尔值或接收文件路径并输出布尔值的函数。请参阅此处的文档

在这里,我使用了一个函数,因此所有中间包都不会写入磁盘,只有整个包:

const webpack = require('webpack');
const webpackconfig = require('./webpack.config.js');
const webpackMiddleware = require("webpack-dev-middleware");

function shouldWrite(filePath) {
    if (path.basename(filePath) == 'bundle.js' || path.basename(filePath) == 'bundle.js.map') {
        return true
    }
    else {
        return false;
    }
}

const webpackCompiler = webpack(webpackconfig);
const wpmw = webpackMiddleware(webpackCompiler, {
        noInfo: true,
        publicPath: webpackConfig.output.publicPath,
        writeToDisk: shouldWrite
    });

//express
app.use(wpmw); // dev-middleware
于 2020-08-11T22:37:21.640 回答