create-rezor 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +25 -0
- package/outfile.js +368 -0
- package/package.json +35 -7
- package/template/base/_editorconfig +9 -0
- package/template/base/_gitignore +6 -0
- package/template/base/_npmrc +1 -0
- package/template/base/package.json.ejs +102 -0
- package/template/base/postcss.config.js +17 -0
- package/template/base/project.config.json +69 -0
- package/template/base/src/app.css +18 -0
- package/template/base/src/app.json +35 -0
- package/template/base/src/components/count-button/index.html +3 -0
- package/template/base/src/components/count-button/index.json +3 -0
- package/template/base/src/images/home-dark-selected.png +0 -0
- package/template/base/src/images/home-dark.png +0 -0
- package/template/base/src/images/home-light-selected.png +0 -0
- package/template/base/src/images/home-light.png +0 -0
- package/template/base/src/images/mine-dark-selected.png +0 -0
- package/template/base/src/images/mine-dark.png +0 -0
- package/template/base/src/images/mine-light-selected.png +0 -0
- package/template/base/src/images/mine-light.png +0 -0
- package/template/base/src/pages/home/index.css +28 -0
- package/template/base/src/pages/home/index.html +8 -0
- package/template/base/src/pages/home/index.json +5 -0
- package/template/base/src/pages/mine/index.css +28 -0
- package/template/base/src/pages/mine/index.html +8 -0
- package/template/base/src/pages/mine/index.json +5 -0
- package/template/base/src/sitemap.json +9 -0
- package/template/base/src/theme.json +20 -0
- package/template/eslint/eslint.config.js.ejs +89 -0
- package/template/javascript/babel.config.js +63 -0
- package/template/javascript/build.js +339 -0
- package/template/javascript/jsconfig.json +12 -0
- package/template/javascript/src/app.js +12 -0
- package/template/javascript/src/components/count-button/index.js +16 -0
- package/template/javascript/src/contexts/theme.js +3 -0
- package/template/javascript/src/pages/home/index.js +15 -0
- package/template/javascript/src/pages/mine/index.js +15 -0
- package/template/prettier/prettier.config.js +5 -0
- package/template/stylelint/_stylelintignore +1 -0
- package/template/stylelint/stylelint.config.js +15 -0
- package/template/typescript/babel.config.js +64 -0
- package/template/typescript/build.js +339 -0
- package/template/typescript/src/app.ts +12 -0
- package/template/typescript/src/components/count-button/index.ts +16 -0
- package/template/typescript/src/contexts/theme.ts +3 -0
- package/template/typescript/src/pages/home/index.ts +15 -0
- package/template/typescript/src/pages/mine/index.ts +15 -0
- package/template/typescript/tsconfig.json +20 -0
- package/template/vitest/__tests__/utils/index.spec.js +10 -0
- package/template/vitest/_gitignore +7 -0
- package/template/vitest/_stylelintignore +2 -0
- package/template/vitest/jsconfig.json +12 -0
- package/template/vitest/src/utils/index.js +12 -0
- package/template/vitest/vitest.config.js +21 -0
- package/template/vitest-typescript/__tests__/utils/index.spec.ts +10 -0
- package/template/vitest-typescript/_gitignore +7 -0
- package/template/vitest-typescript/_stylelintignore +2 -0
- package/template/vitest-typescript/src/utils/index.ts +12 -0
- package/template/vitest-typescript/tsconfig.json +20 -0
- package/template/vitest-typescript/vitest.config.ts +21 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"darkmode": true,
|
|
3
|
+
"themeLocation": "theme.json",
|
|
4
|
+
"pages": ["pages/home/index", "pages/mine/index"],
|
|
5
|
+
"window": {
|
|
6
|
+
"navigationBarBackgroundColor": "@bgColor",
|
|
7
|
+
"navigationBarTextStyle": "@style",
|
|
8
|
+
"navigationBarTitleText": "rezor-project",
|
|
9
|
+
"backgroundColor": "@bgColor",
|
|
10
|
+
"backgroundColorTop": "@bgColor",
|
|
11
|
+
"backgroundColorBottom": "@bgColor"
|
|
12
|
+
},
|
|
13
|
+
"tabBar": {
|
|
14
|
+
"color": "@color",
|
|
15
|
+
"selectedColor": "@color",
|
|
16
|
+
"backgroundColor": "@bgColor",
|
|
17
|
+
"borderStyle": "@style",
|
|
18
|
+
"list": [
|
|
19
|
+
{
|
|
20
|
+
"text": "主页",
|
|
21
|
+
"pagePath": "pages/home/index",
|
|
22
|
+
"iconPath": "@homeIcon",
|
|
23
|
+
"selectedIconPath": "@homeSelectedIcon"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"text": "我",
|
|
27
|
+
"pagePath": "pages/mine/index",
|
|
28
|
+
"iconPath": "@mineIcon",
|
|
29
|
+
"selectedIconPath": "@mineSelectedIcon"
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"sitemapLocation": "sitemap.json",
|
|
34
|
+
"lazyCodeLoading": "requiredComponents"
|
|
35
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
.home {
|
|
2
|
+
text-align: center;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.logo {
|
|
6
|
+
margin: 120px auto 0;
|
|
7
|
+
width: 400px;
|
|
8
|
+
height: 400px;
|
|
9
|
+
font-size: 64px;
|
|
10
|
+
line-height: 400px;
|
|
11
|
+
font-weight: bold;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.greeting {
|
|
15
|
+
font-size: 64px;
|
|
16
|
+
font-weight: bold;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.theme {
|
|
20
|
+
margin-top: 48px;
|
|
21
|
+
font-size: 48px;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.count {
|
|
25
|
+
margin-top: 48px;
|
|
26
|
+
display: flex;
|
|
27
|
+
justify-content: center;
|
|
28
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
.mine {
|
|
2
|
+
text-align: center;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.logo {
|
|
6
|
+
margin: 120px auto 0;
|
|
7
|
+
width: 400px;
|
|
8
|
+
height: 400px;
|
|
9
|
+
font-size: 64px;
|
|
10
|
+
line-height: 400px;
|
|
11
|
+
font-weight: bold;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.greeting {
|
|
15
|
+
font-size: 64px;
|
|
16
|
+
font-weight: bold;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.theme {
|
|
20
|
+
margin-top: 48px;
|
|
21
|
+
font-size: 48px;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.count {
|
|
25
|
+
margin-top: 48px;
|
|
26
|
+
display: flex;
|
|
27
|
+
justify-content: center;
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"light": {
|
|
3
|
+
"style": "black",
|
|
4
|
+
"color": "#181818",
|
|
5
|
+
"bgColor": "#ffffff",
|
|
6
|
+
"homeIcon": "/images/home-light.png",
|
|
7
|
+
"homeSelectedIcon": "/images/home-light-selected.png",
|
|
8
|
+
"mineIcon": "/images/mine-light.png",
|
|
9
|
+
"mineSelectedIcon": "/images/mine-light-selected.png"
|
|
10
|
+
},
|
|
11
|
+
"dark": {
|
|
12
|
+
"style": "white",
|
|
13
|
+
"color": "#ffffff",
|
|
14
|
+
"bgColor": "#181818",
|
|
15
|
+
"homeIcon": "/images/home-dark.png",
|
|
16
|
+
"homeSelectedIcon": "/images/home-dark-selected.png",
|
|
17
|
+
"mineIcon": "/images/mine-dark.png",
|
|
18
|
+
"mineSelectedIcon": "/images/mine-dark-selected.png"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
2
|
+
import eslint from '@eslint/js';
|
|
3
|
+
<%_ if (needsTypeScript) { _%>
|
|
4
|
+
import tseslint from 'typescript-eslint';
|
|
5
|
+
<%_ } _%>
|
|
6
|
+
<%_ if (needsPrettier) { _%>
|
|
7
|
+
import prettier from 'eslint-config-prettier';
|
|
8
|
+
<%_ } _%>
|
|
9
|
+
|
|
10
|
+
<%_ if (needsTypeScript) { _%>
|
|
11
|
+
const config = [
|
|
12
|
+
{
|
|
13
|
+
<%_ if (needsVitest) { _%>
|
|
14
|
+
ignores: ['dist/', 'coverage/'],
|
|
15
|
+
<%_ } else { _%>
|
|
16
|
+
ignores: ['dist/'],
|
|
17
|
+
<%_ } _%>
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
files: ['**/*.js', '**/*.ts'],
|
|
21
|
+
linterOptions: {
|
|
22
|
+
reportUnusedDisableDirectives: true,
|
|
23
|
+
},
|
|
24
|
+
...eslint.configs.recommended,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
files: ['**/*.js'],
|
|
28
|
+
languageOptions: {
|
|
29
|
+
globals: {
|
|
30
|
+
...globals.node,
|
|
31
|
+
wx: 'readonly',
|
|
32
|
+
getApp: 'readonly',
|
|
33
|
+
getCurrentPages: 'readonly',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
<%_ if (needsPrettier) { _%>
|
|
37
|
+
...prettier,
|
|
38
|
+
<%_ } _%>
|
|
39
|
+
},
|
|
40
|
+
...tseslint.configs.recommendedTypeChecked.map((c) => ({
|
|
41
|
+
...c,
|
|
42
|
+
files: ['**/*.ts'],
|
|
43
|
+
})),
|
|
44
|
+
{
|
|
45
|
+
files: ['**/*.ts'],
|
|
46
|
+
languageOptions: {
|
|
47
|
+
parserOptions: {
|
|
48
|
+
projectService: true,
|
|
49
|
+
tsconfigRootDir: import.meta.dirname,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
<%_ if (needsPrettier) { _%>
|
|
53
|
+
...prettier,
|
|
54
|
+
<%_ } _%>
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
<%_ } else { _%>
|
|
58
|
+
const config = [
|
|
59
|
+
{
|
|
60
|
+
files: ['**/*.js'],
|
|
61
|
+
<%_ if (needsVitest) { _%>
|
|
62
|
+
ignores: ['dist/**/*', 'coverage/**/*'],
|
|
63
|
+
<%_ } else { _%>
|
|
64
|
+
ignores: ['dist/**/*'],
|
|
65
|
+
<%_ } _%>
|
|
66
|
+
linterOptions: {
|
|
67
|
+
reportUnusedDisableDirectives: true,
|
|
68
|
+
},
|
|
69
|
+
languageOptions: {
|
|
70
|
+
globals: {
|
|
71
|
+
...globals.node,
|
|
72
|
+
wx: 'readonly',
|
|
73
|
+
getApp: 'readonly',
|
|
74
|
+
getCurrentPages: 'readonly',
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
<%_ if (needsPrettier) { _%>
|
|
78
|
+
rules: {
|
|
79
|
+
...eslint.configs.recommended.rules,
|
|
80
|
+
...prettier.rules,
|
|
81
|
+
},
|
|
82
|
+
<%_ } else { _%>
|
|
83
|
+
...eslint.configs.recommended,
|
|
84
|
+
<%_ } _%>
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
<%_ } _%>
|
|
88
|
+
|
|
89
|
+
export default config;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
|
|
3
|
+
const runtimeVersion = JSON.parse(
|
|
4
|
+
fs.readFileSync(
|
|
5
|
+
new URL(import.meta.resolve('@babel/runtime/package.json')),
|
|
6
|
+
'utf8',
|
|
7
|
+
),
|
|
8
|
+
).version;
|
|
9
|
+
|
|
10
|
+
const config = {
|
|
11
|
+
targets: {},
|
|
12
|
+
assumptions: {
|
|
13
|
+
arrayLikeIsIterable: true,
|
|
14
|
+
constantReexports: true,
|
|
15
|
+
constantSuper: true,
|
|
16
|
+
enumerableModuleMeta: true,
|
|
17
|
+
ignoreFunctionLength: true,
|
|
18
|
+
ignoreToPrimitiveHint: true,
|
|
19
|
+
iterableIsArray: true,
|
|
20
|
+
mutableTemplateObject: true,
|
|
21
|
+
noClassCalls: true,
|
|
22
|
+
noDocumentAll: true,
|
|
23
|
+
noNewArrows: true,
|
|
24
|
+
objectRestNoSymbols: true,
|
|
25
|
+
privateFieldsAsProperties: true,
|
|
26
|
+
pureGetters: true,
|
|
27
|
+
setClassMethods: true,
|
|
28
|
+
setComputedProperties: true,
|
|
29
|
+
setPublicClassFields: true,
|
|
30
|
+
setSpreadProperties: true,
|
|
31
|
+
skipForOfIteratorClosing: true,
|
|
32
|
+
superIsCallableConstructor: true,
|
|
33
|
+
},
|
|
34
|
+
presets: [
|
|
35
|
+
[
|
|
36
|
+
'@babel/preset-env',
|
|
37
|
+
{
|
|
38
|
+
bugfixes: true,
|
|
39
|
+
modules: 'commonjs',
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
],
|
|
43
|
+
plugins: [
|
|
44
|
+
[
|
|
45
|
+
'@babel/plugin-transform-runtime',
|
|
46
|
+
{
|
|
47
|
+
version: runtimeVersion,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
'transform-inline-environment-variables',
|
|
51
|
+
[
|
|
52
|
+
'module-resolver',
|
|
53
|
+
{
|
|
54
|
+
alias: {
|
|
55
|
+
'@': './src',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
'autocomplete-index',
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export default config;
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @version v0.1.0
|
|
3
|
+
* https://github.com/rezorjs/create-rezor
|
|
4
|
+
* 请谨慎修改此文件,改动过多可能会导致你后续升级困难。
|
|
5
|
+
*/
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import process from 'node:process';
|
|
8
|
+
import fs from 'fs-extra';
|
|
9
|
+
import chokidar from 'chokidar';
|
|
10
|
+
import babel from '@babel/core';
|
|
11
|
+
import traverse from '@babel/traverse';
|
|
12
|
+
import t from '@babel/types';
|
|
13
|
+
import { minify } from 'terser';
|
|
14
|
+
import postcss from 'postcss';
|
|
15
|
+
import postcssrc from 'postcss-load-config';
|
|
16
|
+
import { rollup } from 'rollup';
|
|
17
|
+
import replace from '@rollup/plugin-replace';
|
|
18
|
+
import terser from '@rollup/plugin-terser';
|
|
19
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
20
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
21
|
+
import { green, bold } from 'kolorist';
|
|
22
|
+
import { getPackageInfo } from 'local-pkg';
|
|
23
|
+
|
|
24
|
+
let topLevelJobs = [];
|
|
25
|
+
let bundleJobs = [];
|
|
26
|
+
const startTime = Date.now();
|
|
27
|
+
const NODE_ENV = process.env.NODE_ENV || 'production';
|
|
28
|
+
const __PROD__ = NODE_ENV === 'production';
|
|
29
|
+
const terserOptions = {
|
|
30
|
+
ecma: 2016,
|
|
31
|
+
toplevel: true,
|
|
32
|
+
safari10: true,
|
|
33
|
+
format: { comments: false },
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
let independentPackages = [];
|
|
37
|
+
async function findIndependentPackages() {
|
|
38
|
+
const { subpackages } = await fs.readJson(
|
|
39
|
+
path.resolve('src', 'app.json'),
|
|
40
|
+
'utf8',
|
|
41
|
+
);
|
|
42
|
+
if (subpackages) {
|
|
43
|
+
independentPackages = subpackages
|
|
44
|
+
.filter(({ independent }) => independent)
|
|
45
|
+
.map(({ root }) => root);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const builtLibraries = [];
|
|
50
|
+
const bundledModules = new Map();
|
|
51
|
+
async function bundleModule(module, pkg) {
|
|
52
|
+
const bundled = bundledModules.get(pkg);
|
|
53
|
+
if (
|
|
54
|
+
bundled?.has(module) ||
|
|
55
|
+
builtLibraries.some((library) => module.startsWith(library))
|
|
56
|
+
) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (bundled) {
|
|
60
|
+
bundled.add(module);
|
|
61
|
+
} else {
|
|
62
|
+
bundledModules.set(pkg, new Set([module]));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const {
|
|
66
|
+
packageJson: { peerDependencies },
|
|
67
|
+
} = await getPackageInfo(module);
|
|
68
|
+
const bundle = await rollup({
|
|
69
|
+
input: module,
|
|
70
|
+
external: peerDependencies ? Object.keys(peerDependencies) : undefined,
|
|
71
|
+
plugins: [
|
|
72
|
+
commonjs(),
|
|
73
|
+
replace({
|
|
74
|
+
preventAssignment: true,
|
|
75
|
+
values: {
|
|
76
|
+
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
|
|
77
|
+
},
|
|
78
|
+
}),
|
|
79
|
+
resolve(),
|
|
80
|
+
__PROD__ && terser(terserOptions),
|
|
81
|
+
].filter(Boolean),
|
|
82
|
+
});
|
|
83
|
+
await bundle.write({
|
|
84
|
+
exports: 'named',
|
|
85
|
+
file: `${pkg.replace('src', 'dist')}/miniprogram_npm/${module}/index.js`,
|
|
86
|
+
format: 'cjs',
|
|
87
|
+
});
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function traverseAST(ast, pkg, babelOnly = false) {
|
|
92
|
+
traverse.default(ast, {
|
|
93
|
+
CallExpression({ node }) {
|
|
94
|
+
if (
|
|
95
|
+
node.callee.name !== 'require' ||
|
|
96
|
+
!t.isStringLiteral(node.arguments[0]) ||
|
|
97
|
+
node.arguments[0].value.startsWith('.') ||
|
|
98
|
+
(babelOnly && !node.arguments[0].value.startsWith('@babel/runtime'))
|
|
99
|
+
) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const module = node.arguments[0].value;
|
|
104
|
+
let promise = bundleModule(module, pkg);
|
|
105
|
+
if (babelOnly) {
|
|
106
|
+
promise = promise.then((valid) => {
|
|
107
|
+
if (!valid) return;
|
|
108
|
+
return Promise.all(
|
|
109
|
+
independentPackages.map((item) => {
|
|
110
|
+
const bundled = bundledModules.get(item);
|
|
111
|
+
if (bundled) {
|
|
112
|
+
bundled.add(module);
|
|
113
|
+
} else {
|
|
114
|
+
bundledModules.set(pkg, new Set([module]));
|
|
115
|
+
}
|
|
116
|
+
return fs.copy(
|
|
117
|
+
path.resolve('dist', 'miniprogram_npm', module),
|
|
118
|
+
path.resolve('dist', item, 'miniprogram_npm', module),
|
|
119
|
+
);
|
|
120
|
+
}),
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
bundleJobs?.push(promise);
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function buildComponentLibrary(name) {
|
|
130
|
+
const {
|
|
131
|
+
rootPath,
|
|
132
|
+
packageJson: { miniprogram },
|
|
133
|
+
} = await getPackageInfo(name);
|
|
134
|
+
|
|
135
|
+
let source = '';
|
|
136
|
+
if (miniprogram) {
|
|
137
|
+
source = path.join(rootPath, miniprogram);
|
|
138
|
+
} else {
|
|
139
|
+
try {
|
|
140
|
+
const dist = path.join(rootPath, 'miniprogram_dist');
|
|
141
|
+
const stats = await fs.stat(dist);
|
|
142
|
+
if (stats.isDirectory()) {
|
|
143
|
+
source = dist;
|
|
144
|
+
}
|
|
145
|
+
} catch {
|
|
146
|
+
// Empty
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!source) return;
|
|
151
|
+
|
|
152
|
+
builtLibraries.push(name);
|
|
153
|
+
const destination = path.resolve('dist', 'miniprogram_npm', name);
|
|
154
|
+
await fs.copy(source, destination);
|
|
155
|
+
|
|
156
|
+
return new Promise((resolve) => {
|
|
157
|
+
const jobs = [];
|
|
158
|
+
const tnm = async (filePath) => {
|
|
159
|
+
const result = await babel.transformFileAsync(filePath, { ast: true });
|
|
160
|
+
traverseAST(result.ast, 'src', true);
|
|
161
|
+
const code = __PROD__
|
|
162
|
+
? (await minify(result.code, terserOptions)).code
|
|
163
|
+
: result.code;
|
|
164
|
+
await fs.writeFile(filePath, code);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const watcher = chokidar.watch([destination], {
|
|
168
|
+
ignored: (file, stats) => stats?.isFile() && !file.endsWith('.js'),
|
|
169
|
+
});
|
|
170
|
+
watcher.on('add', (filePath) => {
|
|
171
|
+
const promise = tnm(filePath);
|
|
172
|
+
jobs.push(promise);
|
|
173
|
+
});
|
|
174
|
+
watcher.on('ready', async () => {
|
|
175
|
+
const promise = watcher.close();
|
|
176
|
+
jobs.push(promise);
|
|
177
|
+
await Promise.all(jobs);
|
|
178
|
+
if (independentPackages.length > 0) {
|
|
179
|
+
await Promise.all(
|
|
180
|
+
independentPackages.map((item) =>
|
|
181
|
+
fs.copy(
|
|
182
|
+
destination,
|
|
183
|
+
path.resolve('dist', item, 'miniprogram_npm', name),
|
|
184
|
+
),
|
|
185
|
+
),
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
resolve();
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function scanDependencies() {
|
|
194
|
+
const { dependencies } = await fs.readJson('package.json', 'utf8');
|
|
195
|
+
for (const name of Object.keys(dependencies)) {
|
|
196
|
+
const promise = buildComponentLibrary(name);
|
|
197
|
+
topLevelJobs.push(promise);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function processScript(filePath) {
|
|
202
|
+
let ast, code;
|
|
203
|
+
try {
|
|
204
|
+
const result = await babel.transformFileAsync(path.resolve(filePath), {
|
|
205
|
+
ast: true,
|
|
206
|
+
});
|
|
207
|
+
ast = result.ast;
|
|
208
|
+
code = result.code;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
console.error(`Failed to compile ${filePath}`);
|
|
211
|
+
|
|
212
|
+
if (__PROD__) throw error;
|
|
213
|
+
|
|
214
|
+
console.error(error);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const pkg = independentPackages.find((item) =>
|
|
219
|
+
filePath.startsWith(path.normalize(`src/${item}`)),
|
|
220
|
+
);
|
|
221
|
+
// The `src/` prefix is added to to distinguish `src` and `src/src`.
|
|
222
|
+
traverseAST(ast, pkg ? `src/${pkg}` : 'src');
|
|
223
|
+
|
|
224
|
+
if (__PROD__) {
|
|
225
|
+
code = (await minify(code, terserOptions)).code;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const destination = filePath.replace('src', 'dist');
|
|
229
|
+
// Make sure the directory already exists when write file
|
|
230
|
+
await fs.copy(filePath, destination);
|
|
231
|
+
await fs.writeFile(destination, code);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function processTemplate(filePath) {
|
|
235
|
+
const destination = filePath
|
|
236
|
+
.replace('src', 'dist')
|
|
237
|
+
.replace(/\.html$/, '.wxml');
|
|
238
|
+
await fs.copy(filePath, destination);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function processStyle(filePath) {
|
|
242
|
+
const source = await fs.readFile(filePath, 'utf8');
|
|
243
|
+
const { plugins, options } = await postcssrc({ from: filePath });
|
|
244
|
+
|
|
245
|
+
let css;
|
|
246
|
+
try {
|
|
247
|
+
const result = await postcss(plugins).process(source, options);
|
|
248
|
+
css = result.css;
|
|
249
|
+
} catch (error) {
|
|
250
|
+
console.error(`Failed to compile ${filePath}`);
|
|
251
|
+
|
|
252
|
+
if (__PROD__) throw error;
|
|
253
|
+
|
|
254
|
+
console.error(error);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const destination = filePath
|
|
259
|
+
.replace('src', 'dist')
|
|
260
|
+
.replace(/\.css$/, '.wxss');
|
|
261
|
+
// Make sure the directory already exists when write file
|
|
262
|
+
await fs.copy(filePath, destination);
|
|
263
|
+
await fs.writeFile(destination, css);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const cb = async (filePath) => {
|
|
267
|
+
if (filePath.endsWith('.js')) {
|
|
268
|
+
await processScript(filePath);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (filePath.endsWith('.html')) {
|
|
273
|
+
await processTemplate(filePath);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (filePath.endsWith('.css')) {
|
|
278
|
+
await processStyle(filePath);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
await fs.copy(filePath, filePath.replace('src', 'dist'));
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
async function dev() {
|
|
286
|
+
await fs.remove('dist');
|
|
287
|
+
await findIndependentPackages();
|
|
288
|
+
await scanDependencies();
|
|
289
|
+
chokidar
|
|
290
|
+
.watch(['src'], {
|
|
291
|
+
ignored: (file, stats) =>
|
|
292
|
+
stats?.isFile() &&
|
|
293
|
+
(file.endsWith('.gitkeep') || file.endsWith('.DS_Store')),
|
|
294
|
+
})
|
|
295
|
+
.on('add', (filePath) => {
|
|
296
|
+
const promise = cb(filePath);
|
|
297
|
+
topLevelJobs?.push(promise);
|
|
298
|
+
})
|
|
299
|
+
.on('change', (filePath) => {
|
|
300
|
+
cb(filePath);
|
|
301
|
+
})
|
|
302
|
+
.on('ready', async () => {
|
|
303
|
+
await Promise.all(topLevelJobs);
|
|
304
|
+
await Promise.all(bundleJobs);
|
|
305
|
+
console.log(bold(green(`启动完成,耗时:${Date.now() - startTime}ms`)));
|
|
306
|
+
console.log(bold(green('监听文件变化中...')));
|
|
307
|
+
// Release memory.
|
|
308
|
+
topLevelJobs = null;
|
|
309
|
+
bundleJobs = null;
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async function prod() {
|
|
314
|
+
await fs.remove('dist');
|
|
315
|
+
await findIndependentPackages();
|
|
316
|
+
await scanDependencies();
|
|
317
|
+
const watcher = chokidar.watch(['src'], {
|
|
318
|
+
ignored: (file, stats) =>
|
|
319
|
+
stats?.isFile() &&
|
|
320
|
+
(file.endsWith('.gitkeep') || file.endsWith('.DS_Store')),
|
|
321
|
+
});
|
|
322
|
+
watcher.on('add', (filePath) => {
|
|
323
|
+
const promise = cb(filePath);
|
|
324
|
+
topLevelJobs.push(promise);
|
|
325
|
+
});
|
|
326
|
+
watcher.on('ready', async () => {
|
|
327
|
+
const promise = watcher.close();
|
|
328
|
+
topLevelJobs.push(promise);
|
|
329
|
+
await Promise.all(topLevelJobs);
|
|
330
|
+
await Promise.all(bundleJobs);
|
|
331
|
+
console.log(bold(green(`构建完成,耗时:${Date.now() - startTime}ms`)));
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (__PROD__) {
|
|
336
|
+
await prod();
|
|
337
|
+
} else {
|
|
338
|
+
await dev();
|
|
339
|
+
}
|