Socket
Socket
Sign inDemoInstall

@sveltejs/vite-plugin-svelte

Package Overview
Dependencies
Maintainers
4
Versions
107
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sveltejs/vite-plugin-svelte - npm Package Compare versions

Comparing version 1.0.0-next.40 to 1.0.0-next.41

134

dist/index.js

@@ -539,3 +539,3 @@ var __defProp = Object.defineProperty;

// src/utils/options.ts
import path3 from "path";
import path4 from "path";

@@ -570,3 +570,3 @@ // src/utils/dependencies.ts

}
function getSvelteDependencies(deps, pkgDir, path7 = []) {
function getSvelteDependencies(deps, pkgDir, path8 = []) {
const result = [];

@@ -579,14 +579,14 @@ const localRequire = createRequire2(`${pkgDir}/package.json`);

continue;
result.push({ name: pkg.name, type, pkg, dir, path: path7 });
result.push({ name: pkg.name, type, pkg, dir, path: path8 });
if (type === "component-library" && pkg.dependencies) {
let dependencyNames = Object.keys(pkg.dependencies);
const circular = dependencyNames.filter((name) => path7.includes(name));
const circular = dependencyNames.filter((name) => path8.includes(name));
if (circular.length > 0) {
log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) => path7.concat(x).join(">")));
dependencyNames = dependencyNames.filter((name) => !path7.includes(name));
log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) => path8.concat(x).join(">")));
dependencyNames = dependencyNames.filter((name) => !path8.includes(name));
}
if (path7.length === 3) {
log.debug.once(`encountered deep svelte dependency tree: ${path7.join(">")}`);
if (path8.length === 3) {
log.debug.once(`encountered deep svelte dependency tree: ${path8.join(">")}`);
}
result.push(...getSvelteDependencies(dependencyNames, dir, path7.concat(pkg.name)));
result.push(...getSvelteDependencies(dependencyNames, dir, path8.concat(pkg.name)));
}

@@ -772,3 +772,5 @@ }

setup(build) {
disableVitePrebundleSvelte(build);
var _a;
if ((_a = build.initialOptions.plugins) == null ? void 0 : _a.some((v) => v.name === "vite:dep-scan"))
return;
const svelteExtensions = (options.extensions ?? [".svelte"]).map((ext) => ext.slice(1));

@@ -788,19 +790,2 @@ const svelteFilter = new RegExp(`\\.(` + svelteExtensions.join("|") + `)(\\?.*)?$`);

}
function disableVitePrebundleSvelte(build) {
var _a;
const viteDepPrebundlePlugin = (_a = build.initialOptions.plugins) == null ? void 0 : _a.find((v) => v.name === "vite:dep-pre-bundle");
if (!viteDepPrebundlePlugin)
return;
const _setup = viteDepPrebundlePlugin.setup.bind(viteDepPrebundlePlugin);
viteDepPrebundlePlugin.setup = function(build2) {
const _onResolve = build2.onResolve.bind(build2);
build2.onResolve = function(options, callback) {
if (options.filter.source.includes("svelte")) {
options.filter = new RegExp(options.filter.source.replace("|svelte", ""), options.filter.flags);
}
return _onResolve(options, callback);
};
return _setup(build2);
};
}
async function compileSvelte(options, { filename, code }) {

@@ -893,2 +878,3 @@ var _a, _b;

// src/utils/preprocess.ts
import path3 from "path";
var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];

@@ -934,3 +920,3 @@ var supportedScriptLangs = ["ts"];

if (((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
transformResult.map.sources[0] = filename;
transformResult.map.sources[0] = path3.basename(filename);
}

@@ -960,3 +946,6 @@ return {

code: s.toString(),
map: s.generateDecodedMap({ source: filename, hires: true })
map: s.generateDecodedMap({
source: filename ? path3.basename(filename) : void 0,
hires: true
})
};

@@ -1179,3 +1168,3 @@ }

function resolveViteRoot(viteConfig) {
return normalizePath2(viteConfig.root ? path3.resolve(viteConfig.root) : process.cwd());
return normalizePath2(viteConfig.root ? path4.resolve(viteConfig.root) : process.cwd());
}

@@ -1193,6 +1182,15 @@ function buildExtraViteConfig(options, config) {

}
if (options.experimental.prebundleSvelteLibraries) {
extraViteConfig.optimizeDeps = __spreadProps(__spreadValues({}, extraViteConfig.optimizeDeps), {
extensions: options.extensions ?? [".svelte"],
esbuildOptions: {
plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
} }]
}
});
}
extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config, extraViteConfig);
return extraViteConfig;
}
function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
const include = [];

