11import fs from 'node:fs' ;
2- import { toRollupError } from '../utils/error.js' ;
32import { log } from '../utils/log.js' ;
43
54/**
5+ * if svelte config includes files that vite treats as assets (e.g. .svg)
6+ * we have to manually load them to avoid getting urls
7+ *
68 * @param {import('../types/plugin-api.d.ts').PluginAPI } api
79 * @returns {import('vite').Plugin }
810 */
@@ -23,148 +25,18 @@ export function loadCustom(api) {
2325 const ssr = config . consumer === 'server' ;
2426 const svelteRequest = api . idParser ( id , ssr ) ;
2527 if ( svelteRequest ) {
26- const { filename, raw } = svelteRequest ;
27- if ( raw ) {
28- const code = await compileRaw ( svelteRequest , api . compileSvelte , api . options ) ;
29- // prevent vite from injecting sourcemaps in the results.
30- return {
31- code,
32- map : {
33- mappings : ''
34- }
35- } ;
36- } else {
37- // prevent vite asset plugin from loading files as url that should be compiled in transform
38- if ( config . assetsInclude ( filename ) ) {
39- log . debug ( `load returns raw content for ${ filename } ` , undefined , 'load' ) ;
40- return fs . readFileSync ( filename , 'utf-8' ) ;
41- }
28+ const { filename, raw, query } = svelteRequest ;
29+ if ( ! query . url && ( raw || config . assetsInclude ( filename ) ) ) {
30+ log . debug (
31+ `loading ${ filename } to prevent vite asset handling to turn it into a url by default` ,
32+ undefined ,
33+ 'load'
34+ ) ;
35+ return fs . readFileSync ( filename , 'utf-8' ) ;
4236 }
4337 }
4438 }
4539 }
4640 } ;
4741 return plugin ;
4842}
49-
50- /**
51- * utility function to compile ?raw and ?direct requests in load hook
52- *
53- * @param {import('../types/id.d.ts').SvelteRequest } svelteRequest
54- * @param {import('../types/compile.d.ts').CompileSvelte } compileSvelte
55- * @param {import('../types/options.d.ts').ResolvedOptions } options
56- * @returns {Promise<string> }
57- */
58- async function compileRaw ( svelteRequest , compileSvelte , options ) {
59- const { id, filename, query } = svelteRequest ;
60-
61- // raw svelte subrequest, compile on the fly and return requested subpart
62- let compileData ;
63- const source = fs . readFileSync ( filename , 'utf-8' ) ;
64- try {
65- //avoid compileSvelte doing extra ssr stuff unless requested
66- svelteRequest . ssr = query . compilerOptions ?. generate === 'server' ;
67- compileData = await compileSvelte ( svelteRequest , source , {
68- ...options ,
69- // don't use dynamic vite-plugin-svelte defaults here to ensure stable result between ssr,dev and build
70- compilerOptions : {
71- dev : false ,
72- css : 'external' ,
73- hmr : false ,
74- ...svelteRequest . query . compilerOptions
75- } ,
76- emitCss : true
77- } ) ;
78- } catch ( e ) {
79- throw toRollupError ( e , options ) ;
80- }
81- let result ;
82- if ( query . type === 'style' ) {
83- result = compileData . compiled . css ?? { code : '' , map : null } ;
84- } else if ( query . type === 'script' ) {
85- result = compileData . compiled . js ;
86- } else if ( query . type === 'preprocessed' ) {
87- result = compileData . preprocessed ;
88- } else if ( query . type === 'all' && query . raw ) {
89- return allToRawExports ( compileData , source ) ;
90- } else {
91- throw new Error (
92- `invalid "type=${ query . type } " in ${ id } . supported are script, style, preprocessed, all`
93- ) ;
94- }
95- if ( query . direct ) {
96- const supportedDirectTypes = [ 'script' , 'style' ] ;
97- if ( ! supportedDirectTypes . includes ( query . type ) ) {
98- throw new Error (
99- `invalid "type=${
100- query . type
101- } " combined with direct in ${ id } . supported are: ${ supportedDirectTypes . join ( ', ' ) } `
102- ) ;
103- }
104- log . debug ( `load returns direct result for ${ id } ` , undefined , 'load' ) ;
105- let directOutput = result . code ;
106- // @ts -expect-error might not be SourceMap but toUrl check should suffice
107- if ( query . sourcemap && result . map ?. toUrl ) {
108- // @ts -expect-error toUrl might not exist
109- const map = `sourceMappingURL=${ result . map . toUrl ( ) } ` ;
110- if ( query . type === 'style' ) {
111- directOutput += `\n\n/*# ${ map } */\n` ;
112- } else if ( query . type === 'script' ) {
113- directOutput += `\n\n//# ${ map } \n` ;
114- }
115- }
116- return directOutput ;
117- } else if ( query . raw ) {
118- log . debug ( `load returns raw result for ${ id } ` , undefined , 'load' ) ;
119- return toRawExports ( result ) ;
120- } else {
121- throw new Error ( `invalid raw mode in ${ id } , supported are raw, direct` ) ;
122- }
123- }
124-
125- /**
126- * turn compileData and source into a flat list of raw exports
127- *
128- * @param {import('../types/compile.d.ts').CompileData } compileData
129- * @param {string } source
130- */
131- function allToRawExports ( compileData , source ) {
132- // flatten CompileData
133- /** @type {Partial<import('../types/compile.d.ts').CompileData & { source: string }> } */
134- const exports = {
135- ...compileData ,
136- ...compileData . compiled ,
137- source
138- } ;
139- delete exports . compiled ;
140- delete exports . filename ; // absolute path, remove to avoid it in output
141- return toRawExports ( exports ) ;
142- }
143-
144- /**
145- * turn object into raw exports.
146- *
147- * every prop is returned as a const export, and if prop 'code' exists it is additionally added as default export
148- *
149- * eg {'foo':'bar','code':'baz'} results in
150- *
151- * ```js
152- * export const code='baz'
153- * export const foo='bar'
154- * export default code
155- * ```
156- * @param {object } object
157- * @returns {string }
158- */
159- function toRawExports ( object ) {
160- let exports =
161- Object . entries ( object )
162- . filter ( ( [ _key , value ] ) => typeof value !== 'function' ) // preprocess output has a toString function that's enumerable
163- . sort ( ( [ a ] , [ b ] ) => ( a < b ? - 1 : a === b ? 0 : 1 ) )
164- . map ( ( [ key , value ] ) => `export const ${ key } =${ JSON . stringify ( value ) } ` )
165- . join ( '\n' ) + '\n' ;
166- if ( Object . prototype . hasOwnProperty . call ( object , 'code' ) ) {
167- exports += 'export default code\n' ;
168- }
169- return exports ;
170- }
0 commit comments