简体   繁体   English

使用 IIFE 创建独立 web 组件构建

[英]Creating a standalone web component build using as an IIFE

I have create a web component for displaying gists generally in any html content.我创建了一个 web 组件,用于在任何 html 内容中显示要点。

I used the Lit Element Typescript Starter Project as a baseline and it comes with a rollup.config.js file.我使用Lit Element Typescript Starter Project作为基准,它带有一个rollup.config.js文件。

I changed the output format to iife and left the rest the same, with exception of the component and bundle names.我将 output 格式更改为iife并将 rest 保持不变,但组件和捆绑包名称除外。 The reason I did this is that I wanted the bundle to be easily accessible via script tags, and rollup says that the iife format does this.我这样做的原因是我希望通过脚本标签可以轻松访问捆绑包,并且汇总说iife格式可以做到这一点。

This is the modified rollup.config.js file . 这是修改后的rollup.config.js文件

// ============================================
// The configuration is based on the rollup starter
// app found here:
//
// https://github.com/rollup/rollup-starter-app/blob/master/package.json
//
// This project is based
// ============================================


/**
 * @license
 * Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * The complete set of authors may be found at
 * http://polymer.github.io/AUTHORS.txt
 * The complete set of contributors may be found at
 * http://polymer.github.io/CONTRIBUTORS.txt
 * Code distributed by Google as part of the polymer project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import {terser} from 'rollup-plugin-terser';
import replace from '@rollup/plugin-replace';
import filesize from 'rollup-plugin-filesize';

// `npm run build` -> `production` is true
// `npm run dev` -> `production` is false
const production = !process.env.ROLLUP_WATCH;

export default {
  input: 'fs-gist.js',
  output: {
    file: 'fs-gist.bundle.js',
    format: 'iife', // immediately-invoked function expression — suitable for <script> tags
    sourcemap: true,
  },
  onwarn(warning) {
    if (warning.code !== 'THIS_IS_UNDEFINED') {
      console.error(`(!) ${warning.message}`);
    }
  },
  plugins: [
    replace({'Reflect.decorate': 'undefined'}),
    resolve(), // tells Rollup how to find date-fns in node_modules
    commonjs(), // converts date-fns to ES modules
    production && terser({
        module: true,
        warnings: true,
        mangle: {
          properties: {
            regex: /^__/,
          },
        },
      }),
      filesize({
        showBrotliSize: true,
      })
  ],
};

The build seems to be working fine.构建似乎工作正常。 There's a demo here:这里有一个演示:

https://stackblitz.com/edit/typescript-fs-gist?file=index.tshttps://stackblitz.com/edit/typescript-fs-gist?file=index.ts

Just curious if anyone knows whether any of the other rollup settings should be tweaked or changed since I changed the format to iife from esm ?只是好奇是否有人知道是否应该调整或更改任何其他汇总设置,因为我将格式从iife更改为esm

Rollup configs really depend on what you want to do.汇总配置实际上取决于您想要做什么。 If it works currently for what you want to do, then that's great and nothing needs to be changed.如果它目前适用于您想要做的事情,那很好,无需更改任何内容。

Since it's a config file, if it's not working, everything else is up to you and what you want out of it.由于它是一个配置文件,如果它不工作,其他一切都取决于你和你想要什么。 For example, maybe if you want to make it work on older browsers, you would use the plugin @rollup/plugin-babel to transpile your code.例如,如果你想让它在旧浏览器上运行,你会使用插件@rollup/plugin-babel来转译你的代码。 If you want to offer it as umd and es, you could add those to the build steps.如果您想以 umd 和 es 的形式提供它,您可以将它们添加到构建步骤中。

The rollup documentation is pretty extensive, and you should look through what's possible: https://rollupjs.org/guide/en/汇总文档非常广泛,您应该查看可能的内容: https://rollupjs.org/guide/en/

Once you have a better idea of your project's needs, you can search through the docs for examples of how to add specific plugins, steps, etc.一旦您对项目的需求有了更好的了解,您就可以在文档中搜索如何添加特定插件、步骤等的示例。

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

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