|
import MagicString from 'magic-string'; |
|
import { log } from './log.js'; |
|
import path from 'node:path'; |
|
import { normalizePath } from 'vite'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function createInjectScopeEverythingRulePreprocessorGroup() { |
|
return { |
|
name: 'inject-scope-everything-rule', |
|
style({ content, filename }) { |
|
const s = new MagicString(content); |
|
s.append(' *{}'); |
|
return { |
|
code: s.toString(), |
|
map: s.generateDecodedMap({ |
|
source: filename ? path.basename(filename) : undefined, |
|
hires: true |
|
}) |
|
}; |
|
} |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function buildExtraPreprocessors(options, config) { |
|
|
|
const prependPreprocessors = []; |
|
|
|
const appendPreprocessors = []; |
|
|
|
|
|
const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p?.sveltePreprocess); |
|
if (pluginsWithPreprocessorsDeprecated.length > 0) { |
|
log.warn( |
|
`The following plugins use the deprecated 'plugin.sveltePreprocess' field. Please contact their maintainers and ask them to move it to 'plugin.api.sveltePreprocess': ${pluginsWithPreprocessorsDeprecated |
|
.map((p) => p.name) |
|
.join(', ')}` |
|
); |
|
|
|
pluginsWithPreprocessorsDeprecated.forEach((p) => { |
|
if (!p.api) { |
|
p.api = {}; |
|
} |
|
if (p.api.sveltePreprocess === undefined) { |
|
|
|
p.api.sveltePreprocess = p.sveltePreprocess; |
|
} else { |
|
log.error( |
|
`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.` |
|
); |
|
} |
|
}); |
|
} |
|
|
|
const pluginsWithPreprocessors = config.plugins.filter((p) => p?.api?.sveltePreprocess); |
|
|
|
const ignored = []; |
|
|
|
const included = []; |
|
for (const p of pluginsWithPreprocessors) { |
|
if ( |
|
options.ignorePluginPreprocessors === true || |
|
(Array.isArray(options.ignorePluginPreprocessors) && |
|
options.ignorePluginPreprocessors?.includes(p.name)) |
|
) { |
|
ignored.push(p); |
|
} else { |
|
included.push(p); |
|
} |
|
} |
|
if (ignored.length > 0) { |
|
log.debug( |
|
`Ignoring svelte preprocessors defined by these vite plugins: ${ignored |
|
.map((p) => p.name) |
|
.join(', ')}`, |
|
undefined, |
|
'preprocess' |
|
); |
|
} |
|
if (included.length > 0) { |
|
log.debug( |
|
`Adding svelte preprocessors defined by these vite plugins: ${included |
|
.map((p) => p.name) |
|
.join(', ')}`, |
|
undefined, |
|
'preprocess' |
|
); |
|
appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess)); |
|
} |
|
|
|
return { prependPreprocessors, appendPreprocessors }; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function addExtraPreprocessors(options, config) { |
|
const { prependPreprocessors, appendPreprocessors } = buildExtraPreprocessors(options, config); |
|
if (prependPreprocessors.length > 0 || appendPreprocessors.length > 0) { |
|
if (!options.preprocess) { |
|
options.preprocess = [...prependPreprocessors, ...appendPreprocessors]; |
|
} else if (Array.isArray(options.preprocess)) { |
|
options.preprocess.unshift(...prependPreprocessors); |
|
options.preprocess.push(...appendPreprocessors); |
|
} else { |
|
options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors]; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function checkPreprocessDependencies(filename, dependencies) { |
|
|
|
const warnings = []; |
|
|
|
|
|
|
|
|
|
|
|
const selfIdx = []; |
|
const normalizedFullFilename = normalizePath(filename); |
|
const normalizedDeps = dependencies.map(normalizePath); |
|
for (let i = 0; i < normalizedDeps.length; i++) { |
|
if (normalizedDeps[i] === normalizedFullFilename) { |
|
selfIdx.push(i); |
|
} |
|
} |
|
const hasSelfDependency = selfIdx.length > 0; |
|
if (hasSelfDependency) { |
|
warnings.push({ |
|
code: 'vite-plugin-svelte-preprocess-depends-on-self', |
|
message: |
|
'svelte.preprocess returned this file as a dependency of itself. This can be caused by an invalid configuration or importing generated code that depends on .svelte files (eg. tailwind base css)', |
|
filename |
|
}); |
|
} |
|
|
|
if (dependencies.length > 10) { |
|
warnings.push({ |
|
code: 'vite-plugin-svelte-preprocess-many-dependencies', |
|
message: `svelte.preprocess depends on more than 10 external files which can cause slow builds and poor DX, try to reduce them. Found: ${dependencies.join( |
|
', ' |
|
)}`, |
|
filename |
|
}); |
|
} |
|
return { |
|
dependencies: hasSelfDependency |
|
? dependencies.filter((_, i) => !selfIdx.includes(i)) |
|
: dependencies, |
|
warnings |
|
}; |
|
} |
|
|