joeldenning / vite-single-spa-example

MIT License
63 stars 25 forks source link

Cannot use import statement outside a module #2

Closed DenisNikonov closed 3 years ago

DenisNikonov commented 3 years ago

Hello! I will try use u example for single-spa, but i get error: Cannot use import statement outside a module in my main.ts.

src/vite-app/src/main.ts

import { createApp, h } from "vue";
import App from "./App.vue";
import singleSpaVue from "single-spa-vue";

import { router } from './router';

const vueLifecycles = singleSpaVue({
  createApp,
  appOptions: {
    render() {
      return h(App, {
        props: {
          name: this.name,
          mountParcel: (this as any).mountParcel,
          singleSpa: (this as any).singleSpa,
        },
      });
    },
  },
  handleInstance: (app) => {
    app.use(router);
  },
});
export const bootstrap = vueLifecycles.bootstrap;
export const mount = vueLifecycles.mount;
export const unmount = vueLifecycles.unmount;

src/vite-app/vite.config.ts

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
  server: {
    cors: {
      origin: '*'
    }
  },
  build: {
    rollupOptions: {
      input: 'src/main.ts',
      preserveEntrySignatures: 'allow-extension',
      output: {
        format: 'system'
      },
    },
  }
})

src/index.ejs

<script type="systemjs-importmap">
  {
    "imports": {
      "@test/root-config": "//localhost:9000/test-root-config.js",
      "@test/vite-app": "//localhost:3000/src/main.ts"
    }
  }
</script>

UPDATE: I checked my service at http://single-spa-playground.org/playground. All checks have been passed. Most likely the problem is either with the Layout Engine or with the single-spa

RESOLVED Need add systemjs-babel.js.

Example:

<% if (isLocal) { %>
  <script src="https://cdn.jsdelivr.net/npm/systemjs@6.8.3/dist/system.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/systemjs@6.8.3/dist/extras/amd.js"></script>
  <script src="https://unpkg.com/systemjs-babel@0.3.1/dist/systemjs-babel.js"></script>
  <% } else { %>
  <script src="https://cdn.jsdelivr.net/npm/systemjs@6.8.3/dist/system.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/systemjs@6.8.3/dist/extras/amd.min.js"></script>
  <% } %>
joeldenning commented 3 years ago

It seems you may be mixing SystemJS with native modules, which can result in confusing behaviors. See https://single-spa.js.org/docs/ecosystem-vite#native-modules-vs-systemjs for more info. Generally it's best just to choose one or the other rather than having both, since the browser doesn't have sufficient registry apis for systemjs to properly load native modules and their dependencies.