diff --git a/dwds/debug_extension_mv3/build.yaml b/dwds/debug_extension_mv3/build.yaml index a1cdac35e..1ef92a8d1 100644 --- a/dwds/debug_extension_mv3/build.yaml +++ b/dwds/debug_extension_mv3/build.yaml @@ -8,21 +8,4 @@ targets: - --csp generate_for: - web/**.dart - mv3_extension|client_js_copy_builder: - enabled: true - -builders: - client_js_copy_builder: - import: "tool/copy_builder.dart" - builder_factories: - - copyBuilder - build_extensions: - { - "web/{{}}.dart.js": ["compiled/{{}}.dart.js"], - "web/static_assets/{{}}.png": ["compiled/static_assets/{{}}.png"], - "web/static_assets/{{}}.html": ["compiled/static_assets/{{}}.html"], - "web/static_assets/{{}}.css": ["compiled/static_assets/{{}}.css"], - "web/manifest.json": ["compiled/manifest.json"], - } - auto_apply: none - build_to: source + diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index 32e1a2692..d0d30ec60 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -16,6 +16,7 @@ import 'dart:convert'; import 'dart:io'; import 'package:args/args.dart'; +import 'package:io/io.dart'; import 'package:path/path.dart' as p; const _prodFlag = 'prod'; @@ -43,13 +44,25 @@ Future run({required bool isProd, required bool isMV3}) async { _logInfo('Compiling extension with dart2js to /compiled directory'); final compileStep = await Process.start( 'dart', - ['run', 'build_runner', 'build', 'web', '--output', 'build', '--release'], + [ + 'run', + 'build_runner', + 'build', + 'web', + '--output', + 'build', + '--release', + '--delete-conflicting-outputs', + ], ); final compileExitCode = await _handleProcess(compileStep); // Terminate early if compilation failed: if (compileExitCode != 0) { return compileExitCode; } + + _logInfo('Creating /compiled directory if it doesn\'t exist'); + await Directory('compiled').create(); final manifestFileName = isMV3 ? 'manifest_mv3' : 'manifest_mv2'; _logInfo('Copying manifest.json to /compiled directory'); try { @@ -61,6 +74,31 @@ Future run({required bool isProd, required bool isMV3}) async { // Return non-zero exit code to indicate failure: return 1; } + _logInfo('Copying /static_assets to /compiled directory'); + try { + await copyPath( + p.join('web', 'static_assets'), + p.join('compiled', 'static_assets'), + ); + } catch (error) { + _logWarning('Copying static_assets failed: $error'); + // Return non-zero exit code to indicate failure: + return 1; + } + _logInfo('Copying generated *.dart.js files to /compiled directory'); + try { + await for (final file in Directory(p.join('build', 'web')).list()) { + if (file.path.endsWith('dart.js')) { + final fileName = p.split(file.path).last; + File(file.path).copySync(p.join('compiled', fileName)); + } + } + } catch (error) { + _logWarning('Copying *.dart.js files failed: $error'); + // Return non-zero exit code to indicate failure: + return 1; + } + // If we're compiling for prod, skip updating the manifest.json: if (isProd) return 0; // Update manifest.json for dev: diff --git a/dwds/debug_extension_mv3/tool/copy_builder.dart b/dwds/debug_extension_mv3/tool/copy_builder.dart deleted file mode 100644 index 4a5e5d04f..000000000 --- a/dwds/debug_extension_mv3/tool/copy_builder.dart +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2022, 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:build/build.dart'; - -/// Factory for the build script. -Builder copyBuilder(_) => _CopyBuilder(); - -class _CopyBuilder extends Builder { - @override - Map> get buildExtensions => { - "web/{{}}.dart.js": ["compiled/{{}}.dart.js"], - "web/static_assets/{{}}.png": ["compiled/static_assets/{{}}.png"], - "web/static_assets/{{}}.html": ["compiled/static_assets/{{}}.html"], - "web/static_assets/{{}}.css": ["compiled/static_assets/{{}}.css"], - "web/manifest.json": ["compiled/manifest.json"], - }; - - @override - Future build(BuildStep buildStep) async { - final inputAsset = buildStep.inputId; - final allowedOutputs = buildStep.allowedOutputs; - - if (allowedOutputs.length != 1) { - return; - } - - final outputAsset = allowedOutputs.first; - await _copyBinaryFile( - buildStep, - inputAsset: inputAsset, - outputAsset: outputAsset, - ); - } - - Future _copyBinaryFile( - BuildStep buildStep, { - required AssetId inputAsset, - required AssetId outputAsset, - }) { - return buildStep.writeAsBytes( - outputAsset, - buildStep.readAsBytes(inputAsset), - ); - } -}