Giter VIP home page Giter VIP logo

repl's Introduction

@vue/repl

Vue SFC REPL as a Vue 3 component.

Basic Usage

Note: @vue/repl >= 2 now supports Monaco Editor, but also requires explicitly passing in the editor to be used for tree-shaking.

With CodeMirror Editor

Basic editing experience with no intellisense. Lighter weight, fewer network requests, better for embedding use cases.

<script setup>
import { Repl } from '@vue/repl'
import CodeMirror from '@vue/repl/codemirror-editor'
// import '@vue/repl/style.css'
// ^ no longer needed after 3.0
</script>

<template>
  <Repl :editor="CodeMirror" />
</template>

With Monaco Editor

With Volar support, autocomplete, type inference, and semantic highlighting. Heavier bundle, loads dts files from CDN, better for standalone use cases.

<script setup>
import { Repl } from '@vue/repl'
import Monaco from '@vue/repl/monaco-editor'
// import '@vue/repl/style.css'
// ^ no longer needed after 3.0
</script>

<template>
  <Repl :editor="Monaco" />
</template>

Advanced Usage

Customize the behavior of the REPL by manually initializing the store.

See v4 Migration Guide

<script setup>
import { watchEffect, ref } from 'vue'
import { Repl, useStore, useVueImportMap } from '@vue/repl'
import Monaco from '@vue/repl/monaco-editor'

// retrieve some configuration options from the URL
const query = new URLSearchParams(location.search)

const {
  importMap: builtinImportMap,
  vueVersion,
  productionMode,
} = useVueImportMap({
  // specify the default URL to import Vue runtime from in the sandbox
  // default is the CDN link from jsdelivr.com with version matching Vue's version
  // from peerDependency
  runtimeDev: 'cdn link to vue.runtime.esm-browser.js',
  runtimeProd: 'cdn link to vue.runtime.esm-browser.prod.js',
  serverRenderer: 'cdn link to server-renderer.esm-browser.js',
})

const store = useStore(
  {
    // pre-set import map
    builtinImportMap,
    vueVersion,
    // starts on the output pane (mobile only) if the URL has a showOutput query
    showOutput: ref(query.has('showOutput')),
    // starts on a different tab on the output pane if the URL has a outputMode query
    // and default to the "preview" tab
    outputMode: ref(query.get('outputMode') || 'preview'),
  },
  // initialize repl with previously serialized state
  location.hash,
)

// persist state to URL hash
watchEffect(() => history.replaceState({}, '', store.serialize()))

// use a specific version of Vue
vueVersion.value = '3.2.8'
// production mode is enabled
productionMode.value = true
</script>

<template>
  <Repl :store="store" :editor="Monaco" :showCompileOutput="true" />
</template>

repl's People

Contributors

61315 avatar alfred-skyblue avatar antfu avatar baiwusanyu-c avatar blackman99 avatar btea avatar buuing avatar chenxch avatar edison1105 avatar fzf404 avatar gimmyhehe avatar hehehai avatar holazz avatar johnsoncodehk avatar kingwl avatar megrax avatar pdanpdan avatar renovate[bot] avatar replygirl avatar sight-wcg avatar skirtles-code avatar sxzz avatar woshichaorens avatar wxsms avatar youxam avatar yyx990803 avatar zhangenming avatar zhmushan avatar zhousg avatar zqran avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

repl's Issues

Rendering HTML - newline missing

When converting SFC template to HTML, we're missing the newline before the last closing tag:

// SFC
<template>
  <h1>{{ message }}</h1>
</template>

// HTML
<div id="app">
  <h1>{{ message }}</h1></div>

用ip 访问报错

用ip 访问报错,提示 Cannot set properties of undefined (setting 'name') ;
控制台提示 Cannot read properties of undefined (reading 'digest') ;
Cannot set properties of undefined (setting 'name') ;

但是本地用 localhost 访问正常

Suggestion: Maybe we can replace the editor change event with `Ctrl + s`

At present, when we edit the file content, the change event of the editor will be triggered, and then the input content will be parsed.

But most of the time, we only wrote a part, so the analysis will report an error. I don't think it's very friendly.

Therefore, I want to confirm the completion of your code by Ctrl + s, and then parse and update it.

I can create an PR if you think this suggestion works. @yyx990803

How to customize styles and layout correctly?

For example, I want a dark theme. I added class="dark" to the parent container, which meets the expectations. Now I want the code and output to be arranged vertically. There is a description of "vertical" in the css file, but it does not work after adding it. I try to read the source code to solve the problem myself, but some variables do not have proper explanations, which is difficult to read. So please ask me how to make some customized settings. Especially vertical arrangement.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/release.yml
  • actions/checkout v4
  • actions/setup-node v4
