hw-weapp-compiler 1.0.0 → 1.0.2
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/build/config/constants.js +37 -0
- package/build/config/getAppConfig.js +9 -4
- package/build/config/getConfig.js +3 -1
- package/build/config/{getEntrys.js → getEntries.js} +26 -23
- package/build/dev.js +24 -19
- package/build/loader/js-loader.js +29 -14
- package/build/loader/wxml-loader.js +22 -15
- package/build/loader/wxs-loader.js +16 -10
- package/build/plugin/index.js +206 -177
- package/build/prod.js +29 -20
- package/build/utils/buildEnv.js +10 -2
- package/build/utils/compatiblePath.js +3 -1
- package/build/utils/createCompilerHandler.js +29 -0
- package/build/utils/getWxmlAssets.js +27 -31
- package/build/utils/isSubpackage.js +6 -4
- package/build/utils/loadModule.js +4 -7
- package/build/utils/traverseDir.js +3 -1
- package/build/utils/upload.js +14 -22
- package/build/webpack.config.js +42 -78
- package/index.js +1 -1
- package/package.json +3 -3
- package/build/config/env.js +0 -7
- package/build/config/getAssets.js +0 -1
- package/build/config/getContext.js +0 -5
- package/build/config/getEnv.js +0 -5
- package/build/config/getOutput.js +0 -7
- package/build/config/getResourceAccept.js +0 -1
- package/build/utils/recordEnv.js +0 -11
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
const ENV = {
|
|
4
|
+
DEV: 'development',
|
|
5
|
+
PROD: 'production',
|
|
6
|
+
SIMULATION: 'simulation',
|
|
7
|
+
UNKNOWN: 'unknown',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const PATTERNS = {
|
|
11
|
+
NODE_MODULES: /node_modules/,
|
|
12
|
+
JS_EXT: /\.js$/,
|
|
13
|
+
WXSS_EXT: /\.wxss$/,
|
|
14
|
+
WXML_EXT: /\.wxml$/,
|
|
15
|
+
WXS_EXT: /\.wxs$/,
|
|
16
|
+
LESS_EXT: /\.less$/,
|
|
17
|
+
CSS_EXT: /\.css$/,
|
|
18
|
+
JSON_EXT: /\.json$/,
|
|
19
|
+
STYLE_EXTS: /\.(less|wxss|css)$/i,
|
|
20
|
+
PLUGIN_PATH: /^(plugin:|plugin-private:|weui-miniprogram)/,
|
|
21
|
+
ABSOLUTE_PATH: /^\//,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const ASSET_EXT_PATTERN = /\.(png|jpg|gif|jpeg|svg|ttf|woff|eot|woff2|otf|mp3|mp4|wav|html)$/i;
|
|
25
|
+
|
|
26
|
+
const SRC_DIR = path.resolve(process.cwd(), 'src');
|
|
27
|
+
const DIST_DIR = path.resolve(process.cwd(), 'dist');
|
|
28
|
+
const ASSETS_DIR = 'assets';
|
|
29
|
+
|
|
30
|
+
module.exports = {
|
|
31
|
+
ENV,
|
|
32
|
+
PATTERNS,
|
|
33
|
+
ASSET_EXT_PATTERN,
|
|
34
|
+
SRC_DIR,
|
|
35
|
+
DIST_DIR,
|
|
36
|
+
ASSETS_DIR,
|
|
37
|
+
};
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const fse = require('fs-extra');
|
|
3
|
-
const
|
|
3
|
+
const { SRC_DIR } = require('./constants');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
let _appConfig = null;
|
|
6
6
|
|
|
7
7
|
function getAppConfig() {
|
|
8
|
-
|
|
8
|
+
if (!_appConfig) {
|
|
9
|
+
_appConfig = fse.readJSONSync(path.resolve(SRC_DIR, 'app.json'));
|
|
10
|
+
}
|
|
11
|
+
return _appConfig;
|
|
9
12
|
}
|
|
10
13
|
|
|
11
|
-
module.exports =
|
|
14
|
+
module.exports = {
|
|
15
|
+
getAppConfig,
|
|
16
|
+
};
|
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const fse = require('fs-extra');
|
|
3
|
-
const getContext = require('./getContext');
|
|
4
3
|
const { addNodeModulesUsingComponent } = require('../utils/isNodeModulesUsingComponent');
|
|
5
|
-
const compatiblePath = require('../utils/compatiblePath');
|
|
6
|
-
const traverseDir = require('../utils/traverseDir');
|
|
7
|
-
const getAppConfig = require('./getAppConfig');
|
|
8
|
-
|
|
9
|
-
const context = getContext();
|
|
4
|
+
const { compatiblePath } = require('../utils/compatiblePath');
|
|
5
|
+
const { traverseDir } = require('../utils/traverseDir');
|
|
6
|
+
const { getAppConfig } = require('./getAppConfig');
|
|
7
|
+
const { PATTERNS, SRC_DIR } = require('./constants');
|
|
10
8
|
const appConfig = getAppConfig();
|
|
11
9
|
|
|
12
|
-
const
|
|
13
|
-
app: path.resolve(
|
|
10
|
+
const entries = {
|
|
11
|
+
app: path.resolve(SRC_DIR, 'app'),
|
|
14
12
|
};
|
|
15
13
|
const addUsingComponents = (components, parent) => {
|
|
16
14
|
Object.keys(components).forEach((key) => {
|
|
17
15
|
let filePath = components[key];
|
|
18
16
|
|
|
19
|
-
if (
|
|
20
|
-
if (
|
|
21
|
-
filePath = path.resolve(
|
|
17
|
+
if (!PATTERNS.PLUGIN_PATH.test(filePath)) {
|
|
18
|
+
if (PATTERNS.ABSOLUTE_PATH.test(filePath)) {
|
|
19
|
+
filePath = path.resolve(SRC_DIR, filePath.replace(PATTERNS.ABSOLUTE_PATH, ''));
|
|
22
20
|
}
|
|
23
21
|
|
|
24
22
|
if (fse.existsSync(`${path.resolve(parent, filePath)}.js`)) {
|
|
@@ -26,7 +24,7 @@ const addUsingComponents = (components, parent) => {
|
|
|
26
24
|
} else {
|
|
27
25
|
try {
|
|
28
26
|
filePath = require.resolve(filePath);
|
|
29
|
-
filePath = filePath.replace(
|
|
27
|
+
filePath = filePath.replace(PATTERNS.JS_EXT, '');
|
|
30
28
|
} catch (error) {
|
|
31
29
|
console.error(error);
|
|
32
30
|
return;
|
|
@@ -40,18 +38,17 @@ const addUsingComponents = (components, parent) => {
|
|
|
40
38
|
|
|
41
39
|
let entryKey = filePath;
|
|
42
40
|
|
|
43
|
-
if (
|
|
44
|
-
// eslint-disable-next-line
|
|
41
|
+
if (PATTERNS.NODE_MODULES.test(filePath)) {
|
|
45
42
|
entryKey = compatiblePath(filePath).split('/node_modules/')[1];
|
|
46
43
|
|
|
47
44
|
addNodeModulesUsingComponent(entryKey);
|
|
48
45
|
} else {
|
|
49
|
-
entryKey = path.relative(
|
|
46
|
+
entryKey = path.relative(SRC_DIR, filePath);
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
entryKey = compatiblePath(entryKey);
|
|
53
50
|
|
|
54
|
-
|
|
51
|
+
entries[entryKey] = filePath;
|
|
55
52
|
}
|
|
56
53
|
});
|
|
57
54
|
};
|
|
@@ -65,26 +62,32 @@ const readUsingComponents = (file) => {
|
|
|
65
62
|
|
|
66
63
|
// pages
|
|
67
64
|
(appConfig.pages || []).forEach((page) => {
|
|
68
|
-
|
|
65
|
+
entries[page] = path.resolve(SRC_DIR, page);
|
|
69
66
|
});
|
|
70
67
|
|
|
71
68
|
// tabbar
|
|
72
|
-
if (fse.existsSync(path.resolve(
|
|
73
|
-
|
|
69
|
+
if (fse.existsSync(path.resolve(SRC_DIR, 'custom-tab-bar/index.js'))) {
|
|
70
|
+
entries['custom-tab-bar/index'] = path.resolve(SRC_DIR, 'custom-tab-bar/index');
|
|
74
71
|
}
|
|
75
72
|
|
|
76
73
|
// subpackages
|
|
77
74
|
(appConfig.subpackages || []).forEach((pkg) => {
|
|
78
75
|
(pkg.pages || []).forEach((page) => {
|
|
79
|
-
|
|
76
|
+
entries[compatiblePath(path.join(pkg.root, page))] = path.resolve(SRC_DIR, pkg.root, page);
|
|
80
77
|
});
|
|
81
78
|
});
|
|
82
79
|
|
|
83
80
|
// usingComponents
|
|
84
|
-
traverseDir(
|
|
85
|
-
if (
|
|
81
|
+
traverseDir(SRC_DIR).forEach((item) => {
|
|
82
|
+
if (PATTERNS.JSON_EXT.test(item)) {
|
|
86
83
|
readUsingComponents(item);
|
|
87
84
|
}
|
|
88
85
|
});
|
|
89
86
|
|
|
90
|
-
|
|
87
|
+
function getEntries() {
|
|
88
|
+
return entries;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
module.exports = {
|
|
92
|
+
getEntries,
|
|
93
|
+
};
|
package/build/dev.js
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
|
-
const
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fse = require('fs-extra');
|
|
2
3
|
const webpack = require('webpack');
|
|
3
|
-
const ENV = require('./config/
|
|
4
|
+
const { ENV, DIST_DIR } = require('./config/constants');
|
|
4
5
|
const webpackConfig = require('./webpack.config');
|
|
6
|
+
const { createCompilerHandler } = require('./utils/createCompilerHandler');
|
|
5
7
|
|
|
6
8
|
module.exports = (opts) => {
|
|
9
|
+
// 开发模式启动时清理一次 dist 目录
|
|
10
|
+
if (fse.existsSync(DIST_DIR)) {
|
|
11
|
+
const files = fse.readdirSync(DIST_DIR);
|
|
12
|
+
files.forEach((item) => {
|
|
13
|
+
if (!/project\.config\.json/.test(item)) {
|
|
14
|
+
fse.removeSync(path.resolve(DIST_DIR, item));
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
} else {
|
|
18
|
+
fse.mkdirSync(DIST_DIR);
|
|
19
|
+
}
|
|
20
|
+
|
|
7
21
|
const compiler = webpack(
|
|
8
22
|
webpackConfig(
|
|
9
23
|
{
|
|
@@ -14,21 +28,12 @@ module.exports = (opts) => {
|
|
|
14
28
|
),
|
|
15
29
|
);
|
|
16
30
|
|
|
17
|
-
compiler.watch(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
stats.toString({
|
|
26
|
-
chunks: false, // Makes the build much quieter
|
|
27
|
-
colors: true, // Shows colors in the console
|
|
28
|
-
}),
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
console.log(chalk.green(`completed in ${(stats.endTime - stats.startTime) / 1000} seconds`));
|
|
33
|
-
});
|
|
31
|
+
compiler.watch(
|
|
32
|
+
{
|
|
33
|
+
aggregateTimeout: 300,
|
|
34
|
+
poll: false,
|
|
35
|
+
ignored: /node_modules/,
|
|
36
|
+
},
|
|
37
|
+
createCompilerHandler('dev'),
|
|
38
|
+
);
|
|
34
39
|
};
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* js-loader - 微信小程序 JS 加载器
|
|
3
|
+
*
|
|
4
|
+
* 为页面/组件入口 JS 自动注入同名的样式文件 import 和依赖模块,
|
|
5
|
+
* 确保小程序运行时能正确加载关联资源。
|
|
6
|
+
*/
|
|
1
7
|
const path = require('path');
|
|
2
8
|
const fse = require('fs-extra');
|
|
3
|
-
const
|
|
4
|
-
const loadModule = require('../utils/loadModule');
|
|
9
|
+
const { getEntries } = require('../config/getEntries');
|
|
10
|
+
const { loadModule } = require('../utils/loadModule');
|
|
5
11
|
|
|
6
|
-
const
|
|
12
|
+
const entries = getEntries();
|
|
7
13
|
|
|
8
14
|
module.exports = async function loader(source) {
|
|
9
15
|
this.cacheable(true);
|
|
@@ -14,21 +20,30 @@ module.exports = async function loader(source) {
|
|
|
14
20
|
|
|
15
21
|
const imports = [sourceStr];
|
|
16
22
|
|
|
17
|
-
if (Object.values(
|
|
23
|
+
if (Object.values(entries).indexOf(path.resolve(fileInfo.dir, fileInfo.name)) !== -1) {
|
|
18
24
|
const exts = ['.less', '.wxss', '.css', '.json', '.wxml'];
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
const checks = await Promise.all(
|
|
26
|
+
exts.map(async (ext) => {
|
|
27
|
+
const otherFilePath = path.resolve(fileInfo.dir, fileInfo.name + ext);
|
|
28
|
+
const exists = await fse.pathExists(otherFilePath);
|
|
29
|
+
return { ext, otherFilePath, exists };
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
const existing = checks.filter((r) => r.exists);
|
|
33
|
+
const loadResults = await Promise.all(
|
|
34
|
+
existing.map(async ({ ext, otherFilePath }) => {
|
|
25
35
|
if (['.less', '.wxss', '.css'].indexOf(ext) !== -1) {
|
|
26
|
-
|
|
27
|
-
} else {
|
|
28
|
-
await loadModule.call(this, otherFilePath);
|
|
36
|
+
return { type: 'style', ext, path: otherFilePath };
|
|
29
37
|
}
|
|
38
|
+
await loadModule.call(this, otherFilePath);
|
|
39
|
+
return { type: 'module', ext, path: otherFilePath };
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
loadResults.forEach((result) => {
|
|
43
|
+
if (result.type === 'style') {
|
|
44
|
+
imports.unshift(`import './${fileInfo.name}${result.ext}'\n`);
|
|
30
45
|
}
|
|
31
|
-
}
|
|
46
|
+
});
|
|
32
47
|
}
|
|
33
48
|
|
|
34
49
|
callback(null, imports.join(''));
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* wxml-loader - 微信小程序 wxml 模板加载器
|
|
3
|
+
*
|
|
4
|
+
* 解析 wxml 文件中的资源引用(图片等)和 wxml/wxs 引用,
|
|
5
|
+
* 将资源路径替换为构建产物路径,并将引用的 wxml/wxs 模块加入依赖。
|
|
6
|
+
*/
|
|
7
|
+
const { getWxmlAssets } = require('../utils/getWxmlAssets');
|
|
8
|
+
const { loadModule } = require('../utils/loadModule');
|
|
3
9
|
|
|
4
10
|
module.exports = async function loader(source) {
|
|
5
11
|
this.cacheable(true);
|
|
@@ -22,22 +28,23 @@ module.exports = async function loader(source) {
|
|
|
22
28
|
|
|
23
29
|
const [assets, wxmls] = await getWxmlAssets.call(this, filePath, content);
|
|
24
30
|
|
|
25
|
-
// 资源文件
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
// 资源文件 - 并行 loadModule
|
|
32
|
+
const assetResults = await Promise.all(
|
|
33
|
+
assets.map(async ([attr, file]) => {
|
|
34
|
+
const src = await loadModule.call(this, file);
|
|
35
|
+
return src ? [attr, src] : null;
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
for (let index = 0; index < assetResults.length; index += 1) {
|
|
39
|
+
const result = assetResults[index];
|
|
40
|
+
if (result) {
|
|
41
|
+
const [attr, src] = result;
|
|
42
|
+
content = content.replace(attr, withPublicPath(src));
|
|
32
43
|
}
|
|
33
44
|
}
|
|
34
45
|
|
|
35
|
-
// wxml文件
|
|
36
|
-
|
|
37
|
-
const [, file] = wxmls[index];
|
|
38
|
-
|
|
39
|
-
await loadModule.call(this, file);
|
|
40
|
-
}
|
|
46
|
+
// wxml文件 - 并行 loadModule
|
|
47
|
+
await Promise.all(wxmls.map(([, file]) => loadModule.call(this, file)));
|
|
41
48
|
|
|
42
49
|
callback(null, content);
|
|
43
50
|
};
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wxs-loader - 微信小程序 wxs 脚本加载器
|
|
3
|
+
*
|
|
4
|
+
* 解析 wxs 文件中的 require 调用,将被引用的模块加入 webpack 依赖图,
|
|
5
|
+
* 确保构建时 wxs 的依赖被正确追踪。
|
|
6
|
+
*/
|
|
1
7
|
const path = require('path');
|
|
2
|
-
const loadModule = require('../utils/loadModule');
|
|
8
|
+
const { loadModule } = require('../utils/loadModule');
|
|
3
9
|
|
|
4
10
|
module.exports = async function loader(source) {
|
|
5
11
|
this.cacheable(true);
|
|
@@ -10,15 +16,15 @@ module.exports = async function loader(source) {
|
|
|
10
16
|
|
|
11
17
|
const requires = content.match(/require\(("|').*("|')\)/g) || [];
|
|
12
18
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
await Promise.all(
|
|
20
|
+
requires.map(async (item) => {
|
|
21
|
+
const file = path.resolve(
|
|
22
|
+
fileInfo.dir,
|
|
23
|
+
item.replace(/^((require\(')|(require\("))/g, '').replace(/(('\))|("\)))$/g, ''),
|
|
24
|
+
);
|
|
25
|
+
await loadModule.call(this, file);
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
22
28
|
|
|
23
29
|
callback(null, content);
|
|
24
30
|
};
|