4.11.0 • Published 2 months ago

@squared-functions/compress v4.11.0

Weekly downloads
6
License
MIT
Repository
github
Last release
2 months ago

squared-functions 0.3

These are some of the available options when creating archives or copying files with squared 2.0.

// NOTE: common: zip | tar | gz/tgz | node-express: br | squared-apache: 7z | jar | cpio | xz | bz2 | lzma | lz4 | zstd

squared.settings.outputArchiveFormat = '7z'; // default format "zip"

squared.saveAs('archive1', {
    format: '7z',
    assets: [
        {
            pathname: 'app/src/main/res/drawable',
            filename: 'ic_launcher_background.xml',
            uri: 'http://localhost:3000/common/images/ic_launcher_background.xml',
            compress: [{ format: 'gz', level: 9 }, { format: 'br' }, { format: 'bz2' }, { format: 'lzma' }, { format: 'zstd' }, { format: 'lz4' }]
        }
    ],
    exclusions: { // All attributes are optional
        pathname: ['app/build', 'app/libs'],
        filename: ['ic_launcher_foreground.xml'],
        extension: ['iml', 'pro'],
        pattern: ['outputs', 'grad.+\\.', '\\.git']
    }
});

Image conversion can be achieved using the mimeType property in a RequestAsset object. The supported formats are:

  • png
  • jpeg
  • bmp
  • gif
  • tiff

node-express has only read support for GIF and TIFF. Opacity can be applied only to PNG and BMP. squared-apache does not support alignment or background color.

// All commands are optional except "format". Outer groupings and inner brackets are required.

+ <format>

