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

[#16] Escape sequences are not allowed in template strings #17

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"printWidth": 80,
"tabWidth": 4,
"endOfLine": "auto",
"singleQuote": true,
"semi": true,
"quoteProps": "consistent",
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "always"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc"
"build": "tsc",
"format": "prettier 'src/index.ts' --write"
},
"repository": {
"type": "git",
Expand Down
71 changes: 36 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { IndexHtmlTransformContext, IndexHtmlTransformResult, Plugin } from 'vite';
import {
IndexHtmlTransformContext,
IndexHtmlTransformResult,
Plugin,
} from 'vite';

/**
* Inject the CSS compiled with JS.
*
* @return {Plugin}
*/
function cssInjectedByJsPlugin({topExecutionPriority} = { topExecutionPriority: true }): Plugin {
function cssInjectedByJsPlugin(
{ topExecutionPriority } = { topExecutionPriority: true }
): Plugin {
//Globally so we can add it to legacy and non-legacy bundle.
let cssToInject: string = '';

Expand All @@ -14,40 +20,35 @@ function cssInjectedByJsPlugin({topExecutionPriority} = { topExecutionPriority:
enforce: 'post',
name: 'css-in-js-plugin',
generateBundle(opts, bundle) {

let styleCode = '';

for (const key in bundle) {

if (bundle[key]) {

const chunk = bundle[key];

if (chunk.type === 'asset' && chunk.fileName.includes('.css')) {

if (
chunk.type === 'asset' &&
chunk.fileName.includes('.css')
) {
styleCode += chunk.source;
delete bundle[key];

}

}

}

if (styleCode.length > 0) {

cssToInject = styleCode.trim();

cssToInject = styleCode;
}

for (const key in bundle) {

if (bundle[key]) {

const chunk = bundle[key];

if (chunk.type === 'chunk' && chunk.fileName.includes('.js') && !chunk.fileName.includes('polyfill')) {

if (
chunk.type === 'chunk' &&
chunk.fileName.includes('.js') &&
!chunk.fileName.includes('polyfill')
) {
let topCode: string = '';
let bottomCode: string = '';
if (topExecutionPriority) {
Expand All @@ -56,45 +57,45 @@ function cssInjectedByJsPlugin({topExecutionPriority} = { topExecutionPriority:
topCode = chunk.code;
}

chunk.code = `${topCode}(function(){ try {var elementStyle = document.createElement('style'); elementStyle.innerText = \`${cssToInject}\`; document.head.appendChild(elementStyle);} catch(e) {console.error(e, 'vite-plugin-css-injected-by-js: error when trying to add the style.');} })();${bottomCode}`;
chunk.code = topCode;
chunk.code +=
"(function(){ try {var elementStyle = document.createElement('style'); elementStyle.innerText = ";
chunk.code += JSON.stringify(cssToInject.trim());
chunk.code +=
"; document.head.appendChild(elementStyle);} catch(e) {console.error('vite-plugin-css-injected-by-js', e);} })();";
chunk.code += bottomCode;

break;

}

}

}

},
transformIndexHtml: {
enforce: "post",
transform(html: string, ctx?: IndexHtmlTransformContext): IndexHtmlTransformResult {

enforce: 'post',
transform(
html: string,
ctx?: IndexHtmlTransformContext
): IndexHtmlTransformResult {
if (!ctx || !ctx.bundle) return html;

for (const [, value] of Object.entries(ctx.bundle)) {

if (value.fileName.endsWith('.css')) {

// Remove CSS link from HTML generated.
const reCSS = new RegExp(`<link rel="stylesheet"[^>]*?href="/${value.fileName}"[^>]*?>`);
const reCSS = new RegExp(
`<link rel="stylesheet"[^>]*?href="/${value.fileName}"[^>]*?>`
);
html = html.replace(reCSS, '');

}

}

return html;

},
},
};

}

module.exports = cssInjectedByJsPlugin
module.exports = cssInjectedByJsPlugin;

cssInjectedByJsPlugin.default = cssInjectedByJsPlugin
cssInjectedByJsPlugin.default = cssInjectedByJsPlugin;

export default cssInjectedByJsPlugin;
export default cssInjectedByJsPlugin;