Socket
Socket
Sign inDemoInstall

svelte-preprocess-import-assets

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svelte-preprocess-import-assets - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

22

package.json
{
"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 {

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