Skip to content

install a svelte-kit package from git #2772

@milahu

Description

@milahu

Describe the problem

when creating packages with svelte-kit https://kit.svelte.dev/docs#packaging

how is this supposed to work, when i install the package from git?

for example: svelte-jsoneditor

npm install https://github.com/josdejong/svelte-jsoneditor

usually i use the "prepare" script in package.json, to build such a library

{
  "scripts": {
    "prepare": "npm run build",
    "build": "..."
  }
}

but when the build is written to node_modules/pname/package/package.json
then obviously i cannot import it via node_modules/pname/package.json

Describe the proposed solution

naive solution: in svelte.config.js set kit.package.dir = "."
but this will delete the project root folder, including .git

workaround:

in svelte.config.js set kit.package.dir = "package"

package.json

{
  "scripts": {
    "prepare": "node svelte.prepare.mjs",
    "build": "..."
  }
}
svelte.prepare.mjs
import fs from 'fs';
import child_process from 'child_process';



const buildCommand = 'npm run build';
const svelteConfigPath = './svelte.config.js';
let pkgdir = 'package'; // default value
const ignoreFiles = ['.git'];



async function svelteKitPrepare() {

  console.log(buildCommand);
  child_process.execSync(buildCommand, { stdio: 'inherit', windowsHide: true });

  const srcdir = fs.mkdtempSync('src-of-svelte-kit-');
  ignoreFiles.push(srcdir);

  console.log(`move all files to ${srcdir}, except ${ignoreFiles.join(' ')}`);
  const ignoreFilesSet = new Set(ignoreFiles);
  for (const filename of fs.readdirSync('.')) {
    if (ignoreFilesSet.has(filename)) {
      console.log(`ignore ${filename}`);
      continue;
    }
    console.log(`mv -t ${srcdir}/ ${filename}`);
    fs.renameSync(filename, `${srcdir}/${filename}`);
  }

  // get custom pkgdir
  if (fs.existsSync(svelteConfigPath)) {
    console.log(`import ${svelteConfigPath}`)
    const svelteConfig = await import(svelteConfigPath);
    if (svelteConfig.kit?.package?.dir) {
      pkgdir = svelteConfig.kit.package.dir; // custom value
    }
  }

  // move package files back
  for (const filename of fs.readdirSync(`${srcdir}/${pkgdir}`)) {
    console.log(`mv -t ./ ${filename}`);
    fs.renameSync(`${srcdir}/${pkgdir}/${filename}`, filename);
  }

  console.log(`rmdir ${srcdir}/${pkgdir}`)
  fs.rmdirSync(`${srcdir}/${pkgdir}`); // pkgdir should be empty
}



svelteKitPrepare();
old version: svelte.prepare.sh
#!/bin/sh

pkgdir=package # svelte.config.js: kit.package.dir
srcdir=.src

set -e # throw on error
set -o xtrace # print commands

if [ -d "$srcdir" ]
then
  if ! (rmdir "$srcdir") # remove empty srcdir
  then
    echo "error: srcdir $srcdir is not empty"
    exit 1
  fi
fi

mkdir "$srcdir"

npm run build

# move all files except $srcdir and .git
find . -mindepth 1 -maxdepth 1 '(' -not -name "$srcdir" -and -not -name .git ')' -exec mv -t "$srcdir" '{}' ';'

# move package files back
find "$srcdir/$pkgdir" -mindepth 1 -maxdepth 1 -exec mv -t ./ '{}' ';'

rmdir "$srcdir/$pkgdir"

Alternatives considered

No response

Importance

i cannot use SvelteKit without it

(no seriously, "install from git" is a standard feature of npm)

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions