webpack可以将源文件编译成输出路径

时间:2019-04-11 17:06:04

标签: webpack

我已经写了一个webpack.config.babel.js文件,并且我想用babel用该文件编译一些代码。

但是在执行代码后(没有警告和错误),输出路径中没有编译的文件。

使用命令webpack --config webpack.config.babel.js(当然还有.babelrc),任务编译成功结束,并且'dist /'文件夹中有已编译的文件,该文件在webpack.config.babel.js中指示。但是'dist /'不包含任何内容。

webpack.config.babel.js (注意:此文件使用es2015语法编写)

import path from 'path';
import webpack from 'webpack';

export default {
  devtool: '#source-map',
  entry: [
    'webpack-hot-middleware/client?noInfo=true&reload=true',
    path.resolve(__dirname, '../src/views/index.jsx'),
  ],
  output: {
    path: path.resolve(__dirname, '../dist'),
    filename: 'client.js',
    publicPath: '/dist',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
    ],
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
  ],
};

源代码: 如果我处于开发模式,我将尝试使用webpack-dev-middleware和webpack-hot-middleware编译捆绑软件。

server.js

const app = express();
app.use(express.static('./'));

if (process.env.NODE_ENV === 'development') {
  setupDevServer(app); // this function contains setup.dev.server.js
}

....

setup.dev.server.js (上面的 setupDevServer ()函数)

import webpack from 'webpack';
// import MemoryFS from 'memory-fs';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
// import webpackServerConfig from './webpack.server.config.babel';
import webpackClientConfig from './webpack.client.config.babel';

export default function (express) {
  const clientCompiler = webpack(webpackClientConfig);
  // const serverCompiler = webpack(webpackServerConfig);

  express.use(
    webpackDevMiddleware(
      clientCompiler,
      {
        publicPath: webpackClientConfig.output.publicPath,
        noInfo: false,
      },
    ),
  );
  express.use(
    webpackHotMiddleware(clientCompiler),
  );
  // serverCompiler.outputFileSystem = new MemoryFS();

  clientCompiler.run( // for debug purpose
    (error, stats) => {
      console.log(error, stats); // and there will be an error 'can not run webpack twice'
    },
  );
}

执行setupDevServer()之后应该有编译文件。但是什么都没有。我的控制台中没有警告或错误输出,并且'dist /'文件夹中没有编译的文件。

0 个答案:

没有答案