Skip to content

Commit f552256

Browse files
committed
Fix CSS escaping + add tests for Tailwind CSS
1 parent 2da7005 commit f552256

File tree

7 files changed

+80
-7
lines changed

7 files changed

+80
-7
lines changed

packages/build/src/utilities/build-bundles-async/esbuild-css-modules-plugin.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ async function createCssFilePathAsync(
7878
}
7979

8080
const backQuoteRegex = /`/g
81-
const backSlashColonRegex = /\\:/g
81+
const backSlashRegex = /\\/g
82+
83+
function escapeCss(css: string) {
84+
return css.replace(backQuoteRegex, '\\`').replace(backSlashRegex, '\\\\')
85+
}
8286

8387
async function createGlobalCssJavaScriptAsync(
8488
cssFilePath: string,
@@ -105,9 +109,7 @@ async function createGlobalCssJavaScriptAsync(
105109
if (document.getElementById('${elementId}') === null) {
106110
const element = document.createElement('style');
107111
element.id = '${elementId}';
108-
element.innerHTML = \`${css
109-
.replace(backQuoteRegex, '\\`')
110-
.replace(backSlashColonRegex, '\\\\:')}\`;
112+
element.textContent = \`${escapeCss(css)}\`;
111113
document.head.${isBaseCss === true ? 'prepend' : 'append'}(element);
112114
}
113115
export default {};
@@ -152,9 +154,7 @@ async function createCssModulesJavaScriptAsync(
152154
if (document.getElementById('${elementId}') === null) {
153155
const element = document.createElement('style');
154156
element.id = '${elementId}';
155-
element.textContent = \`${css
156-
.replace(backQuoteRegex, '\\`')
157-
.replace(backSlashColonRegex, '\\\\:')}\`;
157+
element.textContent = \`${escapeCss(css)}\`;
158158
document.head.append(element);
159159
}
160160
export default ${classNamesJson};

packages/build/test/build-async/build-async.ts

+33
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,39 @@ test('global CSS', async function (t) {
450450
await cleanUpAsync()
451451
})
452452

453+
test('tailwind CSS', async function (t) {
454+
t.plan(11)
455+
const directoryPath = join(__dirname, 'fixtures', '14-tailwind-css')
456+
process.chdir(directoryPath)
457+
await cleanUpAsync()
458+
t.false(await pathExists('build'))
459+
t.false(await pathExists('manifest.json'))
460+
t.false(await pathExists('node_modules'))
461+
t.false(await pathExists('src/styles.css.d.ts'))
462+
await installFigmaPluginTypingsAsync()
463+
await symlinkCreateFigmaPluginTsConfigAsync()
464+
await buildAsync({ ...buildAsyncOptions, outputDirectory: directoryPath })
465+
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
466+
t.deepEqual(manifestJson, {
467+
api: '1.0.0',
468+
editorType: ['figma'],
469+
id: '42',
470+
name: 'a',
471+
main: 'build/main.js',
472+
ui: 'build/ui.js'
473+
})
474+
t.true(await pathExists('build/main.js'))
475+
t.true(await pathExists('build/ui.js'))
476+
const uiJs = await fs.readFile('build/ui.js', 'utf8')
477+
t.true(uiJs.indexOf('.\\\\!top-\\\\[117px\\\\]') !== -1)
478+
t.true(
479+
uiJs.indexOf(".before\\\\:content-\\\\[\\\\'foo\\\\'\\\\]::before") !== -1
480+
)
481+
t.true(uiJs.indexOf('.hover\\\\:bg-\\\\[\\\\#bada55\\\\]:hover') !== -1)
482+
t.true(await pathExists('src/styles.css.d.ts'))
483+
await cleanUpAsync()
484+
})
485+
453486
test('preact', async function (t) {
454487
t.plan(6)
455488
const directoryPath = join(__dirname, 'fixtures', '15-preact')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"figma-plugin": {
3+
"id": "42",
4+
"name": "a",
5+
"main": "src/main.ts",
6+
"ui": "src/ui.ts"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { showUI } from '@create-figma-plugin/utilities'
2+
3+
export default function () {
4+
const options = { height: 120, width: 240 }
5+
const data = { greeting: 'Hello, World!' }
6+
showUI(options, data)
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.\!top-\[117px\] {
2+
top: 117px !important;
3+
}
4+
5+
.before\:content-\[\'foo\'\]::before {
6+
--tw-content: 'foo';
7+
content: var(--tw-content);
8+
}
9+
10+
.hover\:bg-\[\#bada55\]:hover {
11+
--tw-bg-opacity: 1;
12+
background-color: rgb(186 218 85 / var(--tw-bg-opacity));
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import '!./styles.css'
2+
3+
export default function (rootNode: HTMLElement, data: { greeting: string }) {
4+
rootNode.innerHTML = `<p class="!top-[117px] hover:bg-[#bada55] before:content-['foo']">${data.greeting}</p>`
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@create-figma-plugin/tsconfig",
3+
"compilerOptions": {
4+
"typeRoots": ["node_modules/@figma"]
5+
},
6+
"include": ["src/**/*.ts"]
7+
}

0 commit comments

Comments
 (0)