【问题标题】:Using a Svelte build with a Sails node server使用带有 Sails 节点服务器的 Svelte 构建
【发布时间】:2020-06-19 22:23:29
【问题描述】:

我正在尝试建立一个网站,前端使用 Svelte,后端使用 Sails。 我的问题是我无法将我的 Svelte 公共构建显示为我的 Sails 默认网页。 我想将组织保留在下面(或者可能是类似的东西),并在我继续使用“http://myserver:1337”而不是使用默认的 Sails 页面时拥有我的 Svelte 公共构建页面:file organization

PS:我使用的是 Node:v14.4.0,Sails:v1.2.4 和 Svelte:v6.14.5。 谢谢大家:)

【问题讨论】:

    标签: sails.js svelte svelte-3


    【解决方案1】:

    你可以试试这样的:

    1. 编译 Svelt 以构建到 Sails.js 上的 /public 目录中。

    打开您的 rollup.config.js 并将您的 public/build/bundle.js 和 public/build.bundle.css 的路径更改为公共风帆路径,即“../server/public...”。

    1. 配置 /task/pipeline.js 以包含已编译的 js 和 css 文件:
    // tasks/pipeline.js
    
    var cssFilesToInject = [
      'css/**/global.css',
      'css/**/bundle.css',
      'css/**/*.css',
    ];
    
    var jsFilesToInject = [
      'js/**/bundle.js',
      'js/**/*.js'
    ];
    
    1. 创建一个控制器来加载索引文件:
    // router.js
    
    '/*': { action: 'index', skipAssets: true, skipRegex: /^\/api\/.*$/ },
    

    排除的“/api”路由是为了让你配置CRUD路由。

    1. 索引控制器:
    
    module.exports = {
      friendlyName: 'View homepage',
      description: 'Display a compiled index page',
      exits: {
        success: {
          statusCode: 200,
          viewTemplatePath: 'pages/index'
        },
      },
      fn: async function () {
        return {};
      }
    };
    
    

    您可以在索引页面中包含模板 index.html 或创建自己的 index.ejs 来加载静态内容,与您之前配置的相同:

    // views/templates/template.ejs
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset='utf-8'>
        <meta name='viewport' content='width=device-width,initial-scale=1'>
    
        <title>Svelte app</title>
    
        <link rel='icon' type='image/png' href='/favicon.png'>
        <!--STYLES-->
        <!--STYLES END-->
    </head>
    
    <body>
    <!--TEMPLATES-->
    <!--TEMPLATES END-->
    <%- body %>
    <!-- exposeLocalsToBrowser ( ) %>
    <!--SCRIPTS-->
    <!--SCRIPTS END-->
    </body>
    </html>
    

    还有 index.ejs:

    // views/pages/index.ejs
    
    <!-- Nothing here I mean -->
    

    【讨论】:

      【解决方案2】:

      感谢您的回答,这有助于我了解它是如何工作的。 很抱歉,我没有完全按照您的教程进行操作(因为我无法理解我应该做什么;))。 我将 rollup.config.js 编辑为:

      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';
      
      const production = !process.env.ROLLUP_WATCH;
      const BUILD_PATH = '../server/assets';
      
      export default {
          input: 'src/main.js',
          output: {
              sourcemap: true,
              format: 'iife',
              name: 'app',
              file: `${BUILD_PATH}/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(`${BUILD_PATH}/build/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(BUILD_PATH),
      
              // If we're building for production (npm run build
              // instead of npm run dev), minify
              production && terser()
          ],
          watch: {
              clearScreen: false
          }
      };
      
      function serve() {
          let started = false;
      
          return {
              writeBundle() {
                  if (!started) {
                      started = true;
      
                      require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                          stdio: ['ignore', 'inherit', 'inherit'],
                          shell: true
                      });
                  }
              }
          };
      }
      

      我移动我的文件是资产: file organization

      然后我删除了server/views/pages/中的homepage.ejs

      它有效:)! 再次感谢您的快速回答

      【讨论】:

        猜你喜欢
        • 2021-09-12
        • 1970-01-01
        • 2016-03-20
        • 2022-01-10
        • 1970-01-01
        • 2012-08-05
        • 2016-06-28
        • 2014-02-27
        • 2016-08-06
        相关资源
        最近更新 更多