svelte-preprocess-import-assets
Advanced tools
Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "svelte-preprocess-import-assets", | ||
"description": "Import assets in markup", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -38,16 +38,18 @@ "author": "Bjorn Lu", | ||
], | ||
"packageManager": "pnpm@8.1.0", | ||
"packageManager": "pnpm@8.10.2", | ||
"dependencies": { | ||
"magic-string": "^0.30.0", | ||
"svelte-parse-markup": "^0.1.1" | ||
"estree-walker": "^3.0.3", | ||
"magic-string": "^0.30.5", | ||
"svelte-parse-markup": "^0.1.2" | ||
}, | ||
"peerDependencies": { | ||
"svelte": "^3.37.0 || ^4.0.0" | ||
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.1" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^2.8.7", | ||
"prettier-plugin-svelte": "^2.10.0", | ||
"sass": "^1.61.0", | ||
"svelte": "^4.0.0", | ||
"svelte-preprocess": "^5.0.3", | ||
"prettier": "^3.0.3", | ||
"prettier-plugin-svelte": "^3.1.0", | ||
"sass": "^1.69.5", | ||
"svelte": "^4.2.3", | ||
"svelte-preprocess": "^5.1.0", | ||
"typescript": "^5.2.2", | ||
"uvu": "^0.5.6" | ||
@@ -54,0 +56,0 @@ }, |
# svelte-preprocess-import-assets | ||
Import assets directly in the markup. | ||
Import assets directly in the markup. Supports Svelte 3, 4, and 5. | ||
> NOTE: Svelte 5 support is experimental and may break between versions. | ||
**Convert this:** | ||
@@ -6,0 +8,0 @@ |
@@ -25,3 +25,3 @@ import { PreprocessorGroup } from 'svelte/types/compiler/preprocess' | ||
export declare function importAssets( | ||
options?: ImportAssetsOptions | ||
options?: ImportAssetsOptions, | ||
): PreprocessorGroup |
import MagicString from 'magic-string' | ||
import { walk } from 'estree-walker' | ||
import { parse } from 'svelte-parse-markup' | ||
import { walk } from 'svelte/compiler' | ||
import { | ||
@@ -19,6 +19,11 @@ DEFAULT_SOURCES, | ||
/** @type {import('.').AssetSource[]} */ | ||
let resolvedSources | ||
let hasCustomSources = false | ||
if (typeof sources === 'function') { | ||
hasCustomSources = true | ||
sources = sources(DEFAULT_SOURCES) | ||
resolvedSources = sources(DEFAULT_SOURCES) | ||
} else { | ||
resolvedSources = sources | ||
} | ||
@@ -49,7 +54,5 @@ | ||
let importName = '' | ||
let importName = imports.get(url) | ||
if (imports.has(url)) { | ||
importName = imports.get(url) | ||
} else { | ||
if (!importName) { | ||
importName = importPrefix + imports.size | ||
@@ -65,5 +68,6 @@ imports.set(url, importName) | ||
// @ts-ignore | ||
walk(ast.html, { | ||
/** | ||
* @param {any} node | ||
* @param {import('svelte/types/compiler/interfaces').TemplateNode} node | ||
*/ | ||
@@ -86,3 +90,3 @@ enter(node) { | ||
/** @type {Record<string, string> | undefined} */ | ||
/** @type {Record<string, string>} */ | ||
let lazyAttributes | ||
@@ -93,9 +97,15 @@ | ||
lazyAttributes = {} | ||
node.attributes.forEach((attr) => { | ||
if (attr.type !== 'Attribute') return | ||
// Ensure text only, since text only attribute values will only have one element | ||
if (attr.value.length > 1 && attr.value[0].type !== 'Text') | ||
return | ||
lazyAttributes[attr.name] = attr.value[0].raw | ||
}) | ||
node.attributes.forEach( | ||
/** @param {any} attr */ (attr) => { | ||
if (attr.type !== 'Attribute') return | ||
// Ensure text only, since text only attribute values will only have one element | ||
if ( | ||
attr.value.length > 1 && | ||
attr.value[0].type !== 'Text' | ||
) { | ||
return | ||
} | ||
lazyAttributes[attr.name] = attr.value[0].raw | ||
}, | ||
) | ||
} | ||
@@ -105,5 +115,5 @@ return lazyAttributes | ||
for (let i = 0; i < sources.length; i++) { | ||
for (let i = 0; i < resolvedSources.length; i++) { | ||
/** @type {import('.').AssetSource} */ | ||
const source = sources[i] | ||
const source = resolvedSources[i] | ||
@@ -117,3 +127,4 @@ // Compare node tag match | ||
const attribute = node.attributes.find( | ||
(v) => v.type === 'Attribute' && v.name === attr | ||
/** @param {any} v */ (v) => | ||
v.type === 'Attribute' && v.name === attr, | ||
) | ||
@@ -177,4 +188,6 @@ if (!attribute) return | ||
if (ast.module) { | ||
// @ts-ignore | ||
s.appendLeft(ast.module.content.start, importText) | ||
} else if (ast.instance) { | ||
// @ts-ignore | ||
s.appendLeft(ast.instance.content.start, importText) | ||
@@ -181,0 +194,0 @@ } else { |
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
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
15231
330
142
4
7
+ Addedestree-walker@^3.0.3
+ Addedacorn-typescript@1.4.13(transitive)
+ Addedesm-env@1.0.0(transitive)
+ Addedesrap@1.2.2(transitive)
+ Addedsvelte@5.0.0-next.257(transitive)
+ Addedzimmerframe@1.1.2(transitive)
- Removedcode-red@1.0.4(transitive)
- Removedcss-tree@2.3.1(transitive)
- Removedmdn-data@2.0.30(transitive)
- Removedperiscopic@3.1.0(transitive)
- Removedsource-map-js@1.2.1(transitive)
- Removedsvelte@4.2.19(transitive)
Updatedmagic-string@^0.30.5
Updatedsvelte-parse-markup@^0.1.2