Unverified Commit 07d73630 authored by Jackson Gardner's avatar Jackson Gardner Committed by GitHub

Consume flutter.js from the engine artifacts. (#137113)

Work in progress... currently testing against presubmit.
parent 463c02da
...@@ -81,6 +81,9 @@ enum HostArtifact { ...@@ -81,6 +81,9 @@ enum HostArtifact {
/// The libraries JSON file for web release builds. /// The libraries JSON file for web release builds.
flutterWebLibrariesJson, flutterWebLibrariesJson,
// The flutter.js bootstrapping file provided by the engine.
flutterJsDirectory,
/// Folder that contains platform dill files for the web sdk. /// Folder that contains platform dill files for the web sdk.
webPlatformKernelFolder, webPlatformKernelFolder,
...@@ -232,6 +235,8 @@ String _hostArtifactToFileName(HostArtifact artifact, Platform platform) { ...@@ -232,6 +235,8 @@ String _hostArtifactToFileName(HostArtifact artifact, Platform platform) {
switch (artifact) { switch (artifact) {
case HostArtifact.flutterWebSdk: case HostArtifact.flutterWebSdk:
return ''; return '';
case HostArtifact.flutterJsDirectory:
return 'flutter_js';
case HostArtifact.iosDeploy: case HostArtifact.iosDeploy:
return 'ios-deploy'; return 'ios-deploy';
case HostArtifact.idevicesyslog: case HostArtifact.idevicesyslog:
...@@ -440,6 +445,9 @@ class CachedArtifacts implements Artifacts { ...@@ -440,6 +445,9 @@ class CachedArtifacts implements Artifacts {
case HostArtifact.flutterWebLibrariesJson: case HostArtifact.flutterWebLibrariesJson:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform)); final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform));
return _fileSystem.file(path); return _fileSystem.file(path);
case HostArtifact.flutterJsDirectory:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'flutter_js');
return _fileSystem.directory(path);
case HostArtifact.webPlatformKernelFolder: case HostArtifact.webPlatformKernelFolder:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel'); final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel');
return _fileSystem.file(path); return _fileSystem.file(path);
...@@ -893,6 +901,9 @@ class CachedLocalEngineArtifacts implements Artifacts { ...@@ -893,6 +901,9 @@ class CachedLocalEngineArtifacts implements Artifacts {
case HostArtifact.flutterWebLibrariesJson: case HostArtifact.flutterWebLibrariesJson:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform)); final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform));
return _fileSystem.file(path); return _fileSystem.file(path);
case HostArtifact.flutterJsDirectory:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'flutter_js');
return _fileSystem.directory(path);
case HostArtifact.webPlatformKernelFolder: case HostArtifact.webPlatformKernelFolder:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel'); final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel');
return _fileSystem.file(path); return _fileSystem.file(path);
...@@ -1205,6 +1216,9 @@ class CachedLocalWebSdkArtifacts implements Artifacts { ...@@ -1205,6 +1216,9 @@ class CachedLocalWebSdkArtifacts implements Artifacts {
case HostArtifact.flutterWebLibrariesJson: case HostArtifact.flutterWebLibrariesJson:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform)); final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform));
return _fileSystem.file(path); return _fileSystem.file(path);
case HostArtifact.flutterJsDirectory:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'flutter_js');
return _fileSystem.directory(path);
case HostArtifact.webPlatformKernelFolder: case HostArtifact.webPlatformKernelFolder:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel'); final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel');
return _fileSystem.file(path); return _fileSystem.file(path);
......
...@@ -20,7 +20,6 @@ import '../../globals.dart' as globals; ...@@ -20,7 +20,6 @@ import '../../globals.dart' as globals;
import '../../html_utils.dart'; import '../../html_utils.dart';
import '../../project.dart'; import '../../project.dart';
import '../../web/compile.dart'; import '../../web/compile.dart';
import '../../web/file_generators/flutter_js.dart' as flutter_js;
import '../../web/file_generators/flutter_service_worker_js.dart'; import '../../web/file_generators/flutter_service_worker_js.dart';
import '../../web/file_generators/main_dart.dart' as main_dart; import '../../web/file_generators/main_dart.dart' as main_dart;
import '../../web/file_generators/wasm_bootstrap.dart' as wasm_bootstrap; import '../../web/file_generators/wasm_bootstrap.dart' as wasm_bootstrap;
...@@ -525,11 +524,12 @@ class WebBuiltInAssets extends Target { ...@@ -525,11 +524,12 @@ class WebBuiltInAssets extends Target {
} }
// Write the flutter.js file // Write the flutter.js file
final File flutterJsFile = environment.outputDir.childFile('flutter.js'); final String flutterJsOut = fileSystem.path.join(environment.outputDir.path, 'flutter.js');
final String fileGeneratorsPath = final File flutterJsFile = fileSystem.file(fileSystem.path.join(
environment.artifacts.getArtifactPath(Artifact.flutterToolsFileGenerators); globals.artifacts!.getHostArtifact(HostArtifact.flutterJsDirectory).path,
flutterJsFile.writeAsStringSync( 'flutter.js',
flutter_js.generateFlutterJsFile(fileGeneratorsPath)); ));
flutterJsFile.copySync(flutterJsOut);
} }
} }
......
...@@ -40,7 +40,6 @@ import '../vmservice.dart'; ...@@ -40,7 +40,6 @@ import '../vmservice.dart';
import '../web/bootstrap.dart'; import '../web/bootstrap.dart';
import '../web/chrome.dart'; import '../web/chrome.dart';
import '../web/compile.dart'; import '../web/compile.dart';
import '../web/file_generators/flutter_js.dart' as flutter_js;
import '../web/memory_fs.dart'; import '../web/memory_fs.dart';
typedef DwdsLauncher = Future<Dwds> Function({ typedef DwdsLauncher = Future<Dwds> Function({
...@@ -834,14 +833,11 @@ class WebDevFS implements DevFS { ...@@ -834,14 +833,11 @@ class WebDevFS implements DevFS {
final String entrypoint = globals.fs.path.basename(mainFile.path); final String entrypoint = globals.fs.path.basename(mainFile.path);
webAssetServer.writeBytes(entrypoint, mainFile.readAsBytesSync()); webAssetServer.writeBytes(entrypoint, mainFile.readAsBytesSync());
webAssetServer.writeBytes('require.js', requireJS.readAsBytesSync()); webAssetServer.writeBytes('require.js', requireJS.readAsBytesSync());
webAssetServer.writeBytes('flutter.js', flutterJs.readAsBytesSync());
webAssetServer.writeBytes( webAssetServer.writeBytes(
'stack_trace_mapper.js', stackTraceMapper.readAsBytesSync()); 'stack_trace_mapper.js', stackTraceMapper.readAsBytesSync());
webAssetServer.writeFile( webAssetServer.writeFile(
'manifest.json', '{"info":"manifest not generated in run mode."}'); 'manifest.json', '{"info":"manifest not generated in run mode."}');
final String fileGeneratorsPath = globals.artifacts!
.getArtifactPath(Artifact.flutterToolsFileGenerators);
webAssetServer.writeFile(
'flutter.js', flutter_js.generateFlutterJsFile(fileGeneratorsPath));
webAssetServer.writeFile('flutter_service_worker.js', webAssetServer.writeFile('flutter_service_worker.js',
'// Service worker not loaded in run mode.'); '// Service worker not loaded in run mode.');
webAssetServer.writeFile( webAssetServer.writeFile(
...@@ -934,6 +930,12 @@ class WebDevFS implements DevFS { ...@@ -934,6 +930,12 @@ class WebDevFS implements DevFS {
'require.js', 'require.js',
)); ));
@visibleForTesting
final File flutterJs = globals.fs.file(globals.fs.path.join(
globals.artifacts!.getHostArtifact(HostArtifact.flutterJsDirectory).path,
'flutter.js',
));
@visibleForTesting @visibleForTesting
final File stackTraceMapper = globals.fs.file(globals.fs.path.join( final File stackTraceMapper = globals.fs.file(globals.fs.path.join(
globals.artifacts!.getArtifactPath(Artifact.engineDartSdkPath, platform: TargetPlatform.web_javascript), globals.artifacts!.getArtifactPath(Artifact.engineDartSdkPath, platform: TargetPlatform.web_javascript),
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../../globals.dart' as globals;
/// Generates the flutter.js file.
///
/// flutter.js should be completely static, so **do not use any parameter or
/// environment variable to generate this file**.
String generateFlutterJsFile(String fileGeneratorsPath) {
final String flutterJsPath = globals.localFileSystem.path.join(
fileGeneratorsPath,
'js',
'flutter.js',
);
return globals.localFileSystem.file(flutterJsPath).readAsStringSync();
}
...@@ -15,7 +15,6 @@ import 'package:flutter_tools/src/globals.dart' as globals; ...@@ -15,7 +15,6 @@ import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/html_utils.dart'; import 'package:flutter_tools/src/html_utils.dart';
import 'package:flutter_tools/src/isolated/mustache_template.dart'; import 'package:flutter_tools/src/isolated/mustache_template.dart';
import 'package:flutter_tools/src/web/compile.dart'; import 'package:flutter_tools/src/web/compile.dart';
import 'package:flutter_tools/src/web/file_generators/flutter_js.dart' as flutter_js;
import 'package:flutter_tools/src/web/file_generators/flutter_service_worker_js.dart'; import 'package:flutter_tools/src/web/file_generators/flutter_service_worker_js.dart';
import '../../../src/common.dart'; import '../../../src/common.dart';
...@@ -70,6 +69,8 @@ void main() { ...@@ -70,6 +69,8 @@ void main() {
..writeAsStringSync('foo:foo/lib/\n'); ..writeAsStringSync('foo:foo/lib/\n');
globals.fs.currentDirectory.childDirectory('bar').createSync(); globals.fs.currentDirectory.childDirectory('bar').createSync();
processManager = FakeProcessManager.empty(); processManager = FakeProcessManager.empty();
globals.fs.file('bin/cache/flutter_web_sdk/flutter_js/flutter.js')
.createSync(recursive: true);
environment = Environment.test( environment = Environment.test(
globals.fs.currentDirectory, globals.fs.currentDirectory,
...@@ -1056,41 +1057,6 @@ void main() { ...@@ -1056,41 +1057,6 @@ void main() {
contains('"main.dart.js"')); contains('"main.dart.js"'));
})); }));
test('flutter.js sanity checks', () => testbed.run(() {
final String fileGeneratorsPath = environment.artifacts
.getArtifactPath(Artifact.flutterToolsFileGenerators);
final String flutterJsContents =
flutter_js.generateFlutterJsFile(fileGeneratorsPath);
expect(flutterJsContents, contains('"use strict";'));
expect(flutterJsContents, contains('main.dart.js'));
expect(flutterJsContents, contains('if (!("serviceWorker" in navigator))'));
expect(flutterJsContents, contains(r'/\.js$/,'));
expect(flutterJsContents, contains('flutter_service_worker.js?v='));
expect(flutterJsContents, contains('document.createElement("script")'));
expect(flutterJsContents, contains('"application/javascript"'));
expect(flutterJsContents, contains('const baseUri = '));
expect(flutterJsContents, contains('document.querySelector("base")'));
expect(flutterJsContents, contains('.getAttribute("href")'));
}));
test('flutter.js is not dynamically generated', () => testbed.run(() async {
globals.fs.file('bin/cache/flutter_web_sdk/canvaskit/foo')
..createSync(recursive: true)
..writeAsStringSync('OL');
await WebBuiltInAssets(globals.fs, WebRendererMode.auto, isWasm: false).build(environment);
// No caching of source maps.
final String fileGeneratorsPath = environment.artifacts
.getArtifactPath(Artifact.flutterToolsFileGenerators);
final String flutterJsContents =
flutter_js.generateFlutterJsFile(fileGeneratorsPath);
expect(
environment.outputDir.childFile('flutter.js').readAsStringSync(),
equals(flutterJsContents),
);
}));
test('wasm build copies and generates specific files', () => testbed.run(() async { test('wasm build copies and generates specific files', () => testbed.run(() async {
globals.fs.file('bin/cache/flutter_web_sdk/canvaskit/canvaskit.wasm') globals.fs.file('bin/cache/flutter_web_sdk/canvaskit/canvaskit.wasm')
.createSync(recursive: true); .createSync(recursive: true);
......
...@@ -685,6 +685,7 @@ void main() { ...@@ -685,6 +685,7 @@ void main() {
nullSafetyMode: NullSafetyMode.unsound, nullSafetyMode: NullSafetyMode.unsound,
); );
webDevFS.requireJS.createSync(recursive: true); webDevFS.requireJS.createSync(recursive: true);
webDevFS.flutterJs.createSync(recursive: true);
webDevFS.stackTraceMapper.createSync(recursive: true); webDevFS.stackTraceMapper.createSync(recursive: true);
final Uri uri = await webDevFS.create(); final Uri uri = await webDevFS.create();
...@@ -798,6 +799,7 @@ void main() { ...@@ -798,6 +799,7 @@ void main() {
nullSafetyMode: NullSafetyMode.sound, nullSafetyMode: NullSafetyMode.sound,
); );
webDevFS.requireJS.createSync(recursive: true); webDevFS.requireJS.createSync(recursive: true);
webDevFS.flutterJs.createSync(recursive: true);
webDevFS.stackTraceMapper.createSync(recursive: true); webDevFS.stackTraceMapper.createSync(recursive: true);
final Uri uri = await webDevFS.create(); final Uri uri = await webDevFS.create();
......
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'package:file/file.dart'; import 'package:file/file.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/web/file_generators/flutter_js.dart';
import '../test_utils.dart'; import '../test_utils.dart';
import 'deferred_components_config.dart'; import 'deferred_components_config.dart';
...@@ -60,13 +58,9 @@ abstract class Project { ...@@ -60,13 +58,9 @@ abstract class Project {
} }
deferredComponents?.setUpIn(dir); deferredComponents?.setUpIn(dir);
final String fileGeneratorsPath =
Artifacts.test().getArtifactPath(Artifact.flutterToolsFileGenerators);
final String flutterJsContents = generateFlutterJsFile(fileGeneratorsPath);
// Setup for different flutter web initializations // Setup for different flutter web initializations
writeFile(fileSystem.path.join(dir.path, 'web', 'index.html'), indexHtml); writeFile(fileSystem.path.join(dir.path, 'web', 'index.html'), indexHtml);
writeFile(fileSystem.path.join(dir.path, 'web', 'flutter.js'), flutterJsContents); writeFile(fileSystem.path.join(dir.path, 'web', 'flutter.js'), '');
writeFile(fileSystem.path.join(dir.path, 'web', 'flutter_service_worker.js'), ''); writeFile(fileSystem.path.join(dir.path, 'web', 'flutter_service_worker.js'), '');
writePackages(dir.path); writePackages(dir.path);
await getPackages(dir.path); await getPackages(dir.path);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment