Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add miniprogram plugin #909

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/knip/fixtures/plugins/miniprogram/app-project/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
App({
onLaunch() {
// Create worker
const worker = wx.createWorker('workers/request');

worker.onMessage((msg) => {
console.log(msg);
});
}
});
23 changes: 23 additions & 0 deletions packages/knip/fixtures/plugins/miniprogram/app-project/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"pages": [
"pages/index/index"
],
"window": {
"navigationBarTitleText": "Mini Program"
},
"usingComponents": {
"missing-header": "/components/missing-header/index",
"used-footer": "/components/used-footer/index"
},
"workers": "workers/missing-worker.ts",
"tabBar": {
"list": [
{
"pagePath": "pages/index/index",
"text": "Home",
"iconPath": "images/missing-icon.png",
"selectedIconPath": "images/used-icon.png"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import "/styles/missing-theme";

page {
background: #f8f8f8;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Component({
properties: {
type: String,
text: String
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"component": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"component": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface IProps {
count: number;
onIncrement: () => void;
}

Component<IProps>({
properties: {
count: Number
},
methods: {
handleTap() {
this.triggerEvent('increment');
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Component({
properties: {
text: String
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"component": true
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://unpkg.com/knip@2/schema.json",
"paths": {
"@components/*": ["components/*"],
"@utils/*": ["utils/*"],
"@styles/*": ["styles/*"],
"~/*": ["*"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "miniprogram-test",
"version": "1.0.0",
"devDependencies": {
"miniprogram-api-typings": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var common = require('./missing-common.wxs');
var usedDate = require('./used-date.wxs');

module.exports = {
format: common.format,
usedDate: usedDate.format
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"usingComponents": {
"missing-button": "@components/missing-button",
"missing-helper": "~/utils/missing-helper",
"missing-footer": "./missing-footer",
"used-counter": "../used-counter"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { formatDate } from '~/utils/missing-date';

Page({
data: {
now: Date.now(),
text: ''
},
onLoad() {
this.setData({
text: formatDate(this.data.now)
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<wxs src="/utils/missing-format.wxs" module="format" />
<wxs src="./helper.wxs" module="helper" />

<import src="/templates/missing-list.wxml" />
<import src="/templates/used-list.wxml" />

<include src="/templates/used-item.wxml" />
<include src="/templates/missing-item.wxml" />

<view class="container">
<image src="/images/missing-logo.png" />
<image src="/images/used-logo.png" />

<template is="missing-list" data="{{items}}" />
<template is="used-item" data="{{text}}" />
</view>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import "/styles/missing-theme.wxss";
@import "/styles/used-theme.wxss";

.container {
padding: 20px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function format(timestamp) {
var date = getDate(timestamp);
return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
}

module.exports = {
format: format
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"desc": "Configuration for WeChat search crawling",
"rules": [
{
"action": "allow",
"page": "pages/index/index",
"params": ["id"],
"matching": "exact"
},
{
"action": "disallow",
"page": "pages/private/*"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.unused-theme {
color: #333;
background: #fff;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.unused-theme {
color: #333;
background: #fff;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template name="unused-card">
<view class="card">
<view class="title">{{title}}</view>
<view class="content">{{content}}</view>
</view>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template name="used-item">
<view class="item">{{text}}</view>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function format(number) {
return number.toFixed(2);
}

module.exports = {
format: format
};
4 changes: 4 additions & 0 deletions packages/knip/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@
"title": "metro plugin configuration (https://knip.dev/reference/plugins/metro)",
"$ref": "#/definitions/plugin"
},
"miniprogram": {
"title": "miniprogram plugin configuration (https://knip.dev/reference/plugins/miniprogram)",
"$ref": "#/definitions/plugin"
},
"mocha": {
"title": "Mocha plugin configuration (https://knip.dev/reference/plugins/mocha)",
"$ref": "#/definitions/plugin"
Expand Down
5 changes: 5 additions & 0 deletions packages/knip/src/compilers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RawConfiguration } from '../types/config.js';
import type { DependencySet } from '../types/workspace.js';
import Astro from './astro.js';
import MDX from './mdx.js';
import Miniprogram from './miniprogram.js';
import Svelte from './svelte.js';
import type { AsyncCompilerFn, AsyncCompilers, SyncCompilerFn, SyncCompilers } from './types.js';
import Vue from './vue.js';
Expand Down Expand Up @@ -37,6 +38,10 @@ const compilers = new Map([
['.mdx', MDX],
['.svelte', Svelte],
['.vue', Vue],
['.wxml', Miniprogram.wxml],
['.wxss', Miniprogram.wxss],
['.wxs', Miniprogram.wxs],
['.json', Miniprogram.json],
]);

export const getIncludedCompilers = (
Expand Down
24 changes: 24 additions & 0 deletions packages/knip/src/compilers/miniprogram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { parseWxml, parseWxs, parseWxss } from '../plugins/miniprogram/parser.js';
import type { HasDependency } from './types.js';

// Enable compilers when miniprogram-api-typings is present (indicating a Mini Program project)
const isMiniprogramProject = (hasDependency: HasDependency) => {
return hasDependency('miniprogram-api-typings');
};

// We don't parse JSON files in the compiler because:
// 1. When a component path is referenced (e.g. "./foobar"), we can't determine if it's:
// - "./foobar.json" (single file) or
// - "./foobar/index.json" (directory)
// 2. The only purpose of this JSON compiler is to make getCompilerExtensions include
// JSON files as project files, so they can be analyzed for usage
const parseJson = (text: string) => {
return text;
};

const wxml = { condition: isMiniprogramProject, compiler: parseWxml };
const wxss = { condition: isMiniprogramProject, compiler: parseWxss };
const wxs = { condition: isMiniprogramProject, compiler: parseWxs };
const json = { condition: isMiniprogramProject, compiler: parseJson };

export default { wxml, wxss, wxs, json };
2 changes: 2 additions & 0 deletions packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { default as lockfileLint } from './lockfile-lint/index.js';
import { default as lostPixel } from './lost-pixel/index.js';
import { default as markdownlint } from './markdownlint/index.js';
import { default as metro } from './metro/index.js';
import { default as miniprogram } from './miniprogram/index.js';
import { default as mocha } from './mocha/index.js';
import { default as moonrepo } from './moonrepo/index.js';
import { default as msw } from './msw/index.js';
Expand Down Expand Up @@ -123,6 +124,7 @@ export const Plugins = {
'lost-pixel': lostPixel,
markdownlint,
metro,
miniprogram,
mocha,
moonrepo,
msw,
Expand Down
Loading