in build_web_compilers/lib/src/dart2js_bootstrap.dart [36:150]
Future<void> _bootstrapDart2Js(
BuildStep buildStep,
List<String> dart2JsArgs, {
required bool nativeNullAssertions,
required bool nullAssertions,
required bool soundNullSafety,
}) async {
var dartEntrypointId = buildStep.inputId;
var moduleId =
dartEntrypointId.changeExtension(moduleExtension(dart2jsPlatform));
var args = <String>[];
{
var module = Module.fromJson(
json.decode(await buildStep.readAsString(moduleId))
as Map<String, dynamic>);
List<Module> allDeps;
try {
allDeps = (await module.computeTransitiveDependencies(buildStep,
throwIfUnsupported: true))
..add(module);
} on UnsupportedModules catch (e) {
var librariesString = (await e.exactLibraries(buildStep).toList())
.map((lib) => AssetId(lib.id.package,
lib.id.path.replaceFirst(moduleLibraryExtension, '.dart')))
.join('\n');
log.warning('''
Skipping compiling ${buildStep.inputId} with dart2js because some of its
transitive libraries have sdk dependencies that are not supported on this platform:
$librariesString
https://github.com/dart-lang/build/blob/master/docs/faq.md#how-can-i-resolve-skipped-compiling-warnings
''');
return;
}
var scratchSpace = await buildStep.fetchResource(scratchSpaceResource);
var allSrcs = allDeps.expand((module) => module.sources);
await scratchSpace.ensureAssets(allSrcs, buildStep);
var dartUri = dartEntrypointId.path.startsWith('lib/')
? Uri.parse('package:${dartEntrypointId.package}/'
'${dartEntrypointId.path.substring('lib/'.length)}')
: Uri.parse('$multiRootScheme:///${dartEntrypointId.path}');
var jsOutputPath = p.withoutExtension(dartUri.scheme == 'package'
? 'packages/${dartUri.path}'
: dartUri.path.substring(1)) +
jsEntrypointExtension;
var librariesSpec = p.joinAll([sdkDir, 'lib', 'libraries.json']);
_validateUserArgs(dart2JsArgs);
args = dart2JsArgs.toList()
..addAll([
'--libraries-spec=$librariesSpec',
'--packages=$multiRootScheme:///.dart_tool/package_config.json',
'--multi-root-scheme=$multiRootScheme',
'--multi-root=${scratchSpace.tempDir.uri.toFilePath()}',
for (var experiment in enabledExperiments)
'--enable-experiment=$experiment',
'--${nativeNullAssertions ? '' : 'no-'}native-null-assertions',
if (nullAssertions) '--null-assertions',
'--${soundNullSafety ? '' : 'no-'}sound-null-safety',
'-o$jsOutputPath',
'$dartUri',
]);
}
log.info('Running dart2js with ${args.join(' ')}\n');
var result = await Process.run(
p.join(sdkDir, 'bin', 'dart'),
[
..._dart2jsVmArgs,
p.join(sdkDir, 'bin', 'snapshots', 'dart2js.dart.snapshot'),
...args,
],
workingDirectory: scratchSpace.tempDir.path);
var jsOutputId = dartEntrypointId.changeExtension(jsEntrypointExtension);
var jsOutputFile = scratchSpace.fileFor(jsOutputId);
if (result.exitCode == 0 && await jsOutputFile.exists()) {
log.info('${result.stdout}\n${result.stderr}');
var rootDir = p.dirname(jsOutputFile.path);
var dartFile = p.basename(dartEntrypointId.path);
var fileGlob = Glob('$dartFile.js*');
var archive = Archive();
await for (var jsFile in fileGlob.list(root: rootDir)) {
if (jsFile.path.endsWith(jsEntrypointExtension) ||
jsFile.path.endsWith(jsEntrypointSourceMapExtension)) {
// These are explicitly output, and are not part of the archive.
continue;
}
if (jsFile is File) {
var fileName = p.relative(jsFile.path, from: rootDir);
var fileStats = await jsFile.stat();
archive.addFile(ArchiveFile(
fileName, fileStats.size, await (jsFile as File).readAsBytes())
..mode = fileStats.mode
..lastModTime = fileStats.modified.millisecondsSinceEpoch);
}
}
if (archive.isNotEmpty) {
var archiveId =
dartEntrypointId.changeExtension(jsEntrypointArchiveExtension);
await buildStep.writeAsBytes(archiveId, TarEncoder().encode(archive));
}
// Explicitly write out the original js file and sourcemap - we can't output
// these as part of the archive because they already have asset nodes.
await scratchSpace.copyOutput(jsOutputId, buildStep);
var jsSourceMapId =
dartEntrypointId.changeExtension(jsEntrypointSourceMapExtension);
await _copyModifiedSourceMap(jsSourceMapId, scratchSpace, buildStep);
} else {
log.severe('ExitCode:${result.exitCode}\nStdOut:\n${result.stdout}\n'
'StdErr:\n${result.stderr}');
}
}