| // Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'package:dartpad/dartpad.dart'; | |
| import 'package:web/web.dart'; | |
| Future<void> main() async { | |
| final sdk = DartPadSdk( | |
| // URL where the DartPad SDK assets generated by `dart run dartpad setup` | |
| // are hosted. | |
| assetBaseUrl: Uri.base.resolve('dartpad_sdk/'), | |
| ); | |
| // Create a Web Worker running the dartpad worker environment. | |
| final dartpad = await sdk.dedicatedWorker(); | |
| // Create a workspace with pubspec.yaml and main.dart | |
| final ws = await dartpad.createWorkspace(); | |
| await ws.writeFileFromText('pubspec.yaml', ''' | |
| name: foo | |
| environment: | |
| sdk: ^3.11.0 | |
| dependencies: | |
| http: ^1.0.0 | |
| '''); | |
| await ws.writeFileFromText( | |
| 'main.dart', | |
| 'void main() => print("hello world");', | |
| ); | |
| // Resolve dependenices | |
| await ws.pub(command: 'get'); | |
| // Create a sandboxed iframe to run code in | |
| final iframe = await sdk.createSandboxedIframe(window.document.body!); | |
| // Connect the workspace to the iframe, making the workspace the owner! | |
| final sandbox = await ws.connectSandboxedIframe(iframe.port); | |
| // Print console output | |
| sandbox.console.forEach(print).ignore(); | |
| // Run main.dart in the sanboxed iframe | |
| await sandbox.run('main.dart', mode: 'console'); | |
| // Cleanup | |
| await sandbox.close(); | |
| await iframe.close(); | |
| await ws.close(); | |
| await dartpad.close(); | |
| } |