简体   繁体   English

flask-assets - 如何防止特定应用程序的静态 js 文件被另一个应用程序使用

[英]flask-assets - How to keep an specific app's static js files from being used by another app

I've done this in the past but I'm completely forgetting how I did it.我过去做过这个,但我完全忘记了我是怎么做的。 I'm developing a flask server that contains multiple apps with the static files bundled together but I want the js/css files in each app's static folder to be used only by the routes defined within that app's init.py file.我正在开发一个包含多个应用程序的 Flask 服务器,其中包含捆绑在一起的静态文件,但我希望每个应用程序的静态文件夹中的 js/css 文件只能由该应用程序的 init.py 文件中定义的路由使用。

Lets assume I have an appA and appB:假设我有一个 appA 和 appB:

app
- static // this contains my common js/css files
- apps
 - appA
   - __init__.py
   - static // appA's js/css files
 - appB
  - __init__.py
  - static // appB's js/css files

And I go to "localhost:8000:/appA" (assuming its a route i've defined).然后我转到“localhost:8000:/appA”(假设它是我定义的路线)。 In its js file I have在它的 js 文件中我有

$(document).ready(function(params) {
    console.log('appA ready');
});

And if I go to "localhost:8000/appB" and it has in its js file如果我转到“localhost:8000/appB”并且它的 js 文件中有

$(document).ready(function(params) {
    console.log('appB ready');
});

No matter which route I run I will see "appA ready" and "appB ready" printed in the console.无论我运行哪条路线,我都会在控制台中看到“appA ready”和“appB ready”。 Now I know that this makes sense.现在我知道这是有道理的。 I've bundled and minified them together after all.毕竟我已经将它们捆绑并缩小了。 But for the life of me I know I've used bundles in the past but was able to single out which app used what static file.但是对于我的生活,我知道我过去使用过捆绑包,但能够挑出哪个应用程序使用了什么静态文件。

The point is to use a base static directory for common stuff and the app's static directory for app-specific things.关键是将基本静态目录用于常见内容,而应用程序的静态目录用于特定于应用程序的内容。

I have my assets configured thusly我的资产是这样配置的

from app.apps.appA import appA
from app.apps.appA import appA_js, appA_css

from app.apps.appB import appB
from app.apps.appB import appB_js, appB_css


flask_app.register_blueprint(appA)
flask_app.register_blueprint(appB)

globals_js = ('js/utils/jquery-3.4.1.min.js',
          'js/utils/socket.io.js',
          'js/utils/*.js')
globals_css = ('css/utils/common.css',
           'css/utils/*.css')

assets = Environment(flask_app)
bundle_globals_js = Bundle(*globals_js + appA_js + appB_js, filters='jsmin', output='dist/local_js.js')
bundle_globals_css = Bundle(*globals_css + appA_css + appB_css, filters='cssmin', output='dist/local_css.css')

assets.register('base_js', bundle_globals_js)
assets.register('base_css', bundle_globals_css)

I feel like there's something about my asset bundle configuration that is wrong.我觉得我的资产包配置有问题。 Either that or it's how I'm importing the files in my html.要么是这样,要么就是我在我的 html 中导入文件的方式。

<head>
    {% assets "base_js" %}
        <script type="text/javascript" src="{{ ASSET_URL }}"></script>
    {% endassets %}
    {% assets "base_css" %}
        <link rel="stylesheet" href="{{ ASSET_URL }}"/>
    {% endassets %}
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{title}}</title>
</head>

This is the header code being used by each specific html file appA and appB has.这是每个特定的 html 文件 appA 和 appB 所使用的标头代码。 There may simply be a fundamental misunderstanding here about how bundles are used.这里可能只是对如何使用捆绑包存在根本性的误解。 I just want to specify a common static directory but be able to have my apps use their own js/css files in combination with those base files.我只想指定一个公共静态目录,但能够让我的应用程序将它们自己的 js/css 文件与这些基本文件结合使用。

Could someone point me in the right direction?有人能指出我正确的方向吗?

Thanks!谢谢!

I figured it out, although not in the way I remember doing it.我想通了,虽然不是我记得的方式。

Rather than bundle all my static files together as a "base_js" or "base_css" bundle, I created separate bundles with unique names.我没有将所有静态文件捆绑在一起作为“base_js”或“base_css”包,而是创建了具有唯一名称的单独包。

bundles = {
   'globals_js': Bundle('js/utils/jquery-3.4.1.min.js',
                     'js/utils/socket.io.js',
                     'js/utils/*.js',
                     filters='jsmin',
                     output='dist/local_global_js.js'),

   'globals_css': Bundle('css/utils/common.css',
                     'css/utils/*.css',
                     filters='cssmin',
                     output='dist/local_global_css.css'),       

   'appA_js': Bundle(*appA_js, 
                      filters='jsmin',
                      output='dist/local_appA_js.js'),

   'appA_css': Bundle(*appA_css, 
                       filters='cssmin',
                       output='dist/local_appA_css.css'),          

   'appB_js': Bundle(*appB_js, 
                               filters='jsmin',
                               output='dist/local_appB_js.js'),

   'appB_css': Bundle(*appB_css, 
                       filters='cssmin',
                       output='dist/local_appB_css.css')

}

Now I specify a base html file that adds the global assets at the head现在我指定一个基本的 html 文件,在头部添加全局资产

<head>
    {% assets "globals_js" %}
        <script type="text/javascript" src="{{ ASSET_URL }}"></script>
    {% endassets %}
    {% assets "globals_css" %}
        <link rel="stylesheet" href="{{ ASSET_URL }}"/>
    {% endassets %}
</head>
<body>        
 {%block data %}{% endblock %}

</body>
</html>

and within appA or appB I extend that base html file and add my specific static assets within the extended code block.在 appA 或 appB 中,我扩展了该基本 html 文件并在扩展代码块中添加了我的特定静态资产。

{% extends "base.html" %}

{% block data %}
{% assets "appA_js" %}
    <script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}
{% assets "appA_css" %}
    <link rel="stylesheet" href="{{ ASSET_URL }}"/>
{% endassets %}

{% endblock %}

Now appA/appB only load their respective static files.现在 appA/appB 只加载各自的静态文件。

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Rails 2.3应用程序中使捆绑的gem中的静态资产保持同步? - How do I keep static assets from a bundled gem in-sync in my Rails 2.3 app? 如何在没有静态资产的情况下不启动EmberJs App - How to not start an EmberJs App with static assets present 如何排除特定文件在 Chrome 中的缓存。 (如 js 文件) - How to exclude specific file/s from being Cached in Chrome. (like js files) 如何从另一个js应用程序启动js应用程序? - How do I start js app from another js app? 将一个应用的下一个 js 包文件导入另一个下一个 js 应用 - Importing one app's next js bundle files into another next js app 节点js express应用提供静态文件 - node js express app serve static files 如何从Angular资产文件夹中将js文件加载到特定的Angular(2,4,5)组件或模块中? - How to load js files from Angular assets folder to specific Angular(2,4,5) component or module? 更改 Flask 应用程序中的 Static 文件夹 - Changing the Static folder in a Flask App 如何从自定义表单将多个文件上传到 Magnolia CMS 资产应用程序 - How can I upload multiple files into Magnolia CMS assets app from a custom form 如何确定网站/ Web应用程序中未使用哪些文件? - How do I figure out what files are not being used in a website/web app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM