index.js 5.59 KB
const path = require('path')
const fs = require('fs')
const vantComponentNames = require("../vantComponentConf")

const handleGetPatterns = (keyArr) => {
  // 这些为公共组件
  const defaultArr = [
    {
      from: "src/components/vant-weapp/wxs",
      to: "dist/components/vant-weapp/wxs",
    },
    {
      from: "src/components/vant-weapp/common",
      to: "dist/components/vant-weapp/common",
    },
    {
      from: "src/components/vant-weapp/share-sheet/options.wxs",
      to: "dist/components/vant-weapp/share-sheet/options.wxs",
    },
  ];

  const componentArr = keyArr
    .map((componentName) => {
      // 同步检测这个文件是否存在
      // 因为vant weapp部分组件文件下的不存在 wxs 文件
      const componentPath = path.resolve(
        __dirname,
        `../src/components/vant-weapp/${componentName}/index.wxs`
      );

      const isState = fs.statSync(componentPath, { throwIfNoEntry: false });

      return [
        {
          from: `src/components/vant-weapp/${componentName}/index.${isState ? "wxs" : "wxss"
            }`,
          to: `dist/components/vant-weapp/${componentName}/index.${isState ? "wxs" : "wxss"
            }`,
        },
        {
          from:
            "src/components/vant-weapp/" + componentName + "/index.wxml",
          to:
            "dist/components/vant-weapp/" + componentName + "/index.wxml",
        },
      ];
    })
    .flat(Infinity);

  return [...defaultArr, ...componentArr];
};

const config = {
  projectName: 'swx-weapp',
  date: '2022-10-01',
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2
  },
  alias: {
    "@/utils": path.resolve(__dirname, "../src/utils"),
    "@/vant": path.resolve(__dirname, "../src/components/vant-weapp"),
    "@/components": path.resolve(__dirname, "../src/components"),
    "@/images": path.resolve(__dirname, "../src/assets/images"),
    "@/assets": path.resolve(__dirname, "../src/assets"),
    "@/composables": path.resolve(__dirname, "../src/composables"),
    "@/api": path.resolve(__dirname, "../src/api"),
    "@/stores": path.resolve(__dirname, "../src/stores"),
    "@/hooks": path.resolve(__dirname, "../src/hooks"),
  },
  sourceRoot: 'src',
  outputRoot: 'dist',
  // outputRoot: `dist/${process.env.TARO_ENV}`,
  plugins: [
    'taro-plugin-pinia',
    "tarojs-plugin-platform-miniprogram", // 自定义 wxml 支持
    ['@tarojs/plugin-framework-vue3', {
      mini: {
        compilerOptions: {
          isCustomElement: tag => ["van-tab", "van-tabs"].includes(tag),
          whitespace: 'preserve'
        }
      }
    }],
    '@tarojs/plugin-html'
  ],
  defineConstants: {
  },
  copy: {
    patterns: handleGetPatterns(vantComponentNames),
    // patterns: [
    //   // 前面2个是基础的
    //   {
    //     from: "src/components/vant-weapp/wxs",
    //     to: "dist/components/vant-weapp/wxs",
    //   },
    //   {
    //     from: "src/components/vant-weapp/common",
    //     to: "dist/components/vant-weapp/common",
    //   },
    //   // 需要用到vant哪个组件就引入
    //   {
    //     from: "src/components/vant-weapp/button",
    //     to: "dist/components/vant-weapp/button",
    //   },
    //   {
    //     from: "src/components/vant-weapp/info",
    //     to: "dist/components/vant-weapp/info",
    //   },
    //   {
    //     from: "src/components/vant-weapp/icon",
    //     to: "dist/components/vant-weapp/icon",
    //   },
    //   {
    //     from: "src/components/vant-weapp/loading",
    //     to: "dist/components/vant-weapp/loading",
    //   },
    //   {
    //     from: "src/components/vant-weapp/tab",
    //     to: "dist/components/vant-weapp/tab",
    //   },
    //   {
    //     from: "src/components/vant-weapp/tabs",
    //     to: "dist/components/vant-weapp/tabs",
    //   },
    //   {
    //     from: "src/components/vant-weapp/sticky",
    //     to: "dist/components/vant-weapp/sticky",
    //   },
    // ],
    options: {
    }
  },
  framework: 'vue3',
  mini: {
    optimizeMainPackage: {
      enable: true
    },
    webpackChain(chain) {
      chain.merge({
        module: {
          rule: {
            mjsScript: {
              test: /\.mjs$/,
              include: [/pinia/],
              use: {
                babelLoader: {
                  loader: require.resolve('babel-loader')
                }
              }
            }
          }
        }
      })
    },
    postcss: {
      pxtransform: {
        enable: true,
        config: {
          // 过滤 vant 组件库的前缀:van-
          selectorBlackList: [/van-/],
        }
      },
      url: {
        enable: true,
        config: {
          limit: 1024 // 设定转换尺寸上限
        }
      },
      cssModules: {
        enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
        config: {
          namingPattern: 'module', // 转换模式,取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    }
  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {
        }
      },
      cssModules: {
        enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
        config: {
          namingPattern: 'module', // 转换模式,取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    }
  }
}

module.exports = function (merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  return merge({}, config, require('./prod'))
}