| // Copyright 2021 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import cssnano from 'cssnano'; | |
| import cssnanoPresetLite from 'cssnano-preset-lite'; | |
| import * as fs from 'node:fs/promises'; | |
| import * as path from 'node:path'; | |
| import {parseArgs} from 'node:util'; | |
| import postcss from 'postcss'; | |
| import {writeIfChanged} from './ninja/write-if-changed.js'; | |
| async function runCSSMinification(input, fileName) { | |
| // postcss needs to be given a fileName, even though it doesn't read from it nor write to it. | |
| // So we pass in the correct name, even though it has no impact on the resulting code. | |
| const result = await postcss([ | |
| cssnano({preset: cssnanoPresetLite}), | |
| ]).process(input, {from: fileName}); | |
| return result.css; | |
| } | |
| // Exported only so it can be unit tested. | |
| export async function codeForFile({ | |
| fileName, | |
| input, | |
| isDebug, | |
| buildTimestamp, | |
| }) { | |
| input = input.replace(/\`/g, '\\\''); | |
| input = input.replace(/\\/g, '\\\\'); | |
| const stylesheetContents = isDebug ? input : await runCSSMinification(input, fileName); | |
| const year = new Date(Number(buildTimestamp) * 1000).getUTCFullYear(); | |
| return `// Copyright ${year} The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| // IMPORTANT: this file is auto generated. Please do not edit this file. | |
| /* istanbul ignore file */ | |
| export default \`${stylesheetContents} | |
| /*# sourceURL=\${import.meta.resolve('./${fileName}')} */\`;`; | |
| } | |
| async function runMain() { | |
| const {values} = parseArgs({ | |
| options: { | |
| 'build-timestamp': { | |
| type: 'string', | |
| }, | |
| debug: { | |
| type: 'boolean', | |
| default: false, | |
| }, | |
| 'src-dir': { | |
| type: 'string', | |
| }, | |
| 'target-gen-dir': { | |
| type: 'string', | |
| }, | |
| files: { | |
| type: 'string', | |
| }, | |
| }, | |
| strict: true, | |
| }); | |
| const { | |
| 'build-timestamp': buildTimestamp, | |
| debug: isDebug, | |
| 'src-dir': srcDir, | |
| 'target-gen-dir': targetGenDir, | |
| files, | |
| } = values; | |
| if (!buildTimestamp || !srcDir || !targetGenDir || !files) { | |
| throw new Error( | |
| 'Missing required arguments: --build-timestamp, --src-dir, --target-gen-dir, and --files must be provided'); | |
| } | |
| const filenames = files.split(','); | |
| await Promise.all(filenames.map(async fileName => { | |
| const contents = await fs.readFile(path.join(srcDir, fileName), { | |
| encoding: 'utf8', | |
| flag: 'r', | |
| }); | |
| const newContents = await codeForFile({ | |
| fileName, | |
| isDebug, | |
| input: contents, | |
| buildTimestamp, | |
| }); | |
| const generatedFileName = `${fileName}.js`; | |
| const generatedFileLocation = path.join(targetGenDir, generatedFileName); | |
| await writeIfChanged(generatedFileLocation, newContents); | |
| })); | |
| } | |
| if (import.meta.main) { | |
| await runMain(); | |
| } |