def add_webpack_filter(app): if os.environ['FLASK_ENV'] != 'production': from jinja2_webpack import Environment as WebpackEnvironment from jinja2_webpack.filter import WebpackFilter webpack_env = WebpackEnvironment( publicRoot='/static', manifest='./av_dashboard/static/webpack-manifest.json') app.jinja_env.filters['webpack'] = WebpackFilter(webpack_env)
def __webpack_to_jinja(self): """Intergrate webpack into jinja2 render template""" from jinja2_webpack import Environment as WebpackEnvironment from jinja2_webpack.filter import WebpackFilter try: # Time to load in NPM manifest manifest = self._get_pathlib('static', 'vue', 'manifest.json') if not manifest.is_file(): with open(str(manifest), 'w') as f: f.write("{}") webpack_env = WebpackEnvironment(manifest=str(pathlib.PurePosixPath(manifest)), publicRoot='') self.server.jinja_env.filters['webpack'] = WebpackFilter(webpack_env) except FileNotFoundError: raise FileNotFoundError('The frontend needs to be compiled first')
def includeme(config): registry_settings = config.registry.settings settings = webpack_settings_from_settings(registry_settings) registry_settings['webpack'] = settings # Load the webpack environment environment = Environment(**settings) config.registry.registerUtility(environment, IWebpackEnvironment) # Add callbacks config.add_directive('get_webpack_environment', get_webpack_environment) config.add_request_method(get_webpack_environment, 'webpack_environment', reify=True) # Expose a webpack filter to jinja2 environment webpack_filter = WebpackFilter(environment) try: jinja2_env = config.get_jinja2_environment() except AttributeError: raise Jinja2EnvironmentMissingException( 'Unable to find jinja2 environment. ' 'Try config.commit() after including jinja2') jinja2_env.filters['webpack'] = webpack_filter
def test_invalid_error(): env = Environment(manifest=None, errorOnInvalidReference=True) f = WebpackFilter(env) with pytest.raises(AssetNotFoundException): f('a')
def test_invalid_empty(): env = Environment(manifest=None, errorOnInvalidReference=False) f = WebpackFilter(env) assert f('a') is None
def test_basename_lookup(): env = Environment(manifest={'a': 'b'}) f = WebpackFilter(env) assert f('/path/to/a') == '/static/pack/b'
def test_simple_lookup(): env = Environment(manifest={'a': 'b'}) f = WebpackFilter(env) assert f assert f('a') == '/static/pack/b'
def create_app(test_config=None) -> Flask: logger.info("Creating app") assets_env._named_bundles = {} app = Flask(__name__, instance_relative_config=True) # Default settings from config file app.config.from_object(Config) if test_config is not None: app.config.from_object(test_config) # Check mandatory settings, and throw if they don't exist if app.config.get("UPLOAD_FOLDER") is None: raise ValueError("Missing setting for UPLOAD_FOLDER") # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError as e: logger.info(f"Exception occured: {e} ") # Init addons init_db(app.config['SQLALCHEMY_DATABASE_URI']) @app.teardown_appcontext def cleanup(resp_or_exc): session.remove() login_manager.init_app(app) csrf.init_app(app) mail.init_app(app) app.jinja_env.add_extension('webassets.ext.jinja2.AssetsExtension') webpack_manifest = Path( __file__).absolute().parent / 'static' / 'manifest.json' if webpack_manifest.exists(): webpack_env = WebpackEnvironment(manifest=webpack_manifest, publicRoot="") app.jinja_env.filters['webpack'] = WebpackFilter(webpack_env) app.jinja_env.assets_environment = assets_env # Register blueprints from . import auth logger.debug("Registering blueprint auth") app.register_blueprint(auth.bp) from . import main logger.debug("Registering blueprint main") app.register_blueprint(main.bp) from . import assets app.register_blueprint(assets.bp) from . import profile logger.debug("Registering blueprint profile") app.register_blueprint(profile.bp, url_prefix='/profile') from . import content logger.debug("Registering blueprint content") app.register_blueprint(content.bp, url_prefix='/content') from . import userassets logger.debug("Registering assets module") app.register_blueprint(userassets.bp, url_prefix='/assets') app.register_blueprint(userassets.apibp, url_prefix='/api/assets') from . import character logger.debug("Registering blueprint character") app.register_blueprint(character.bp, url_prefix='/character') app.register_blueprint(character.api, url_prefix='/api/character') character.register_assets(assets_env) from . import campaign app.register_blueprint(campaign.bp, url_prefix='/campaign') app.register_blueprint(campaign.apibp, url_prefix='/api/campaign') campaign.register_assets(assets_env) app.add_url_rule('/', endpoint='profile.index') @app.route('/hello') def hello(): return "Hello, World!" with app.app_context(): from .database import cli # noqa, Add some commands for database handling. logger.debug("Registering assets") assets_env.url = app.static_url_path assets_env.config['TYPESCRIPT_CONFIG'] = '--target ES6' scss = Bundle('scss/main.scss', filters='pyscss', output='css/all.css') assets_env.register('scss_all', scss) css_profile = Bundle('scss/profile.scss', filters='pyscss', output='css/profile.css') assets_env.register('scss_profile', css_profile) return app