EncryptPlugin.js 838 B

123456789101112131415161718192021222324252627282930
  1. import webpackSources from 'webpack-sources';
  2. import { encrypt } from './crypt.js';
  3. const { RawSource } = webpackSources;
  4. export const RICOCHET_FILE = process.env.RICOCHET_FILE || 'ricochet.json';
  5. class EncryptPlugin {
  6. constructor({ algorithm = 'aes-256-cbc', key }) {
  7. this.algorithm = algorithm;
  8. this.key = key;
  9. }
  10. apply(compiler) {
  11. compiler.hooks.compilation.tap('EncryptPlugin', (compilation) => {
  12. compilation.hooks.afterProcessAssets.tap('EncryptPlugin', () => {
  13. console.log(`Encrypt ${RICOCHET_FILE} content.`);
  14. compilation.updateAsset(RICOCHET_FILE, (rawSource) => {
  15. return new RawSource(
  16. JSON.stringify(
  17. encrypt(rawSource.buffer(), this.key, this.algorithm)
  18. )
  19. );
  20. });
  21. });
  22. });
  23. }
  24. }
  25. export default EncryptPlugin;