为 webpack 生成的 url 添加静态前缀 [英] Add a static prefix to urls generated in webpack

查看:177
本文介绍了为 webpack 生成的 url 添加静态前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 webpack 生成的每个静态 url 添加一个前缀 ("/static/").但是我希望生成的包忽略这个,所以 app.js 和 staticfiles 都在同一个目录中.文件加载器允许使用 ?name=static/[name].[ext] 指定前缀,但我的包随后出现在输出中的 static/ 目录中.

我想这样做是因为我从 tornado 提供我的应用程序,所以每个路径都需要某种前缀,否则我无法提供主页

Webpack 配置

模块:{装载机: [...{test:/\.(jpg|ttf|html|eot|woff2?|svg)$/, 加载器: "file?name=static/[hash].[ext]"},]},

龙卷风配置

ROOT_DIR = os.path.abspath(os.path.dirname(__file__))application = tornado.web.Application(handlers=[(r'/', MainHandler),(r'/socket', SocketHandler),(r'/utilization', UtilizationHandler)],自动重载=真,调试=假,template_path=os.path.join(ROOT_DIR, 'templates'),static_path=os.path.join(ROOT_DIR, 'public'),static_url_prefix='/static/')

上面设置的 static_url_prefix 实际上是默认设置.我无法将其设置为空或根路径转到 tornado 的 staticHandler 而不是我的 mainHandler.

解决方案

在 webpack.config.js 中设置 publicPath 选项.

输出:{路径:/home/proj/public/assets",公共路径:/静态/"}

https://github.com/webpack/docs/wiki/configuration#outputpublicpath

I want to add a prefix ("/static/") to every static url generated by webpack. However I want the generated bundle to ignore this, so app.js and staticfiles all end up in the same directory. The file loader allows specifying a prefix with ?name=static/[name].[ext] but my bundle then comes within a static/ dir in the output.

I want to do this because I am serving my app from tornado, so every path needs some kind of prefix or I can't serve the homepage

Webpack Config

module: {
    loaders: [
        ...
        {test: /\.(jpg|ttf|html|eot|woff2?|svg)$/, loader: "file?name=static/[hash].[ext]"},
    ]
},

Tornado config

ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

application = tornado.web.Application(handlers=[
        (r'/', MainHandler),
        (r'/socket', SocketHandler),
        (r'/utilization', UtilizationHandler)
    ],
    autoreload=True,
    debug=False,
    template_path=os.path.join(ROOT_DIR, 'templates'),
    static_path=os.path.join(ROOT_DIR, 'public'),
    static_url_prefix='/static/'
)

The static_url_prefix set above is actually the default. I can't set it to empty or the root path goes to tornado's staticHandler instead of my mainHandler.

解决方案

In webpack.config.js set the publicPath option.

output: {
    path: "/home/proj/public/assets",
    publicPath: "/static/"
}

https://github.com/webpack/docs/wiki/configuration#outputpublicpath

这篇关于为 webpack 生成的 url 添加静态前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