0.35.5 • Published 13 days ago

@merkur/plugin-css-scrambler v0.35.5

Weekly downloads
-
License
ISC
Repository
github
Last release
13 days ago

Merkur - plugin-css-scrambler - deprectecated

Build Status NPM package version npm bundle size (scoped version) code style: prettier

merkur/plugin-css-scrambler scrambles css classes for production build.

npm i --save @merkur/plugin-css-scrambler
npm i --save-dev postcss postcss-loader

1. Generate hashtable at build time

This can be achieved by extending webpack config with provided drop-in function. This function will find existing postcss-loader and extend it's config to use scrambling plugin or will define postcss-loader for default CSS rule.

+ const { applyPostCssScramblePlugin } = require('@merkur/plugin-css-scrambler/postcss');
module.exports = Promise.all([
-  pipe(createWebConfig, applyBabelLoader)(),
+  pipe(createWebConfig, applyBabelLoader, applyPostCssScramblePlugin)(),
-  pipe(createWebConfig, applyBabelLoader, applyES9Transformation)(),
+  pipe(createWebConfig, applyBabelLoader, applyES9Transformation, applyPostCssScramblePlugin)(),
  pipe(createNodeConfig, applyBabelLoader)(),
]);

If you have custom webpack configuration you can just define postcss-loader for your styles rule and then call applyPostCssScramblePlugin or you can apply the scramler plugin manually.

const path = require('path');
const { postCssScrambler } = require('@merkur/plugin-css-scrambler/postcss');
{
    loader: 'postcss-loader',
    options: {
        postcssOptions: {
            plugins: [
                postCssScrambler({
                    generateHashTable: true,
                    hashTable: path.resolve(
                        process.env.WIDGET_DIRNAME,
                        './build/static/hashtable.json'
                    ),
                }),
            ],
        },
    },
}

2. Load generated hashtable in widget router

const { loadClassnameHashtable } = require('@merkur/plugin-css-scrambler/server');
const merkurModule = require('../../../build/widget.cjs');
const classnameHashtable = loadClassnameHashtable(
    path.resolve(__dirname, '../../build/static/hashtable.json')
);
router.get('/widget', asyncMiddleware(async (req, res) => {
    const widget = await merkurModule.createWidget({
        classnameHashtable
        props: {
            // ...
        }
    });
    // ...
}));

3. Register plugin in your widget.js.

import { cssScramblePlugin } from '@merkur/plugin-css-scrambler';
export const widgetProperties = {
    // ...
    $plugins: [
        // ...
        cssScramblePlugin,
    ]
}

Now you should use cn function from widget object instead of classname package directly.

import WidgetContext from './WidgetContext';
export default function Counter({ counter }) {
  const widget = useContext(WidgetContext);
  const [visible, setVisibility] = useState(false);
  return (
    <div className={widget.cn({
        counter: true,
        'counter--visible': visible
    })}>
        {* ... *}
    </div>
  );
}

About Merkur

The Merkur is tiny extensible javascript library for front-end microservices(micro frontends). It allows by default server side rendering for loading performance boost. You can connect it with other frameworks or languages because merkur defines easy API. You can use one of six predefined template's library Preact, µhtml, Svelte and vanilla but you can easily extend for others.

0.35.5

13 days ago

0.35.0

1 month ago

0.32.0

10 months ago

0.34.0

7 months ago

0.33.0

9 months ago

0.31.0

1 year ago

0.30.0

1 year ago

0.29.1

2 years ago

0.29.0

2 years ago

0.28.0

2 years ago

0.27.1

3 years ago

0.27.0

3 years ago

0.26.0

3 years ago

0.25.0

3 years ago

0.24.3

3 years ago

0.24.2

3 years ago

0.24.1

3 years ago