Future compileDDC()

in lib/src/compiler.dart [121:205]


  Future<DDCCompilationResults> compileDDC(String input) async {
    final imports = getAllImportsFor(input);
    final unsupportedImports =
        getUnsupportedImports(imports, devMode: _sdk.devMode);
    if (unsupportedImports.isNotEmpty) {
      return DDCCompilationResults.failed([
        for (var import in unsupportedImports)
          CompilationProblem._('unsupported import: ${import.uri.stringValue}'),
      ]);
    }

    final temp = await Directory.systemTemp.createTemp('dartpad');
    _logger.info('Temp directory created: ${temp.path}');

    try {
      final usingFlutter = usesFlutterWeb(imports, devMode: _sdk.devMode);
      if (usesFirebase(imports)) {
        await copyPath(_projectTemplates.firebasePath, temp.path);
      } else if (usingFlutter) {
        await copyPath(_projectTemplates.flutterPath, temp.path);
      } else {
        await copyPath(_projectTemplates.dartPath, temp.path);
      }

      await Directory(path.join(temp.path, 'lib')).create(recursive: true);

      final mainPath = path.join(temp.path, 'lib', kMainDart);
      final bootstrapPath = path.join(temp.path, 'lib', kBootstrapDart);
      final bootstrapContents =
          usingFlutter ? kBootstrapFlutterCode : kBootstrapDartCode;

      await File(bootstrapPath).writeAsString(bootstrapContents);
      await File(mainPath).writeAsString(input);

      final arguments = <String>[
        '--modules=amd',
        if (usingFlutter) ...[
          '-s',
          _projectTemplates.summaryFilePath,
          '-s',
          '${_sdk.flutterWebSdkPath}/flutter_ddc_sdk_sound.dill',
        ],
        ...['-o', path.join(temp.path, '$kMainDart.js')],
        ...['--module-name', 'dartpad_main'],
        '--enable-asserts',
        '--sound-null-safety',
        bootstrapPath,
        '--packages=${path.join(temp.path, '.dart_tool', 'package_config.json')}',
      ];

      final mainJs = File(path.join(temp.path, '$kMainDart.js'));

      _logger.info('About to exec dartdevc worker: ${arguments.join(' ')}"');

      final response =
          await _ddcDriver.doWork(WorkRequest()..arguments.addAll(arguments));

      if (response.exitCode != 0) {
        return DDCCompilationResults.failed(
            [CompilationProblem._(response.output)]);
      } else {
        // The `--single-out-file` option for dartdevc was removed in v2.7.0. As
        // a result, the JS code produced above does *not* provide a name for
        // the module it contains. That's a problem for DartPad, since it's
        // adding the code to a script tag in an iframe rather than loading it
        // as an individual file from baseURL. As a workaround, this replace
        // statement injects a name into the module definition.
        final processedJs = (await mainJs.readAsString())
            .replaceFirst('define([', "define('dartpad_main', [");

        final results = DDCCompilationResults(
          compiledJS: processedJs,
          modulesBaseUrl: 'https://storage.googleapis.com/nnbd_artifacts'
              '/${_sdk.versionFull}/',
        );
        return results;
      }
    } catch (e, st) {
      _logger.warning('Compiler failed: $e\n$st');
      rethrow;
    } finally {
      await temp.delete(recursive: true);
      _logger.info('temp folder removed: ${temp.path}');
    }
  }