【问题标题】:Make a webcomponents bundle an npm package so webcomponents can be selectively imported将 webcomponents 捆绑到一个 npm 包中,以便可以选择性地导入 webcomponents
【发布时间】:2018-08-27 13:24:34
【问题描述】:

在我当前的项目中,我们正在根据 v1 规范构建本机 Web 组件,我们目前 webpack 在单个捆绑包中 comp-webcomponents.js

这是我们当前包的入口点文件:

import 'document-register-element';
import 'nodelist-foreach-polyfill';
import 'babel-polyfill';

import 'components/tabs/comp-tabs';
import 'components/workspace-switcher/workspace-switcher';
import 'components/table/comp-table';
import 'components/date/comp-date';
import 'components/datepicker/comp-datepicker';
import 'components/datetime/comp-datetime';
import 'components/decimal/comp-decimal';
import 'components/number/comp-number';
import 'components/editor/comp-editor';
import 'components/time/comp-time';
import 'components/input/comp-input';
import 'components/button/comp-button';
import 'components/toggle-button/comp-toggle-button';
import 'components/yearmonth/comp-yearmonth';

一些组件具有供应商依赖性,例如 jQuery、datatables.net、jquery-ui、lodash 等。

问题 1:需要哪些步骤以便其他项目可以选择性地导入单个 Web 组件?

类似

import { compTable, compYearmonth } from "@comp-webcomponents";

问题 2:使每个 Web 组件成为可以使用单个命名空间安装的 npm 包需要什么?

有点像

npm install --save @comp-webcomponents/comp-div

示例组件:

class CompDiv extends HTMLDivElement {
  constructor(...args) {
    const self = super(...args);
    self.property = null;
    return self;
  }
  connectedCallback() {
    console.log('connected CompDiv');
  }
}

customElements.define('comp-div', CompDiv, { extends: 'div' });

非常感谢链接或其他有用的资源!

【问题讨论】:

标签: javascript npm webpack web-component


【解决方案1】:

我在我的 Web 组件项目中使用 Typescript。我在 src 文件夹中保存了所有单独的组件,并在其中创建了一个 index.ts 文件并单独导出每个组件。

export {compDiv, compTable, comeYearmonth} from './compDiv';
export {example} from './example';

您可以阅读它并按照tutorial 中的说明进行尝试。在我使用 tsc 构建并且我的类型声明在我的 dist 文件夹中之后,我 publish to NPM。发布后,我可以像这样导入:

import { compTable, compYearmonth } from "@comp-webcomponents";

您还可以查看 Stencil,这是一个生成可重用 Web 组件的编译器。

这是我的 Web 组件的样子:

export class ExampleComponent extends HTMLElement {

  constructor() {
    super();

    const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(ExampleComponent.template.content.cloneNode(true));        
    }

    connectedCallback() {
      console.log('hello');
    }
}

    ExampleComponent.tag = 'example-component';
    ExampleComponent.template = document.createElement('template');
    ExampleComponent.template.innerHTML = `
    <template>
      <p>hello</p>
    </template>
    `;
    customElements.define(ExampleComponent.tag, ExampleComponent);

【讨论】:

  • 我们没有使用 Typescript,但还是很有趣的方法。今天我试着改变comp-webcomponents.js,就像你建议的那样:我把那行改为export { compTabs } from 'components/tabs/comp-tabs';,而不是import 'components/tabs/comp-tabs';。那种工作,但是当我将所有导入语句更改为导出语句时,我总是收到整个包,即使在一个新文件中我只做了import { compTabs } from './comp-webcomponents';
  • 我必须如何调整示例组件才能使这种方法有效?
  • Stencil 不是我们项目的选项,因为我们 95% 的 Web 组件都是定制的内置插件,Polymer 和 Stencil 都不支持。
  • 嗯在每个单独的组件中都使用导出默认值?
  • 组件看起来像我在问题中显示的那个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多