npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sveltekit-adapter-wordpress-shortcode

v0.4.3

Published

Build wordpress shortcode plugins with SvelteKit

Downloads

70

Readme

sveltekit-adapter-wordpress-shortcode (SKAWPSC)

Adapter for SvelteKit which turns your app into a wordpress shortcode.

Usage

Install with npm i -D sveltekit-adapter-wordpress-shortcode, setup the adapter in svelte.config.js, add index.php to the project root, and mark the parts of your template you want to include in the shortcode.

Example svelte.config.js

Note: It's likely you will need to set custom base paths for Wordpress.

// svelte.config.js
import adapter from "sveltekit-adapter-wordpress-shortcode"

/** @type {import('@sveltejs/kit').Config} */
const config = {
	kit: {
		// all the default options
		adapter: adapter({
			pages: "build",
			assets: "build",
			fallback: null,
			indexPath: "index.php",
			shadow: false,
			shortcode: "svelte-kit-shortcode",
			prefix: "skawpsc_svelte_kit_shortcode",
			renderHead: head =>
				[...head.querySelectorAll(`link[rel="modulepreload"]`)]
					.map(element => element.outerHTML)
					.join(""),
			renderBody: body => body.innerHTML
		})
	}
}

// handle wordpress url structure
if (process.env.NODE_ENV === "production") {
	const base = "/wp-content/plugins/my-shortcode-plugin"
	config.kit.paths = {
		base,
		assets: "https://example.com" + base
	}
}

export default config

Example index.php

Note: You can choose the path by setting indexPath in the adapter config.

<!-- index.php -->
<?php
/**
 * Plugin Name: My Shortcode
 */

include plugin_dir_path( __FILE__ ) . 'svelte_kit_shortcode.php';
?>

Passing attributes and content

Both are inserted right before the svelte kit body.

[my-shortcode attribute-a attribute-b attribute-c]
<a href="/">Home</a>
[/my-shortcode]

becomes

<script id="my-shortcode-attributes" type="application/json">
	["attribute-a", "attribute-b", "attribute-c"]
</script>
<template id="my-shortcode-content">
	<a href="/">Home</a>
</template>
<!-- svelte kit body stuff -->

Style Isolation

(1) Shadow dom

Setting the shadow option to true puts the head, body, and shortcode data under one shadow dom.

or (2) Postcss

The following configuration of postcss plugins should provide enough isolation from Wordpress styles.

Note that postcss-autoreset is using the fork at tomatrow/postcss-autoreset.

const autoprefixer = require("autoprefixer")
const cssnano = require("cssnano")
const safeImportant = require("postcss-safe-important")
const prefixer = require("postcss-prefix-selector")
const initial = require("postcss-initial")
const autoReset = require("postcss-autoreset")

const mode = process.env.NODE_ENV
const dev = mode === "development"

const config = {
	plugins: [
		autoReset({ reset: "revert" }),
		initial(),
		prefixer({
			prefix: "#svelte",
			transform(prefix, selector, prefixedSelector) {
				return ["html", "body"].includes(selector) ? `${selector} ${prefix}` : prefixedSelector
			}
		}),
		autoprefixer(),
		safeImportant(),
		!dev &&
			cssnano({
				preset: "default"
			})
	]
}

module.exports = config

License

MIT