使用rollup打包组件说明:

    需要使用rollup的插件机制来对各种文件进行打包。

    打包Vue组件库需要的插件和配置大致如下

    1. import { babel } from "@rollup/plugin-babel";
    2. import resolve from "@rollup/plugin-node-resolve";
    3. import vue from "rollup-plugin-vue";
    4. import cjs from "@rollup/plugin-commonjs";
    5. import replace from "@rollup/plugin-replace";
    6. import json from "@rollup/plugin-json";
    7. import requireContext from "rollup-plugin-require-context";
    8. import { string } from "rollup-plugin-string";
    9. import autoprefixer from "autoprefixer";
    10. import typescript from "rollup-plugin-typescript2";
    11. const config = require("../package.json");
    12. export default {
    13. input: "src/index.js",
    14. plugins: [
    15. json(),
    16. resolve({
    17. mainFields: ["module", "jsnext", "main", "browser"],
    18. jsnext: true,
    19. preferBuiltins: true,
    20. browser: true,
    21. }),
    22. string({
    23. include: "**/*.svg",
    24. }),
    25. vue({
    26. css: false,
    27. style: {
    28. postcssPlugins: [autoprefixer],
    29. },
    30. }),
    31. babel({
    32. exclude: "node_modules/**",
    33. babelHelpers: "runtime",
    34. presets: [
    35. [
    36. "@babel/env",
    37. {
    38. modules: false,
    39. },
    40. ],
    41. ],
    42. plugins: [
    43. [
    44. "@babel/plugin-transform-runtime",
    45. {
    46. useESModules: true, // 使用 esm 形式的 helper
    47. },
    48. ],
    49. ],
    50. }),
    51. cjs({
    52. browser: true,
    53. exclude: "src/**",
    54. }),
    55. requireContext(),
    56. replace({
    57. // VERSION: JSON.stringify(config.version),
    58. preventAssignment: true,
    59. }),
    60. typescript({
    61. tsconfig: "build/tsconfigComponent.json",
    62. useTsconfigDeclarationDir: true,
    63. }), // 解析TypeScript
    64. ],
    65. external: ["vue"],
    66. };

    后续待更新