Skip to content

Commit ef4a0b9

Browse files
committed
feat: add asString option
closes #35
1 parent 1523c11 commit ef4a0b9

File tree

7 files changed

+85
-13
lines changed

7 files changed

+85
-13
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = {
7171

7272
<br></details>
7373

74-
## Example
74+
## Options
7575

7676
```ts
7777
import { Features } from 'lightningcss'
@@ -82,6 +82,12 @@ export default {
8282
options: {
8383
include: Features.Nesting,
8484
},
85+
86+
/**
87+
* Whether to export the transformed CSS as a default string export.
88+
* @default false
89+
*/
90+
asString: false,
8591
}),
8692
],
8793
}

src/core/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export type Options = {
66
exclude?: FilterPattern
77
enforce?: 'pre' | 'post' | undefined
88
options?: Omit<TransformOptions<any>, 'code' | 'filename'>
9+
/**
10+
* Whether to export the transformed CSS as a default string export.
11+
* @default false
12+
*/
13+
asString?: boolean
914
}
1015

1116
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
@@ -21,5 +26,6 @@ export function resolveOption(options: Options): OptionsResolved {
2126
exclude: options.exclude || [/node_modules/],
2227
enforce: 'enforce' in options ? options.enforce : 'pre',
2328
options: options.options || {},
29+
asString: options.asString || false,
2430
}
2531
}

src/core/transform.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@ export async function transformCss(
1111
id: string,
1212
code: string,
1313
options: Options['options'],
14-
): Promise<{ code: string; map?: string }> {
14+
asString?: boolean,
15+
): Promise<{ code: string; map?: string; moduleType: 'css' | 'js' }> {
1516
const filename = cleanUrl(id)
1617
const { transform } = await import('lightningcss')
17-
const res = transform({
18+
const result = transform({
1819
...options,
1920
filename,
2021
code: Buffer.from(code),
2122
})
23+
let resultCode = result.code.toString()
24+
if (asString) {
25+
resultCode = `export default ${JSON.stringify(resultCode)}`
26+
}
2227
return {
23-
code: res.code.toString(),
24-
map: 'map' in res ? res.map?.toString() : undefined,
28+
code: resultCode,
29+
map: asString
30+
? undefined
31+
: 'map' in result
32+
? result.map?.toString()
33+
: undefined,
34+
moduleType: asString ? 'js' : 'css',
2535
}
2636
}
2737

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
2929
},
3030
},
3131
handler(code, id) {
32-
return transformCss(id, code, options.options)
32+
return transformCss(id, code, options.options, options.asString)
3333
},
3434
},
3535

tests/__snapshots__/transform.test.ts.snap

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,53 @@ document.body.append(btn);
1414
"
1515
`;
1616

17-
exports[`transform > tests/fixtures/basic.css 1`] = `
17+
exports[`rolldown transform > tests/fixtures/as-string.css 1`] = `
18+
"// as-string.js
19+
//#region tests/fixtures/as-string.css
20+
var as_string_default = ".red{color:red}";
21+
22+
//#endregion
23+
export { as_string_default as default };"
24+
`;
25+
26+
exports[`rolldown transform > tests/fixtures/basic.css 1`] = `
27+
"// basic.css
28+
.red{color:red}
29+
30+
// basic.js
31+
"
32+
`;
33+
34+
exports[`rolldown transform > tests/fixtures/import.css 1`] = `
35+
"// import.css
36+
.red{color:red}
37+
38+
39+
// import.js
40+
//#region tests/fixtures/basic.css
41+
var basic_default = {};
42+
43+
//#endregion"
44+
`;
45+
46+
exports[`rolldown transform > tests/fixtures/nesting.css 1`] = `
47+
"// nesting.css
48+
.a .b{color:red}
49+
50+
// nesting.js
51+
"
52+
`;
53+
54+
exports[`rollup transform > tests/fixtures/as-string.css 1`] = `
55+
"// as-string.js
56+
57+
58+
// assets/asset-CRu7KFtz
59+
export default ".red{color:red}"
60+
"
61+
`;
62+
63+
exports[`rollup transform > tests/fixtures/basic.css 1`] = `
1864
"// assets/asset-CuZ0KhGL
1965
.red{color:red}
2066
@@ -23,7 +69,7 @@ exports[`transform > tests/fixtures/basic.css 1`] = `
2369
"
2470
`;
2571

26-
exports[`transform > tests/fixtures/import.css 1`] = `
72+
exports[`rollup transform > tests/fixtures/import.css 1`] = `
2773
"// assets/asset-C9cPOTFW
2874
@import "./basic.css";
2975
@@ -32,7 +78,7 @@ exports[`transform > tests/fixtures/import.css 1`] = `
3278
"
3379
`;
3480

35-
exports[`transform > tests/fixtures/nesting.css 1`] = `
81+
exports[`rollup transform > tests/fixtures/nesting.css 1`] = `
3682
"// assets/asset-B4RRm-s2
3783
.a .b{color:red}
3884

tests/fixtures/as-string.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.red {
2+
color: red;
3+
}

tests/transform.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import { resolve } from 'node:path'
2-
import { rollupBuild, testFixtures } from '@sxzz/test-utils'
2+
import { rolldownBuild, rollupBuild, testFixtures } from '@sxzz/test-utils'
33
import css from 'rollup-plugin-css-only'
44
import { describe, expect, it } from 'vitest'
55
import LightningCSS from '../src/rollup'
66

7-
describe('transform', async () => {
7+
describe.each(['rollup', 'rolldown'] as const)('%s transform', async (type) => {
88
await testFixtures(
99
['tests/fixtures/*.css'],
1010
async (args, id) =>
1111
(
12-
await rollupBuild(id, [
12+
await (type === 'rollup' ? rollupBuild : rolldownBuild)(id, [
1313
LightningCSS({
1414
options: {
1515
minify: true,
1616
targets: {
1717
ie: 11,
1818
},
1919
},
20+
asString: id.includes('as-string'),
2021
}),
21-
css(),
22+
type === 'rollup' && css(),
2223
])
2324
).snapshot,
2425
{ cwd: resolve(__dirname, '..'), promise: true },

0 commit comments

Comments
 (0)