Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dcasia/mini-program-tailwind-webpack-plugin

Package Overview
Dependencies
Maintainers
2
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dcasia/mini-program-tailwind-webpack-plugin - npm Package Compare versions

Comparing version 1.2.3 to 1.3.0-beta.0

dist/taro.js

82

dist/index.js
'use strict';
var universalHandler = require('./universal-handler');
var webpackSources = require('webpack-sources');

@@ -36,9 +37,17 @@ /*! *****************************************************************************

})(FileType || (FileType = {}));
var FrameworkUsedInTaro;
(function (FrameworkUsedInTaro) {
FrameworkUsedInTaro["React"] = "react";
FrameworkUsedInTaro["Vue2"] = "vue2";
FrameworkUsedInTaro["Vue3"] = "vue3";
})(FrameworkUsedInTaro || (FrameworkUsedInTaro = {}));
var _a;
var regExpStyle = /.+\.(?:wx|ac|jx|tt|q|c)ss$/;
var regExpTemplate = /.+\.(wx|ax|jx|ks|tt|q)ml$/;
function isStyleFile(filename) {
return /.+\.(?:wx|ac|jx|tt|q|c)ss$/.test(filename);
return regExpStyle.test(filename);
}
function isTemplateFile(filename) {
return /.+\.(wx|ax|jx|ks|tt|q)ml$/.test(filename);
return regExpTemplate.test(filename);
}

@@ -60,27 +69,52 @@ (_a = {},

var _this = this;
var webpack = compiler.webpack;
var sources = webpack.sources, Compilation = webpack.Compilation;
var ConcatSource = sources.ConcatSource;
compiler.hooks.thisCompilation.tap(MiniProgramTailwindWebpackPlugin.pluginName, function (compilation) {
compilation.hooks.processAssets.tap({
name: MiniProgramTailwindWebpackPlugin.pluginName,
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
}, function (assets) {
for (var pathname in assets) {
var originalSource = assets[pathname];
var rawSource = originalSource.source().toString();
var handledSource = '';
if (isStyleFile(pathname)) {
handledSource = universalHandler.handleSource(FileType.Style, rawSource, _this.options);
var isWebpackV5 = compiler.webpack && compiler.webpack.version >= '5';
if (isWebpackV5) {
var webpack = compiler.webpack;
var sources = webpack.sources, Compilation_1 = webpack.Compilation;
var ConcatSource_1 = sources.ConcatSource;
compiler.hooks.thisCompilation.tap(MiniProgramTailwindWebpackPlugin.pluginName, function (compilation) {
compilation.hooks.processAssets.tap({
name: MiniProgramTailwindWebpackPlugin.pluginName,
stage: Compilation_1.PROCESS_ASSETS_STAGE_SUMMARIZE
}, function (assets) {
for (var pathname in assets) {
var originalSource = assets[pathname];
var rawSource = originalSource.source().toString();
var handledSource = '';
if (isStyleFile(pathname)) {
handledSource = universalHandler.handleSource(FileType.Style, rawSource, _this.options);
}
else if (isTemplateFile(pathname)) {
handledSource = universalHandler.handleSource(FileType.Template, rawSource, _this.options);
}
if (handledSource) {
var source = new ConcatSource_1(handledSource);
compilation.updateAsset(pathname, source);
}
}
else if (isTemplateFile(pathname)) {
handledSource = universalHandler.handleSource(FileType.Template, rawSource, _this.options);
});
});
}
else {
compiler.hooks.thisCompilation.tap(MiniProgramTailwindWebpackPlugin.pluginName, function (compilation) {
compilation.hooks.afterOptimizeAssets.tap(MiniProgramTailwindWebpackPlugin.pluginName, function (assets) {
for (var pathname in assets) {
var originalSource = assets[pathname];
var rawSource = originalSource.source().toString();
var handledSource = '';
if (isStyleFile(pathname)) {
handledSource = universalHandler.handleSource(FileType.Style, rawSource, _this.options);
}
else if (isTemplateFile(pathname)) {
handledSource = universalHandler.handleSource(FileType.Template, rawSource, _this.options);
}
if (handledSource) {
var source = new webpackSources.ConcatSource(handledSource);
// @ts-ignore
compilation.updateAsset(pathname, source);
}
}
if (handledSource) {
var source = new ConcatSource(handledSource);
compilation.updateAsset(pathname, source);
}
}
});
});
});
}
};

@@ -87,0 +121,0 @@ MiniProgramTailwindWebpackPlugin.pluginName = 'mini-program-tailwind-webpack-plugin';

@@ -7,2 +7,3 @@ 'use strict';

var wxml = require('@vivaxy/wxml');
var babel = require('@babel/core');

@@ -31,2 +32,3 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var wxml__namespace = /*#__PURE__*/_interopNamespace(wxml);
var babel__namespace = /*#__PURE__*/_interopNamespace(babel);