npm
package.json
  • @babel/types ^7.24.0
  • @rollup/plugin-replace ^5.0.5
  • @shikijs/monaco ^1.1.7
  • @types/codemirror ^5.60.15
  • @types/hash-sum ^1.0.2
  • @types/node ^20.11.25
  • @typescript-eslint/eslint-plugin ^7.1.1
  • @vitejs/plugin-vue ^5.0.4
  • @volar/cdn ~1.11.1
  • @volar/monaco ~1.11.1
  • @volar/typescript ~1.11.1
  • @vue/language-service 1.9.0-alpha.3
  • bumpp ^9.4.0
  • codemirror ^5.65.16
  • conventional-changelog-cli ^4.1.0
  • eslint ^8.57.0
  • eslint-plugin-vue ^9.22.0
  • fflate ^0.8.2
  • hash-sum ^2.0.0
  • lint-staged ^15.2.2
  • monaco-editor-core ^0.46.0
  • prettier ^3.2.5
  • shiki ^1.1.7
  • simple-git-hooks ^2.10.0
  • sucrase ^3.35.0
  • typescript ^5.4.2
  • vite ^5.1.5
  • vite-plugin-dts ^3.7.3
  • vue ^3.4.21
  • vue-tsc ^2.0.6
  • pnpm 8.15.4

  • Check this box to trigger a request for Renovate to run again on this repository

About the introduction of third party packages

I would like to create a feature that allows importing third-party packages, but I have found that there is no way to query all available third-party packages. Can I write a common list here for loading these third-party packages? I am willing to contribute to this. I'm wondering if the official team has any plans regarding this.

Please add a License

I am thinking to fork this project but notice that there is no License assigned to it. Can you please add a License to this project?

The container listing components has a tall scrollbar on Firefox

On Windows 10, the scrollbar makes it difficult to reach components. A solution would be to set scrollbar-width: thin on the file selector div. Other users don't experience this issue, but it might be due to the OS.

image

If possible I would like to contribute and make this simple change myself. It seems to have a more prevalent fade effect on Chrome, so I'm not sure as to what the issue is.

compileScript 如何支持第三方插件 比如 antdv

export default (descriptor: defaultCompiler.SFCDescriptor, id: string) => {
  if (descriptor.script || descriptor.scriptSetup) {
    // 1.编译script
    const compiledScript = defaultCompiler.compileScript(descriptor, {
      inlineTemplate: true, // 是否编译模板并直接在setup()里面内联生成的渲染函数
      id, // 用于样式的作用域
      templateOptions: {
        // 编译模板的选项
        compilerOptions: {
          // 模板编译选项
        },
      },
    });
    return (
      extendVueInstance().constString +
      "\n" +
      deleteImport(compiledScript.content.replace("export default ", "return "))
    );
  }
};

根据官方demo 在我的系统内部进行了一些改造,但是我同时希望他能够支持 vue的其他ui组件,不知道该如何实现

@vue/repl crypto problems

@vue/repl依赖中的crypto,你用的是web标准中的crypto,我在http生产环境中crypto下的subtle 报undefined,然后我在Stack Overflow上看到有说是 Web Crypto API 只能在安全环境下(本地localhost、127.0.0.1或者https)使用,在其他局域网地址或者其他ip下无法使用。报错文件为 vue-repl.js 下的 hashId方法。
我希望能够不使用Crypto 来对文件名进行加密,因为这会导致在非安全环境下报错,而我们大多数开发者都是在http这种非安全环境下开发的,所以我建议你们能够重写 vue-repl.js 下的 hashId方法,因为只因为想加密文件名而导致整个程序无法继续运行,就很可惜。

Code search

Add Ctrl+F to code search because the browser's built-in document search cannot find when the editor scrolls.

Register global components from locally imported files

I'm have documentation for an existing component library. I'd like to add this into the examples to showcase the components, but I dont see anything about registering components. Is that possible?

If so, can I register global components, or use a plugin in the Vue instance that is responsible for rendering the results?

bug in firefox

image
repl在火狐浏览器下运行(mac下所有也会出现这问题,我想是所有用webkit内核的浏览器都会出现)时,出现此问题,希望官方大大解决一下,目前我也在自己寻找解决方法,

Add ability to disable automatic url generation

Hi. Is it possible to add some option, a switch, so I could turn on/off automatic URL generation or maybe a button to copy generated URL to the clipboard? (and the setting would be saved in localStorage). My reason is that every time I change the code, the url is also updated, making the browser history cluttered with unnecessary entries. I always have to remember to open the playground in incognito mode to bypass this, which is a little annoying to me :/. Thanks for considering my request.

