Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lit]: Add build time mangler for lit-html templates #27703

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module.exports = {
],
'object-shorthand': 0,
'n/no-callback-literal': 0,
'no-template-curly-in-string': 0,
'@typescript-eslint/await-thenable': 0,
'@typescript-eslint/consistent-generic-constructors': 0,
'@typescript-eslint/ban-ts-comment': 0,
Expand Down
36 changes: 35 additions & 1 deletion PRESUBMIT.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,36 @@ def _web_files_filter(affected_file):
except RuntimeError as err:
return [output_api.PresubmitError(err.args[1])]


# Ensure Typescript files which we use as part of the build process are typechecked.
def CheckTypescriptBuildFilesCompile(input_api, output_api):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this if we do the typechecking during the build.


def build_files_filter(affected_file):
return input_api.FilterSourceFile(affected_file,
files_to_check=[
r'.+\.mangle\.html\.ts$',
r'.+lit_mangler_cli\.ts$',
r'.+lit_mangler\.ts$',
])

affected_files = input_api.AffectedFiles(file_filter=build_files_filter)
if not any(affected_files):
return []

files_to_check = [f.AbsoluteLocalPath() for f in affected_files]

try:
brave_node.RunNode([
brave_node.PathInNodeModules('tsx', 'dist', 'cli.mjs'),
'--tsconfig',
'./tsconfig-mangle.json',
'./tools/chromium_src/lit_mangler/lit_mangler_cli.ts',
'typecheck',
] + files_to_check)
except RuntimeError as err:
return [output_api.PresubmitError(err.args[1])]
return []

# Check and fix formatting issues (supports --fix).
def CheckPatchFormatted(input_api, output_api):
# Use git cl format to format supported files with Chromium formatters.
Expand Down Expand Up @@ -147,7 +177,11 @@ def CheckESLint(input_api, output_api):
r'.+\.ts$',
r'.+\.tsx$',
)
files_to_skip = input_api.DEFAULT_FILES_TO_SKIP

# ESLint struggles with the dynamic import for the mangler here, and it
# isn't possible to disable that specific check.
files_to_skip = input_api.DEFAULT_FILES_TO_SKIP + (
r".+lit_mangler.*\.ts", )

file_filter = lambda f: input_api.FilterSourceFile(
f, files_to_check=files_to_check, files_to_skip=files_to_skip)
Expand Down
7 changes: 7 additions & 0 deletions build/commands/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,13 @@ const util = {
sourceFiles.forEach(chromiumSrcFile => {
const relativeChromiumSrcFile = chromiumSrcFile.slice(chromiumSrcDirLen)
let overriddenFile = path.join(config.srcDir, relativeChromiumSrcFile)

// .mangle.html.ts files are used to modify the upstream .html.ts file at
// build time.
if (overriddenFile.endsWith('.mangle.html.ts')) {
overriddenFile = overriddenFile.replace('.mangle.html.ts', '.html.ts')
}

if (fs.existsSync(overriddenFile)) {
// If overriddenFile is older than file in chromium_src, touch it to trigger rebuild.
isDirty |= updateFileUTimesIfOverrideIsNewer(overriddenFile, chromiumSrcFile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2025 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

import { mangle } from 'lit_mangler'

mangle((element) => {
const section = element.querySelector('.section-content')
if (!section) {
throw new Error('[Brave Extensions Overrides] Could not find .section-content. Has the ID changed?')
}
section.textContent = '$i18n{privateInfoWarning}'
section.append('<span ?hidden=${!this.data.incognitoAccess.isActive}> $i18n{spanningInfoWarning}</span>')
section.append('<span> $i18n{privateAndTorInfoWarning}</span>')
}, t => t.text.includes('id="allow-incognito"'))
43 changes: 0 additions & 43 deletions chromium_src/chrome/browser/resources/extensions/detail_view.ts

This file was deleted.

34 changes: 33 additions & 1 deletion chromium_src/tools/grit/preprocess_if_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import shutil
import json

from brave_chromium_utils import get_chromium_src_override, get_webui_overriden_file_name, get_webui_overridden_but_referenced_files
from brave_chromium_utils import get_chromium_src_override, get_webui_overriden_file_name, get_webui_overridden_but_referenced_files, get_src_dir


def purge_overrides(out_folder, in_files):
Expand Down Expand Up @@ -44,17 +44,42 @@ def maybe_keep_upstream_version(override_in_folder, out_folder, override_file):
return None


def run_mangler(mangler_file, preprocess_file):
"""Runs the mangler on the given file"""
import subprocess
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reported by reviewdog 🐶
[semgrep] Consider possible security implications associated with subprocess module.


Source: https://semgrep.dev/r/gitlab.bandit.B404


Cc @thypon @kdenhartog

import brave_node

tsx = brave_node.PathInNodeModules('tsx', 'dist', 'cli.mjs')
ts_config = os.path.join(get_src_dir(), 'brave', 'tsconfig-mangle.json')
lit_mangler = os.path.join(get_src_dir(), 'brave', 'tools', 'chromium_src',
'lit_mangler', 'lit_mangler_cli.ts')

# Note: We read from and write to the preprocess file - this way any
# preprocessing that upstream does will be mangled.
brave_node.RunNode([
tsx, '--tsconfig', ts_config, lit_mangler, 'mangle', '--typecheck',
'-m', mangler_file, '-i', preprocess_file, '-o', preprocess_file
])


def get_chromium_src_files(in_folder, in_files):
"""Gets all the overrides we have in brave-core for this target"""
override_files = []
for file in in_files:
in_file = get_chromium_src_override(os.path.join(in_folder, file))
if os.path.exists(in_file):
override_files.append(file)
elif os.path.exists(in_file.replace('.html.ts', '.mangle.html.ts')):
override_files.append(file.replace('.html.ts', '.mangle.html.ts'))

return override_files


def should_run_mangler(in_file):
"""Determines if we should run the mangler on the given file"""
return in_file.endswith('.mangle.html.ts')


# Used to indicate that there are no overrides, so we don't need to reprocess
class NoOverridesException(Exception):
pass
Expand Down Expand Up @@ -92,6 +117,13 @@ def parse_args(self, original_method, argv):
manifest_data = json.load(open(manifest_path))

for override_file in overrides:
if should_run_mangler(override_file):
upstream_file = override_file.replace('.mangle.html.ts',
'.html.ts')
run_mangler(os.path.join(override_root_folder, override_file),
os.path.join(out_folder, upstream_file))
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we expect both overrides to exist for a file (mangle and not mangle)? Does it make sense? If not, let's drop .mangle extension and remove chromium_src customization to support .mangle files. Instead to determine if a mangler should be run, scan the .ts file for from 'lit_mangler'.


overridden_name = maybe_keep_upstream_version(
override_root_folder, out_folder, override_file)
if overridden_name and args.out_manifest:
Expand Down
Loading
Loading