| // 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. |
| const cssnano = require('cssnano'); |
| const cssnanoPresetLite = require('cssnano-preset-lite'); |
| const fs = require('fs'); |
| const path = require('path'); |
| const postcss = require('postcss'); |
| |
| const {writeIfChanged} = require('./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; |
| } |
| |
| 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}')} */\`;`; |
| } |
| |
| // Exported only so it can be unit tested. |
| exports.codeForFile = codeForFile; |
| |
| async function runMain() { |
| const [, , buildTimestamp, isDebugString, targetName, srcDir, targetGenDir, files, ] = process.argv; |
| |
| const filenames = files.split(','); |
| const configFiles = []; |
| const isDebug = isDebugString === 'true'; |
| |
| for (const fileName of filenames) { |
| const contents = fs.readFileSync(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); |
| |
| writeIfChanged(generatedFileLocation, newContents); |
| |
| configFiles.push(`\"${generatedFileName}\"`); |
| } |
| |
| writeIfChanged( |
| path.join(targetGenDir, `${targetName}-tsconfig.json`), |
| `{ |
| "compilerOptions": { |
| "composite": true, |
| "outDir": "." |
| }, |
| "files": [ |
| ${configFiles.join(',\n ')} |
| ] |
| } |
| `, |
| ); |
| } |
| |
| if (require.main === module) { |
| runMain(); |
| } |