简体   繁体   English

部署 Lit Web 组件 - 在部署时遇到困难

[英]Deploying a Lit Web Component - Struggling at deployment

I am new to lit specifically, and web components in general.我是专门点亮的新手,一般来说是 web 组件。 I have built a web component for usage in Wix (as a custom element), just using straight vanilla js, and have found some success.我构建了一个 web 组件用于 Wix(作为自定义元素),仅使用普通 js,并取得了一些成功。 See https://app.njaplatform.one/rating-element.js for a sample of what I built.有关我构建的示例,请参见https://app.njaplatform.one/rating-element.js

However, I have been asked to redo the work in Lit ( https://lit.dev/ ).但是,我被要求在 Lit ( https://lit.dev/ ) 中重做工作。 I like the syntax of the code better, and it feels like a smarter way to go (as a developer).我更喜欢代码的语法,感觉它是 go(作为开发人员)的一种更聪明的方式。 However, I am really struggling with publishing it.但是,我真的很难发布它。

Whilst I have been able to rebuild my component in Lit, and it is working when I run a local "Web Dev Server", I am really uncertain as to how I publish my lit component so that it is available to other sites.虽然我已经能够在 Lit 中重建我的组件,并且当我运行本地“Web 开发服务器”时它可以正常工作,但我真的不确定我如何发布我的 Lit 组件以便它可用于其他站点。 I haven't found any great documentation, walkthroughs, or tutorials on this area.我还没有找到关于该领域的任何优秀文档、演练或教程。 I am really struggling to get over this implementation hump (I am more of a front-end developer and can do some deployments, but I am often very dependent on documentation and well-established ways of doing things).我真的很难克服这个实施障碍(我更像是一名前端开发人员,可以进行一些部署,但我通常非常依赖文档和行之有效的做事方式)。

Key points:关键点:

  • I started with the typescript starter kit at https://lit.dev/docs/tools/starter-kits/我从 typescript 入门套件开始,网址为https://lit.dev/docs/tools/starter-kits/

  • Running npm run serve serves the app to a 'Web Dev server' and that appears fine locally for testing.运行npm run serve将应用程序提供给“Web 开发服务器”,这在本地看起来很好,可以进行测试。

  • I am not sure what I should be doing when I build/deploy the app.我不确定在构建/部署应用程序时应该做什么。

  • I have made some attempts to publish on some services online, and I am getting a mix of failures, or partial successes which fashion some documentation on the component (generated by eleventity), but the actual javascript generated appears to break as either我已经尝试在某些服务上在线发布,但我遇到了失败或部分成功的混合,这些成功形成了组件上的一些文档(由 elentity 生成),但生成的实际 javascript 似乎中断了

1: file not founds (404), or, 2: Error resolving module specifier "Lit". Relative module specifiers must start with "./", "../", or "/" 1:找不到文件 (404),或者,2: Error resolving module specifier "Lit". Relative module specifiers must start with "./", "../", or "/" Error resolving module specifier "Lit". Relative module specifiers must start with "./", "../", or "/"

Not sure how to proceed, would appreciate some "Lit 101" input on strategies as to what I should be doing at this point.不确定如何进行,希望得到一些“Lit 101”关于我此时应该做什么的策略输入。

The starter kits are set up primarily to be a development bed for standalone components, and not exactly for web apps.初学者工具包主要设置为独立组件的开发平台,而不是 web 应用程序。

The errors you are getting about resolving module specifiers are due to lit being published to npm with bare module specifiers as lit and it's dependencies can be referenced by package name in the code, and deduplicated by npm. Running with Web Dev Server in the starter kit does not have this problem because it uses a plugin @rollup/plugin-node-resolve which resolves those bare specifiers to paths in node_modules directory.您在解析模块说明符时遇到的错误是由于lit被发布到 npm,裸模块说明符为 lit,它的依赖项可以通过代码中的 package 名称引用,并通过 npm 进行重复数据删除。使用入门工具包中的 Web Dev Server 运行没有这个问题,因为它使用插件@rollup/plugin-node-resolve将那些裸说明符解析为node_modules目录中的路径。

The optimal thing to do depends on what your use case is.最佳做法取决于您的用例。

If you are creating a web app/site, the common thing to do for production apps would be to create a JS bundle with your source code.如果您正在创建一个 web 应用程序/站点,对于生产应用程序通常要做的事情是使用您的源代码创建一个 JS 包。 Have an entrypoint file that imports all of your components.有一个导入所有组件的入口点文件。 You can use something like rollup and also apply the aforementioned @rollup/plugin-node-resolve to create a single JS file.您可以使用类似rollup的东西,也可以应用前面提到的@rollup/plugin-node-resolve来创建单个 JS 文件。 This should be added to your index.html , and have your server or static host serve them.这应该添加到您的index.html ,并让您的服务器或 static 主机为它们服务。

If you're creating reusable components, you could just leave your code as is and it would be up to the end user to take the steps above to bundle.如果您正在创建可重用的组件,您可以将代码保持原样,最终用户将采取上述步骤进行捆绑。 This would be ideal since if the end user uses multiple components authored with Lit, they won't get duplicate copies of Lit.这将是理想的,因为如果最终用户使用多个使用 Lit 创作的组件,他们将不会获得 Lit 的重复副本。

If you want your user to be able to simply add a script tag for a js file and use your web components, you'd need to publish the bundled component, which you can also prepare with rollup.如果您希望您的用户能够简单地为 js 文件添加脚本标签并使用您的 web 组件,您需要发布捆绑组件,您也可以使用 rollup 来准备它。 The down side of this is that each component will come with its own copy of Lit.不利的一面是每个组件都将带有自己的 Lit 副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM