简体   繁体   English

减少/拆分 Flutter 生成的 Web 输出文件“main.dart.js”的大小?

[英]Reduce/split size of Flutter generated web-output file "main.dart.js"?

I just built an example Flutter app and I wanted to deploy it as an web application.我刚刚构建了一个示例 Flutter 应用程序,我想将其部署为 web 应用程序。 Flutter generates a single file called main.dart.js. Flutter 生成一个名为 main.dart.js 的文件。 However, using a few dialogs, animations and so on, this js file is already almost 2MB of size (built using flutter build web).但是,使用一些对话框、动画等,这个 js 文件已经有将近 2MB 大小(使用 flutter 构建 web 构建)。

Now I have deployed it on an aws-webserver --- but opening it takes about 5sec to appear while the screen is completely blank.现在我已经将它部署在 aws-webserver 上——但是在屏幕完全空白的情况下打开它大约需要 5 秒。 This is not a good experience:-(这不是一个很好的体验:-(

So, the question is how to reduce/split the size of the generated main.dart.js, so that the web application starts sooner?那么,问题是如何减少/拆分生成的main.dart.js的大小,以便web应用程序启动得更快? I already tried to use Dart deferred loading:我已经尝试使用 Dart 延迟加载:

import 'secondpage.dart' deferred as secondpage;
<...>
class MyApp extends StatelessWidget {
  final Future<void> loadedLibrary = secondpage.loadLibrary();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Admin Panel',
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: bgColor,
        textTheme: GoogleFonts.poppinsTextTheme(Theme.of(context).textTheme)
            .apply(bodyColor: Colors.white),
        canvasColor: secondaryColor,
      ),
      navigatorObservers: [TransitionRouteObserver()],
      initialRoute: 'login',
      onUnknownRoute: (context) => null,
      routes: {
        'login': (context) => const LoginScreen(),
        'admin': (context) =>FutureBuilder(
            future: loadedLibrary,
            builder: (snapshot, context) {
              return secondpage.SecondPage();
            })
      },
    );

This approach indeed generates a second js file called main.dart.js_1.part.js.这种方法确实会生成一个名为 main.dart.js_1.part.js 的第二个 js 文件。 But this file is only 1KB of size while the main.dart.js is still of 2MB size.. So no improvement here.但是这个文件只有 1KB 大小,而 main.dart.js 仍然是 2MB 大小。所以这里没有改进。

Are there any other options or ideas to "improve startup time" of a Flutter web-app?是否有任何其他选项或想法可以“提高 Flutter 网络应用程序的启动时间”? Thanks!谢谢!

I know that I'm late, but for anyone who stumbles across this question: there is currently no official solution to the problem, but if you check this GitHub issue discussion , by the end of it, you will be led to a Medium article .我知道我迟到了,但是对于任何偶然发现这个问题的人:目前没有官方解决这个问题,但是如果你查看这个GitHub 问题讨论,到最后,你将被引导到一篇Medium 文章. This is merely a workaround based on the idea of deferred loading with the use of qlevar_router package.这只是一种基于使用qlevar_router package 延迟加载的想法的解决方法。

For a proof-of-concept project, please check the GitHub repository of the aforementioned Medium article author.有关概念验证项目,请查看上述 Medium 文章作者的GitHub 存储库 A short excerpt, so you get the gist of it:一段简短的摘录,让你了解它的要点:

class DefferedLoader extends QMiddleware {
  final Future<dynamic> Function() loader;

  DefferedLoader(this.loader);
  @override
  Future onEnter() async {
    await loader();
  }
}

class Routes {
  static final routes = <QRoute>[
    QRoute(path: '/', builder: () => const MainPage()),
    QRoute(
      path: '/users',
      builder: () => users.UsersPage(),
      middleware: [
        DefferedLoader(users.loadLibrary),
      ],
    ),
    QRoute(
      path: '/categories',
      builder: () => categories.CategorysPage(),
      middleware: [
        DefferedLoader(categories.loadLibrary),
      ],
    ),
  ];
}

I agree with previous answer and the shared links there are on spot.我同意以前的答案,并且现场有共享链接。

However, as a partial solution to the overall problem, it is important to check the imported libraries and consider if their weight is justified on your app.但是,作为整体问题的部分解决方案,检查导入的库并考虑它们的权重是否适合您的应用程序非常重要。 If you need 2MB more just to load a font, maybe you can find a workaround for that.如果您仅需要 2MB 来加载字体,也许您可以找到解决方法。

It can help to have a look at the main.js itself (both minified and unminified version, as in the web release in debug if it helps).查看 main.js 本身会有所帮助(缩小版和未缩小版, 如调试中的 web 版本,如果有帮助的话)。 In some cases there is a huge amount of repeated functions that may just define wrappers for similar params.在某些情况下,有大量重复的函数可能只是为类似的参数定义包装器。

In my case avoiding google-fonts and use a simpler almost equal font reduced the size of 40% (24% faster load after compression).在我的情况下,避免使用 google-fonts 并使用更简单的几乎相等的字体将大小减小了 40%(压缩后加载速度提高了 24%)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM