简体   繁体   English

如何在不安装节点的情况下在 django-admin 小部件中使用 npm 中的模块?

[英]How can I use a module from npm in a django-admin widget, without installing node?

Background背景

I have a django app that I want to create an admin widget for.我有一个 Django 应用程序,我想为其创建一个管理小部件。 The widget will display text in a particular way (like a terminal).该小部件将以特定方式(如终端)显示文本。 It's so that app admins can see forwarded logs from an analytics process that is orchestrated by django (the app is django-twined ).这样应用程序管理员就可以看到来自 django 编排的分析过程的转发日志(该应用程序是django-twined )。

To do that I want to use something like terminal-kit or one of the other libraries requiring npm install <whatever>为此,我想使用诸如 terminal-kit 之类的东西或其他需要npm install <whatever>的库之一

Building the app构建应用程序

The app is built in docker, and I don't want the entire node stack to end up in my production image.该应用程序是在 docker 中构建的,我不希望整个节点堆栈最终出现在我的生产映像中。

I could use a multi-stage docker build;可以使用多阶段 docker 构建; so install node and a lib from NPM in the first stage, then copy the library from node_modules in the second stage, but this feels unnecessarily slow.所以在第一阶段从 NPM 安装 node 和一个库,然后在第二阶段从node_modules复制库,但这感觉不必要地慢。

Also, because all I'm doing then is using the raw js static assets that get bundled with the django app, I'm not sure how to go about importing the module (or if this is even possible).另外,因为我当时所做的只是使用与 django 应用程序捆绑在一起的原始 js 静态资产,所以我不确定如何导入模块(或者这是否可能)。

The questions问题

  • Can I install an npm module without having the node stack present, and therefore avoid dealing with unwieldy multi stage builds?我可以在没有节点堆栈的情况下安装 npm 模块,从而避免处理笨重的多阶段构建吗?

  • How can I then import or require the contents of that module into vanilla javascript to use in a django widget?然后我怎样才能将该模块的内容importrequire到 vanilla javascript 中以在 django 小部件中使用?

  • Is this even in general possible?这甚至在一般情况下可能吗? If it looks like moving a mountain, I'll give up and just slap a text area with monospace font on there... but it would be nice if log highlighting and colours were properly handled in a terminal-like way.如果它看起来像移动一座山,我会放弃,只是在那里拍一个带有等宽字体的文本区域……但如果以类似终端的方式正确处理日志突出显示和颜色,那就太好了。

Can I install an npm module without having the node stack present, and therefore avoid dealing with unwieldy multi stage builds?我可以在没有节点堆栈的情况下安装 npm 模块,从而避免处理笨重的多阶段构建吗?

You can rollup an npm package using a dev tool like Browserify.你可以使用像 Browserify 这样的开发工具来汇总一个 npm 包。 This can be done by rolling up the entire package using something like:这可以通过使用类似以下内容的方式卷起整个包来完成:

browserify --require terminal-kit

Browserify will parse the package and create a single JS file that you can try loading in the browser. Browserify 将解析包并创建一个 JS 文件,您可以尝试在浏览器中加载该文件。 There are some limitations to this so I'd recommend experimenting and exploring the Browserify docs.这有一些限制,所以我建议您试验和探索 Browserify 文档。

How can I then import or require the contents of that module into vanilla javascript to use in a django widget?然后我怎样才能将该模块的内容导入或要求到 vanilla javascript 中以在 django 小部件中使用?

You can do this by including a Django template file reference in the backend admin class definition.您可以通过在后端管理类定义中包含 Django 模板文件引用来完成此操作。 In the template you'll need to include an html JS source tag that points to the JS script you want to load.在模板中,您需要包含一个 html JS 源标记,该标记指向您要加载的 JS 脚本。 Django can include static files when building, you can use that to include the JS file during build time and then a local resource reference to point the template file to the right location. Django 可以在构建时包含静态文件,您可以在构建时使用它来包含 JS 文件,然后使用本地资源引用将模板文件指向正确的位置。

Is this even in general possible?这甚至在一般情况下可能吗?

Generally speaking this is definitely possible but YMMV.一般来说,这绝对是可能的,但 YMMV。 It boils down to the complexities of the npm package and what exactly it is trying to do in the browser.它归结为 npm 包的复杂性以及它在浏览器中究竟要做什么。

In steps I would do the following:在步骤中,我将执行以下操作:

  1. Use Browserify to convert the npm package to a single JS file.使用 Browserify 将 npm 包转换为单个 JS 文件。
  2. Create an html file that loads the local JS file, open this in the browser.创建一个加载本地 JS 文件的 html 文件,在浏览器中打开它。
  3. Open the console and see if the commands/context you're hoping to reproduce are working as expected in the browser.打开控制台并查看您希望重现的命令/上下文是否在浏览器中按预期工作。 You could also write another vanilla JS file and load that in the html file to test.您还可以编写另一个 vanilla JS 文件并将其加载到 html 文件中进行测试。
  4. Include the JS file reference in the Django admin template/widget.在 Django 管理模板/小部件中包含 JS 文件引用。
  5. Write custom JS code in the widget that uses/shows the globally instantiated JS script.在使用/显示全局实例化 JS 脚本的小部件中编写自定义 JS 代码。

This strategy is based off my personal experience, I have had success following this strategy, hopefully it is helpful.此策略基于我的个人经验,我遵循此策略取得了成功,希望它有所帮助。

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

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