Webpack publicPath未在服务器端构建

时间:2016-07-26 11:28:38

标签: javascript webpack webpack-file-loader

我有一个带有两个webpack配置的通用JavaScript应用程序 - 一个用于服务器,另一个用于客户端。在这两个配置中,我使用file-loader来请求静态文件(字体,图像等)并获取这些资产的URL。

在这两个配置中,我都在使用publicPath /static/。在客户端构建中,资产URL正确地加上了前缀,但是在服务器端构建它们不是。

这是服务器端配置:

const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const universalConfig = require('./universal.config.js');

// Exclude all modules in node_modules
// http://jlongster.com/Backend-Apps-with-Webpack--Part-I
const nodeModules = fs.readdirSync('node_modules')
  .filter(x => {
    return x !== '.bin' && x !== 'gsap';
  }).reduce((container, module) => {
    container[module] = `commonjs ${module}`;
    return container;
  }, {});

module.exports = {
  target: 'node',
  resolve: resolve: {
    root: path.resolve(__dirname, '../'),
    extensions: ["", ".js", ".jsx"]
  },
  entry: [
    'babel-polyfill',
    'server/entry.js'
  ],
  output: {
    filename: 'server.js',
    path: path.resolve(__dirname, '../build/server'),
    publicPath: '/static/'
  },
  externals: nodeModules,
  plugins: [
    new webpack.BannerPlugin(
      'require("source-map-support").install();',
      { raw: true, entryOnly: false }
    ),
    new webpack.NormalModuleReplacementPlugin(/gsap/, 'node-noop')
  ],
  node: {
    __dirname: true
  },
  module: loaders: [
    {test: /\.jsx?$/, exclude: /node_modules/,loader: 'babel'},
    {test: /\.(eot|svg|ttf|woff2?|jpg|png)$/i, loader: 'file-loader'}
  ]
};

任何人都可以看到为什么output.publicPath配置选项未在require调用中使用?

0 个答案:

没有答案