0

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.

Lets assume I have an appA and 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). In its js file I have

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

And if I go to "localhost:8000/appB" and it has in its js file

$(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. 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.

<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. 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.

Could someone point me in the right direction?

Thanks!

huitlacoche
  • 173
  • 1
  • 2
  • 15

1 Answers1

3

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.

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

<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.

{% 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.

Thanks!

huitlacoche
  • 173
  • 1
  • 2
  • 15