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 |
|
17 | const CleanCSS = require('clean-css');
|
18 | const process = require('process');
|
19 | const replace = require('replace-in-file');
|
20 | const 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 |
|
24 | const inputCSSFile = [argv['i']];
|
25 |
|
26 | const 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 |
|
35 | new CleanCSS({returnPromise: true})
|
36 | .minify(inputCSSFile)
|
37 | .then(replaceLink)
|
38 | .then(_ => console.log('CSS Replaced'));
|