@@ -1202,7 +1200,7 @@ const exclude = ["svelte-hmr"];

var _a;
return include.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.include) == null ? void 0 : _a.includes(dep));
return include.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.include) == null ? void 0 : _a.includes(dep));
};
const isExcluded = (dep) => {
var _a;
return exclude.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.exclude) == null ? void 0 : _a.some((id) => dep === id || id.startsWith(`${dep}/`)));
return exclude.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.exclude) == null ? void 0 : _a.some((id) => dep === id || id.startsWith(`${dep}/`)));
};

@@ -1217,10 +1215,3 @@ if (!isExcluded("svelte")) {

if (options.experimental.prebundleSvelteLibraries) {
return {
include,
exclude,
esbuildOptions: {
plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
} }]
}
};
return { include, exclude };
}

@@ -1347,4 +1338,4 @@ svelteDeps = svelteDeps.filter((dep) => dep.type === "component-library");

}
getDependants(path7) {
const dependants = this._dependants.get(path7);
getDependants(path8) {
const dependants = this._dependants.get(path8);
return dependants ? [...dependants] : [];

@@ -1365,3 +1356,3 @@ }

import fs5 from "fs";
import path4 from "path";
import path5 from "path";
function setupWatchers(options, cache, requestParser) {

@@ -1405,3 +1396,3 @@ const { server, configFile: svelteConfigFile } = options;

};
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path4.join(root, cfg));
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path5.join(root, cfg));
const restartOnConfigAdd = (filename) => {

@@ -1436,3 +1427,3 @@ if (possibleSvelteConfigs.includes(filename)) {

if (file && !file.startsWith(root + "/") && !file.includes("\0") && fs5.existsSync(file)) {
watcher.add(path4.resolve(file));
watcher.add(path5.resolve(file));
}

@@ -1442,3 +1433,3 @@ }

// src/utils/resolve.ts
import path5 from "path";
import path6 from "path";
import { builtinModules, createRequire as createRequire4 } from "module";

@@ -1456,3 +1447,3 @@ function resolveViaPackageJsonSvelte(importee, importer, cache) {

if (pkg.svelte) {
const result = path5.resolve(dir, pkg.svelte);
const result = path6.resolve(dir, pkg.svelte);
cache.setResolvedSvelteField(importee, importer, result);

@@ -1468,3 +1459,3 @@ return result;

function isBareImport(importee) {
if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path5.isAbsolute(importee)) {
if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path6.isAbsolute(importee)) {
return false;

@@ -1484,5 +1475,4 @@ }

// src/utils/optimizer.ts
import fs6 from "fs";
import path6 from "path";
import { optimizeDeps } from "vite";
import { promises as fs6 } from "fs";
import path7 from "path";
var PREBUNDLE_SENSITIVE_OPTIONS = [

@@ -1496,19 +1486,16 @@ "compilerOptions",

];
async function handleOptimizeDeps(options, viteConfig) {
if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
return;
const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
if (!viteMetadataPath)
return;
const svelteMetadataPath = path6.resolve(viteMetadataPath, "../_svelte_metadata.json");
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
async function saveSvelteMetadata(cacheDir, options) {
const svelteMetadata = generateSvelteMetadata(options);
const svelteMetadataPath = path7.resolve(cacheDir, "_svelte_metadata.json");
const currentSvelteMetadata = JSON.stringify(svelteMetadata, (_, value) => {
return typeof value === "function" ? value.toString() : value;
});
if (fs6.existsSync(svelteMetadataPath)) {
const existingSvelteMetadata = fs6.readFileSync(svelteMetadataPath, "utf8");
if (existingSvelteMetadata === currentSvelteMetadata)
return;
let existingSvelteMetadata;
try {
existingSvelteMetadata = await fs6.readFile(svelteMetadataPath, "utf8");
} catch {
}
await optimizeDeps(viteConfig, true);
fs6.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
await fs6.mkdir(cacheDir, { recursive: true });
await fs6.writeFile(svelteMetadataPath, currentSvelteMetadata);
return currentSvelteMetadata !== existingSvelteMetadata;
}

@@ -1522,10 +1509,2 @@ function generateSvelteMetadata(options) {

}
function findViteMetadataPath(cacheDir) {
const metadataPaths = ["_metadata.json", "deps/_metadata.json"];
for (const metadataPath of metadataPaths) {
const viteMetadataPath = path6.resolve(cacheDir, metadataPath);
if (fs6.existsSync(viteMetadataPath))
return viteMetadataPath;
}
}

@@ -1567,3 +1546,8 @@ // src/index.ts

async buildStart() {
await handleOptimizeDeps(options, viteConfig);
if (!options.experimental.prebundleSvelteLibraries)
return;
const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
if (isSvelteMetadataChanged) {
viteConfig.server.force = true;
}
},

@@ -1570,0 +1554,0 @@ configureServer(server) {

{
"name": "@sveltejs/vite-plugin-svelte",
"version": "1.0.0-next.40",
"version": "1.0.0-next.41",
"license": "MIT",

@@ -44,3 +44,3 @@ "author": "dominikg",

"@rollup/pluginutils": "^4.2.0",
"debug": "^4.3.3",
"debug": "^4.3.4",
"kleur": "^4.1.4",

@@ -53,3 +53,3 @@ "magic-string": "^0.26.1",

"svelte": "^3.44.0",
"vite": "^2.7.0"
"vite": "^2.9.0"
},

@@ -65,7 +65,7 @@ "peerDependenciesMeta": {

"diff-match-patch": "^1.0.5",
"esbuild": "^0.14.26",
"esbuild": "^0.14.31",
"rollup": "^2.70.1",
"svelte": "^3.46.4",
"tsup": "^5.12.1",
"vite": "^2.8.6"
"svelte": "^3.46.6",
"tsup": "^5.12.4",
"vite": "^2.9.1"
},

@@ -72,0 +72,0 @@ "scripts": {

@@ -22,3 +22,3 @@ import fs from 'fs';

import { toRollupError } from './utils/error';
import { handleOptimizeDeps } from './utils/optimizer';
import { saveSvelteMetadata } from './utils/optimizer';

@@ -74,3 +74,9 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {

async buildStart() {
await handleOptimizeDeps(options, viteConfig);
if (!options.experimental.prebundleSvelteLibraries) return;
const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
if (isSvelteMetadataChanged) {
// Force Vite to optimize again. Although we mutate the config here, it works because
// Vite's optimizer runs after `buildStart()`.
viteConfig.server.force = true;
}
},

@@ -77,0 +83,0 @@

@@ -11,3 +11,2 @@ import { promises as fs } from 'fs';

type EsbuildPlugin = NonNullable<EsbuildOptions['plugins']>[number];
type EsbuildPluginBuild = Parameters<EsbuildPlugin['setup']>[0];

@@ -20,3 +19,5 @@ export const facadeEsbuildSveltePluginName = 'vite-plugin-svelte:facade';

setup(build) {
disableVitePrebundleSvelte(build);
// Skip in scanning phase as Vite already handles scanning Svelte files.
// Otherwise this would heavily slow down the scanning phase.
if (build.initialOptions.plugins?.some((v) => v.name === 'vite:dep-scan')) return;

@@ -39,26 +40,2 @@ const svelteExtensions = (options.extensions ?? ['.svelte']).map((ext) => ext.slice(1));

function disableVitePrebundleSvelte(build: EsbuildPluginBuild) {
const viteDepPrebundlePlugin = build.initialOptions.plugins?.find(
(v) => v.name === 'vite:dep-pre-bundle'
);
if (!viteDepPrebundlePlugin) return;
// Prevent vite:dep-pre-bundle from externalizing svelte files
const _setup = viteDepPrebundlePlugin.setup.bind(viteDepPrebundlePlugin);
viteDepPrebundlePlugin.setup = function (build) {
const _onResolve = build.onResolve.bind(build);
build.onResolve = function (options, callback) {
if (options.filter.source.includes('svelte')) {
options.filter = new RegExp(
options.filter.source.replace('|svelte', ''),
options.filter.flags
);
}
return _onResolve(options, callback);
};
return _setup(build);
};
}
async function compileSvelte(

@@ -65,0 +42,0 @@ options: ResolvedOptions,

@@ -1,4 +0,3 @@

import fs from 'fs';
import { promises as fs } from 'fs';
import path from 'path';
import { optimizeDeps, ResolvedConfig } from 'vite';
import { ResolvedOptions } from './options';

@@ -16,20 +15,24 @@

export async function handleOptimizeDeps(options: ResolvedOptions, viteConfig: ResolvedConfig) {
if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir) return;
/**
* @returns Whether the Svelte metadata has changed
*/
export async function saveSvelteMetadata(cacheDir: string, options: ResolvedOptions) {
const svelteMetadata = generateSvelteMetadata(options);
const svelteMetadataPath = path.resolve(cacheDir, '_svelte_metadata.json');
const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
if (!viteMetadataPath) return;
const svelteMetadataPath = path.resolve(viteMetadataPath, '../_svelte_metadata.json');
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
const currentSvelteMetadata = JSON.stringify(svelteMetadata, (_, value) => {
// Handle preprocessors
return typeof value === 'function' ? value.toString() : value;
});
if (fs.existsSync(svelteMetadataPath)) {
const existingSvelteMetadata = fs.readFileSync(svelteMetadataPath, 'utf8');
if (existingSvelteMetadata === currentSvelteMetadata) return;
let existingSvelteMetadata: string | undefined;
try {
existingSvelteMetadata = await fs.readFile(svelteMetadataPath, 'utf8');
} catch {
// ignore
}
await optimizeDeps(viteConfig, true);
fs.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
await fs.mkdir(cacheDir, { recursive: true });
await fs.writeFile(svelteMetadataPath, currentSvelteMetadata);
return currentSvelteMetadata !== existingSvelteMetadata;
}

@@ -44,9 +47,1 @@

}
function findViteMetadataPath(cacheDir: string) {
const metadataPaths = ['_metadata.json', 'deps/_metadata.json'];
for (const metadataPath of metadataPaths) {
const viteMetadataPath = path.resolve(cacheDir, metadataPath);
if (fs.existsSync(viteMetadataPath)) return viteMetadataPath;
}
}

@@ -210,2 +210,17 @@ /* eslint-disable no-unused-vars */

if (options.experimental.prebundleSvelteLibraries) {
extraViteConfig.optimizeDeps = {
...extraViteConfig.optimizeDeps,
// Experimental Vite API to allow these extensions to be scanned and prebundled
// @ts-ignore
extensions: options.extensions ?? ['.svelte'],
// Add esbuild plugin to prebundle Svelte files.
// Currently a placeholder as more information is needed after Vite config is resolved,
// the real Svelte plugin is added in `patchResolvedViteConfig()`
esbuildOptions: {
plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {} }]
}
};
}
// @ts-ignore

@@ -246,9 +261,3 @@ extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config, extraViteConfig);

if (options.experimental.prebundleSvelteLibraries) {
return {
include,
exclude,
esbuildOptions: {
plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {} }]
}
};
return { include, exclude };
}

@@ -255,0 +264,0 @@

@@ -14,2 +14,3 @@ import {

import { buildSourceMap } from './sourcemap';
import path from 'path';

@@ -61,3 +62,3 @@ const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss'];

if (transformResult.map?.sources?.[0] === moduleId) {
transformResult.map.sources[0] = filename;
transformResult.map.sources[0] = path.basename(filename);
}

@@ -99,3 +100,6 @@ return {

code: s.toString(),
map: s.generateDecodedMap({ source: filename, hires: true })
map: s.generateDecodedMap({
source: filename ? path.basename(filename) : undefined,
hires: true
})
};

@@ -102,0 +106,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc