{"_id":"@canvases/ifdef-loader","_rev":"2-e78ba1dbeca6a4bfae07019f23d93fd9","name":"@canvases/ifdef-loader","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@canvases/ifdef-loader","version":"1.0.0","keywords":["multi-project","config-loader","ifdef","project management"],"author":{"name":"canvases"},"license":"MIT","_id":"@canvases/ifdef-loader@1.0.0","maintainers":[{"name":"canvases","email":"canvases@163.com"}],"dist":{"shasum":"7c7ffa4dcef4af35d4e48cdfe2cf63f542ae19ab","tarball":"https://registry.npmjs.org/@canvases/ifdef-loader/-/ifdef-loader-1.0.0.tgz","fileCount":5,"integrity":"sha512-FCLqvPbY+vRIuzAAyA4OyZFFYRcQnEyQMfiPu7jrDCVyl+VFu2YLAhmpjJeAt5V9hWHoJ+mzNRIy8Xk/JgZAvA==","signatures":[{"sig":"MEUCIHqge64ieSgl0SmH4kqYcguYUKTdd098NBdcqQxC+qoVAiEAs7BNpjeZMAkpqdUlDICOH9VRMnzPPfIgj9zFZ/uf6H0=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":5847},"main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"canvases","email":"canvases@163.com"},"_npmVersion":"10.8.2","description":"\"A loader used to distinguish and manage the configurations of multiple projects. It utilizes the ifdef conditional compilation directive and clearly marks the configurations of different projects through comments, facilitating development and compilation","directories":{},"_nodeVersion":"18.20.5","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/ifdef-loader_1.0.0_1742187408167_0.7643817666625172","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@canvases/ifdef-loader","version":"1.0.1","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["multi-project","config-loader","ifdef","project management"],"author":{"name":"canvases"},"license":"MIT","description":"\"A loader used to distinguish and manage the configurations of multiple projects. It utilizes the ifdef conditional compilation directive and clearly marks the configurations of different projects through comments, facilitating development and compilation","_id":"@canvases/ifdef-loader@1.0.1","_nodeVersion":"18.20.5","_npmVersion":"10.8.2","dist":{"integrity":"sha512-lvVEae1vQXOppJMcS6aYSmMVPugv+xiFA3SqZ0ThOf8Ml3w1HDPU5P45PkfbYLH5qJoXDZbHhKF4qR3YHxbfyA==","shasum":"1032a3ef2225cb3bd1a6d7595c272757d725fe63","tarball":"https://registry.npmjs.org/@canvases/ifdef-loader/-/ifdef-loader-1.0.1.tgz","fileCount":6,"unpackedSize":9061,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCICv9MyIuFxDVu7in5JmJVG1Ct1ciIE0i3A2VM0dak5XgAiAs+tqyNnk62/2rg6+0bJ604y+QRagIER06k9ZNMhRpkQ=="}]},"_npmUser":{"name":"canvases","email":"canvases@163.com"},"directories":{},"maintainers":[{"name":"canvases","email":"canvases@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/ifdef-loader_1.0.1_1742189039117_0.848779160249491"},"_hasShrinkwrap":false}},"time":{"created":"2025-03-17T04:56:48.069Z","modified":"2025-03-17T05:23:59.472Z","1.0.0":"2025-03-17T04:56:48.360Z","1.0.1":"2025-03-17T05:23:59.303Z"},"author":{"name":"canvases"},"license":"MIT","keywords":["multi-project","config-loader","ifdef","project management"],"description":"\"A loader used to distinguish and manage the configurations of multiple projects. It utilizes the ifdef conditional compilation directive and clearly marks the configurations of different projects through comments, facilitating development and compilation","maintainers":[{"name":"canvases","email":"canvases@163.com"}],"readme":"### **README: UniApp 条件编译 Loader for Webpack + Vue2**\r\n\r\n本项目提供了一个 Webpack Loader，用于在 Vue2 项目中模拟 **UniApp** 的条件编译功能。它允许你在 Web 和不同平台（如 `APP-PLUS`、`H5`、`MP-WEIXIN` 等）之间切换不同的业务代码。\r\n\r\n---\r\n\r\n## **📌 功能特点**\r\n✅ **支持 `#ifdef` / `#ifndef` 条件编译**，根据 `platform` 变量选择代码块。  \r\n✅ **支持 `#elseif` / `#else`**，确保只有一个分支生效。  \r\n✅ **支持 `||` 逻辑运算**，如 `APP-PLUS || H5`。  \r\n✅ **适用于 Webpack + Vue2 项目**，可在**运行**和**打包**时自动处理条件编译。\r\n\r\n---\r\n\r\n## **📂 使用示例**\r\n\r\n在 Vue2 组件或 JavaScript 代码中，你可以使用 UniApp 风格的条件编译语法：\r\n\r\n```js\r\n// #ifdef APP-PLUS\r\nconsole.log('This is for APP-PLUS');\r\n// #elseif H5\r\nconsole.log('This is for H5');\r\n// #else\r\nconsole.log('This is for other platforms');\r\n// #endif\r\n```\r\n\r\n\r\n``` css\r\n.cColor {\r\n  color: #ffc400;\r\n\r\n  /* #ifdef  APP-PLUS */\r\n  background: #42b983;\r\n  /* #elseif H5  */\r\n  background: #0077ff;\r\n  /* #else */\r\n  background: red;\r\n  /* #endif */\r\n}\r\n```\r\n```html\r\n<!-- #ifdef APP-PLUS -->\r\n<div class=\"XAM-only\">if APP-PLUS平台显示</div>\r\n<!--  #elseif  H5 || MP-WEIXIN  -->\r\n<div class=\"app-only\">elseif MP-WEIXIN || H5 平台显示</div>\r\n<!--  #else -->\r\n<div class=\"app-only\">else 平台显示</div>\r\n<!-- #endif -->\r\n```\r\n\r\n---\r\n\r\n## **⚙ 安装与配置**\r\n\r\n### **1️⃣ 安装 Loader**\r\n```sh\r\nnpm install @canvases/ifdef-loader\r\n```\r\n\r\n---\r\n\r\n### **2️⃣ 配置 Webpack**\r\n在 `webpack.config.js` 中添加 Loader 配置：\r\n\r\n```js\r\nmodule.exports = {\r\n  module: {\r\n    rules: [\r\n        // 处理 Vue 文件\r\n        {\r\n            test: /\\.vue$/,\r\n            use: [\r\n\r\n                {\r\n                    loader: '@canvases/ifdef-loader',\r\n                    options: { platform: process.env.PLATFORM } // 注入环境变量\r\n                }\r\n            ]\r\n        },\r\n        // 处理 JS 文件\r\n        {\r\n            test: /\\.js$/,\r\n            use: [\r\n\r\n                {\r\n                    loader: '@canvases/ifdef-loader',\r\n                    options: { platform: process.env.PLATFORM } // 注入环境变量\r\n                }\r\n            ]\r\n        },\r\n        // 处理 CSS/SCSS 文件\r\n        {\r\n            test: /\\.(css|scss)$/,\r\n            use: [\r\n\r\n                {\r\n                    loader: '@canvases/ifdef-loader',\r\n                    options: { platform: process.env.PLATFORM } // 注入环境变量\r\n                }\r\n        }\r\n    ]\r\n  }\r\n};\r\n```\r\n\r\n在 `package.json` 中添加不同平台的构建命令：\r\n\r\n```json\r\n\"scripts\": {\r\n  \"serve:h5\": \"cross-env PLATFORM=H5 vue-cli-service serve\",\r\n  \"build:h5\": \"cross-env PLATFORM=H5 webpack --mode production\",\r\n  \"build:app\": \"cross-env PLATFORM=APP-PLUS webpack --mode production\",\r\n  \"build:wx\": \"cross-env PLATFORM=MP-WEIXIN webpack --mode production\"\r\n}\r\n```\r\n\r\n然后，你可以运行：\r\n```sh\r\nnpm run serve:h5    # 运行 H5 代码\r\nnpm run build:h5    # 生成 H5 代码\r\nnpm run build:app   # 生成 App 代码\r\nnpm run build:wx    # 生成小程序代码\r\n```\r\n\r\n","readmeFilename":"README.md"}