我的webpack配置有些麻烦,我觉得我对我使用的目录结构感到困惑

时间:2016-09-25 22:35:06

标签: javascript node.js webpack

我正在尝试学习webpack,我认为我对__dirname感到困惑,并且正确的输入文件正常工作。基本上,我要使用的文件位于app/index.js,我的server.jswebpack.config.js位于config/

当我在根目录中保留server.jswebpack.config.js时,我的工作正常,但我想将其移到config文件夹中,我还添加了{{1 }}而不是保留app的内容,因为它现在在根目录中。

我有以下文件结构:

app

我的. |++[app] |----[actions] |----[modules] |----[reducers] |----[store] |----index.html |----index.js |--[config] |----server.js |----webpack.config.js |--[node_modules] |--package.json 中有以下内容:

server.js

var webpack = require('webpack'); var webpackDevMiddleware = require('webpack-dev-middleware'); var webpackHotMiddleware = require('webpack-hot-middleware'); var config = require('./webpack.config'); var path = require('path'); var app = new (require('express'))(); var port = 9001; var compiler = webpack(config); app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath })); app.use(webpackHotMiddleware(compiler)); app.get('/*', function(req, res) { res.sendFile(path.resolve(__dirname) + 'app/index.html'); }); app.listen(port, function(error) { if (error) { console.error(error); } else { console.info('started on localhost://%s.', port); } });

webpack.config.js

1 个答案:

答案 0 :(得分:0)

如果有帮助,以下是我当前的webpack配置

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

export default {
  target: 'node',
  node: {
    // prevents webpack from overwriting the values from node
    // https://github.com/webpack/webpack/issues/1599
    __dirname: false,
    __filename: false
  },
  devtool: 'source-map',
  resolve: {
    root: path.resolve(__dirname + '/app'),
    extensions: ['', '.js']
  },
  entry: {
    server: './app/app'
  },
  output: {
    path: path.join(__dirname + '/../dist'),
    filename: '[name].bundle.js',
    libraryTarget: 'commonjs'
  },
  // prevents node_modules from being bundled by webpack
  externals: [/^(?!\.|\/).+/i,],
  module: {
    loaders: [{
      loader: 'babel-loader',
      test: /\.js$/,
      exclude: /(node_modules|dist)/,
      cacheDirectory: 'webpack-cache',
      query: {
        presets: ['es2015'],
        plugins: [
          'transform-runtime',
          'transform-async-to-generator'
        ]
      }
    }]
  }
}