Skip to content

Instantly share code, notes, and snippets.

@Ghostbird
Last active December 3, 2021 15:31
Show Gist options
  • Save Ghostbird/cea7619a4995a0997f2622548c2b0a72 to your computer and use it in GitHub Desktop.
Save Ghostbird/cea7619a4995a0997f2622548c2b0a72 to your computer and use it in GitHub Desktop.
Node script to load a CSS file as if it is a JS file. Workaround for Angular 13 extractCss deprecation.
#!/usr/bin/env node
process.stdout.write(
`const styleElement = document.createElement('style');styleElement.appendChild(document.createTextNode(\``,
() => process.stdin.pipe(process.stdout, { end: false })
);
process.stdin.on('end', () =>
process.stdout.write(
`\`));document.getElementsByTagName('head')[0].appendChild(styleElement);\n`,
() => process.stdout.end()
)
);
@Ghostbird
Copy link
Author

Ghostbird commented Dec 3, 2021

To use, call it like this:

$ cat styles.css | node wrap-css.js > styles.js

Keep in mind that you must trust your styles.css file! Otherwise you open yourself up to malicious javascript injection. This script was written specifically to repackage the styles.css file created by the Angular compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment