blob: 02ffe8e939520d86e7889868e3b79b63f11195bf [file] [log] [blame]
Kriti Sapra15269c42021-06-18 09:46:291// 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.
4const fs = require('fs');
5const path = require('path');
Tim van der Lippe28af9fb2022-01-13 14:42:406const {writeIfChanged} = require('./ninja/write-if-changed.js');
Jack Franklina682d932022-12-16 11:52:047const postcss = require('postcss');
8const cssnano = require('cssnano');
Kriti Sapra15269c42021-06-18 09:46:299
Jack Franklina682d932022-12-16 11:52:0410async 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 Sapra15269c42021-06-18 09:46:2916
Jack Franklinc712a3e2022-12-09 12:12:0817async function codeForFile({fileName, input, isDebug, isLegacy = false, buildTimestamp}) {
Jack Franklin43f33bf2022-12-09 10:16:0818 input = input.replace(/\`/g, '\\\'');
19 input = input.replace(/\\/g, '\\\\');
Kriti Sapra15269c42021-06-18 09:46:2920
Jack Franklina682d932022-12-16 11:52:0421 const stylesheetContents = isDebug ? input : await runCSSMinification(input, fileName);
Tim van der Lippe1b5567f2021-11-12 12:54:1222
23 let exportStatement;
24 if (isLegacy) {
25 exportStatement = `export default {
26 cssContent: \`${stylesheetContents}\`
27};`;
28 } else {
29 exportStatement = `const styles = new CSSStyleSheet();
30styles.replaceSync(
31\`${stylesheetContents}
32/*# sourceURL=${fileName} */
33\`);
34export default styles;`;
35 }
36
Tim van der Lippe28af9fb2022-01-13 14:42:4037 const newContents = `// Copyright ${
38 new Date(Number(buildTimestamp) * 1000).getUTCFullYear()} The Chromium Authors. All rights reserved.
Kriti Sapra15269c42021-06-18 09:46:2939// Use of this source code is governed by a BSD-style license that can be
40// found in the LICENSE file.
Kriti Sapraafc55282021-09-16 12:37:0941// IMPORTANT: this file is auto generated. Please do not edit this file.
Tim van der Lippeb1953da2021-10-14 10:12:2542/* istanbul ignore file */
Tim van der Lippe1b5567f2021-11-12 12:54:1243${exportStatement}
Tim van der Lippe28af9fb2022-01-13 14:42:4044`;
Jack Franklin43f33bf2022-12-09 10:16:0845 return newContents;
Kriti Sapra15269c42021-06-18 09:46:2946}
47
Jack Franklin43f33bf2022-12-09 10:16:0848// Exported only so it can be unit tested.
49exports.codeForFile = codeForFile;
50
Jack Franklinc712a3e2022-12-09 12:12:0851async function runMain() {
Jack Franklin43f33bf2022-12-09 10:16:0852 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 Franklinc712a3e2022-12-09 12:12:0861 const newContents = await codeForFile({fileName, isDebug, input: contents, isLegacy, buildTimestamp});
Jack Franklin43f33bf2022-12-09 10:16:0862 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 Sapra15269c42021-06-18 09:46:2971 "compilerOptions": {
72 "composite": true,
73 "outDir": "."
74 },
75 "files": [
76 ${configFiles.join(',\n ')}
77 ]
78}
79`);
Jack Franklin43f33bf2022-12-09 10:16:0880}
81
82if (require.main === module) {
83 runMain();
84}