- @|%
- ( minSize(n,0) , maxSize(n,*) )
- ( width(n|auto) x height(n|auto) [bilinear|bicubic|hermite|bezier]? ^(cover|contain|scale)?[left|center|right|top|middle|bottom]? #background-color? )
- ( left(+|-n) , top(+|-n) | cropWidth(n) x cropHeight(n) )
- { ...rotate(n) #background-color? }
- | opacity(d) |

@ - replace
% - smaller

Placing an @ symbol (png@) after the format will remove the original file from the package. Using the % symbol (png%) instead will choose the smaller of the two files. You can also use these commands with the setting "convertImages" in the Android framework.

// NOTE: Multiple transformations per asset use the ':' as the separator when using "data-chrome-file"

png(50000,*)(800x600[bezier]^contain[right|bottom]#FFFFFF)(-50,50|200x200){45,135,215,315#FFFFFF}|0.5|
// NOTE: squared-apache uses TinyPNG <https://tinypng.com/developers> for resizing and refitting (contain|cover|scale) and supports only PNG and JPEG.

const options = {
    assets: [
        {
            pathname: 'images',
            filename: 'pencil.png',
            mimeType: 'image/png',
            commands: ['jpeg'],
            uri: 'http://localhost:3000/common/images/pencil.png'
        },
        {
            pathname: 'images',
            filename: 'pencil.png',
            mimeType: 'image/png',
            commands: ['bmp@(50000,100000)'],
            uri: 'http://localhost:3000/common/images/pencil.png'
        }
    ]
};

CHROME: Saving web page assets

Bundling options are available with these HTML tag names.

  • saveAs: html + script + link
  • exportAs: script + style
  • exclude: script + link + style

JS and CSS files can be optimized further using these settings (node-express):

  • beautify
  • minify
  • es5 (Babel)
  • es5-minify (UglifyJS)
  • custom name

You can also define your own optimizations in squared.settings.json:

These particular plugins can be configured using a plain object literal. Manual installation is required when using any of these packages npm run install-chrome. Transpiling with Babel is also configurable with a .babelrc file in the base folder for any presets and additional settings. Other non-builtin minifiers can similarly be applied and chained by defining a custom string-based synchronous function.

chrome -> html | js | css -> npm package name -> custom name
  • Function object
  • file relative to serve.js
  • function closure
// squared.settings.json
{
  "chrome": {
    "html": { // built-in minifier
      "prettier": {
        "beautify": {
          "parser": "html",
          "printWidth": 120,
          "tabWidth": 4
        }
      }
    },
    "js": { // custom function (chrome -> eval_function: true)
      "terser": {
        "minify-example": "const options = { keep_classnames: true }; return context.minify(value, options).code;" // arguments are always 'context' and 'value'
      },
      "@babel/core": {
        "es5-example": "./es5.js" // startsWith './'
      }
    },
    "css": {
      "node-sass": { // npm i node-sass
        "sass-example": "function (sass, value) { return sass.renderSync({ data: value }, functions: {}); }" // first transpiler in chain
      }
    }
  }
}

// .babelrc
{
  "presets": ["@babel/preset-env"]
}

// es5.js
function (context, value) {
    const options = { presets: ['@babel/preset-env'] }; // <https://babeljs.io/docs/en/options>
    return context.transformSync(value, options).code;
}

The same concept can be used inline anywhere using a <script> tag with the type attribute set to "text/template". The script template will be completely removed from the final output.

// "es5-example" is a custom name (chrome -> eval_text_template: true)

<script type="text/template" data-chrome-template="js::@babel/core::es5-example">
    function (context, value) {
        const options = { presets: ['@babel/preset-env'] };
        return context.transformSync(value, options).code;
    }
</script>

Bundling

JS and CSS files can be bundled together with the "saveAs" or "exportAs" action. Multiple transformations per asset can be chained using the "+" symbol. The "preserve" command will prevent unused styles from being deleted.

<link data-chrome-file="saveAs:css/prod.css::beautify::preserve|inline" rel="stylesheet" href="css/dev.css" />
<style data-chrome-file="exportAs:css/prod.css::minify+beautify">
    body {
        font: 1em/1.4 Helvetica, Arial, sans-serif;
        background-color: #fafafa;
    }
</style>
<script data-chrome-file="saveAs:js/bundle1.js::minify" src="/dist/squared.js"></script>
<script data-chrome-file="saveAs:js/bundle1.js::minify" src="/dist/squared.base.js"></script>
<script data-chrome-file="saveAs:js/bundle2.js" src="/dist/chrome.framework.js"></script>

The entire page can similarly be transformed as a group using the "saveAs" attribute in options. Extension plugins will still be applied to any qualified assets.

const options = {
    saveAs: { // All attributes are optional
        html: { filename: 'index.html', format: 'beautify' }
        script: { pathname: '../js', filename: 'bundle.js', format: 'es5+es5-minify' },
        link: { pathname: 'css', filename: 'bundle.css', preserve: true, inline: true },
        image: { format: 'base64' },
        base64: { format: 'png' }
    },
    transforms: [
        { id: 'image1', pathname: '../images/harbour', filename: 'image1.png' commands: ['png@(10000,75000)(800x600[bezier]^contain[right|bottom])'], compress: true },
        { id: 'image2', base64: true }
    ]
};

There are a few ways to save the entire page or portions using the system methods.

squared.saveAs('index', { // default is false
    format: 'zip', // optional
    removeUnusedStyles: true, // Use only when you are not switching classnames with JavaScript
    productionRelease: true, // Ignore local url rewriting and load assets using absolute paths
    preserveCrossOrigin: true // Ignore downloading a local copy of assets hosted on other domains
}); 

The file action commands (save | saveAs | copyTo | appendTo) should only be used one at a time in the Chrome framework. Calling multiple consecutively may conflict if you do not use async/await.

saveTo command

saveTo: directory (~same) :: transformations? (~image) :: compress?|base64? (image)

You can use images commands with saveTo on any element when the image is the primary display output. Encoding with base64 is also available using the "::base64" commmand as the third argument.

<!-- NOTE (saveTo): img | video | audio | source | track | object | embed | iframe -->

<img
    id="image1"
    src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/harbour1.jpg"
    data-chrome-file="saveTo:../images/harbour::png@(10000,75000)(800x600[bezier]^contain[right|bottom])::base64|compress" /> <!-- "saveTo:~::~::base64" -->

Asset exclusion

You can exclude unnecessary processing files using the dataset attribute in <script|link|style> tags.

<script data-chrome-file="exclude" src="/dist/squared.js"></script>
<script data-chrome-file="exclude" src="/dist/squared.base.js"></script>
<script data-chrome-file="exclude" src="/dist/chrome.framework.js"></script>
<script data-chrome-file="exclude">
    squared.setFramework(chrome);
    squared.save();
</script>

You can similarly prevent an asset from being downloaded or transformed using the "ignore" command.

<iframe src="https://www.google.com/maps" data-chrome-file="ignore" />

YAML/JSON configuration

YAML (yml/yaml) configuration is optional and is provided for those who prefer to separate the bundling and transformations from the HTML. Any assets inside the configuration file will override any settings either inline or from JavaScript. You can also use the equivalent in JSON (json/js) for configuring as well.

- selector: html
  type: html
  filename: index.html
  process:
    - beautify
- selector: .card:nth-of-type(1) img
  type: image
  saveTo: ../images/harbour
  commands:
    - png(10000,75000)(100x200^cover){90,180,270#FFFFFF}
    - jpeg(300x100^contain){90,180,270#FFFFFF}
squared.saveAs('bundle.zip', { configUri: 'http://localhost:3000/chrome/bundle.yml' });

Here is the equivalent page with "data-chrome-file" using only inline commands.

LICENSE

MIT

4.11.0

2 months ago

4.10.5

3 months ago

4.10.4

3 months ago

4.10.3

4 months ago

4.10.2

5 months ago

4.10.1

5 months ago

4.9.20

10 months ago

4.10.0

9 months ago

4.8.45

11 months ago

4.9.19

11 months ago

4.9.18

11 months ago

4.9.8

1 year ago

4.9.9

1 year ago

3.10.0

1 year ago

4.8.41

1 year ago

4.8.40

1 year ago

4.8.43

1 year ago

4.9.11

1 year ago

4.8.42

1 year ago

4.9.10

1 year ago

4.9.13

1 year ago

4.8.44

1 year ago

4.9.12

1 year ago

4.9.15

1 year ago

4.9.14

1 year ago

4.9.17

1 year ago

4.9.16

1 year ago

4.8.34

1 year ago

4.8.35

1 year ago

4.8.38

1 year ago

4.8.39

1 year ago

4.9.7

1 year ago

4.9.6

1 year ago

4.9.5

1 year ago

3.9.3

1 year ago

3.9.27

1 year ago

4.8.32

1 year ago

4.8.33

1 year ago

4.9.4

1 year ago

4.8.31

1 year ago

4.9.3

1 year ago

4.9.0

1 year ago

4.9.2

1 year ago

4.9.1

1 year ago

3.8.14

1 year ago

3.8.15

1 year ago

3.9.2

1 year ago

3.9.1

1 year ago

3.9.0

1 year ago

4.8.90

1 year ago

4.8.92

1 year ago

4.8.91

1 year ago

4.8.94

1 year ago

4.8.93

1 year ago

4.8.96

1 year ago

4.8.95

1 year ago

4.8.50

1 year ago

4.8.85

1 year ago

4.8.87

1 year ago

4.8.86

1 year ago

4.8.89

1 year ago

4.8.88

1 year ago

4.8.76

1 year ago

4.8.75

1 year ago

4.8.78

1 year ago

4.8.77

1 year ago

4.8.79

1 year ago

4.8.25

1 year ago

4.8.27

1 year ago

4.8.26

1 year ago

4.8.29

1 year ago

4.8.28

1 year ago

4.8.30

1 year ago

4.8.36

1 year ago

4.8.37

1 year ago

3.8.12

1 year ago

3.8.13

1 year ago

3.8.10

1 year ago

3.8.11

1 year ago

4.8.9

1 year ago

4.8.8

1 year ago

4.8.5

1 year ago

4.8.4

1 year ago

4.8.7

1 year ago

4.8.6

1 year ago

4.8.1

1 year ago

4.8.0

2 years ago

4.8.3

1 year ago

4.8.2

1 year ago

4.7.0

2 years ago

4.7.5

2 years ago

4.7.2

2 years ago

4.7.1

2 years ago

4.7.4

2 years ago

4.7.3

2 years ago

3.8.0

2 years ago

4.6.0

2 years ago

3.8.9

1 year ago

3.8.4

2 years ago

3.8.3

2 years ago

3.8.2

2 years ago

3.8.1

2 years ago

3.8.8

1 year ago

3.8.7

1 year ago

3.8.6

1 year ago

3.8.5

2 years ago

4.5.0

2 years ago

4.5.2

2 years ago

4.5.1

2 years ago

4.5.7

2 years ago

4.5.4

2 years ago

4.5.3

2 years ago

4.5.6

2 years ago

4.5.5

2 years ago

4.8.21

1 year ago

4.8.20

1 year ago

4.8.23

1 year ago

4.8.22

1 year ago

4.8.24

1 year ago

4.8.10

1 year ago

4.8.12

1 year ago

4.8.11

1 year ago

4.8.14

1 year ago

4.8.13

1 year ago

4.8.16

1 year ago

4.8.15

1 year ago

4.8.18

1 year ago

4.8.17

1 year ago

4.8.19

1 year ago

3.7.5

2 years ago

3.7.4

2 years ago

3.7.3

2 years ago

3.7.2

2 years ago

3.7.7

2 years ago

3.7.6

2 years ago

4.4.1

2 years ago

4.4.0

2 years ago

4.4.3

2 years ago

4.4.2

2 years ago

4.4.5

2 years ago

4.4.4

2 years ago

4.3.4

2 years ago

4.3.3

2 years ago

4.3.9

2 years ago

4.3.6

2 years ago

4.3.5

2 years ago

4.3.8

2 years ago

4.3.7

2 years ago

4.3.13

2 years ago

4.3.12

2 years ago

4.3.11

2 years ago

4.3.10

2 years ago

4.3.14

2 years ago

4.3.2

2 years ago

3.7.1

2 years ago

3.7.0

2 years ago

4.3.1

2 years ago

4.3.0

2 years ago

4.2.3

2 years ago

4.2.2

2 years ago

4.2.4

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

3.6.13

2 years ago

3.6.12

2 years ago

3.6.11

2 years ago

4.1.4

2 years ago

4.1.3

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.7

2 years ago

4.0.6

2 years ago

4.0.3

2 years ago

3.6.9

2 years ago

3.6.10

2 years ago

4.1.0

2 years ago

4.0.1

2 years ago

4.0.2

2 years ago

3.6.8

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.4.2

2 years ago

3.6.2

2 years ago

3.6.1

2 years ago

3.6.0

2 years ago

4.0.0

2 years ago

3.6.6

2 years ago

3.6.5

2 years ago

3.6.4

2 years ago

3.6.3

2 years ago

3.6.7

2 years ago

3.5.3

2 years ago

3.5.2

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.4.0

2 years ago

3.4.4

2 years ago

3.4.3

2 years ago

3.4.2

2 years ago

3.4.1

2 years ago

3.4.6

2 years ago

3.4.5

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.3.3

2 years ago

3.3.2

2 years ago

3.2.0

2 years ago

2.3.6

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.1.5

2 years ago

3.1.4

2 years ago

3.0.8

2 years ago

2.3.8

2 years ago

2.3.7

2 years ago

2.3.9

2 years ago

1.3.4

2 years ago

2.3.5

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.5

3 years ago

1.3.3

3 years ago

1.2.14

3 years ago

2.3.0

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

3.0.0

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.13

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

1.2.12

3 years ago

2.1.3

3 years ago

1.2.11

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.9

3 years ago

1.2.10

3 years ago

1.2.9

3 years ago

2.0.10

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.5

3 years ago

1.2.8

3 years ago

2.0.4

3 years ago

1.2.7

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.9

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.1.8

3 years ago

1.2.4

3 years ago

1.1.7

3 years ago

1.2.3

3 years ago

1.1.6

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.5

3 years ago

1.2.0

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.7

3 years ago

0.15.3

3 years ago

1.0.6

3 years ago

0.15.2

3 years ago

1.0.5

3 years ago

0.15.1

3 years ago

0.15.0

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago

0.14.4

3 years ago

0.14.3

3 years ago

0.14.2

3 years ago

0.14.1

3 years ago

0.14.0

3 years ago

0.13.3

3 years ago

0.13.2

3 years ago

0.13.0

3 years ago

0.13.1

3 years ago

0.12.0

3 years ago

0.11.0

3 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.9.11

3 years ago

0.9.10

3 years ago

0.9.9

3 years ago

0.9.8

3 years ago

0.9.7

3 years ago

0.9.6

3 years ago

0.9.5

3 years ago

0.9.4

3 years ago

0.9.3

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.5

3 years ago

0.7.4

3 years ago

0.7.3

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.4

3 years ago

0.5.5

3 years ago

0.5.3

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.3.4

4 years ago

0.3.2

4 years ago

0.3.3

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago