【问题标题】:webpack-dev-middleware does not compile output into folderwebpack-dev-middleware 不会将输出编译到文件夹中
【发布时间】:2015-01-06 20:00:23
【问题描述】:

我在我的服务器中使用 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”中。此文件夹始终为空。我做错了什么? 有人有想法吗?

最好的问候 一月

【问题讨论】:

    标签: javascript node.js express webpack


    【解决方案1】:

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

    writeToFile 可以是布尔值,也可以是接收文件路径并输出布尔值的函数。见documentation here

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

    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
    

    【讨论】:

      【解决方案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);
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        猜你喜欢
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        • 2017-07-06
        • 2019-03-02
        • 1970-01-01
        • 2017-11-28
        相关资源
        最近更新 更多