@sveltejs/vite-plugin-svelte 1.0.0-next.36 → 1.0.0-next.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +33 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +33 -35
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/src/index.ts +9 -33
- package/src/utils/__tests__/dependencies.spec.ts +14 -3
- package/src/utils/dependencies.ts +17 -6
- package/src/utils/optimizer.ts +11 -4
- package/src/utils/resolve.ts +11 -4
package/dist/index.js
CHANGED
|
@@ -684,11 +684,20 @@ function needsOptimization(dep, localRequire) {
|
|
|
684
684
|
if (!depData)
|
|
685
685
|
return false;
|
|
686
686
|
const pkg = depData.pkg;
|
|
687
|
-
const
|
|
688
|
-
if (
|
|
687
|
+
const hasEsmFields = pkg.module || pkg.exports;
|
|
688
|
+
if (hasEsmFields)
|
|
689
689
|
return false;
|
|
690
|
-
|
|
691
|
-
|
|
690
|
+
if (pkg.main) {
|
|
691
|
+
const entryExt = path2.extname(pkg.main);
|
|
692
|
+
return !entryExt || entryExt === ".js" || entryExt === ".cjs";
|
|
693
|
+
} else {
|
|
694
|
+
try {
|
|
695
|
+
localRequire.resolve(`${dep}/index.js`);
|
|
696
|
+
return true;
|
|
697
|
+
} catch {
|
|
698
|
+
return false;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
692
701
|
}
|
|
693
702
|
|
|
694
703
|
// src/utils/options.ts
|
|
@@ -1408,9 +1417,9 @@ function ensureWatchedFile(watcher, file, root) {
|
|
|
1408
1417
|
|
|
1409
1418
|
// src/utils/resolve.ts
|
|
1410
1419
|
import path5 from "path";
|
|
1411
|
-
import { createRequire as createRequire4 } from "module";
|
|
1420
|
+
import { builtinModules, createRequire as createRequire4 } from "module";
|
|
1412
1421
|
function resolveViaPackageJsonSvelte(importee, importer, cache) {
|
|
1413
|
-
if (importer && isBareImport(importee) && !is_common_without_svelte_field(importee)) {
|
|
1422
|
+
if (importer && isBareImport(importee) && !isNodeInternal(importee) && !is_common_without_svelte_field(importee)) {
|
|
1414
1423
|
const cached = cache.getResolvedSvelteField(importee, importer);
|
|
1415
1424
|
if (cached) {
|
|
1416
1425
|
return cached;
|
|
@@ -1424,11 +1433,12 @@ function resolveViaPackageJsonSvelte(importee, importer, cache) {
|
|
|
1424
1433
|
cache.setResolvedSvelteField(importee, importer, result);
|
|
1425
1434
|
return result;
|
|
1426
1435
|
}
|
|
1427
|
-
} else {
|
|
1428
|
-
throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
|
|
1429
1436
|
}
|
|
1430
1437
|
}
|
|
1431
1438
|
}
|
|
1439
|
+
function isNodeInternal(importee) {
|
|
1440
|
+
return importee.startsWith("node:") || builtinModules.includes(importee);
|
|
1441
|
+
}
|
|
1432
1442
|
function isBareImport(importee) {
|
|
1433
1443
|
if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path5.isAbsolute(importee)) {
|
|
1434
1444
|
return false;
|
|
@@ -1459,10 +1469,10 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
|
|
|
1459
1469
|
async function handleOptimizeDeps(options, viteConfig) {
|
|
1460
1470
|
if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
|
|
1461
1471
|
return;
|
|
1462
|
-
const viteMetadataPath =
|
|
1463
|
-
if (!
|
|
1472
|
+
const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
|
|
1473
|
+
if (!viteMetadataPath)
|
|
1464
1474
|
return;
|
|
1465
|
-
const svelteMetadataPath = path6.resolve(
|
|
1475
|
+
const svelteMetadataPath = path6.resolve(viteMetadataPath, "../_svelte_metadata.json");
|
|
1466
1476
|
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
|
|
1467
1477
|
return typeof value === "function" ? value.toString() : value;
|
|
1468
1478
|
});
|
|
@@ -1481,6 +1491,14 @@ function generateSvelteMetadata(options) {
|
|
|
1481
1491
|
}
|
|
1482
1492
|
return metadata;
|
|
1483
1493
|
}
|
|
1494
|
+
function findViteMetadataPath(cacheDir) {
|
|
1495
|
+
const metadataPaths = ["_metadata.json", "deps/_metadata.json"];
|
|
1496
|
+
for (const metadataPath of metadataPaths) {
|
|
1497
|
+
const viteMetadataPath = path6.resolve(cacheDir, metadataPath);
|
|
1498
|
+
if (fs6.existsSync(viteMetadataPath))
|
|
1499
|
+
return viteMetadataPath;
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1484
1502
|
|
|
1485
1503
|
// src/index.ts
|
|
1486
1504
|
function svelte(inlineOptions) {
|
|
@@ -1489,7 +1507,6 @@ function svelte(inlineOptions) {
|
|
|
1489
1507
|
}
|
|
1490
1508
|
validateInlineOptions(inlineOptions);
|
|
1491
1509
|
const cache = new VitePluginSvelteCache();
|
|
1492
|
-
const pkg_resolve_errors = /* @__PURE__ */ new Set();
|
|
1493
1510
|
let requestParser;
|
|
1494
1511
|
let options;
|
|
1495
1512
|
let viteConfig;
|
|
@@ -1571,29 +1588,17 @@ function svelte(inlineOptions) {
|
|
|
1571
1588
|
log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
|
|
1572
1589
|
return resolved;
|
|
1573
1590
|
}
|
|
1574
|
-
} catch (
|
|
1575
|
-
|
|
1591
|
+
} catch (e) {
|
|
1592
|
+
log.debug.once(`error trying to resolve ${importee} from ${importer} via package.json svelte field `, e);
|
|
1576
1593
|
}
|
|
1577
1594
|
},
|
|
1578
1595
|
async transform(code, id, opts) {
|
|
1579
1596
|
var _a;
|
|
1580
1597
|
const ssr = !!(opts == null ? void 0 : opts.ssr);
|
|
1581
1598
|
const svelteRequest = requestParser(id, ssr);
|
|
1582
|
-
if (!svelteRequest) {
|
|
1599
|
+
if (!svelteRequest || svelteRequest.query.svelte) {
|
|
1583
1600
|
return;
|
|
1584
1601
|
}
|
|
1585
|
-
const { filename, query } = svelteRequest;
|
|
1586
|
-
if (query.svelte) {
|
|
1587
|
-
if (query.type === "style") {
|
|
1588
|
-
const css = cache.getCSS(svelteRequest);
|
|
1589
|
-
if (css) {
|
|
1590
|
-
log.debug(`transform returns css for ${filename}`);
|
|
1591
|
-
return css;
|
|
1592
|
-
}
|
|
1593
|
-
}
|
|
1594
|
-
log.error("failed to transform tagged svelte request", svelteRequest);
|
|
1595
|
-
throw new Error(`failed to transform tagged svelte request for id ${id}`);
|
|
1596
|
-
}
|
|
1597
1602
|
let compileData;
|
|
1598
1603
|
try {
|
|
1599
1604
|
compileData = await compileSvelte2(svelteRequest, code, options);
|
|
@@ -1607,7 +1612,7 @@ function svelte(inlineOptions) {
|
|
|
1607
1612
|
ensureWatchedFile(options.server.watcher, d, options.root);
|
|
1608
1613
|
});
|
|
1609
1614
|
}
|
|
1610
|
-
log.debug(`transform returns compiled js for ${filename}`);
|
|
1615
|
+
log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
|
|
1611
1616
|
return compileData.compiled.js;
|
|
1612
1617
|
},
|
|
1613
1618
|
handleHotUpdate(ctx) {
|
|
@@ -1618,13 +1623,6 @@ function svelte(inlineOptions) {
|
|
|
1618
1623
|
if (svelteRequest) {
|
|
1619
1624
|
return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
|
|
1620
1625
|
}
|
|
1621
|
-
},
|
|
1622
|
-
buildEnd() {
|
|
1623
|
-
if (pkg_resolve_errors.size > 0) {
|
|
1624
|
-
log.warn(`vite-plugin-svelte was unable to find package.json of the following packages and wasn't able to resolve via their "svelte" field.
|
|
1625
|
-
If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
|
|
1626
|
-
${Array.from(pkg_resolve_errors, (s) => `- ${s}`).join("\n")}`.replace(/\t/g, ""));
|
|
1627
|
-
}
|
|
1628
1626
|
}
|
|
1629
1627
|
};
|
|
1630
1628
|
}
|