Kriti Sapra | 15269c4 | 2021-06-18 09:46:29 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | const fs = require('fs'); |
| 5 | const path = require('path'); |
Tim van der Lippe | 28af9fb | 2022-01-13 14:42:40 | [diff] [blame] | 6 | const {writeIfChanged} = require('./ninja/write-if-changed.js'); |
Jack Franklin | a682d93 | 2022-12-16 11:52:04 | [diff] [blame] | 7 | const postcss = require('postcss'); |
| 8 | const cssnano = require('cssnano'); |
Kriti Sapra | 15269c4 | 2021-06-18 09:46:29 | [diff] [blame] | 9 | |
Jack Franklin | a682d93 | 2022-12-16 11:52:04 | [diff] [blame] | 10 | async function runCSSMinification(input, fileName) { |
| 11 | // postcss needs to be given a fileName, even though it doesn't read from it nor write to it. |
| 12 | // So we pass in the correct name, even though it has no impact on the resulting code. |
| 13 | const result = await postcss([cssnano({preset: require('cssnano-preset-lite')})]).process(input, {from: fileName}); |
| 14 | return result.css; |
| 15 | } |
Kriti Sapra | 15269c4 | 2021-06-18 09:46:29 | [diff] [blame] | 16 | |
Jack Franklin | c712a3e | 2022-12-09 12:12:08 | [diff] [blame] | 17 | async function codeForFile({fileName, input, isDebug, isLegacy = false, buildTimestamp}) { |
Jack Franklin | 43f33bf | 2022-12-09 10:16:08 | [diff] [blame] | 18 | input = input.replace(/\`/g, '\\\''); |
| 19 | input = input.replace(/\\/g, '\\\\'); |
Kriti Sapra | 15269c4 | 2021-06-18 09:46:29 | [diff] [blame] | 20 | |
Jack Franklin | a682d93 | 2022-12-16 11:52:04 | [diff] [blame] | 21 | const stylesheetContents = isDebug ? input : await runCSSMinification(input, fileName); |
Tim van der Lippe | 1b5567f | 2021-11-12 12:54:12 | [diff] [blame] | 22 | |
| 23 | let exportStatement; |
| 24 | if (isLegacy) { |
| 25 | exportStatement = `export default { |
| 26 | cssContent: \`${stylesheetContents}\` |
| 27 | };`; |
| 28 | } else { |
| 29 | exportStatement = `const styles = new CSSStyleSheet(); |
| 30 | styles.replaceSync( |
| 31 | \`${stylesheetContents} |
| 32 | /*# sourceURL=${fileName} */ |
| 33 | \`); |
| 34 | export default styles;`; |
| 35 | } |
| 36 | |
Tim van der Lippe | 28af9fb | 2022-01-13 14:42:40 | [diff] [blame] | 37 | const newContents = `// Copyright ${ |
| 38 | new Date(Number(buildTimestamp) * 1000).getUTCFullYear()} The Chromium Authors. All rights reserved. |
Kriti Sapra | 15269c4 | 2021-06-18 09:46:29 | [diff] [blame] | 39 | // Use of this source code is governed by a BSD-style license that can be |
| 40 | // found in the LICENSE file. |
Kriti Sapra | afc5528 | 2021-09-16 12:37:09 | [diff] [blame] | 41 | // IMPORTANT: this file is auto generated. Please do not edit this file. |
Tim van der Lippe | b1953da | 2021-10-14 10:12:25 | [diff] [blame] | 42 | /* istanbul ignore file */ |
Tim van der Lippe | 1b5567f | 2021-11-12 12:54:12 | [diff] [blame] | 43 | ${exportStatement} |
Tim van der Lippe | 28af9fb | 2022-01-13 14:42:40 | [diff] [blame] | 44 | `; |
Jack Franklin | 43f33bf | 2022-12-09 10:16:08 | [diff] [blame] | 45 | return newContents; |
Kriti Sapra | 15269c4 | 2021-06-18 09:46:29 | [diff] [blame] | 46 | } |
| 47 | |
Jack Franklin | 43f33bf | 2022-12-09 10:16:08 | [diff] [blame] | 48 | // Exported only so it can be unit tested. |
| 49 | exports.codeForFile = codeForFile; |
| 50 | |
Jack Franklin | c712a3e | 2022-12-09 12:12:08 | [diff] [blame] | 51 | async function runMain() { |
Jack Franklin | 43f33bf | 2022-12-09 10:16:08 | [diff] [blame] | 52 | const [, , buildTimestamp, isDebugString, legacyString, targetName, srcDir, targetGenDir, files] = process.argv; |
| 53 | |
| 54 | const filenames = files.split(','); |
| 55 | const configFiles = []; |
| 56 | const isDebug = isDebugString === 'true'; |
| 57 | const isLegacy = legacyString === 'true'; |
| 58 | |
| 59 | for (const fileName of filenames) { |
| 60 | const contents = fs.readFileSync(path.join(srcDir, fileName), {encoding: 'utf8', flag: 'r'}); |
Jack Franklin | c712a3e | 2022-12-09 12:12:08 | [diff] [blame] | 61 | const newContents = await codeForFile({fileName, isDebug, input: contents, isLegacy, buildTimestamp}); |
Jack Franklin | 43f33bf | 2022-12-09 10:16:08 | [diff] [blame] | 62 | const generatedFileName = `${fileName}${isLegacy ? '.legacy' : ''}.js`; |
| 63 | const generatedFileLocation = path.join(targetGenDir, generatedFileName); |
| 64 | |
| 65 | writeIfChanged(generatedFileLocation, newContents); |
| 66 | |
| 67 | configFiles.push(`\"${generatedFileName}\"`); |
| 68 | } |
| 69 | |
| 70 | writeIfChanged(path.join(targetGenDir, `${targetName}-tsconfig.json`), `{ |
Kriti Sapra | 15269c4 | 2021-06-18 09:46:29 | [diff] [blame] | 71 | "compilerOptions": { |
| 72 | "composite": true, |
| 73 | "outDir": "." |
| 74 | }, |
| 75 | "files": [ |
| 76 | ${configFiles.join(',\n ')} |
| 77 | ] |
| 78 | } |
| 79 | `); |
Jack Franklin | 43f33bf | 2022-12-09 10:16:08 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (require.main === module) { |
| 83 | runMain(); |
| 84 | } |