簡體   English   中英

如何將 Sapper 添加到現有的 Svelte 項目中?

[英]How to add Sapper to existing Svelte project?

我在苗條中有廣泛的應用,后來我需要在它上面附加一個工兵以進行進一步的工作,可能嗎? 我嘗試通過以下方式進行:

npm i @sapper

但是當我嘗試從這個 package eq 導入一些東西時:

import { goto } from '@sapper/app';

編譯器告訴我我對這個工兵方法沒有依賴:(

我不知道如何將工兵附加到我的項目中

我的匯總

import svelte from "rollup-plugin-svelte";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import livereload from "rollup-plugin-livereload";
import { terser } from "rollup-plugin-terser";
// library that helps you import in svelte with
// absolute paths, instead of
// import Component  from "../../../../components/Component.svelte";
// we will be able to say
// import Component from "components/Component.svelte";
import alias from "@rollup/plugin-alias";

const production = !process.env.ROLLUP_WATCH;

// configure aliases for absolute imports
const aliases = alias({
  resolve: [".svelte", ".js"], //optional, by default this will just look for .js files or folders
  entries: [
    { find: "components", replacement: "src/components" },
    { find: "views", replacement: "src/views" },
    { find: "assets", replacement: "src/assets" },
  ],
});

function serve() {
  let server;

  function toExit() {
    if (server) server.kill(0);
  }

  return {
    writeBundle() {
      if (server) return;
      server = require("child_process").spawn(
        "npm",
        ["run", "start", "--", "--dev"],
        {
          stdio: ["ignore", "inherit", "inherit"],
          shell: true,
        }
      );

      process.on("SIGTERM", toExit);
      process.on("exit", toExit);
    },
  };
}

export default {
  input: "src/main.js",
  output: {
    sourcemap: true,
    format: "iife",
    name: "app",
    file: "public/build/bundle.js",
  },
  plugins: [
    svelte({
      // enable run-time checks when not in production
      dev: !production,
      // we'll extract any component CSS out into
      // a separate file - better for performance
      css: (css) => {
        css.write("bundle.css");
      },
    }),

    // If you have external dependencies installed from
    // npm, you'll most likely need these plugins. In
    // some cases you'll need additional configuration -
    // consult the documentation for details:
    // https://github.com/rollup/plugins/tree/master/packages/commonjs
    resolve({
      browser: true,
      dedupe: ["svelte"],
    }),
    commonjs(),

    // In dev mode, call `npm run start` once
    // the bundle has been generated
    !production && serve(),

    // Watch the `public` directory and refresh the
    // browser on changes when not in production
    !production && livereload("public"),

    // If we're building for production (npm run build
    // instead of npm run dev), minify
    production && terser(),

    // for absolut imports
    // i.e., instead of
    // import Component  from "../../../../components/Component.svelte";
    // we will be able to say
    // import Component from "components/Component.svelte";
    aliases,
  ],
  watch: {
    clearScreen: false,
  },
};

我的 package.json:

{
  "name": "svelte-app",
  "version": "1.0.0",
  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public -s",
    "build:tailwind": "tailwind build public/assets/styles/index.css -o public/assets/styles/tailwind.css",
    "build:fontawesome": "mkdir -p public/assets/vendor/@fortawesome/fontawesome-free/webfonts && mkdir -p public/assets/vendor/@fortawesome/fontawesome-free/css && cp -a ./node_modules/@fortawesome/fontawesome-free/webfonts public/assets/vendor/@fortawesome/fontawesome-free/ && cp ./node_modules/@fortawesome/fontawesome-free/css/all.min.css public/assets/vendor/@fortawesome/fontawesome-free/css/all.min.css",
    "install:clean": "rm -rf node_modules/ && rm -rf package-lock.json && rm -rf public/build && npm install && npm run build:tailwind && npm run build:fontawesome && npm run dev"
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "^16.0.0",
    "@rollup/plugin-node-resolve": "^10.0.0",
    "rollup": "^2.3.4",
    "rollup-plugin-commonjs": "^10.1.0",
    "rollup-plugin-css-only": "^3.1.0",
    "rollup-plugin-livereload": "^2.0.0",
    "rollup-plugin-node-polyfills": "^0.2.1",
    "rollup-plugin-svelte": "^7.0.0",
    "rollup-plugin-terser": "^7.0.0",
    "svelte": "^3.0.0",
    "webpack": "^5.11.1",
    "webpack-cli": "^4.3.1"
  },
  "dependencies": {
    "@fortawesome/fontawesome-free": "5.14.0",
    "@popperjs/core": "2.5.1",
    "@rollup/plugin-alias": "3.1.1",
    "@rollup/plugin-replace": "^2.3.4",
    "@tailwindcss/custom-forms": "0.2.1",
    "body-parser": "^1.19.0",
    "chart.js": "2.9.3",
    "compression": "^1.7.4",
    "express": "^4.17.1",
    "express-session": "^1.17.1",
    "fontawesome-svelte": "^2.0.1",
    "node-fetch": "^2.6.1",
    "node-sass": "^5.0.0",
    "page": "^1.11.6",
    "polka": "^0.5.2",
    "rollup-plugin-scss": "^2.6.1",
    "session-file-store": "^1.5.0",
    "sirv": "^1.0.10",
    "sirv-cli": "1.0.6",
    "svelte-routing": "1.4.2",
    "tailwindcss": "1.8.10"
  }
}

我一直在尋找這個答案,我認為@Romain Durand 的答案是最接近的,我認為我們不能將 ssaper 添加到現有的苗條項目中。 我必須在 ssaper 中初始化項目並使用 svelte。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM