使用webpack提供公共静态文件

时间:2016-08-21 08:51:49

标签: webpack webpack-dev-server

我很难按照自己的意愿配置webpack。 我在 localhost:8080 上运行我的开发服务器,并希望通过 localhost:8080 / static / js / bundle.js 来提供我的应用程序,这就是我得到的webpack.config.js文件我已在下面附上。在我的文件结构中我已经附加了我想要将位于 dist / static 中的其他文件作为静态文件提供,因此 localhost:8080 / static / css / style.css 将提供此 dist / static / css / style.css 文件。

我在配置文件中可能出错了,我对webpack并不熟悉,所以我不知道我是否提出问题以便你理解。

我的目录树是:

client
  -- /dist
    -- /templates
      -- /admin
        -- index.html
    -- /static
      -- /css
        -- style.css
      -- /js
  -- /node_modules
  -- /src
  -- /test
  -- package.json
  -- webpack.config.json

webpack.config.js

var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

var plugins = [
    new webpack.ProvidePlugin({
        'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
    }),
    new ExtractTextPlugin('app.css', {
        allChunks: true
    }),
    new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
    })
];

var cssLoader = {};

if(process.env.NODE_ENV == 'production'){
    plugins.push(
        new webpack.optimize.UglifyJsPlugin()
    );

    cssLoader = {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]')
    };

} else {

    cssLoader = {
        test: /\.css$/,
        loaders: [
            'style?sourceMap',
            'css?modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]'
        ]
    }

}

module.exports = {
    entry: [
        'react-hot-loader/patch',
        'webpack-dev-server/client?http://localhost:8080',
        'webpack/hot/only-dev-server',
        './src/index.js'
    ],
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                loaders: ['babel']
            },
            cssLoader
        ]
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    output: {
        path: __dirname + '/dist/static',
        publicPath: '/static/js',
        filename: 'bundle.js'
    },
    devServer: {
        contentBase: './dist/templates/admin',
        hot: true,
        historyApiFallback: true
    },
    plugins: plugins
};

DIST /模板/管理/ index.html的

<!DOCTYPE html>
<html>
<head>
    <title>My App</title>
    <link href="static/css/style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="app"></div>
<script src="static/js/bundle.js"></script>
</body>
</html>

谢谢你们:)

3 个答案:

答案 0 :(得分:8)

问题

问题的根源是WebpackDevServer 仅从一个文件夹提供。您应该提供的文件夹是包含index.html的文件夹,这正是您正在进行的操作。

到目前为止,您只为 提供./dist/templates/admin的内容,因此当您在其他目录中查找文件时,您会获得404.唯一的例外是您的捆绑,因为您设置publicPath,将对路由/static/js的任何请求重定向到您的输出,该输出存储在内存中。

您需要WebpackDevServer才能从其他文件夹提供服务。在您的具体情况下,当您请求路径./dist/static/css时,您还需要从/static/css投放。


解决方案

您需要在WebpackDevServer中设置一些中间件。您可以按照documentation of devServer.setup中的说明执行此操作。为此,我建议您使用express.static,因为您可能已经在使用Express。

您需要要求Express:

const express = require('express')

然后,只需修改devServer,如下所示:

devServer: {
  contentBase: './dist/templates/admin',
  hot: true,
  historyApiFallback: true,
  setup (app) {
    app.use('/static/css/',
      express.static(path.join(__dirname, 'dist', 'static', 'css')));
    /* Using this commented code will break the HMR, see edit
    app.use('/static/js/',
      express.static(path.join(__dirname, 'dist', 'static', 'js')));
    */

    // in general
    app.use('/public/route/',
      express.static('/your/local/path'));
  }
}

这样您的开发和构建路径保持不变,WebpackDevServer在您需要的路径上提供您需要的静态文件。

修改

我刚刚发现上面的代码打破了热模块替换。原因是设置中的中间件正在处理/static/js/,因此捆绑包是从文件系统而不是内存中提供的。

要继续从内存中获取捆绑包,请在publicPath属性中定义output,并且不要在devServer.setup内的自定义中间件中处理它。

module.exports = {

  output: {
    path: ...,
    filename: ...,
    publicPath: '/static/js/'
  },

  devServer: {
    contentBase: './dist/templates/admin',
    hot: true,
    historyApiFallback: true,
    setup (app) {
      app.use('/static/css/',
        express.static(path.join(__dirname, 'dist', 'static', 'css')));
    }
  },

  // other properties: entry, module, etc.
}

答案 1 :(得分:1)

您正在端口8080上提供应用程序。在您的webpack配置中,我看到您设置内容基础dist / template / Admin但我在任何地方都看不到该文件夹​​。当您设置内容库时,它将从该文件夹中为Admin.html提供服务,如果您在该文件中声明了依赖项,则它将从您那里加载。 You can take a look at this seed了解有关它的更多信息。

答案 2 :(得分:1)

显然,从某种意义上来说,当你在另一台服务器上运行时,webpack-dev-server不应该被用作加载静态文件(如CSS或其他静态JS)的后端。当您将 contentBase 键设置为特定目录时,文档说您必须创建一个 index.html 文件,并且您可以根据此目录提供文件。

如果你想从其他目录提供静态文件,而你有另一台服务器与webpack-dev-server并行运行,请注意this section of the documentation

我所做的是在 localhost:8080 上运行webpack-dev-server,在 localhost:3000 上运行我的后端服务器,我的后端服务器为 admin /提供服务index.html 来自它的根路径,在 localhost:3000 / 上,并且在html文件本身中将bundle.js的导入更改为绝对路径 {{ 1}}

热重新加载现在运行正常,现在我也可以从我的webpack配置文件中删除 contentBase 参数。

重要:

  1. 现在按照我们的意愿运行webpack并使热重新加载真正起作用,只需在运行时添加 - inline ,例如:<script src="http://localhost:8080/static/js/bundle.js"></script>
  2. 你也可以使用iframe模式,但我没有潜入它。
  3. 仍然有一个问题,在构建项目时我必须手动删除绝对路径,这很烦人。现在看看如何自动解决它。