@sveltejs/vite-plugin-svelte
Advanced tools
Comparing version 1.0.0-next.7 to 1.0.0-next.8
# @sveltejs/vite-plugin-svelte | ||
## 1.0.0-next.8 | ||
### Minor Changes | ||
- [#35](https://github.com/sveltejs/vite-plugin-svelte/pull/35) [`4018ce6`](https://github.com/sveltejs/vite-plugin-svelte/commit/4018ce621b4df75877e0e18057c332f27158d42b) Feature: Support esm in svelte.config.js and svelte.config.mjs | ||
* [#35](https://github.com/sveltejs/vite-plugin-svelte/pull/35) [`4018ce6`](https://github.com/sveltejs/vite-plugin-svelte/commit/4018ce621b4df75877e0e18057c332f27158d42b) Feature: add configFile option | ||
### Patch Changes | ||
- [#34](https://github.com/sveltejs/vite-plugin-svelte/pull/34) [`e5d4749`](https://github.com/sveltejs/vite-plugin-svelte/commit/e5d4749c0850260a295daab9cb15866fe58ee709) fix: watch preprocessor dependencies and trigger hmr on change | ||
## 1.0.0-next.7 | ||
@@ -7,3 +19,3 @@ | ||
- [#32](https://github.com/sveltejs/vite-plugin-svelte/pull/32) [`113bb7d`](https://github.com/sveltejs/vite-plugin-svelte/commit/113bb7dc330a7517085d12d1d0758a376a12253f) Thanks [@dominikg](https://github.com/dominikg)! - Reduced cache usage, share css cache between SSR and client | ||
- [#32](https://github.com/sveltejs/vite-plugin-svelte/pull/32) [`113bb7d`](https://github.com/sveltejs/vite-plugin-svelte/commit/113bb7dc330a7517085d12d1d0758a376a12253f) Reduced cache usage, share css cache between SSR and client | ||
@@ -10,0 +22,0 @@ ## 1.0.0-next.6 |
@@ -44,2 +44,4 @@ import { Plugin as Plugin_2 } from 'vite'; | ||
export declare interface Options { | ||
/** path to svelte config file, either absolute or relative to vite root*/ | ||
configFile?: string; | ||
/** One or more minimatch patterns */ | ||
@@ -46,0 +48,0 @@ include?: Arrayable<string>; |
@@ -222,71 +222,4 @@ var __create = Object.create; | ||
// src/utils/preprocess.ts | ||
var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"]; | ||
var supportedScriptLangs = ["ts"]; | ||
function createPreprocessorFromVitePlugin(config, options, pluginName, supportedLangs) { | ||
const plugin = config.plugins.find((p) => p.name === pluginName); | ||
if (!plugin) { | ||
throw new Error(`failed to find plugin ${pluginName}`); | ||
} | ||
if (!plugin.transform) { | ||
throw new Error(`plugin ${pluginName} has no transform`); | ||
} | ||
const pluginTransform = plugin.transform.bind(null); | ||
return async ({attributes, content, filename}) => { | ||
var _a, _b, _c, _d; | ||
const lang = attributes.lang; | ||
if (!supportedLangs.includes(lang)) { | ||
return {code: content}; | ||
} | ||
const moduleId = `${filename}.${lang}`; | ||
const moduleGraph = (_a = options.server) == null ? void 0 : _a.moduleGraph; | ||
if (moduleGraph && !moduleGraph.getModuleById(moduleId)) { | ||
await moduleGraph.ensureEntryFromUrl(moduleId); | ||
} | ||
const transformResult = await pluginTransform(content, moduleId); | ||
const hasMap = !!((_b = transformResult.map) == null ? void 0 : _b.mappings); | ||
if (((_d = (_c = transformResult.map) == null ? void 0 : _c.sources) == null ? void 0 : _d[0]) === moduleId) { | ||
transformResult.map.sources[0] = filename; | ||
} | ||
return { | ||
code: transformResult.code, | ||
map: hasMap ? transformResult.map : null, | ||
dependencies: transformResult.deps | ||
}; | ||
}; | ||
} | ||
function createVitePreprocessorGroup(config, options) { | ||
return { | ||
script: createPreprocessorFromVitePlugin(config, options, "vite:esbuild", supportedScriptLangs), | ||
style: createPreprocessorFromVitePlugin(config, options, "vite:css", supportedStyleLangs) | ||
}; | ||
} | ||
function createInjectScopeEverythingRulePreprocessorGroup() { | ||
return { | ||
style({content}) { | ||
return { | ||
code: `${content} *{}` | ||
}; | ||
} | ||
}; | ||
} | ||
function buildExtraPreprocessors(options, config) { | ||
const extraPreprocessors = []; | ||
if (options.useVitePreprocess) { | ||
log.debug("adding vite preprocessor"); | ||
extraPreprocessors.push(createVitePreprocessorGroup(config, options)); | ||
} | ||
const pluginsWithPreprocessors = config.plugins.filter((p) => p == null ? void 0 : p.sveltePreprocess); | ||
if (pluginsWithPreprocessors.length > 0) { | ||
log.debug(`adding preprocessors from other vite plugins: ${pluginsWithPreprocessors.map((p) => p.name).join(", ")}`); | ||
extraPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.sveltePreprocess)); | ||
} | ||
if (options.hot && !options.disableCssHmr) { | ||
extraPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup()); | ||
} | ||
return extraPreprocessors; | ||
} | ||
// src/utils/compile.ts | ||
var _createCompileSvelte = (makeHot, extraPreprocessors) => async function compileSvelte(svelteRequest, code, options) { | ||
var _createCompileSvelte = (makeHot) => async function compileSvelte(svelteRequest, code, options) { | ||
const {filename, normalizedFilename, cssId, ssr} = svelteRequest; | ||
@@ -307,13 +240,4 @@ const {onwarn, emitCss = true} = options; | ||
let preprocessed; | ||
const preprocessors = []; | ||
if (options.preprocess) { | ||
if (Array.isArray(options.preprocess)) { | ||
preprocessors.push(...options.preprocess); | ||
} else { | ||
preprocessors.push(options.preprocess); | ||
} | ||
} | ||
preprocessors.push(...extraPreprocessors || []); | ||
if (preprocessors.length > 0) { | ||
preprocessed = await (0, import_compiler.preprocess)(code, preprocessors, {filename}); | ||
preprocessed = await (0, import_compiler.preprocess)(code, options.preprocess, {filename}); | ||
if (preprocessed.dependencies) | ||
@@ -348,5 +272,7 @@ dependencies.push(...preprocessed.dependencies); | ||
return { | ||
filename, | ||
normalizedFilename, | ||
compiled, | ||
ssr | ||
ssr, | ||
dependencies | ||
}; | ||
@@ -368,6 +294,5 @@ }; | ||
} | ||
function createCompileSvelte(options, config) { | ||
function createCompileSvelte(options) { | ||
const makeHot = buildMakeHot(options); | ||
const extraPreprocessors = buildExtraPreprocessors(options, config); | ||
return _createCompileSvelte(makeHot, extraPreprocessors); | ||
return _createCompileSvelte(makeHot); | ||
} | ||
@@ -448,22 +373,129 @@ | ||
var import_fs = __toModule(require("fs")); | ||
var knownSvelteConfigNames = ["svelte.config.js", "svelte.config.cjs"]; | ||
function loadSvelteConfig(root = process.cwd()) { | ||
const foundConfigs = knownSvelteConfigNames.map((candidate) => import_path.default.resolve(root, candidate)).filter((file) => import_fs.default.existsSync(file)); | ||
if (foundConfigs.length === 0) { | ||
log.debug(`no svelte config found at ${root}`); | ||
return; | ||
} else if (foundConfigs.length > 1) { | ||
log.warn(`found more than one svelte config file, using ${foundConfigs[0]}. you should only have one!`, foundConfigs); | ||
var knownSvelteConfigNames = ["svelte.config.js", "svelte.config.cjs", "svelte.config.mjs"]; | ||
var dynamicImportDefault = new Function("path", "return import(path).then(m => m.default)"); | ||
async function loadSvelteConfig(viteConfig, inlineOptions) { | ||
const configFile = findConfigToLoad(viteConfig, inlineOptions); | ||
if (configFile) { | ||
let err; | ||
if (configFile.endsWith(".js") || configFile.endsWith(".mjs")) { | ||
try { | ||
return await dynamicImportDefault(configFile); | ||
} catch (e) { | ||
log.debug(`failed to import config ${configFile}`, e); | ||
err = e; | ||
} | ||
} | ||
try { | ||
return require(configFile); | ||
} catch (e) { | ||
log.error(`failed to require config ${configFile}`, e); | ||
if (!err) { | ||
err = e; | ||
} | ||
} | ||
throw err; | ||
} | ||
try { | ||
const config = require(foundConfigs[0]); | ||
log.debug(`loaded svelte config ${foundConfigs[0]}`, config); | ||
return config; | ||
} catch (e) { | ||
log.error(`failed to load config ${foundConfigs[0]}`, e); | ||
} | ||
function findConfigToLoad(viteConfig, inlineOptions) { | ||
const root = viteConfig.root || process.cwd(); | ||
if (inlineOptions.configFile) { | ||
const abolutePath = import_path.default.isAbsolute(inlineOptions.configFile) ? inlineOptions.configFile : import_path.default.resolve(root, inlineOptions.configFile); | ||
if (!import_fs.default.existsSync(abolutePath)) { | ||
throw new Error(`failed to find svelte config file ${abolutePath}.`); | ||
} | ||
return abolutePath; | ||
} else { | ||
const existingKnownConfigFiles = knownSvelteConfigNames.map((candidate) => import_path.default.resolve(root, candidate)).filter((file) => import_fs.default.existsSync(file)); | ||
if (existingKnownConfigFiles.length === 0) { | ||
log.debug(`no svelte config found at ${root}`); | ||
return; | ||
} else if (existingKnownConfigFiles.length > 1) { | ||
log.warn(`found more than one svelte config file, using ${existingKnownConfigFiles[0]}. you should only have one!`, existingKnownConfigFiles); | ||
} | ||
return existingKnownConfigFiles[0]; | ||
} | ||
} | ||
// src/utils/preprocess.ts | ||
var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"]; | ||
var supportedScriptLangs = ["ts"]; | ||
function createPreprocessorFromVitePlugin(config, options, pluginName, supportedLangs) { | ||
const plugin = config.plugins.find((p) => p.name === pluginName); | ||
if (!plugin) { | ||
throw new Error(`failed to find plugin ${pluginName}`); | ||
} | ||
if (!plugin.transform) { | ||
throw new Error(`plugin ${pluginName} has no transform`); | ||
} | ||
const pluginTransform = plugin.transform.bind(null); | ||
return async ({attributes, content, filename}) => { | ||
var _a, _b, _c, _d; | ||
const lang = attributes.lang; | ||
if (!supportedLangs.includes(lang)) { | ||
return {code: content}; | ||
} | ||
const moduleId = `${filename}.${lang}`; | ||
const moduleGraph = (_a = options.server) == null ? void 0 : _a.moduleGraph; | ||
if (moduleGraph && !moduleGraph.getModuleById(moduleId)) { | ||
await moduleGraph.ensureEntryFromUrl(moduleId); | ||
} | ||
const transformResult = await pluginTransform(content, moduleId); | ||
const hasMap = !!((_b = transformResult.map) == null ? void 0 : _b.mappings); | ||
if (((_d = (_c = transformResult.map) == null ? void 0 : _c.sources) == null ? void 0 : _d[0]) === moduleId) { | ||
transformResult.map.sources[0] = filename; | ||
} | ||
return { | ||
code: transformResult.code, | ||
map: hasMap ? transformResult.map : null, | ||
dependencies: transformResult.deps | ||
}; | ||
}; | ||
} | ||
function createVitePreprocessorGroup(config, options) { | ||
return { | ||
script: createPreprocessorFromVitePlugin(config, options, "vite:esbuild", supportedScriptLangs), | ||
style: createPreprocessorFromVitePlugin(config, options, "vite:css", supportedStyleLangs) | ||
}; | ||
} | ||
function createInjectScopeEverythingRulePreprocessorGroup() { | ||
return { | ||
style({content}) { | ||
return { | ||
code: `${content} *{}` | ||
}; | ||
} | ||
}; | ||
} | ||
function buildExtraPreprocessors(options, config) { | ||
const extraPreprocessors = []; | ||
if (options.useVitePreprocess) { | ||
log.debug("adding vite preprocessor"); | ||
extraPreprocessors.push(createVitePreprocessorGroup(config, options)); | ||
} | ||
const pluginsWithPreprocessors = config.plugins.filter((p) => p == null ? void 0 : p.sveltePreprocess); | ||
if (pluginsWithPreprocessors.length > 0) { | ||
log.debug(`adding preprocessors from other vite plugins: ${pluginsWithPreprocessors.map((p) => p.name).join(", ")}`); | ||
extraPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.sveltePreprocess)); | ||
} | ||
if (options.hot && !options.disableCssHmr) { | ||
extraPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup()); | ||
} | ||
return extraPreprocessors; | ||
} | ||
function addExtraPreprocessors(options, config) { | ||
const extra = buildExtraPreprocessors(options, config); | ||
if ((extra == null ? void 0 : extra.length) > 0) { | ||
if (!options.preprocess) { | ||
options.preprocess = extra; | ||
} else if (Array.isArray(options.preprocess)) { | ||
options.preprocess.push(...extra); | ||
} else { | ||
options.preprocess = [options.preprocess, ...extra]; | ||
} | ||
} | ||
} | ||
// src/utils/options.ts | ||
var knownOptions = new Set([ | ||
"configFile", | ||
"include", | ||
@@ -547,8 +579,9 @@ "exclude", | ||
} | ||
function resolveOptions(inlineOptions = {}, viteConfig) { | ||
async function resolveOptions(inlineOptions = {}, viteConfig) { | ||
const defaultOptions = buildDefaultOptions(viteConfig, inlineOptions); | ||
const svelteConfig = loadSvelteConfig(viteConfig.root) || {}; | ||
const svelteConfig = await loadSvelteConfig(viteConfig, inlineOptions) || {}; | ||
const resolvedOptions = mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfig); | ||
enforceOptionsForProduction(resolvedOptions); | ||
enforceOptionsForHmr(resolvedOptions); | ||
addExtraPreprocessors(resolvedOptions, viteConfig); | ||
log.debug("resolved options", resolvedOptions); | ||
@@ -563,10 +596,57 @@ return resolvedOptions; | ||
this._js = new Map(); | ||
this._dependencies = new Map(); | ||
this._dependants = new Map(); | ||
} | ||
update(compileData) { | ||
const id = compileData.normalizedFilename; | ||
this._css.set(id, compileData.compiled.css); | ||
this.updateCSS(compileData); | ||
this.updateJS(compileData); | ||
this.updateDependencies(compileData); | ||
} | ||
updateCSS(compileData) { | ||
this._css.set(compileData.normalizedFilename, compileData.compiled.css); | ||
} | ||
updateJS(compileData) { | ||
if (!compileData.ssr) { | ||
this._js.set(id, compileData.compiled.js); | ||
this._js.set(compileData.normalizedFilename, compileData.compiled.js); | ||
} | ||
} | ||
updateDependencies(compileData) { | ||
const id = compileData.normalizedFilename; | ||
const prevDependencies = this._dependencies.get(id) || []; | ||
const dependencies = compileData.dependencies; | ||
this._dependencies.set(id, dependencies); | ||
const removed = prevDependencies.filter((d) => !dependencies.includes(d)); | ||
const added = dependencies.filter((d) => !prevDependencies.includes(d)); | ||
added.forEach((d) => { | ||
if (!this._dependants.has(d)) { | ||
this._dependants.set(d, new Set()); | ||
} | ||
this._dependants.get(d).add(compileData.filename); | ||
}); | ||
removed.forEach((d) => { | ||
this._dependants.get(d).delete(compileData.filename); | ||
}); | ||
} | ||
remove(svelteRequest) { | ||
const id = svelteRequest.normalizedFilename; | ||
let removed = false; | ||
if (this._js.delete(id)) { | ||
removed = true; | ||
} | ||
if (this._css.delete(id)) { | ||
removed = true; | ||
} | ||
const dependencies = this._dependencies.get(id); | ||
if (dependencies) { | ||
removed = true; | ||
dependencies.forEach((d) => { | ||
const dependants = this._dependants.get(d); | ||
if (dependants && dependants.has(svelteRequest.filename)) { | ||
dependants.delete(svelteRequest.filename); | ||
} | ||
}); | ||
this._dependencies.delete(id); | ||
} | ||
return removed; | ||
} | ||
getCSS(svelteRequest) { | ||
@@ -580,2 +660,6 @@ return this._css.get(svelteRequest.normalizedFilename); | ||
} | ||
getDependants(path3) { | ||
const dependants = this._dependants.get(path3); | ||
return dependants ? [...dependants] : []; | ||
} | ||
}; | ||
@@ -599,2 +683,31 @@ | ||
// src/utils/watch.ts | ||
var import_fs2 = __toModule(require("fs")); | ||
function setupWatchers(server, cache, requestParser) { | ||
const watcher = server.watcher; | ||
const emitChangeEventOnDependants = (filename) => { | ||
const dependants = cache.getDependants(filename); | ||
dependants.forEach((dependant) => { | ||
if (import_fs2.default.existsSync(dependant)) { | ||
log.debug(`emitting virtual change event for "${dependant}" because depdendency "${filename}" changed`); | ||
watcher.emit("change", dependant); | ||
} | ||
}); | ||
}; | ||
const removeUnlinkedFromCache = (filename) => { | ||
const svelteRequest = requestParser(filename, false); | ||
if (svelteRequest) { | ||
const removedFromCache = cache.remove(svelteRequest); | ||
if (removedFromCache) { | ||
log.debug(`cleared VitePluginSvelteCache for deleted file ${filename}`); | ||
} | ||
} | ||
}; | ||
watcher.on("change", emitChangeEventOnDependants); | ||
watcher.on("unlink", (filename) => { | ||
removeUnlinkedFromCache(filename); | ||
emitChangeEventOnDependants(filename); | ||
}); | ||
} | ||
// src/index.ts | ||
@@ -641,9 +754,10 @@ var pkg_export_errors = new Set(); | ||
}, | ||
configResolved(config) { | ||
options = resolveOptions(inlineOptions, config); | ||
async configResolved(config) { | ||
options = await resolveOptions(inlineOptions, config); | ||
requestParser = buildIdParser(options); | ||
compileSvelte = createCompileSvelte(options, config); | ||
compileSvelte = createCompileSvelte(options); | ||
}, | ||
configureServer(server) { | ||
options.server = server; | ||
setupWatchers(server, cache, requestParser); | ||
}, | ||
@@ -706,2 +820,3 @@ load(id, ssr) { | ||
async transform(code, id, ssr) { | ||
var _a; | ||
const svelteRequest = requestParser(id, !!ssr); | ||
@@ -726,2 +841,5 @@ if (!svelteRequest) { | ||
cache.update(compileData); | ||
if (((_a = compileData.dependencies) == null ? void 0 : _a.length) && options.server) { | ||
compileData.dependencies.forEach((d) => this.addWatchFile(d)); | ||
} | ||
log.debug(`transform returns compiled js for ${filename}`); | ||
@@ -746,3 +864,3 @@ return compileData.compiled.js; | ||
if (pkg_export_errors.size > 0) { | ||
log.warn(`The following packages did not export their \`package.json\` file so we could not check the "svelte" field.If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.`, Array.from(pkg_export_errors, (s) => `- ${s}`).join("\n")); | ||
log.warn(`The following packages did not export their \`package.json\` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.`, Array.from(pkg_export_errors, (s) => `- ${s}`).join("\n")); | ||
} | ||
@@ -749,0 +867,0 @@ } |
{ | ||
"name": "@sveltejs/vite-plugin-svelte", | ||
"version": "1.0.0-next.7", | ||
"version": "1.0.0-next.8", | ||
"license": "MIT", | ||
@@ -36,13 +36,13 @@ "author": "dominikg", | ||
"@rollup/pluginutils": "^4.1.0", | ||
"chalk": "^4.1.0", | ||
"chalk": "^4.1.1", | ||
"debug": "^4.3.2", | ||
"hash-sum": "^2.0.0", | ||
"require-relative": "^0.8.7", | ||
"slash": "^3.0.0", | ||
"slash": "^4.0.0", | ||
"source-map": "^0.7.3", | ||
"svelte-hmr": "^0.14.0" | ||
"svelte-hmr": "^0.14.2" | ||
}, | ||
"peerDependencies": { | ||
"svelte": "^3.37.0", | ||
"vite": "^2.1.5" | ||
"vite": "^2.2.3" | ||
}, | ||
@@ -54,10 +54,10 @@ "devDependencies": { | ||
"@types/hash-sum": "^1.0.0", | ||
"@windicss/plugin-utils": "^0.12.2", | ||
"@windicss/plugin-utils": "^0.15.7", | ||
"esbuild": "~0.9.7", | ||
"locate-character": "^2.0.5", | ||
"magic-string": "^0.25.7", | ||
"rollup": "^2.44.0", | ||
"rollup": "^2.45.2", | ||
"svelte": "^3.37.0", | ||
"vite": "^2.1.5" | ||
"vite": "^2.2.3" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
42265
1050
7
1
19
+ Addedslash@4.0.0(transitive)
- Removedslash@3.0.0(transitive)
Updatedchalk@^4.1.1
Updatedslash@^4.0.0
Updatedsvelte-hmr@^0.14.2