Skip to content

Commit f3568f6

Browse files
committed
fix: support import attributes on vite
closes #23
1 parent cbd119a commit f3568f6

File tree

3 files changed

+70
-46
lines changed

3 files changed

+70
-46
lines changed

src/index.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,26 @@ const unplugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
1818
name: 'unplugin-raw',
1919
enforce: options.enforce,
2020

21-
resolveId:
22-
meta.framework === 'rollup' || meta.framework === 'rolldown'
23-
? async function (this, id, importer, opt) {
24-
const attributeType = (opt as any)?.attributes?.type
25-
if (attributeType === 'text') {
26-
id += `${id.includes('?') ? '&' : '?'}raw`
27-
} else if (attributeType === 'bytes') {
28-
id += `${id.includes('?') ? '&' : '?'}bytes`
29-
}
30-
if (!rawRE.test(id)) return
21+
resolveId: ['rollup', 'rolldown', 'vite'].includes(meta.framework)
22+
? async function (this, id, importer, opt) {
23+
const attributeType = (opt as any)?.attributes?.type
24+
if (attributeType === 'text') {
25+
id += `${id.includes('?') ? '&' : '?'}raw`
26+
} else if (attributeType === 'bytes') {
27+
id += `${id.includes('?') ? '&' : '?'}bytes`
28+
}
29+
if (!rawRE.test(id)) return
3130

32-
const file = cleanUrl(id)
33-
const resolved = await (this as PluginContext).resolve(
34-
file,
35-
importer,
36-
)
37-
if (!resolved) return
31+
const file = cleanUrl(id)
32+
const resolved = await (this as PluginContext).resolve(
33+
file,
34+
importer,
35+
)
36+
if (!resolved) return
3837

39-
return id.replace(file, resolved.id)
40-
}
41-
: undefined,
38+
return id.replace(file, resolved.id)
39+
}
40+
: undefined,
4241

4342
load: {
4443
filter: { id: { include: rawRE } },

tests/__snapshots__/basic.test.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,12 @@ var text4 = "export const msg = \\"hello with\\";\\n";
4949
console.log(text, text2, text3, text4);
5050
"
5151
`;
52+
53+
exports[`vite 1`] = `
54+
"const text = 'export const msg = "hello ts";\\n';
55+
const text2 = 'export const msg = "hello js";\\n';
56+
const text3 = 'export const msg = /* @__PURE__ */ React.createElement("div", null, "hello jsx");\\n';
57+
const text4 = 'export const msg = "hello with";\\n';
58+
console.log(text, text2, text3, text4);
59+
"
60+
`;

tests/basic.test.ts

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'node:path'
2-
import { build } from 'esbuild'
3-
import { rollup } from 'rollup'
2+
import { build as esbuild } from 'esbuild'
3+
import { rollup, type Plugin, type RollupOutput } from 'rollup'
4+
import { build as vite } from 'vite'
45
import { expect, test } from 'vitest'
56
import Raw from '../src'
67

@@ -16,7 +17,7 @@ test('esbuild', async () => {
1617
console.log(text, text2, text3, text4, bytes1)
1718
`
1819

19-
const result = await build({
20+
const result = await esbuild({
2021
stdin: {
2122
contents,
2223
resolveDir,
@@ -29,34 +30,49 @@ test('esbuild', async () => {
2930
expect(result.outputFiles[0].text).matchSnapshot()
3031
})
3132

33+
const rollupCode = `
34+
import text from "./ts.ts?raw"
35+
import text2 from "./js.js?raw"
36+
import text3 from "./jsx.jsx?raw"
37+
import text4 from "./with.js" with { type: "text" }
38+
console.log(text, text2, text3, text4)
39+
`
40+
const entryFile = path.resolve(resolveDir, 'main.js')
41+
const rollupPlugin: Plugin = {
42+
name: 'entry',
43+
resolveId(id) {
44+
if (id === entryFile) {
45+
return entryFile
46+
}
47+
},
48+
load(id) {
49+
if (id === entryFile) {
50+
return rollupCode
51+
}
52+
},
53+
}
54+
3255
test('rollup', async () => {
33-
const contents = `
34-
import text from "./ts.ts?raw"
35-
import text2 from "./js.js?raw"
36-
import text3 from "./jsx.jsx?raw"
37-
import text4 from "./with.js" with { type: "text" }
38-
console.log(text, text2, text3, text4)
39-
`
40-
const entryFile = path.resolve(resolveDir, 'main.js')
4156
const bundle = await rollup({
4257
input: [entryFile],
43-
plugins: [
44-
Raw.rollup(),
45-
{
46-
name: 'entry',
47-
resolveId(id) {
48-
if (id === entryFile) {
49-
return entryFile
50-
}
51-
},
52-
load(id) {
53-
if (id === entryFile) {
54-
return contents
55-
}
56-
},
57-
},
58-
],
58+
plugins: [Raw.rollup(), rollupPlugin],
5959
})
6060
const result = await bundle.generate({ format: 'esm' })
6161
expect(result.output[0].code).matchSnapshot()
6262
})
63+
64+
test('vite', async () => {
65+
const output = await vite({
66+
root: resolveDir,
67+
plugins: [Raw.vite(), rollupPlugin],
68+
build: {
69+
rollupOptions: {
70+
input: [entryFile],
71+
},
72+
minify: false,
73+
write: false,
74+
},
75+
logLevel: 'silent',
76+
})
77+
expect((output as RollupOutput).output[0].code).matchSnapshot()
78+
})

0 commit comments

Comments
 (0)