Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: option to inline js and css files #39

Merged
merged 2 commits into from
Jun 6, 2023

Conversation

gnosticdev
Copy link
Contributor

I had to make a few small changes for my use case that bundled all resources into a single html file without external references, and thought it might be a useful option to others.

Changes:

  • added optional inline property to HTMLFileConfiguration.
  • modified injectFiles function to first check inline property before adding attributes/appending to body.
  • injectFiles now async to allow fs.promises to be used for inlining.
  • if js file is inlined = true, the script tag will not have any attributes, only contents of the js file.
  • if css file is inlined, the link tag is replaced with a style tag containing the contents of the css file.
  • added JSDoc comments to HTMLFileConfiguration.
  • updated README with inline option

new inline property added to HTMLFileConfiguration:

 inline?: boolean | { js?: boolean; css?: boolean }

Examples

No inline for js (default):

<body>
    <div id="container"></div>
    <script src="index.js" defer=""></script>
</body>

inline js file:

<body>
    <div id="container"></div>
    <script>
        // index.js
        (() => {
            const content = "hi mom"
            document.getElementById("container").innerText = content
        })()
    </script>
</body>

No inline for css file (default)

<head>
    <link rel="stylesheet" href="style.css">
</head>

inline css file:

<head>
    <style>
        /* style.css */
        body {
            background-color: blue;
        }
    </style>
</head>

- added optional inline property to HTMLFileConfiguration.
- if js is inlined, the script tag will not have any attributes, only contents of the js file.
- if css is inlined, the link tag is replaced with a style tag containing the contents of the css file.
- added JSDoc comments to HTMLFileConfiguration.
- made injectFiles function async to allow fs.promises to be used for inlining.
- updated README with inline option
Copy link
Owner

@craftamap craftamap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR - such a cool feature!

I left some minor comments, but overall it looks quite good!

lib/cjs/index.d.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/index.ts Show resolved Hide resolved
index.d.ts:
- Added semi colons back to index.d.ts

index.ts:
- Removed params from isInline function
- Added logInfo to console.log
@gnosticdev
Copy link
Contributor Author

Glad you found it useful! Issues should be resolved now.

Thanks!

@craftamap
Copy link
Owner

Looks good now! I will do a release including this soon-ish

@craftamap craftamap merged commit 9a99147 into craftamap:master Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants