buildChunks function

Future<void> buildChunks()

Implementation

Future<void> buildChunks() async {
  final rootPath = Directory.current.path;

  final mainJsFile = File('$rootPath/build/web/main.dart.js');
  final mainJsFileStr = await mainJsFile.readAsString();

  final chunks = StringSplitter.chunk(mainJsFileStr, 97000);

  final folderName = '${DateTime.now().millisecondsSinceEpoch}';
  final versionFolder = Directory('$rootPath/build/web/$folderName');
  await versionFolder.create(recursive: true);

  final newResources = <String>[];

  var k = 0;

  final chunkId = randomAlpha(5);

  for (final part in chunks) {
    final newPart = '$k-$chunkId.html';
    newResources.add('$folderName/$newPart');
    await File('$rootPath/build/web/$folderName/$newPart').writeAsString(part);

    k++;
  }

  await File('$rootPath/build/web/resources.json').writeAsString(
    json.encode({
      'parts': newResources,
      'dateTime': DateTime.now().millisecondsSinceEpoch,
    }),
  );

  print('chunks generated successfully [${chunks.length}]');
}