这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: support assets can be imported as strings #1026

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions src/client.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*?raw' {
const value: string
export default value
}
24 changes: 24 additions & 0 deletions src/esbuild/import-raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { readFile } from 'fs/promises'
import type { Plugin } from 'esbuild'
import { join } from 'path'

/**
* Importing a resource as a string
*/
export const importRawPlugin = (): Plugin => {
return {
name: 'importRawPlugin',
setup(build) {
const rawReg = /(?:\?|&)raw(?:&|$)/
build.onResolve({ filter: rawReg }, (args) => {
const { resolveDir, path } = args
return { path: join(resolveDir, path) }
})
build.onLoad({ filter: rawReg }, async (args) => {
const path = args.path.replace(rawReg, '')
const content = await readFile(path, 'utf-8')
return { contents: JSON.stringify(content), loader: 'text' }
})
},
}
}
2 changes: 2 additions & 0 deletions src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { swcPlugin } from './swc'
import { nativeNodeModulesPlugin } from './native-node-module'
import { PluginContainer } from '../plugin'
import { OutExtensionFactory } from '../options'
import { importRawPlugin } from './import-raw'

const getOutputExtensionMap = (
options: NormalizedOptions,
Expand Down Expand Up @@ -146,6 +147,7 @@ export async function runEsbuild(
cssLoader: loader['.css'],
}),
sveltePlugin({ css }),
importRawPlugin(),
...(options.esbuildPlugins || []),
]

Expand Down
49 changes: 43 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1345,9 +1345,14 @@ test('should emit a declaration file per format', async () => {
format: ['esm', 'cjs'],
dts: true
}`,
});
expect(outFiles).toEqual(['input.d.mts', 'input.d.ts', 'input.js', 'input.mjs'])
});
})
expect(outFiles).toEqual([
'input.d.mts',
'input.d.ts',
'input.js',
'input.mjs',
])
})

test('should emit a declaration file per format (type: module)', async () => {
const { outFiles } = await run(getTestName(), {
Expand All @@ -1361,6 +1366,38 @@ test('should emit a declaration file per format (type: module)', async () => {
format: ['esm', 'cjs'],
dts: true
}`,
});
expect(outFiles).toEqual(['input.cjs', 'input.d.cts', 'input.d.ts', 'input.js'])
});
})
expect(outFiles).toEqual([
'input.cjs',
'input.d.cts',
'input.d.ts',
'input.js',
])
})

test('should importing a resource as a string', async () => {
const { outFiles } = await run(getTestName(), {
'input.ts': `
import fragText from "./frag.glsl?raw"
console.log(fragText)
`,
'frag.glsl': `void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}`,
'package.json': `{
"type": "module"
}`,
'tsup.config.ts': `
export default {
entry: ['src/input.ts'],
format: ['esm', 'cjs'],
dts: true
}`,
})
expect(outFiles).toEqual([
'input.cjs',
'input.d.cts',
'input.d.ts',
'input.js',
])
})