@@ -38,4 +40,10 @@ var FileType;

})(FileType || (FileType = {}));
var FrameworkUsedInTaro;
(function (FrameworkUsedInTaro) {
FrameworkUsedInTaro["React"] = "react";
FrameworkUsedInTaro["Vue2"] = "vue2";
FrameworkUsedInTaro["Vue3"] = "vue3";
})(FrameworkUsedInTaro || (FrameworkUsedInTaro = {}));
var _a;
var _a$1;
var charactersMap = {

@@ -56,6 +64,6 @@ '[': '-',

};
var backslashMap = (_a = {},
_a[FileType.Style] = '\\\\\\',
_a[FileType.Template] = '\\',
_a);
var backslashMap = (_a$1 = {},
_a$1[FileType.Style] = '\\\\\\',
_a$1[FileType.Template] = '\\',
_a$1);
function handleCharacters(content, type) {

@@ -157,2 +165,8 @@ for (var from in charactersMap) {

var _a;
(_a = {},
_a[FrameworkUsedInTaro.React] = ['className'],
_a[FrameworkUsedInTaro.Vue2] = ['class', 'staticClass'],
_a[FrameworkUsedInTaro.Vue3] = ['class', 'staticClass'],
_a);
function replaceStringLiteralPlugin(_a) {

@@ -163,5 +177,8 @@ var t = _a.types;

StringLiteral: function (path) {
var newContent = handleCharacters(path.node.value, FileType.Template);
path.replaceWith(t.stringLiteral(newContent));
path.skip();
var rawContent = path.node.value;
var newContent = handleCharacters(rawContent, FileType.Template);
if (newContent !== rawContent) {
path.replaceWith(t.stringLiteral(newContent));
path.skip();
}
}

@@ -172,3 +189,2 @@ }

var babel = require('@babel/core');
var matchScriptsInsideClassNames = /{{.+?}}/g;

@@ -190,6 +206,7 @@ var replaceMarker = '__MP_TW_PLUGIN_REPLACE__';

var script = scriptsMatchResults_1[_i];
var output = babel.transformSync(script[0], {
var output = babel__namespace.transformSync(script[0], {
generatorOpts: {
minified: true
},
configFile: false,
plugins: [replaceStringLiteralPlugin]

@@ -215,3 +232,8 @@ });

}
var universalHandler = {
handleStyle: handleStyle,
handleTemplate: handleTemplate
};
exports["default"] = universalHandler;
exports.handleSource = handleSource;
{
"name": "@dcasia/mini-program-tailwind-webpack-plugin",
"version": "1.2.3",
"version": "1.3.0-beta.0",
"description": "",

@@ -10,3 +10,3 @@ "main": "dist/index.js",

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest",
"lint": "eslint . --ext .ts --fix",

@@ -28,3 +28,6 @@ "build": "rollup --config rollup.config.ts",

"@rollup/plugin-commonjs": "^21.0.2",
"@rollup/plugin-multi-entry": "^4.1.0",
"@rollup/plugin-typescript": "^8.3.1",
"@tarojs/service": "^3.4.9",
"@types/jest": "^27.4.1",
"@typescript-eslint/eslint-plugin": "^5.13.0",

@@ -36,3 +39,5 @@ "@typescript-eslint/parser": "^5.13.0",

"eslint-plugin-import": "^2.25.4",
"rollup": "^2.68.0"
"jest": "^27.5.1",
"rollup": "^2.68.0",
"webpack": "^5.69.1"
},

@@ -43,4 +48,5 @@ "dependencies": {

"postcss": "^8.4.7",
"webpack": "^5.69.1"
"windicss-webpack-plugin": "^1.7.2",
"webpack-sources": "^1.4.3"
}
}

@@ -26,9 +26,9 @@ ![wechat-mp-plugin](https://user-images.githubusercontent.com/3030587/158826917-30c79222-b79d-4857-88f9-7e4184d1e771.jpg)

相关文章:[让你的小程序用上原汁原味的 Tailwind/Windi CSS](https://juejin.cn/post/7093809282272985119/)
- - -
## 快速开始
### 基于原生小程序
**以下示范操作步骤以集成 Windi CSS 为例*
待更新...
### 基于 MPX 框架

@@ -48,2 +48,3 @@

//...
prefixer: false,
extract: {

@@ -54,2 +55,6 @@ // 将 .mpx 文件纳入范围

exclude: ['node_modules', '.git', 'dist']
},
corePlugins: {
// 禁用掉在小程序环境中不可能用到的 plugins
container: false
}

@@ -94,4 +99,37 @@ //...

[示例项目](./examples/mpx)
#### 案例
[MPX 集成案例](./examples/mpx)
### 基于原生小程序
基于原生小程序的开发模式来集成这款插件,过程通常因每个团队的工作流不同而异。有的团队会有内部定制的一套 Webpack 或 Gulp 工作流,而有的团队甚至不会借助任何文件打包或处理的工作流去编写小程序。
但这里需要明确的一点是,若要想在以原生开发模式的基础之上去增加文件处理的功能,我们必须去额外的启动一套文件监听处理服务,这个服务通常由自定义配置好的 Webpack, Gulp 等第三方工具完成。
为了使这款插件具备超出 Webpack 适配范围之外尽可能大的兼容性,我们将核心功能分离并打包进了 `dist/universal-handler.js` 文件中,若你想在自己的工作流中使用该插件的核心功能,可以先在工作流逻辑中引入 `universal-handler`:
```javascript
const { handleSource } = require('@dcasia/mini-program-tailwind-webpack-plugin/dist/universal-handler')
```
随后处理 template:
```javascript
const template = '<view class="w-10 h-[0.5px] text-[#ffffff]"></view>'
const handledTemplate = handleSource('template', template, options)
```
处理 style:
```javascript
const style = '.h-\\[0\\.5px\\] {height: 0.5px;}'
const handledStyle = handleSource('style', style, options)
```
此后你便可以将处理过的字符串返回至工作流原本的流程中来生成最终的文件。
> 对于集成过程中涉及到其他方面的细节可参考[小程序集成 Windi CSS 的自定义实现](https://juejin.cn/post/7093809282272985119#heading-5)
#### 案例
[原生小程序集成案例(基于 Gulp)](./examples/native)
- - -

@@ -106,2 +144,15 @@ ## 可配置参数

- - -
## 陷阱
- 在小程序中为了使组件样式可以被 Tailwind/Windi 的 CSS 产物作用到,我们需要在每一个组件的 JSON 配置文件中设置其[样式的作用域](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html#%E7%BB%84%E4%BB%B6%E6%A0%B7%E5%BC%8F%E9%9A%94%E7%A6%BB) `styleIsolation`,否则即使 Tailwind/Windi CSS 工作正常也无法用来开发组件 UI。([Issue#1](https://github.com/dcasia/wechat-mini-program-tailwind/issues/1))
```json
{
"component": true,
"styleIsolation": "apply-shared"
}
```
- 由于目前微信开发者工具的热重载功能无法监听到样式文件内由 `@import` 导入的 wxss 文件内容的变动,所以当启用热重载功能开发时,模拟器不会随着你对 Tailwind/Windi CSS 的更改而更新 UI。目前微信官方已知晓该 bug 的存在,在该 bug 修复之前,我们建议你在开发时关闭热重载,用传统的页面自动刷新来预览每一次的 UI 更新。([Issue#3](https://github.com/dcasia/wechat-mini-program-tailwind/issues/3))
- - -
## FAQ

@@ -108,0 +159,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc