Skip to content

Commit

Permalink
fix: build style i metropolis when fixing multiple tabs(close:#53 )
Browse files Browse the repository at this point in the history
  • Loading branch information
baiwusanyu-c committed May 4, 2023
1 parent fb51878 commit 541d4d1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"peerDependencies": {
"baiwusanyu-utils": "^1.0.5",
"chalk": "^4.1.2",
"estree-walker-ts": "^1.0.0-beta.2",
"estree-walker-ts": "^1.0.0",
"fast-glob": "^3.2.12",
"fs-extra": "^11.1.1",
"hash-sum": "^2.0.0",
Expand All @@ -86,7 +86,7 @@
"dependencies": {
"baiwusanyu-utils": "^1.0.5",
"chalk": "^4.1.2",
"estree-walker-ts": "^1.0.0-beta.2",
"estree-walker-ts": "^1.0.0",
"fast-glob": "^3.2.12",
"fs-extra": "^11.1.1",
"hash-sum": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`inject-css > injectCssOnBuild: basic 1`] = `
"<style lang=\\"scss\\" >
"
<style lang=\\"scss\\" >
body { background-color: black; }
/* foo.scss -> test2.css -> test.css */
Expand All @@ -14,8 +15,30 @@ div {
</style>"
`;

exports[`inject-css > injectCssOnBuild: mutiple style tags 1`] = `
"
<style lang=\\"scss\\" >
body { background-color: black; }
/* foo.scss -> test2.css -> test.css */
/* foo.scss -> test.scss -> test2.css */
/*@import \\"./assets/less/less-foo\\";*/
div {
color: v-bind(color)
}
</style>
<style lang=\\"scss\\" >
.el-popup-parent--hidden {
.fixed-header {
padding-right: 17px;
}
} </style> "
`;

exports[`inject-css > injectCssOnBuild: no lang 1`] = `
"<style lang=\\"css\\" >
"
<style lang=\\"css\\" >
body { background-color: black; }
/* foo.scss -> test2.css -> test.css */
Expand Down
52 changes: 52 additions & 0 deletions packages/core/inject/__test__/inject-css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,58 @@ describe('inject-css', () => {
expect(result.toString()).toMatchSnapshot()
})

// fix: #53
test('injectCssOnBuild: mutiple style tags', () => {
const code = '<style lang="scss">\n'
+ '/* foo.scss -> test2.css -> test.css */\n'
+ '/* foo.scss -> test.scss -> test2.css */\n'
+ '\n'
+ '/*@import "./assets/less/less-foo";*/\n'
+ 'div {\n'
+ ' color: v-bind(color)\n'
+ '}\n'
+ "@import './assets/scss/foo.scss';\n"
+ '</style> '
+ '<style lang="scss">\n'
+ ' .el-popup-parent--hidden {\n'
+ ' .fixed-header {\n'
+ ' padding-right: 17px;\n'
+ ' }\n'
+ ' }\n'
+ '</style>'
const mgcStr = new MagicString(code)
const injectCSSContent = new Set([{
content: '@import \'./assets/scss/foo.scss\';\nbody { background-color: black; }',
lang: 'scss',
styleTagIndex: 0,
}])
const descriptor = {
styles: [
{
lang: 'scss',
content: '/* foo.scss -> test2.css -> test.css */\n'
+ '/* foo.scss -> test.scss -> test2.css */\n'
+ '\n'
+ '/*@import "./assets/less/less-foo";*/\n'
+ 'div {\n'
+ ' color: v-bind(color)\n'
+ '}\n'
+ "@import './assets/scss/foo.scss';",
},
{
lang: 'scss',
content: ' .el-popup-parent--hidden {\n'
+ ' .fixed-header {\n'
+ ' padding-right: 17px;\n'
+ ' }\n'
+ ' }',
},
],
}
const result = injectCssOnBuild(mgcStr, injectCSSContent, descriptor as any)
expect(result.toString()).toMatchSnapshot()
})

test('injectCssOnBuild: no styles', () => {
const code = 'test'
const injectCSSContent = new Set([{
Expand Down
2 changes: 1 addition & 1 deletion packages/core/inject/inject-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function injectCssOnBuild(
})
const lang = value.lang || 'css'
const scoped = value.scoped ? 'scoped' : ''
resCode = `<style lang="${lang}" ${scoped}> ${injectCssCode}\n${transformInjectCSS(value.content, parseImports(value.content).imports)} </style>`
resCode = `${resCode}\n<style lang="${lang}" ${scoped}> ${injectCssCode}\n${transformInjectCSS(value.content, parseImports(value.content).imports)} </style>`
})
resCode && (mgcStr = removeStyleTagsAndContent(mgcStr))
return mgcStr.prependRight(mgcStr.length(), resCode)
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 541d4d1

Please sign in to comment.