UNPKG

1.35 kBJavaScriptView Raw
1/**
2 * Copyright 2017 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17const CleanCSS = require('clean-css');
18const process = require('process');
19const replace = require('replace-in-file');
20const argv = require('minimist')(process.argv.slice(2));
21
22// Replaces the CSS that matches one very specific string with the compressed output of the input file
23
24const inputCSSFile = [argv['i']];
25
26const replaceLink = (css) => {
27 const styles = css.styles;
28 replace({
29 files: 'dist/server/public/assets/templates/*.html',
30 from: /<link rel="stylesheet" href="\/styles\/main.css" \/>/g,
31 to: `<style nonce='style-{{= it.nonce.inlinedcss }}'>${styles}</style>`
32 });
33};
34
35new CleanCSS({returnPromise: true})
36 .minify(inputCSSFile)
37 .then(replaceLink)
38 .then(_ => console.log('CSS Replaced'));