在开发Ember插件时触发实时重新加载

huangapple go评论57阅读模式
英文:

Triggering live-reloading when developing an Ember addon

问题

我正在为我的团队开发一个私有的Ember插件,其中包含一些我们将在多个不同应用程序中重用的品牌UI组件,例如<BrandHeader /><BrandFooter />

是否有一种标准的方法来创建开发环境,允许您在构建组件时获得实时重新加载的预览?如果没有标准方法,是否有人有为您有效的工作流程?

我尝试过使用使用ember-cli创建插件ember addon <name>,然后通过npm link将其链接到Ember应用程序,其中我调用了这些组件,但是使用这种方法,我需要在每次对插件进行更改时重新构建Ember应用程序。

英文:

I'm developing a private Ember addon for my team that will contain a few branded UI components that we will reuse in a bunch of different apps, such as a &lt;BrandHeader /&gt; and &lt;BrandFooter /&gt;.

Is there an standard for creating a development environment that allows you to get live-reloading previews of your components as you build them? If there's not a standard, does anyone have a workflow that you've created that works well for you?

I tried using the strategy of creating an addon with the ember-cli ember addon &lt;name&gt; and then npm linking to an Ember app where I call the components, but with that method I need to rebuild the ember app every time I make a change in to the addon.

答案1

得分: 1

在Ember文档中,我找不到明确解释这一点的地方,但我找到了答案:

ember addon <name>命令会自动在tests/dummy目录中创建一个"虚拟"应用程序。 您可以在此应用程序中使用您的插件组件,无需特殊配置。在下面的示例中,我只是添加了一行,您可以看到<BrandHeader>组件被调用。

// tests/dummy/templates/application.hbs

{{page-title "Dummy"}}

<BrandHeader>Header text</BrandHeader>
<h2 id="title">Welcome to Ember</h2>

{{outlet}}

在项目的根文件夹中运行ember serve会启动一个开发服务器,就像在普通的Ember应用程序中一样,它会为虚拟应用程序提供服务。对组件的更改将实时更新。

英文:

I couldn't find a place in the Ember documentation where this is clearly explained, but I found the answer:

The ember addon &lt;name&gt; command automatically creates a "dummy" app in the tests/dummy directory. You can use your addon components in this app with no special configuration. In the example below, I've just added the line where you see the &lt;BrandHeader&gt; component being called.

// tests/dummy/templates/application.hbs

{{page-title &quot;Dummy&quot;}}

&lt;BrandHeader&gt;Header text&lt;/BrandHeader&gt;
&lt;h2 id=&quot;title&quot;&gt;Welcome to Ember&lt;/h2&gt;

{{outlet}}

Running ember serve in the root folder of your project will start a development server like in a normal Ember app, serving the dummy app. Changes to components will live-update.

答案2

得分: 0

在确保在npm/yarn/pnpm项目之间进行链接时,使live-reload正常工作,有一些事情你需要注意:

在v1插件的<project-root>/index.js中,你需要临时设置isDevelopingAddon,如下所示:

module.exports = {
  name: require('./package').name,

  isDevelopingAddon: () => true,
}

如果你已安装Watchman,你可能可以跳过此步骤。

接下来,在你链接的应用程序中,你可能需要在ember-cli-build.js中设置autoImport.watchDependencies,如下所示:

// 在你的ember-cli-build.js文件中
let app = new EmberApp(defaults, {
  autoImport: {
    watchDependencies: [
      // 如果“some-lib”在开发过程中更改,则触发重建
      'some-lib',
      // 如果“some-lib”的内部依赖“other-lib”更改,则触发重建
      ['some-lib', 'other-lib'],
      // 如果在开发过程中任何依赖项更改,则触发重建
      ...Object.keys(require('./package').dependencies || {}),
    ],
  },
});

embroider下,我认为所有这些监视操作都会自动发生。

英文:

There are a couple things you'll want to look at to make sure that live-reload works between npm/yarn/pnpm link between projects:

In the v1 addon's &lt;project-root&gt;/index.js, you'll want to temporarily set isDevelopingAddon

module.exports = {
  name: require(&#39;./package&#39;).name,

  isDevelopingAddon: () =&gt; true,
}

if you have Watchman installed, you may be able to skip this step.

Next, in your app that you linked to, you may need to set autoImport.watchDependencies in your ember-cli-build.js, like this:

// In your ember-cli-build.js file
let app = new EmberApp(defaults, {
  autoImport: {
    watchDependencies: [
      // trigger rebuilds if &quot;some-lib&quot; changes during development
      &#39;some-lib&#39;,
      // trigger rebuilds if &quot;some-lib&quot;&#39;s inner dependency &quot;other-lib&quot; changes
      [&#39;some-lib&#39;, &#39;other-lib&#39;],
      // trigger rebuilds if any of your dependencies change during development
      ...Object.keys(require(&#39;./package&#39;).dependencies || {}),
    ],
  },
});

Under embroider, I believe all this watching happens automatically.

huangapple
  • 本文由 发表于 2023年7月14日 03:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定