Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

svelte-preprocess-import-assets

Package Overview
Dependencies
Maintainers
2
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 0.2.3 to 0.2.4

230

dist/index.js

@@ -24,2 +24,22 @@ var __create = Object.create;

var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};

@@ -128,7 +148,7 @@ // src/index.ts

srcsetAttributes: ["imagesrcset"],
filter({ attributes }) {
filter({ attribute, attributes }) {
if (attributes.rel && ALLOWED_REL.includes(attributes.rel.trim().toLowerCase())) {
return true;
}
if (attributes.itemprop && ALLOWED_ITEMPROP.includes(attributes.itemprop.trim().toLowerCase())) {
if (attribute !== "imagesrcset" && attributes.itemprop && ALLOWED_ITEMPROP.includes(attributes.itemprop.trim().toLowerCase())) {
return true;

@@ -172,111 +192,113 @@ }

return {
async markup({ content, filename }) {
if (!parse) {
parse = (await import("svelte-parse-markup")).parse;
}
const s = new import_magic_string.default(content);
const ast = parse(content, { filename });
const imports = /* @__PURE__ */ new Map();
function addImport(attributeValue) {
const url = attributeValue.raw.trim();
if (url.startsWith("#"))
return;
if (!http && /^https?:\/\//.test(url))
return;
if (urlFilter && !urlFilter(url))
return;
let importName = "";
if (imports.has(url)) {
importName = imports.get(url);
} else {
importName = importPrefix + imports.size;
imports.set(url, importName);
markup(_0) {
return __async(this, arguments, function* ({ content, filename }) {
if (!parse) {
parse = (yield import("svelte-parse-markup")).parse;
}
s.overwrite(attributeValue.start, attributeValue.end, `{${importName}}`);
}
let ignoreNextElement = false;
(0, import_compiler.walk)(ast.html, {
enter(node) {
var _a, _b;
if (node.type === "Comment") {
if (node.data.trim() === IGNORE_FLAG) {
ignoreNextElement = true;
}
} else if (node.type === "Element" || hasCustomSources && node.type === "InlineComponent") {
let getAttributes = function() {
if (!lazyAttributes) {
lazyAttributes = {};
node.attributes.forEach((attr) => {
if (attr.value.length > 1 && attr.value[0].type !== "Text")
return;
lazyAttributes[attr.name] = attr.value[0].raw;
});
const s = new import_magic_string.default(content);
const ast = parse(content, { filename });
const imports = /* @__PURE__ */ new Map();
function addImport(attributeValue) {
const url = attributeValue.raw.trim();
if (url.startsWith("#"))
return;
if (!http && /^https?:\/\//.test(url))
return;
if (urlFilter && !urlFilter(url))
return;
let importName = "";
if (imports.has(url)) {
importName = imports.get(url);
} else {
importName = importPrefix + imports.size;
imports.set(url, importName);
}
s.overwrite(attributeValue.start, attributeValue.end, `{${importName}}`);
}
let ignoreNextElement = false;
(0, import_compiler.walk)(ast.html, {
enter(node) {
var _a2, _b;
if (node.type === "Comment") {
if (node.data.trim() === IGNORE_FLAG) {
ignoreNextElement = true;
}
return lazyAttributes;
};
if (ignoreNextElement) {
ignoreNextElement = false;
return;
}
let lazyAttributes;
for (let i = 0; i < sources.length; i++) {
const source = sources[i];
if (source.tag === node.name) {
let getAttrValue = function(attr) {
const attribute = node.attributes.find((v) => v.name === attr);
if (!attribute)
return;
if (attribute.value.length !== 1 || attribute.value[0].type !== "Text")
return;
if (source.filter && !source.filter({
tag: source.tag,
attribute: attr,
value: content.slice(attribute.start, attribute.end),
attributes: getAttributes()
}))
return;
return attribute.value[0];
};
(_a = source.srcAttributes) == null ? void 0 : _a.forEach((attr) => {
const value = getAttrValue(attr);
if (!value)
return;
addImport(value);
});
(_b = source.srcsetAttributes) == null ? void 0 : _b.forEach((attr) => {
const value = getAttrValue(attr);
if (!value)
return;
const srcsetRegex = /\s*([^,\s]+).*?(?:,|$)\s*/gm;
let match;
while (match = srcsetRegex.exec(value.raw)) {
addImport({
raw: match[1],
start: value.start + match.index,
end: value.start + match.index + match[1].length
});
}
});
} else if (node.type === "Element" || hasCustomSources && node.type === "InlineComponent") {
let getAttributes = function() {
if (!lazyAttributes) {
lazyAttributes = {};
node.attributes.forEach((attr) => {
if (attr.value.length > 1 && attr.value[0].type !== "Text")
return;
lazyAttributes[attr.name] = attr.value[0].raw;
});
}
return lazyAttributes;
};
if (ignoreNextElement) {
ignoreNextElement = false;
return;
}
let lazyAttributes;
for (let i = 0; i < sources.length; i++) {
const source = sources[i];
if (source.tag === node.name) {
let getAttrValue = function(attr) {
const attribute = node.attributes.find((v) => v.name === attr);
if (!attribute)
return;
if (attribute.value.length !== 1 || attribute.value[0].type !== "Text")
return;
if (source.filter && !source.filter({
tag: source.tag,
attribute: attr,
value: content.slice(attribute.start, attribute.end),
attributes: getAttributes()
}))
return;
return attribute.value[0];
};
(_a2 = source.srcAttributes) == null ? void 0 : _a2.forEach((attr) => {
const value = getAttrValue(attr);
if (!value)
return;
addImport(value);
});
(_b = source.srcsetAttributes) == null ? void 0 : _b.forEach((attr) => {
const value = getAttrValue(attr);
if (!value)
return;
const srcsetRegex = /\s*([^,\s]+).*?(?:,|$)\s*/gm;
let match;
while (match = srcsetRegex.exec(value.raw)) {
addImport({
raw: match[1],
start: value.start + match.index,
end: value.start + match.index + match[1].length
});
}
});
}
}
}
}
});
if (imports.size) {
let importText = "";
for (const [path, importName] of imports.entries()) {
importText += `import ${importName} from "${path}";`;
}
if (ast.module) {
s.appendLeft(ast.module.content.start, importText);
} else if (ast.instance) {
s.appendLeft(ast.instance.content.start, importText);
} else {
s.append(`<script>${importText}<\/script>`);
}
}
return {
code: s.toString(),
map: s.generateMap()
};
});
if (imports.size) {
let importText = "";
for (const [path, importName] of imports.entries()) {
importText += `import ${importName} from "${path}";`;
}
if (ast.module) {
s.appendLeft(ast.module.content.start, importText);
} else if (ast.instance) {
s.appendLeft(ast.instance.content.start, importText);
} else {
s.append(`<script>${importText}<\/script>`);
}
}
return {
code: s.toString(),
map: s.generateMap()
};
}

@@ -283,0 +305,0 @@ };

{
"name": "svelte-preprocess-import-assets",
"description": "Import assets in markup",
"version": "0.2.3",
"version": "0.2.4",
"license": "MIT",

@@ -42,11 +42,11 @@ "author": "Bjorn Lu",

"devDependencies": {
"@types/node": "^18.7.23",
"prettier": "^2.7.1",
"prettier-plugin-svelte": "^2.7.1",
"sass": "^1.55.0",
"svelte": "^3.50.1",
"@types/node": "^18.11.10",
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.8.1",
"sass": "^1.56.1",
"svelte": "^3.53.1",
"svelte-preprocess": "^4.10.7",
"tsm": "^2.2.2",
"tsup": "^6.2.3",
"typescript": "^4.8.4",
"tsm": "^2.3.0",
"tsup": "^6.5.0",
"typescript": "^4.9.3",
"uvu": "^0.5.6"

@@ -58,4 +58,4 @@ },

"test:update": "tsm test/index.ts -u",
"format": "prettier -w {src,test}/**/*.{ts,svelte}"
"format": "prettier -w {src,test}/**/*"
}
}

@@ -39,3 +39,3 @@ # svelte-preprocess-import-assets

// or in svelte.config.js
const config = {
export default {
preprocess: [importAssets()],

@@ -42,0 +42,0 @@ // ... other svelte options

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