Skip to content

Commit 61718f5

Browse files
erickzanardowolfenrainfelangel
authored
feat: adding initial implementation (#1)
* feat: adding initial implementation * remove build files and ignore them * fix: small adjustments * removing files that should be ignored * fix: hooks formatting * fix: hooks linter workflow * fix: coverage report * fixes * Apply suggestions from code review Co-authored-by: Jochum van der Ploeg <[email protected]> * Apply suggestions from code review Co-authored-by: Felix Angelov <[email protected]> * feat: PR suggestions --------- Co-authored-by: Jochum van der Ploeg <[email protected]> Co-authored-by: Felix Angelov <[email protected]>
1 parent 158dece commit 61718f5

File tree

15 files changed

+861
-0
lines changed

15 files changed

+861
-0
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Every request must be reviewed and accepted by:
2+
3+
* @felangel @renancaraujo @wolfenrain @erickzanardo
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
name: Bug Report
3+
about: Create a report to help us improve
4+
title: "fix: "
5+
labels: bug
6+
---
7+
8+
**Description**
9+
A clear and concise description of what the bug is.
10+
11+
**Steps To Reproduce**
12+
13+
1. Go to '...'
14+
2. Click on '....'
15+
3. Scroll down to '....'
16+
4. See error
17+
18+
**Expected Behavior**
19+
A clear and concise description of what you expected to happen.
20+
21+
**Screenshots**
22+
If applicable, add screenshots to help explain your problem.
23+
24+
**Additional Context**
25+
Add any other context about the problem here.

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
blank_issues_enabled: false

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
Thanks for contributing!
3+
4+
Provide a description of your changes below and a general summary in the title
5+
6+
Please look at the following checklist to ensure that your PR can be accepted quickly:
7+
-->
8+
9+
## Description
10+
11+
<!--- Describe your changes in detail -->
12+
13+
## Type of Change
14+
15+
<!--- Put an `x` in all the boxes that apply: -->
16+
17+
- [ ] ✨ New feature (non-breaking change which adds functionality)
18+
- [ ] 🛠️ Bug fix (non-breaking change which fixes an issue)
19+
- [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
20+
- [ ] 🧹 Code refactor
21+
- [ ] ✅ Build configuration change
22+
- [ ] 📝 Documentation
23+
- [ ] 🗑️ Chore

.github/workflows/hooks.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: flutter_web_preloader
2+
3+
concurrency:
4+
group: ${{ github.workflow }}-${{ github.ref }}
5+
cancel-in-progress: true
6+
7+
on:
8+
pull_request:
9+
paths:
10+
- ".github/workflows/hooks.yaml"
11+
- "hooks/**"
12+
push:
13+
branches:
14+
- main
15+
paths:
16+
- ".github/workflows/hooks.yaml"
17+
- "hooks/**"
18+
19+
jobs:
20+
semantic-pull-request:
21+
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/semantic_pull_request.yml@v1
22+
23+
build:
24+
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1
25+
with:
26+
working_directory: hooks
27+
analyze_directories: .
28+
report_on: pre_gen.dart
29+

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hooks/build
2+
hooks/coverage
3+
hooks/.dart_tool

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 0.1.0+1
2+
3+
- feat: initial release.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Very Good Ventures
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

__brick__/web/index.html

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<!--
5+
If you are serving your web app in a path other than the root, change the
6+
href value below to reflect the base path you are serving from.
7+
8+
The path provided below has to start and end with a slash "/" in order for
9+
it to work correctly.
10+
11+
For more details:
12+
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
13+
14+
This is a placeholder for base href that will be replaced by the value of
15+
the `--base-href` argument provided to `flutter build`.
16+
-->
17+
<base href="$FLUTTER_BASE_HREF">
18+
19+
<meta charset="UTF-8">
20+
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
21+
<meta name="description" content="{{{project_description}}}">
22+
23+
<!-- iOS meta tags & icons -->
24+
<meta name="apple-mobile-web-app-capable" content="yes">
25+
<meta name="apple-mobile-web-app-status-bar-style" content="black">
26+
<meta name="apple-mobile-web-app-title" content="{{project_title}}">
27+
<link rel="apple-touch-icon" href="icons/Icon-192.png">
28+
29+
<!-- Favicon -->
30+
<link rel="icon" type="image/png" href="favicon.png"/>
31+
32+
<title>{{project_title}}</title>
33+
<link rel="manifest" href="manifest.json">
34+
<style type="text/css">
35+
body.loading-mode {
36+
display: flex;
37+
align-items: center;
38+
justify-content: center;
39+
}
40+
</style>
41+
42+
<script>
43+
// The value below is injected by flutter build, do not touch.
44+
var serviceWorkerVersion = null;
45+
</script>
46+
</head>
47+
<body class="loading-mode">
48+
<div id="progress-bar" style="border: 1px solid blue; width: 250px; height: 50px;">
49+
<div id="progress-indicator" style="background-color: blue; height: 100%; width: 0%;"></div>
50+
</div>
51+
<script>
52+
// The value below is injected by flutter build, do not touch.
53+
var serviceWorkerVersion = null;
54+
</script>
55+
<script>
56+
(function() {
57+
const progressBar = document.querySelector('#progress-bar');
58+
const progressIndicator = document.querySelector('#progress-indicator');
59+
60+
const additionalScripts = [
61+
// Add additional scripts here.
62+
];
63+
64+
function injectScript(url) {
65+
return new Promise(function(resolve) {
66+
let scriptTag = document.createElement('script');
67+
scriptTag.src = url;
68+
scriptTag.type = 'application/javascript';
69+
scriptTag.onload = function() {
70+
resolve();
71+
};
72+
73+
document.body.append(scriptTag);
74+
});
75+
}
76+
77+
function beginPreloading(manifestAssets) {
78+
var assets = [
79+
'flutter.js',
80+
'main.dart.js',
81+
{{#canvaskit}}
82+
'canvaskit/canvaskit.wasm',
83+
'canvaskit/canvaskit.js',
84+
{{/canvaskit}}
85+
...additionalScripts,
86+
...manifestAssets,
87+
];
88+
let totalAssets = assets.length + 1;
89+
let loaded = 0;
90+
91+
const batchSize = {{batch_size}};
92+
93+
async function reportProgress() {
94+
loaded++;
95+
const value = Math.floor((loaded / totalAssets) * 100) + '%';
96+
progressIndicator.style.width = value;
97+
98+
if (assets.length == 0) {
99+
dispatchAppLoad();
100+
} else {
101+
load(assets.shift());
102+
}
103+
}
104+
105+
function load(url) {
106+
const req = new XMLHttpRequest();
107+
req.onload = reportProgress;
108+
req.open('get', url);
109+
req.send();
110+
}
111+
112+
function startBatch() {
113+
const end = Math.min(batchSize, assets.length);
114+
for (let i = 0; i < end; i++) {
115+
load(assets.shift());
116+
}
117+
}
118+
119+
120+
var scriptLoaded = false;
121+
async function dispatchAppLoad() {
122+
if (scriptLoaded) {
123+
return;
124+
}
125+
scriptLoaded = true;
126+
127+
for (let i = 0; i < additionalScripts.length; i++) {
128+
await injectScript(additionalScripts[i]);
129+
}
130+
131+
await injectScript('flutter.js');
132+
133+
// Download main.dart.js
134+
_flutter.loader.loadEntrypoint({
135+
serviceWorker: {
136+
serviceWorkerVersion: serviceWorkerVersion,
137+
},
138+
onEntrypointLoaded: function(engineInitializer) {
139+
engineInitializer.initializeEngine().then(async function(appRunner) {
140+
progressBar.remove();
141+
document.body.classList.remove('loading-mode');
142+
appRunner.runApp();
143+
});
144+
}
145+
});
146+
}
147+
148+
startBatch();
149+
}
150+
151+
window.addEventListener('load', async function(ev) {
152+
const response = await fetch('assets/AssetManifest.json');
153+
const manifest = await response.json();
154+
const assets = Object.values(manifest)
155+
.map((list) => list.map((url) => 'assets/' + url))
156+
.reduce((arr, curr) => [...arr, ...curr]);
157+
158+
beginPreloading(assets);
159+
});
160+
})();
161+
</script>
162+
</body>
163+
</html>

brick.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: flutter_web_preloader
2+
description: A brick that creates a smart web entry point for Flutter that preloads its assets before starting the app
3+
4+
version: 0.1.0+1
5+
6+
environment:
7+
mason: ">=0.1.0-dev.41 <0.1.0"
8+
9+
vars:
10+
project_title:
11+
type: string
12+
description: The project title, defaults to the pubspec name value.
13+
default: ''
14+
prompt: What is project title?
15+
project_description:
16+
type: string
17+
description: The project description, defaults to the pubspec description value.
18+
default: ''
19+
prompt: What is project description?
20+
batch_size:
21+
type: number
22+
description: The number of concurrent loads that the preload will trigger.
23+
default: 20
24+
prompt: Pre load batch size (defaults to 20)?
25+
canvaskit:
26+
type: boolean
27+
description: If the project should pre load canvaskit.
28+
default: true
29+
prompt: Pre load canvas kit (defaults to true)?

0 commit comments

Comments
 (0)