compress_css_js_files.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:django-webpacker 作者: MicroPyramid 项目源码 文件源码
def handle(self, *args, **options):
        call_subprocess('./node_modules/.bin/webpack --config webpack.config.js')


        for each in settings.WEB_PACK_FILES:
            directory = settings.BASE_DIR + '/static/webpack_bundles/'
            css_file = max([os.path.join(directory, d) for d in os.listdir(directory) if d.startswith(each['webpack_js']) and d.endswith('css')], key=os.path.getmtime)
            js_file = max([os.path.join(directory, d) for d in os.listdir(directory) if d.startswith(each['webpack_js']) and d.endswith('js')], key=os.path.getmtime)

            if settings.ENABLE_DJANGO_WEBPACK_S3_STORAGES:
                upload_to_s3(css_file)
                upload_to_s3(js_file)

            import re
            regex = r'(.*?<link rel="stylesheet" type="text/css" href=")(.*?)(" id="packer_css"/>.*?<script id="packer_js" src=")(.*?)(" type="text/javascript"></script>.*)'

            with open(each['html_file_name'], 'r+') as f:
                content = f.read()
                m = re.match(regex, content, re.DOTALL)
                href = settings.STATIC_URL + css_file.split('/static/')[-1]
                src = settings.STATIC_URL + js_file.split('/static/')[-1]
                content = m.group(1) + href + m.group(3) + src + m.group(5)

            with open(each['html_file_name'], 'w') as f:
                f.write(content)

        result = {'message': "Successfully Created Compressed CSS, JS Files"}
        return json.dumps(result)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号