long, long list...

ss

Suggestion: Adding HMR like in Vite

In the current implementation, the re-rendering of the components is not replacing the changed parts like HMR does in real-time but in fact re-rendering all components and mounting them again. Is there way to enable HMR like the vite's dev server offers?

It would be nice if we could support importing full components into vue

Here's a demo of an element-plus I wrote

and this is element-plus official demo

I've seen a lot of community implementations like https://element-plus.run, which is actually bad for developers, and the pain point is that we can't bring in a whole component library at once. As you can see, the lack of el-rows in my demo caused my code to only partially render

So I have a proposal

// if it works will be great 
import { use } from 'vue'
import ElementPlus from 'element-plus'
use(ElementPlus)

[Bug] There are some problem with js-eval runtime

Problem 1.

in file repl\src\output\srcdoc.html#55

Promise.resolve event and script code eval sometimes run at a wrong time.
which may cause CSS code inject failed:
from source code we want window.__css__ = xxxxxxCode evaluate before document.getElementById('__sfc-styles').innerHTML = window.__css__
But this will cause when we use window.__css__ , it is still nothing
image

Resovle:

window.__next__ = () => {
    setTimeout(() => resolve(), 0)
}

it is a microtask vs macrotask problem

Problem 2.

in file Preview.vue#152

sometimes event not fire when we are typing, especially in App.vue with style tag,change watch fuction will fix this.

About these two problem,I create a PR at #47

Standalone Output Component

As initially discussed here, for both the standalone and the embedded use cases, you could consider extracting the Output component whose only purpose is to show compiled results or preview. Store data like files and importmap could be provided through the prop.

Then it's possible to use any favorable editor(monaco or codemirror) with the Output component, and we could use the Output component alone as a readonly preview of a project.

Update sucrase version to support TypeScript 4.9

Hello, thank you so much for developing such a nice library, but I found some typescript(4.9) syntax support not very good when using it. The version of surcrase shows 3.21.0 in the pnpm-lock.yaml file, but the latest version of sucrase is 3.29.0 and the syntax for Typescript4.9 is already supported, should the sucrase version be updated?

inconsistent browser specific behavior

caused by vuejs/core#6158

repro url

repro:

last chrome:

  1. open url
  2. open file Comp.vue
  3. remove attribute "scoped" from style tag. both "p" tag has yellow background
  4. add attribute "scoped" to style tag. only first "p" tag has yellow background

last firefox:

  1. open url
  2. open file Comp.vue
  3. remove attribute "scoped" from style tag. both "p" tag has yellow background
  4. add attribute "scoped" to style tag. both "p" tag has yellow background

expected:
because there is a bug vuejs/core#6158 firefox should have only first "p" tag with yellow background

Suggestion: Add pre-processors for <style>

I made a very initial version to support Less.
I think repl can add an option about custom file extension transformer.
Or we can just support less transform in repl.

I can create an PR for it if you agree with the suggestion.

/cc @yyx990803

Bug:es-module-shims cause CSS inject code failed

When use https://sfc.vuejs.org/ sometimes css is not in the output iframe page.
I trace the code and find when code use window.__css__,it is still nothing
this is a problem with es-module-shims witch causes normal script run as module script in twice.

here is a demo:

<html>
<head>
    <meta charset="utf-8">
    <script async="" src="https://unpkg.com/[email protected]/dist/es-module-shims.wasm.js"></script>
</head>
<body>
<script>
    async function start() {
      const scripts = [
        `console.log('script run!!!!!! but run twice')`
      ]
      for(let script of scripts) {
        const scriptNode = document.createElement('script')
        scriptNode.setAttribute('type', 'module')

        scriptNode.innerHTML = script
        document.head.appendChild(scriptNode)
      }
    }
    setTimeout(() => start(), 2000)
</script>
<div>this is a test</div>
<div id="app"></div>
</body>
</html>

you will see two log with script run!!!!!! but run twice

Thus window.__css__ = 'xxxxxxxx' code was covered by window.__css__ = ''(run twice)

playground hangs indefinitely with keep-alive and missing adjecent v-if combination

replace template with this code in the sfc playground.

<template>
  <keep-alive>
    <A  v-if="1" ></A>
    break
    <A v-else></A>
  </keep-alive>    
</template>

this combination should trigger 2 errors
Vue template compilation error: <KeepAlive> expects exactly one child component.(45)
Vue template compilation error: v-else/v-else-if has no adjacent v-if or v-else-if.(30)

but instead it causes infinite loop

severity: annoyance, edge case

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.