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 {
/// The libraries JSON file for web release builds.
flutterWebLibrariesJson,
// The flutter.js bootstrapping file provided by the engine.
flutterJsDirectory,
/// Folder that contains platform dill files for the web sdk.
webPlatformKernelFolder,
......@@ -232,6 +235,8 @@ String _hostArtifactToFileName(HostArtifact artifact, Platform platform) {
switch (artifact) {
case HostArtifact.flutterWebSdk:
return '';
case HostArtifact.flutterJsDirectory:
return 'flutter_js';
case HostArtifact.iosDeploy:
return 'ios-deploy';
case HostArtifact.idevicesyslog:
......@@ -440,6 +445,9 @@ class CachedArtifacts implements Artifacts {
case HostArtifact.flutterWebLibrariesJson:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform));
return _fileSystem.file(path);
case HostArtifact.flutterJsDirectory:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'flutter_js');
return _fileSystem.directory(path);
case HostArtifact.webPlatformKernelFolder:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel');
return _fileSystem.file(path);
......@@ -893,6 +901,9 @@ class CachedLocalEngineArtifacts implements Artifacts {
case HostArtifact.flutterWebLibrariesJson:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform));
return _fileSystem.file(path);
case HostArtifact.flutterJsDirectory:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'flutter_js');
return _fileSystem.directory(path);
case HostArtifact.webPlatformKernelFolder:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel');
return _fileSystem.file(path);
......@@ -1205,6 +1216,9 @@ class CachedLocalWebSdkArtifacts implements Artifacts {
case HostArtifact.flutterWebLibrariesJson:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform));
return _fileSystem.file(path);
case HostArtifact.flutterJsDirectory:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'flutter_js');
return _fileSystem.directory(path);
case HostArtifact.webPlatformKernelFolder:
final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel');
return _fileSystem.file(path);
......
......@@ -20,7 +20,6 @@ import '../../globals.dart' as globals;
import '../../html_utils.dart';
import '../../project.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/main_dart.dart' as main_dart;
import '../../web/file_generators/wasm_bootstrap.dart' as wasm_bootstrap;
......@@ -525,11 +524,12 @@ class WebBuiltInAssets extends Target {
}
// Write the flutter.js file
final File flutterJsFile = environment.outputDir.childFile('flutter.js');
final String fileGeneratorsPath =
environment.artifacts.getArtifactPath(Artifact.flutterToolsFileGenerators);
flutterJsFile.writeAsStringSync(
flutter_js.generateFlutterJsFile(fileGeneratorsPath));
final String flutterJsOut = fileSystem.path.join(environment.outputDir.path, 'flutter.js');
final File flutterJsFile = fileSystem.file(fileSystem.path.join(
globals.artifacts!.getHostArtifact(HostArtifact.flutterJsDirectory).path,
'flutter.js',
));
flutterJsFile.copySync(flutterJsOut);
}
}
......
......@@ -40,7 +40,6 @@ import '../vmservice.dart';
import '../web/bootstrap.dart';
import '../web/chrome.dart';
import '../web/compile.dart';
import '../web/file_generators/flutter_js.dart' as flutter_js;
import '../web/memory_fs.dart';
typedef DwdsLauncher = Future<Dwds> Function({
......@@ -834,14 +833,11 @@ class WebDevFS implements DevFS {
final String entrypoint = globals.fs.path.basename(mainFile.path);
webAssetServer.writeBytes(entrypoint, mainFile.readAsBytesSync());
webAssetServer.writeBytes('require.js', requireJS.readAsBytesSync());
webAssetServer.writeBytes('flutter.js', flutterJs.readAsBytesSync());
webAssetServer.writeBytes(
'stack_trace_mapper.js', stackTraceMapper.readAsBytesSync());
webAssetServer.writeFile(
'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',
'// Service worker not loaded in run mode.');
webAssetServer.writeFile(
......@@ -934,6 +930,12 @@ class WebDevFS implements DevFS {
'require.js',
));
@visibleForTesting
final File flutterJs = globals.fs.file(globals.fs.path.join(
globals.artifacts!.getHostArtifact(HostArtifact.flutterJsDirectory).path,
'flutter.js',
));
@visibleForTesting
final File stackTraceMapper = globals.fs.file(globals.fs.path.join(
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;
import 'package:flutter_tools/src/html_utils.dart';
import 'package:flutter_tools/src/isolated/mustache_template.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 '../../../src/common.dart';
......@@ -70,6 +69,8 @@ void main() {
..writeAsStringSync('foo:foo/lib/\n');
globals.fs.currentDirectory.childDirectory('bar').createSync();
processManager = FakeProcessManager.empty();
globals.fs.file('bin/cache/flutter_web_sdk/flutter_js/flutter.js')
.createSync(recursive: true);
environment = Environment.test(
globals.fs.currentDirectory,
......@@ -1056,41 +1057,6 @@ void main() {
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 {
globals.fs.file('bin/cache/flutter_web_sdk/canvaskit/canvaskit.wasm')
.createSync(recursive: true);
......
......@@ -685,6 +685,7 @@ void main() {
nullSafetyMode: NullSafetyMode.unsound,
);
webDevFS.requireJS.createSync(recursive: true);
webDevFS.flutterJs.createSync(recursive: true);
webDevFS.stackTraceMapper.createSync(recursive: true);
final Uri uri = await webDevFS.create();
......@@ -798,6 +799,7 @@ void main() {
nullSafetyMode: NullSafetyMode.sound,
);
webDevFS.requireJS.createSync(recursive: true);
webDevFS.flutterJs.createSync(recursive: true);
webDevFS.stackTraceMapper.createSync(recursive: true);
final Uri uri = await webDevFS.create();
......
......@@ -3,8 +3,6 @@
// found in the LICENSE file.
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 'deferred_components_config.dart';
......@@ -60,13 +58,9 @@ abstract class Project {
}
deferredComponents?.setUpIn(dir);
final String fileGeneratorsPath =
Artifacts.test().getArtifactPath(Artifact.flutterToolsFileGenerators);
final String flutterJsContents = generateFlutterJsFile(fileGeneratorsPath);
// Setup for different flutter web initializations
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'), '');
writePackages(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