@taro-minify-pack/plugin-async-pack 0.1.3-alpha.1 → 0.1.4-alpha.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/README.md +20 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.js +32 -10
- package/dist/index.js.map +1 -1
- package/dist/inject-dynamic-style.d.ts +26 -0
- package/dist/inject-dynamic-style.js +63 -0
- package/dist/inject-dynamic-style.js.map +1 -0
- package/dist/merge-output.d.ts +1 -1
- package/dist/merge-output.js +4 -4
- package/dist/merge-output.js.map +1 -1
- package/dist/transform-before-compression-plugin.d.ts +1 -1
- package/dist/transform-before-compression-plugin.js +4 -4
- package/dist/transform-before-compression-plugin.js.map +1 -1
- package/dist/utils.js +4 -3
- package/dist/utils.js.map +1 -1
- package/package.json +2 -1
- package/src/index.ts +19 -18
- package/src/inject-dynamic-style.ts +87 -0
- package/src/merge-output.ts +4 -4
- package/src/transform-before-compression-plugin.ts +4 -4
- package/src/utils.ts +4 -3
- package/dist/inject-style-component.d.ts +0 -14
- package/dist/inject-style-component.js +0 -46
- package/dist/inject-style-component.js.map +0 -1
- package/src/inject-style-component.ts +0 -54
package/README.md
CHANGED
|
@@ -178,11 +178,11 @@ plugins: [
|
|
|
178
178
|
customDynamicPackages: [
|
|
179
179
|
{
|
|
180
180
|
name: 'sync-style',
|
|
181
|
-
test: (module) => /src[\\/]pages[\\/]sync-style[\\/]/.test(module.
|
|
181
|
+
test: (module) => /src[\\/]pages[\\/]sync-style[\\/]/.test(module.identifier()),
|
|
182
182
|
},
|
|
183
183
|
{
|
|
184
184
|
name: 'async-style',
|
|
185
|
-
test: (module) => /src[\\/]pages[\\/]async-style[\\/]/.test(module.
|
|
185
|
+
test: (module) => /src[\\/]pages[\\/]async-style[\\/]/.test(module.identifier()),
|
|
186
186
|
asyncStyle: true
|
|
187
187
|
}
|
|
188
188
|
]
|
|
@@ -200,8 +200,24 @@ plugins: [
|
|
|
200
200
|
- 确认使用了 Webpack 5 编译器
|
|
201
201
|
- 确保代码中使用了 `import()` 动态导入语法
|
|
202
202
|
|
|
203
|
-
### 2.
|
|
204
|
-
-
|
|
203
|
+
### 2. 为什么样式文件没有异步?
|
|
204
|
+
- 先检查 `customDynamicPackages[].test` 是否真的命中了目标模块;如果没有命中,样式会走默认同步合并逻辑,而不会进入对应自定义异步分包
|
|
205
|
+
- 再检查该自定义分包的 `asyncStyle` 是否为 `true`;只有开启后才会额外生成 `inject-style` 组件,并触发异步样式注入
|
|
206
|
+
- `module.identifier()` 代表 webpack 给当前模块生成的唯一标识,通常会包含 loader 链、查询参数和源文件绝对路径,比单纯看文件名更适合排查 `test` 是否命中
|
|
207
|
+
- 排查时可以先在 `test` 回调里临时打印 `module.identifier()`,确认真实值后再收窄正则规则,例如只匹配其中稳定的源码路径片段:
|
|
208
|
+
|
|
209
|
+
```js
|
|
210
|
+
const customDynamicPackageConfig = {
|
|
211
|
+
name: 'async-style',
|
|
212
|
+
test: (module) => {
|
|
213
|
+
console.log(module.identifier())
|
|
214
|
+
return /src[\\/]pages[\\/]async-style[\\/]/.test(module.identifier())
|
|
215
|
+
},
|
|
216
|
+
asyncStyle: true
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
- 如果打印出来的内容带有 `css-loader`、`sass-loader`、`?taro` 之类前缀或查询参数,通常是正常现象,优先匹配其中稳定的业务目录路径即可
|
|
205
221
|
|
|
206
222
|
### 3. 配置后编译失败?
|
|
207
223
|
- 请检查配置选项是否正确
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { IPluginContext } from '@tarojs/service';
|
|
2
2
|
import { AsyncPackOpts } from './types';
|
|
3
|
-
export
|
|
3
|
+
export * from './types';
|
|
4
|
+
export * from './inject-dynamic-style';
|
|
4
5
|
declare const _default: (ctx: IPluginContext, pluginOpts: Partial<AsyncPackOpts>) => void;
|
|
5
6
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
17
|
const transform_webpack_runtime_1 = require("./transform-webpack-runtime");
|
|
4
18
|
const transform_before_compression_plugin_1 = require("./transform-before-compression-plugin");
|
|
5
|
-
const
|
|
19
|
+
const inject_dynamic_style_1 = require("./inject-dynamic-style");
|
|
6
20
|
const transform_app_config_1 = require("./transform-app-config");
|
|
7
21
|
const transform_pages_wxml_1 = require("./transform-pages-wxml");
|
|
8
22
|
const utils_1 = require("./utils");
|
|
9
23
|
const transform_app_stylesheet_1 = require("./transform-app-stylesheet");
|
|
10
24
|
const merge_output_1 = require("./merge-output");
|
|
25
|
+
__exportStar(require("./types"), exports);
|
|
26
|
+
__exportStar(require("./inject-dynamic-style"), exports);
|
|
11
27
|
const dynamicPackOptsDefaultOpt = {
|
|
12
28
|
dynamicPackageNamePrefix: 'dynamic-package',
|
|
13
29
|
dynamicPackageCount: 1,
|
|
@@ -46,7 +62,7 @@ exports.default = (ctx, pluginOpts) => {
|
|
|
46
62
|
const chunkFilename = (pathData) => (0, utils_1.generateChunkFilename)(Object.assign(Object.assign({}, finalOpts), { pathData, ext: '.wxss' }));
|
|
47
63
|
return [Object.assign(Object.assign({}, options), { chunkFilename })];
|
|
48
64
|
});
|
|
49
|
-
chain.plugin(transform_before_compression_plugin_1.
|
|
65
|
+
chain.plugin(transform_before_compression_plugin_1.TransformBeforeCompressionPlugin.pluginName).use(transform_before_compression_plugin_1.TransformBeforeCompressionPlugin, [{
|
|
50
66
|
test: /^(runtime\.js|app\.wxss)$/,
|
|
51
67
|
transform: (opt) => {
|
|
52
68
|
const { source, assetName, assets } = opt;
|
|
@@ -58,21 +74,27 @@ exports.default = (ctx, pluginOpts) => {
|
|
|
58
74
|
return source;
|
|
59
75
|
}
|
|
60
76
|
}]);
|
|
61
|
-
chain.plugin(merge_output_1.
|
|
62
|
-
test: (assetName) =>
|
|
77
|
+
chain.plugin(merge_output_1.MergeOutputPlugin.pluginName).use(merge_output_1.MergeOutputPlugin, [{
|
|
78
|
+
test: (assetName) => {
|
|
79
|
+
const isStyleAsset = (0, utils_1.matchSuffix)('wxss', assetName);
|
|
80
|
+
const isInjectDynamicStyleAsset = new RegExp(`${inject_dynamic_style_1.InjectDynamicStylePlugin.componentName}\\.wxss$`).test(assetName);
|
|
81
|
+
return isStyleAsset && !isInjectDynamicStyleAsset && (0, utils_1.isSyncStyleDynamicPackageAsset)(finalOpts, assetName);
|
|
82
|
+
},
|
|
63
83
|
outputFile: `${finalOpts.dynamicPackageNamePrefix}.wxss`
|
|
64
84
|
}]);
|
|
65
|
-
chain.plugin(
|
|
85
|
+
chain.plugin(inject_dynamic_style_1.InjectDynamicStylePlugin.pluginName).use(inject_dynamic_style_1.InjectDynamicStylePlugin, [finalOpts]);
|
|
66
86
|
});
|
|
67
87
|
ctx.modifyBuildAssets(({ assets }) => {
|
|
68
88
|
const hasDynamicModule = Object.keys(assets).some((key) => (0, utils_1.isDynamicPackageAsset)(finalOpts, key));
|
|
69
89
|
if (!hasDynamicModule)
|
|
70
90
|
return;
|
|
71
|
-
const asyncComponents =
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
91
|
+
const asyncComponents = Object.keys(assets).reduce((result, assetName) => {
|
|
92
|
+
const regExp = new RegExp(`^(${finalOpts.dynamicPackageNamePrefix}-([^/]+)/${inject_dynamic_style_1.InjectDynamicStylePlugin.componentName})\\.json$`);
|
|
93
|
+
const [_, fullPathWithoutExt, key] = assetName.match(regExp) || [];
|
|
94
|
+
if (!(0, utils_1.isDynamicPackageAsset)(finalOpts, assetName) || !key)
|
|
95
|
+
return result;
|
|
96
|
+
const componentName = `${inject_dynamic_style_1.InjectDynamicStylePlugin.componentName}-${key}`;
|
|
97
|
+
return Object.assign(Object.assign({}, result), { [componentName]: fullPathWithoutExt });
|
|
76
98
|
}, {});
|
|
77
99
|
(0, transform_app_config_1.transformAppConfig)(Object.assign(Object.assign({}, finalOpts), { assets, asyncComponents }));
|
|
78
100
|
(0, transform_pages_wxml_1.transformPagesWXml)({ assets, asyncComponents });
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA,2EAAqE;AACrE,+FAG8C;AAC9C,iEAAiE;AACjE,iEAA2D;AAC3D,iEAA2D;AAC3D,mCAIgB;AAEhB,yEAAmE;AACnE,iDAAkD;AAElD,0CAAuB;AACvB,yDAAsC;AAEtC,MAAM,yBAAyB,GAAkB;IAC/C,wBAAwB,EAAE,iBAAiB;IAC3C,mBAAmB,EAAE,CAAC;IACtB,qBAAqB,EAAE,EAAE;CAC1B,CAAA;AAED,kBAAe,CAAC,GAAmB,EAAE,UAAkC,EAAE,EAAE;IACzE,MAAM,SAAS,mCAAQ,yBAAyB,GAAK,UAAU,CAAE,CAAA;IAEjE,IAAI,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CAAC,CAAA;YAC/H,MAAM,CAAC,CAAC,EAAE,kBAAkB,EAAE,GAAG,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,CAAA;YAClE,IAAI,CAAC,IAAA,6BAAqB,EAAC,SAAS,EAAE,SAAS,CAAC,IAAI,CAAC,GAAG;gBAAE,OAAO,MAAM,CAAA;YACvE,MAAM,aAAa,GAAG,GAAG,+CAAwB,CAAC,aAAa,IAAI,GAAG,EAAE,CAAA;YACxE,uCAAY,MAAM,KAAE,CAAC,aAAa,CAAC,EAAE,kBAAkB,IAAE;QAC3D,CAAC,EAAE,EAAE,CAAC,CAAA;QAEN,IAAA,yCAAkB,kCAAM,SAAS,KAAE,MAAM,EAAE,eAAe,IAAG,CAAA;QAE7D,IAAA,yCAAkB,EAAC,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC,CAAA;IACjD,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Compiler, Compilation } from 'webpack';
|
|
2
|
+
import { AsyncPackOpts } from './types';
|
|
3
|
+
import { SyncHook } from 'tapable';
|
|
4
|
+
export interface InjectDynamicStyleOpt {
|
|
5
|
+
key: string;
|
|
6
|
+
stylesheet: string;
|
|
7
|
+
}
|
|
8
|
+
export interface Hooks {
|
|
9
|
+
emitDynamicStyle: SyncHook<InjectDynamicStyleOpt>;
|
|
10
|
+
}
|
|
11
|
+
type Opt = AsyncPackOpts;
|
|
12
|
+
export declare class InjectDynamicStylePlugin {
|
|
13
|
+
private readonly opt;
|
|
14
|
+
private readonly WXmlContent;
|
|
15
|
+
private readonly JsonContent;
|
|
16
|
+
private readonly JsContent;
|
|
17
|
+
static readonly pluginName = "InjectDynamicStyle";
|
|
18
|
+
static readonly componentName = "inject-dynamic-style";
|
|
19
|
+
private static readonly hooksMap;
|
|
20
|
+
private dynamicStyleMap;
|
|
21
|
+
constructor(opt: Opt);
|
|
22
|
+
static getCompilationHooks(compilation: Compilation): Hooks | undefined;
|
|
23
|
+
injectDynamicStyleCode(compilation: Compilation, opt: InjectDynamicStyleOpt): void;
|
|
24
|
+
apply(compiler: Compiler): void;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.InjectDynamicStylePlugin = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const webpack_sources_1 = require("webpack-sources");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
const tapable_1 = require("tapable");
|
|
11
|
+
class InjectDynamicStylePlugin {
|
|
12
|
+
constructor(opt) {
|
|
13
|
+
this.WXmlContent = '<block/>';
|
|
14
|
+
this.JsonContent = '{"component": true,"styleIsolation": "shared"}';
|
|
15
|
+
this.JsContent = 'Component({})';
|
|
16
|
+
this.dynamicStyleMap = new Map();
|
|
17
|
+
this.opt = opt;
|
|
18
|
+
}
|
|
19
|
+
static getCompilationHooks(compilation) {
|
|
20
|
+
if (!this.hooksMap.has(compilation)) {
|
|
21
|
+
const emitDynamicStyle = new tapable_1.SyncHook(['emitDynamicStyleOpt']);
|
|
22
|
+
this.hooksMap.set(compilation, { emitDynamicStyle });
|
|
23
|
+
}
|
|
24
|
+
return this.hooksMap.get(compilation);
|
|
25
|
+
}
|
|
26
|
+
injectDynamicStyleCode(compilation, opt) {
|
|
27
|
+
const { key, stylesheet } = opt;
|
|
28
|
+
const componentPath = `${key}/${InjectDynamicStylePlugin.componentName}`;
|
|
29
|
+
compilation.assets[`${componentPath}.wxss`] = new webpack_sources_1.RawSource(stylesheet);
|
|
30
|
+
compilation.assets[`${componentPath}.wxml`] = new webpack_sources_1.RawSource(this.WXmlContent);
|
|
31
|
+
compilation.assets[`${componentPath}.json`] = new webpack_sources_1.RawSource(this.JsonContent);
|
|
32
|
+
compilation.assets[`${componentPath}.js`] = new webpack_sources_1.RawSource(this.JsContent);
|
|
33
|
+
}
|
|
34
|
+
apply(compiler) {
|
|
35
|
+
compiler.hooks.compilation.tap(InjectDynamicStylePlugin.pluginName, (compilation) => {
|
|
36
|
+
const hooks = InjectDynamicStylePlugin.getCompilationHooks(compilation);
|
|
37
|
+
const stage = compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL; // 最早阶段,在优化前
|
|
38
|
+
hooks === null || hooks === void 0 ? void 0 : hooks.emitDynamicStyle.tap(InjectDynamicStylePlugin.pluginName, (opt) => this.dynamicStyleMap.set(opt.key, opt));
|
|
39
|
+
compilation.hooks.processAssets.tap({ name: InjectDynamicStylePlugin.pluginName, stage }, (assets) => {
|
|
40
|
+
const { customDynamicPackages } = this.opt;
|
|
41
|
+
customDynamicPackages.forEach(customDynamicPackageItem => {
|
|
42
|
+
if (!customDynamicPackageItem.asyncStyle)
|
|
43
|
+
return;
|
|
44
|
+
const customDynamicPackageName = (0, utils_1.generateCustomDynamicPackageName)(this.opt, customDynamicPackageItem.name);
|
|
45
|
+
const styleFileContent = Object.keys(assets).reduce((result, assetPath) => {
|
|
46
|
+
if (!new RegExp(`^${customDynamicPackageName}\\/.*\\.wxss$`).test(assetPath))
|
|
47
|
+
return result;
|
|
48
|
+
const relativePath = path_1.default.posix.relative(customDynamicPackageName, assetPath);
|
|
49
|
+
const code = `@import './${relativePath}';`;
|
|
50
|
+
return result + code + '\n';
|
|
51
|
+
}, '');
|
|
52
|
+
this.dynamicStyleMap.set(customDynamicPackageName, { key: customDynamicPackageName, stylesheet: styleFileContent });
|
|
53
|
+
});
|
|
54
|
+
this.dynamicStyleMap.forEach((opt) => this.injectDynamicStyleCode(compilation, opt));
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.InjectDynamicStylePlugin = InjectDynamicStylePlugin;
|
|
60
|
+
InjectDynamicStylePlugin.pluginName = 'InjectDynamicStyle';
|
|
61
|
+
InjectDynamicStylePlugin.componentName = 'inject-dynamic-style';
|
|
62
|
+
InjectDynamicStylePlugin.hooksMap = new WeakMap();
|
|
63
|
+
//# sourceMappingURL=inject-dynamic-style.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-dynamic-style.js","sourceRoot":"","sources":["../src/inject-dynamic-style.ts"],"names":[],"mappings":";;;;;;AACA,gDAAuB;AAEvB,qDAA2C;AAC3C,mCAA0D;AAC1D,qCAAkC;AAalC,MAAa,wBAAwB;IAiBnC,YAAa,GAAQ;QAdJ,gBAAW,GAAW,UAAU,CAAA;QAEhC,gBAAW,GAAU,gDAAgD,CAAA;QAErE,cAAS,GAAW,eAAe,CAAA;QAQ5C,oBAAe,GAAuC,IAAI,GAAG,EAAE,CAAA;QAGrE,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;IAChB,CAAC;IAEM,MAAM,CAAC,mBAAmB,CAAE,WAAwB;QACzD,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,CAAC;YACpC,MAAM,gBAAgB,GAAG,IAAI,kBAAQ,CAAwB,CAAC,qBAAqB,CAAC,CAAC,CAAA;YACrF,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,EAAE,EAAE,gBAAgB,EAAE,CAAC,CAAA;QACtD,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;IACvC,CAAC;IAED,sBAAsB,CAAE,WAAwB,EAAE,GAAyB;QACzE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,GAAG,CAAA;QAC/B,MAAM,aAAa,GAAG,GAAG,GAAG,IAAI,wBAAwB,CAAC,aAAa,EAAE,CAAA;QACxE,WAAW,CAAC,MAAM,CAAC,GAAG,aAAa,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
4
3
|
test: (assetName: string) => boolean;
|
|
5
4
|
outputFile: string;
|
|
6
5
|
}
|
|
7
6
|
export declare class MergeOutputPlugin {
|
|
8
7
|
private readonly opts;
|
|
8
|
+
static readonly pluginName = "MergeOutput";
|
|
9
9
|
constructor(opts: MergeOutputOpt);
|
|
10
10
|
apply(compiler: Compiler): void;
|
|
11
11
|
}
|
package/dist/merge-output.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MergeOutputPlugin =
|
|
4
|
-
exports.PLUGIN_NAME = 'MergeOutput';
|
|
3
|
+
exports.MergeOutputPlugin = void 0;
|
|
5
4
|
class MergeOutputPlugin {
|
|
6
5
|
constructor(opts) {
|
|
7
6
|
this.opts = opts;
|
|
8
7
|
}
|
|
9
8
|
apply(compiler) {
|
|
10
|
-
compiler.hooks.compilation.tap(
|
|
9
|
+
compiler.hooks.compilation.tap(MergeOutputPlugin.pluginName, (compilation) => {
|
|
11
10
|
const stage = compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL; // 最早阶段,在优化前
|
|
12
|
-
compilation.hooks.processAssets.tap({ name:
|
|
11
|
+
compilation.hooks.processAssets.tap({ name: MergeOutputPlugin.pluginName, stage }, (assets) => {
|
|
13
12
|
const mergedResult = Object.keys(assets).reduce((result, item) => {
|
|
14
13
|
if (!this.opts.test(item))
|
|
15
14
|
return result;
|
|
@@ -23,4 +22,5 @@ class MergeOutputPlugin {
|
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
24
|
exports.MergeOutputPlugin = MergeOutputPlugin;
|
|
25
|
+
MergeOutputPlugin.pluginName = 'MergeOutput';
|
|
26
26
|
//# sourceMappingURL=merge-output.js.map
|
package/dist/merge-output.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge-output.js","sourceRoot":"","sources":["../src/merge-output.ts"],"names":[],"mappings":";;;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|
|
@@ -9,9 +9,9 @@ export interface PluginOpt {
|
|
|
9
9
|
test?: RegExp;
|
|
10
10
|
transform: (opt: TransformOpt) => string;
|
|
11
11
|
}
|
|
12
|
-
export declare const PLUGIN_NAME = "TransformBeforeCompression";
|
|
13
12
|
export declare class TransformBeforeCompressionPlugin {
|
|
14
13
|
private readonly opt;
|
|
14
|
+
static readonly pluginName = "TransformBeforeCompression";
|
|
15
15
|
constructor(opt: PluginOpt);
|
|
16
16
|
apply(compiler: webpack.Compiler): void;
|
|
17
17
|
}
|
|
@@ -3,17 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.TransformBeforeCompressionPlugin =
|
|
6
|
+
exports.TransformBeforeCompressionPlugin = void 0;
|
|
7
7
|
const webpack_1 = __importDefault(require("webpack"));
|
|
8
|
-
exports.PLUGIN_NAME = 'TransformBeforeCompression';
|
|
9
8
|
class TransformBeforeCompressionPlugin {
|
|
10
9
|
constructor(opt) {
|
|
11
10
|
this.opt = opt;
|
|
12
11
|
}
|
|
13
12
|
apply(compiler) {
|
|
14
|
-
compiler.hooks.compilation.tap(
|
|
13
|
+
compiler.hooks.compilation.tap(TransformBeforeCompressionPlugin.pluginName, (compilation) => {
|
|
15
14
|
const stage = webpack_1.default.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE; // 压缩前
|
|
16
|
-
compilation.hooks.processAssets.tap({ name:
|
|
15
|
+
compilation.hooks.processAssets.tap({ name: TransformBeforeCompressionPlugin.pluginName, stage }, (assets) => {
|
|
17
16
|
const { test, transform } = this.opt;
|
|
18
17
|
const assetNames = Object.keys(assets);
|
|
19
18
|
assetNames.forEach((assetName) => {
|
|
@@ -28,4 +27,5 @@ class TransformBeforeCompressionPlugin {
|
|
|
28
27
|
}
|
|
29
28
|
}
|
|
30
29
|
exports.TransformBeforeCompressionPlugin = TransformBeforeCompressionPlugin;
|
|
30
|
+
TransformBeforeCompressionPlugin.pluginName = 'TransformBeforeCompression';
|
|
31
31
|
//# sourceMappingURL=transform-before-compression-plugin.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform-before-compression-plugin.js","sourceRoot":"","sources":["../src/transform-before-compression-plugin.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA6B;
|
|
1
|
+
{"version":3,"file":"transform-before-compression-plugin.js","sourceRoot":"","sources":["../src/transform-before-compression-plugin.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA6B;AAc7B,MAAa,gCAAgC;IAK3C,YAAa,GAAc;QACzB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;IAChB,CAAC;IAED,KAAK,CAAE,QAA0B;QAC/B,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,gCAAgC,CAAC,UAAU,EAAE,CAAC,WAAgC,EAAE,EAAE;YAC/G,MAAM,KAAK,GAAG,iBAAO,CAAC,WAAW,CAAC,6BAA6B,CAAA,CAAC,MAAM;YAEtE,WAAW,CAAC,KAAK,CAAC,aAAa,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,gCAAgC,CAAC,UAAU,EAAE,KAAK,EAAE,EAAE,CAAC,MAAM,EAAE,EAAE;gBAC3G,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC,GAAG,CAAA;gBAEpC,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;gBAEtC,UAAU,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE;oBAC/B,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;wBAAE,OAAM;oBAE1C,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,EAAE,CAAA;oBAEzC,MAAM,eAAe,GAAG,SAAS,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAkB,CAAC,CAAA;oBAEhF,WAAW,CAAC,WAAW,CAAC,SAAS,EAAE,IAAI,iBAAO,CAAC,OAAO,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC,CAAA;gBACpF,CAAC,CAAC,CAAA;YACJ,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC;;AA7BH,4EA8BC;AA3BwB,2CAAU,GAAG,4BAA4B,CAAA"}
|
package/dist/utils.js
CHANGED
|
@@ -16,7 +16,7 @@ const generateKeyByOrder = (order) => {
|
|
|
16
16
|
exports.generateKeyByOrder = generateKeyByOrder;
|
|
17
17
|
const generateDefaultDynamicPackageName = (opt) => {
|
|
18
18
|
if (!(0, exports.isNumber)(opt.order) || opt.dynamicPackageCount <= 1)
|
|
19
|
-
return opt.dynamicPackageNamePrefix
|
|
19
|
+
return `${opt.dynamicPackageNamePrefix}-default`;
|
|
20
20
|
return `${opt.dynamicPackageNamePrefix}-${(0, exports.generateKeyByOrder)(opt.order)}`;
|
|
21
21
|
};
|
|
22
22
|
exports.generateDefaultDynamicPackageName = generateDefaultDynamicPackageName;
|
|
@@ -37,7 +37,7 @@ const isNumber = (val) => {
|
|
|
37
37
|
};
|
|
38
38
|
exports.isNumber = isNumber;
|
|
39
39
|
const isDefaultDynamicPackageAsset = (opt, assetName) => {
|
|
40
|
-
const dynamicModuleRegExp = new RegExp(`^${opt.dynamicPackageNamePrefix}(?:-[a-z]{2})?/`);
|
|
40
|
+
const dynamicModuleRegExp = new RegExp(`^${opt.dynamicPackageNamePrefix}(?:-([a-z]{2}|default))?/`);
|
|
41
41
|
return dynamicModuleRegExp.test(assetName);
|
|
42
42
|
};
|
|
43
43
|
exports.isDefaultDynamicPackageAsset = isDefaultDynamicPackageAsset;
|
|
@@ -53,7 +53,8 @@ const isDynamicPackageAsset = (opt, assetName) => {
|
|
|
53
53
|
};
|
|
54
54
|
exports.isDynamicPackageAsset = isDynamicPackageAsset;
|
|
55
55
|
const isAsyncStyleDynamicPackageAsset = (opt, assetName) => {
|
|
56
|
-
const
|
|
56
|
+
const asyncStyleDynamicPackages = opt.customDynamicPackages.filter(item => item.asyncStyle);
|
|
57
|
+
const asyncStylDynamicPackageNames = asyncStyleDynamicPackages.map(item => (0, exports.generateCustomDynamicPackageName)(opt, item.name));
|
|
57
58
|
if (!asyncStylDynamicPackageNames.length)
|
|
58
59
|
return false;
|
|
59
60
|
return new RegExp(`^(${asyncStylDynamicPackageNames.join('|')})/`).test(assetName);
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAGO,MAAM,aAAa,GAAG,CAAC,IAAY,EAAE,GAAW,EAAE,EAAE;IACzD,IAAI,CAAC,IAAI,IAAI,GAAG,IAAI,CAAC;QAAE,OAAO,CAAC,CAAA;IAC/B,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;AAClD,CAAC,CAAA;AAHY,QAAA,aAAa,iBAGzB;AAEM,MAAM,kBAAkB,GAAG,CAAC,KAAa,EAAE,EAAE;IAClD,MAAM,QAAQ,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;IAClC,MAAM,WAAW,GAAG,MAAM,CAAC,YAAY,CAAC,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,EAAE,CAAC,CAAC,CAAA;IAC1E,MAAM,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC,QAAQ,GAAG,KAAK,GAAG,EAAE,CAAC,CAAA;IAC/D,OAAO,GAAG,WAAW,GAAG,YAAY,EAAE,CAAA;AACxC,CAAC,CAAA;AALY,QAAA,kBAAkB,sBAK9B;AAEM,MAAM,iCAAiC,GAAG,CAAC,GAAuC,EAAE,EAAE;IAC3F,IAAI,CAAC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B,GAAG,CAAC,GAAkB,EAAE,SAAiB,EAAE,EAAE;IACtF,OAAO,IAAA,6BAAqB,EAAC,GAAG,EAAE,SAAS,CAAC,IAAI,CAAC,IAAA,uCAA+B,EAAC,GAAG,EAAE,SAAS,CAAC,CAAA;AAClG,CAAC,CAAA;AAFY,QAAA,8BAA8B,kCAE1C;AAEM,MAAM,WAAW,GAAG,CAAC,MAAc,EAAE,SAAiB,EAAE,EAAE;IAC/D,OAAO,IAAI,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;AACpD,CAAC,CAAA;AAFY,QAAA,WAAW,eAEvB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taro-minify-pack/plugin-async-pack",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4-alpha.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"@babel/template": "^7.26.9",
|
|
22
22
|
"@babel/traverse": "^7.26.10",
|
|
23
23
|
"@babel/types": "^7.26.10",
|
|
24
|
+
"tapable": "^2.3.3",
|
|
24
25
|
"webpack": "^5.98.0",
|
|
25
26
|
"webpack-sources": "^3.2.3"
|
|
26
27
|
},
|
package/src/index.ts
CHANGED
|
@@ -3,14 +3,9 @@ import type { Module, PathData } from 'webpack'
|
|
|
3
3
|
import { transformWebpackRuntime } from './transform-webpack-runtime'
|
|
4
4
|
import {
|
|
5
5
|
TransformOpt,
|
|
6
|
-
TransformBeforeCompressionPlugin
|
|
7
|
-
PLUGIN_NAME as TransformBeforeCompressionPluginName
|
|
6
|
+
TransformBeforeCompressionPlugin
|
|
8
7
|
} from './transform-before-compression-plugin'
|
|
9
|
-
import {
|
|
10
|
-
InjectStyleComponentPlugin,
|
|
11
|
-
PLUGIN_NAME as InjectStyleComponentPluginName,
|
|
12
|
-
InjectStyleComponentName
|
|
13
|
-
} from './inject-style-component'
|
|
8
|
+
import { InjectDynamicStylePlugin } from './inject-dynamic-style'
|
|
14
9
|
import { transformAppConfig } from './transform-app-config'
|
|
15
10
|
import { transformPagesWXml } from './transform-pages-wxml'
|
|
16
11
|
import {
|
|
@@ -20,9 +15,10 @@ import {
|
|
|
20
15
|
} from './utils'
|
|
21
16
|
import { AsyncPackOpts } from './types'
|
|
22
17
|
import { transformAppStylesheet } from './transform-app-stylesheet'
|
|
23
|
-
import { MergeOutputPlugin
|
|
18
|
+
import { MergeOutputPlugin } from './merge-output'
|
|
24
19
|
|
|
25
|
-
export
|
|
20
|
+
export * from './types'
|
|
21
|
+
export * from './inject-dynamic-style'
|
|
26
22
|
|
|
27
23
|
const dynamicPackOptsDefaultOpt: AsyncPackOpts = {
|
|
28
24
|
dynamicPackageNamePrefix: 'dynamic-package',
|
|
@@ -80,7 +76,7 @@ export default (ctx: IPluginContext, pluginOpts: Partial<AsyncPackOpts>) => {
|
|
|
80
76
|
return [{ ...options, chunkFilename }]
|
|
81
77
|
})
|
|
82
78
|
|
|
83
|
-
chain.plugin(
|
|
79
|
+
chain.plugin(TransformBeforeCompressionPlugin.pluginName).use(TransformBeforeCompressionPlugin, [{
|
|
84
80
|
test: /^(runtime\.js|app\.wxss)$/,
|
|
85
81
|
transform: (opt: TransformOpt) => {
|
|
86
82
|
const { source, assetName, assets } = opt
|
|
@@ -91,12 +87,16 @@ export default (ctx: IPluginContext, pluginOpts: Partial<AsyncPackOpts>) => {
|
|
|
91
87
|
}
|
|
92
88
|
}])
|
|
93
89
|
|
|
94
|
-
chain.plugin(
|
|
95
|
-
test: (assetName: string) =>
|
|
90
|
+
chain.plugin(MergeOutputPlugin.pluginName).use(MergeOutputPlugin, [{
|
|
91
|
+
test: (assetName: string) => {
|
|
92
|
+
const isStyleAsset = matchSuffix('wxss', assetName)
|
|
93
|
+
const isInjectDynamicStyleAsset = new RegExp(`${InjectDynamicStylePlugin.componentName}\\.wxss$`).test(assetName)
|
|
94
|
+
return isStyleAsset && !isInjectDynamicStyleAsset && isSyncStyleDynamicPackageAsset(finalOpts, assetName)
|
|
95
|
+
},
|
|
96
96
|
outputFile: `${finalOpts.dynamicPackageNamePrefix}.wxss`
|
|
97
97
|
}])
|
|
98
98
|
|
|
99
|
-
chain.plugin(
|
|
99
|
+
chain.plugin(InjectDynamicStylePlugin.pluginName).use(InjectDynamicStylePlugin, [finalOpts])
|
|
100
100
|
})
|
|
101
101
|
|
|
102
102
|
ctx.modifyBuildAssets(({ assets }) => {
|
|
@@ -104,11 +104,12 @@ export default (ctx: IPluginContext, pluginOpts: Partial<AsyncPackOpts>) => {
|
|
|
104
104
|
|
|
105
105
|
if (!hasDynamicModule) return
|
|
106
106
|
|
|
107
|
-
const asyncComponents =
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
107
|
+
const asyncComponents = Object.keys(assets).reduce((result, assetName) => {
|
|
108
|
+
const regExp = new RegExp(`^(${finalOpts.dynamicPackageNamePrefix}-([^/]+)/${InjectDynamicStylePlugin.componentName})\\.json$`)
|
|
109
|
+
const [_, fullPathWithoutExt, key] = assetName.match(regExp) || []
|
|
110
|
+
if (!isDynamicPackageAsset(finalOpts, assetName) || !key) return result
|
|
111
|
+
const componentName = `${InjectDynamicStylePlugin.componentName}-${key}`
|
|
112
|
+
return { ...result, [componentName]: fullPathWithoutExt }
|
|
112
113
|
}, {})
|
|
113
114
|
|
|
114
115
|
transformAppConfig({ ...finalOpts, assets, asyncComponents })
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Compiler, Compilation } from 'webpack'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import { AsyncPackOpts } from './types'
|
|
4
|
+
import { RawSource } from 'webpack-sources'
|
|
5
|
+
import { generateCustomDynamicPackageName } from './utils'
|
|
6
|
+
import { SyncHook } from 'tapable'
|
|
7
|
+
|
|
8
|
+
export interface InjectDynamicStyleOpt{
|
|
9
|
+
key: string
|
|
10
|
+
stylesheet: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Hooks {
|
|
14
|
+
emitDynamicStyle: SyncHook<InjectDynamicStyleOpt>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type Opt = AsyncPackOpts
|
|
18
|
+
|
|
19
|
+
export class InjectDynamicStylePlugin {
|
|
20
|
+
private readonly opt: Opt
|
|
21
|
+
|
|
22
|
+
private readonly WXmlContent: string = '<block/>'
|
|
23
|
+
|
|
24
|
+
private readonly JsonContent:string = '{"component": true,"styleIsolation": "shared"}'
|
|
25
|
+
|
|
26
|
+
private readonly JsContent: string = 'Component({})'
|
|
27
|
+
|
|
28
|
+
public static readonly pluginName = 'InjectDynamicStyle'
|
|
29
|
+
|
|
30
|
+
public static readonly componentName = 'inject-dynamic-style'
|
|
31
|
+
|
|
32
|
+
private static readonly hooksMap: WeakMap<Compilation, Hooks> = new WeakMap<Compilation, Hooks>()
|
|
33
|
+
|
|
34
|
+
private dynamicStyleMap: Map<string, InjectDynamicStyleOpt> = new Map()
|
|
35
|
+
|
|
36
|
+
constructor (opt: Opt) {
|
|
37
|
+
this.opt = opt
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public static getCompilationHooks (compilation: Compilation) {
|
|
41
|
+
if (!this.hooksMap.has(compilation)) {
|
|
42
|
+
const emitDynamicStyle = new SyncHook<InjectDynamicStyleOpt>(['emitDynamicStyleOpt'])
|
|
43
|
+
this.hooksMap.set(compilation, { emitDynamicStyle })
|
|
44
|
+
}
|
|
45
|
+
return this.hooksMap.get(compilation)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
injectDynamicStyleCode (compilation: Compilation, opt:InjectDynamicStyleOpt) {
|
|
49
|
+
const { key, stylesheet } = opt
|
|
50
|
+
const componentPath = `${key}/${InjectDynamicStylePlugin.componentName}`
|
|
51
|
+
compilation.assets[`${componentPath}.wxss`] = new RawSource(stylesheet)
|
|
52
|
+
compilation.assets[`${componentPath}.wxml`] = new RawSource(this.WXmlContent)
|
|
53
|
+
compilation.assets[`${componentPath}.json`] = new RawSource(this.JsonContent)
|
|
54
|
+
compilation.assets[`${componentPath}.js`] = new RawSource(this.JsContent)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
apply (compiler: Compiler) {
|
|
58
|
+
compiler.hooks.compilation.tap(InjectDynamicStylePlugin.pluginName, (compilation: Compilation) => {
|
|
59
|
+
const hooks = InjectDynamicStylePlugin.getCompilationHooks(compilation)
|
|
60
|
+
|
|
61
|
+
const stage = compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL // 最早阶段,在优化前
|
|
62
|
+
|
|
63
|
+
hooks?.emitDynamicStyle.tap(InjectDynamicStylePlugin.pluginName, (opt) => this.dynamicStyleMap.set(opt.key, opt))
|
|
64
|
+
|
|
65
|
+
compilation.hooks.processAssets.tap({ name: InjectDynamicStylePlugin.pluginName, stage }, (assets) => {
|
|
66
|
+
const { customDynamicPackages } = this.opt
|
|
67
|
+
|
|
68
|
+
customDynamicPackages.forEach(customDynamicPackageItem => {
|
|
69
|
+
if (!customDynamicPackageItem.asyncStyle) return
|
|
70
|
+
|
|
71
|
+
const customDynamicPackageName = generateCustomDynamicPackageName(this.opt, customDynamicPackageItem.name)
|
|
72
|
+
|
|
73
|
+
const styleFileContent = Object.keys(assets).reduce((result, assetPath) => {
|
|
74
|
+
if (!new RegExp(`^${customDynamicPackageName}\\/.*\\.wxss$`).test(assetPath)) return result
|
|
75
|
+
const relativePath = path.posix.relative(customDynamicPackageName, assetPath)
|
|
76
|
+
const code = `@import './${relativePath}';`
|
|
77
|
+
return result + code + '\n'
|
|
78
|
+
}, '')
|
|
79
|
+
|
|
80
|
+
this.dynamicStyleMap.set(customDynamicPackageName, { key: customDynamicPackageName, stylesheet: styleFileContent })
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
this.dynamicStyleMap.forEach((opt) => this.injectDynamicStyleCode(compilation, opt))
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
}
|
package/src/merge-output.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Compiler, Compilation } from 'webpack'
|
|
2
2
|
|
|
3
|
-
export const PLUGIN_NAME = 'MergeOutput'
|
|
4
|
-
|
|
5
3
|
interface MergeOutputOpt {
|
|
6
4
|
test: (assetName: string) => boolean,
|
|
7
5
|
outputFile: string,
|
|
@@ -10,15 +8,17 @@ interface MergeOutputOpt {
|
|
|
10
8
|
export class MergeOutputPlugin {
|
|
11
9
|
private readonly opts: MergeOutputOpt
|
|
12
10
|
|
|
11
|
+
public static readonly pluginName = 'MergeOutput'
|
|
12
|
+
|
|
13
13
|
constructor (opts: MergeOutputOpt) {
|
|
14
14
|
this.opts = opts
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
apply (compiler: Compiler) {
|
|
18
|
-
compiler.hooks.compilation.tap(
|
|
18
|
+
compiler.hooks.compilation.tap(MergeOutputPlugin.pluginName, (compilation: Compilation) => {
|
|
19
19
|
const stage = compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL // 最早阶段,在优化前
|
|
20
20
|
|
|
21
|
-
compilation.hooks.processAssets.tap({ name:
|
|
21
|
+
compilation.hooks.processAssets.tap({ name: MergeOutputPlugin.pluginName, stage }, (assets) => {
|
|
22
22
|
const mergedResult = Object.keys(assets).reduce((result, item) => {
|
|
23
23
|
if (!this.opts.test(item)) return result
|
|
24
24
|
const code = assets[item].source().toString()
|
|
@@ -12,20 +12,20 @@ export interface PluginOpt {
|
|
|
12
12
|
transform: (opt: TransformOpt) => string;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export const PLUGIN_NAME = 'TransformBeforeCompression'
|
|
16
|
-
|
|
17
15
|
export class TransformBeforeCompressionPlugin {
|
|
18
16
|
private readonly opt: PluginOpt
|
|
19
17
|
|
|
18
|
+
public static readonly pluginName = 'TransformBeforeCompression'
|
|
19
|
+
|
|
20
20
|
constructor (opt: PluginOpt) {
|
|
21
21
|
this.opt = opt
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
apply (compiler: webpack.Compiler) {
|
|
25
|
-
compiler.hooks.compilation.tap(
|
|
25
|
+
compiler.hooks.compilation.tap(TransformBeforeCompressionPlugin.pluginName, (compilation: webpack.Compilation) => {
|
|
26
26
|
const stage = webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE // 压缩前
|
|
27
27
|
|
|
28
|
-
compilation.hooks.processAssets.tap({ name:
|
|
28
|
+
compilation.hooks.processAssets.tap({ name: TransformBeforeCompressionPlugin.pluginName, stage }, (assets) => {
|
|
29
29
|
const { test, transform } = this.opt
|
|
30
30
|
|
|
31
31
|
const assetNames = Object.keys(assets)
|
package/src/utils.ts
CHANGED
|
@@ -14,7 +14,7 @@ export const generateKeyByOrder = (order: number) => {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export const generateDefaultDynamicPackageName = (opt: AsyncPackOpts & { order?: number }) => {
|
|
17
|
-
if (!isNumber(opt.order) || opt.dynamicPackageCount <= 1) return opt.dynamicPackageNamePrefix
|
|
17
|
+
if (!isNumber(opt.order) || opt.dynamicPackageCount <= 1) return `${opt.dynamicPackageNamePrefix}-default`
|
|
18
18
|
return `${opt.dynamicPackageNamePrefix}-${generateKeyByOrder(opt.order!)}`
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -34,7 +34,7 @@ export const isNumber = (val: any) => {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
export const isDefaultDynamicPackageAsset = (opt: AsyncPackOpts, assetName: string) => {
|
|
37
|
-
const dynamicModuleRegExp = new RegExp(`^${opt.dynamicPackageNamePrefix}(?:-[a-z]{2})?/`)
|
|
37
|
+
const dynamicModuleRegExp = new RegExp(`^${opt.dynamicPackageNamePrefix}(?:-([a-z]{2}|default))?/`)
|
|
38
38
|
return dynamicModuleRegExp.test(assetName)
|
|
39
39
|
}
|
|
40
40
|
|
|
@@ -49,7 +49,8 @@ export const isDynamicPackageAsset = (opt: AsyncPackOpts, assetName: string) =>
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
export const isAsyncStyleDynamicPackageAsset = (opt: AsyncPackOpts, assetName: string) => {
|
|
52
|
-
const
|
|
52
|
+
const asyncStyleDynamicPackages = opt.customDynamicPackages.filter(item => item.asyncStyle)
|
|
53
|
+
const asyncStylDynamicPackageNames = asyncStyleDynamicPackages.map(item => generateCustomDynamicPackageName(opt, item.name))
|
|
53
54
|
if (!asyncStylDynamicPackageNames.length) return false
|
|
54
55
|
return new RegExp(`^(${asyncStylDynamicPackageNames.join('|')})/`).test(assetName)
|
|
55
56
|
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Compiler } from 'webpack';
|
|
2
|
-
import { AsyncPackOpts } from './types';
|
|
3
|
-
export declare const PLUGIN_NAME = "InjectStyleComponent";
|
|
4
|
-
export declare const InjectStyleComponentName = "inject-style";
|
|
5
|
-
type Opt = AsyncPackOpts;
|
|
6
|
-
export declare class InjectStyleComponentPlugin {
|
|
7
|
-
private readonly opt;
|
|
8
|
-
private readonly WXmlContent;
|
|
9
|
-
private readonly JsonContent;
|
|
10
|
-
private readonly JsContent;
|
|
11
|
-
constructor(opt: Opt);
|
|
12
|
-
apply(compiler: Compiler): void;
|
|
13
|
-
}
|
|
14
|
-
export {};
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.InjectStyleComponentPlugin = exports.InjectStyleComponentName = exports.PLUGIN_NAME = void 0;
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
|
-
const webpack_sources_1 = require("webpack-sources");
|
|
9
|
-
const utils_1 = require("./utils");
|
|
10
|
-
exports.PLUGIN_NAME = 'InjectStyleComponent';
|
|
11
|
-
exports.InjectStyleComponentName = 'inject-style';
|
|
12
|
-
class InjectStyleComponentPlugin {
|
|
13
|
-
constructor(opt) {
|
|
14
|
-
this.WXmlContent = '<block/>';
|
|
15
|
-
this.JsonContent = '{"component": true,"styleIsolation": "shared"}';
|
|
16
|
-
this.JsContent = 'Component({})';
|
|
17
|
-
this.opt = opt;
|
|
18
|
-
}
|
|
19
|
-
apply(compiler) {
|
|
20
|
-
compiler.hooks.compilation.tap(exports.PLUGIN_NAME, (compilation) => {
|
|
21
|
-
const stage = compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL; // 最早阶段,在优化前
|
|
22
|
-
compilation.hooks.processAssets.tap({ name: exports.PLUGIN_NAME, stage }, (assets) => {
|
|
23
|
-
const { customDynamicPackages } = this.opt;
|
|
24
|
-
customDynamicPackages.forEach(customDynamicPackageItem => {
|
|
25
|
-
if (!customDynamicPackageItem.asyncStyle)
|
|
26
|
-
return;
|
|
27
|
-
const customDynamicPackageName = (0, utils_1.generateCustomDynamicPackageName)(this.opt, customDynamicPackageItem.name);
|
|
28
|
-
const styleFileContent = Object.keys(assets).reduce((result, assetPath) => {
|
|
29
|
-
if (!new RegExp(`^${customDynamicPackageName}\\/.*\\.wxss$`).test(assetPath))
|
|
30
|
-
return result;
|
|
31
|
-
const relativePath = path_1.default.posix.relative(customDynamicPackageName, assetPath);
|
|
32
|
-
const code = `@import './${relativePath}';`;
|
|
33
|
-
return result + code + '\n';
|
|
34
|
-
}, '');
|
|
35
|
-
const componentPath = `${customDynamicPackageName}/${exports.InjectStyleComponentName}`;
|
|
36
|
-
compilation.assets[`${componentPath}.wxss`] = new webpack_sources_1.RawSource(styleFileContent);
|
|
37
|
-
compilation.assets[`${componentPath}.wxml`] = new webpack_sources_1.RawSource(this.WXmlContent);
|
|
38
|
-
compilation.assets[`${componentPath}.json`] = new webpack_sources_1.RawSource(this.JsonContent);
|
|
39
|
-
compilation.assets[`${componentPath}.js`] = new webpack_sources_1.RawSource(this.JsContent);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
exports.InjectStyleComponentPlugin = InjectStyleComponentPlugin;
|
|
46
|
-
//# sourceMappingURL=inject-style-component.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"inject-style-component.js","sourceRoot":"","sources":["../src/inject-style-component.ts"],"names":[],"mappings":";;;;;;AACA,gDAAuB;AAEvB,qDAA2C;AAC3C,mCAA0D;AAE7C,QAAA,WAAW,GAAG,sBAAsB,CAAA;AAEpC,QAAA,wBAAwB,GAAG,cAAc,CAAA;AAItD,MAAa,0BAA0B;IASrC,YAAa,GAAQ;QANJ,gBAAW,GAAW,UAAU,CAAA;QAEhC,gBAAW,GAAU,gDAAgD,CAAA;QAErE,cAAS,GAAW,eAAe,CAAA;QAGlD,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;IAChB,CAAC;IAED,KAAK,CAAE,QAAkB;QACvB,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,mBAAW,EAAE,CAAC,WAAwB,EAAE,EAAE;YACvE,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,WAAW,CAAC,+BAA+B,CAAA,CAAC,YAAY;YAEvF,WAAW,CAAC,KAAK,CAAC,aAAa,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,mBAAW,EAAE,KAAK,EAAE,EAAE,CAAC,MAAM,EAAE,EAAE;gBAC3E,MAAM,EAAE,qBAAqB,EAAE,GAAG,IAAI,CAAC,GAAG,CAAA;gBAE1C,qBAAqB,CAAC,OAAO,CAAC,wBAAwB,CAAC,EAAE;oBACvD,IAAI,CAAC,wBAAwB,CAAC,UAAU;wBAAE,OAAM;oBAEhD,MAAM,wBAAwB,GAAG,IAAA,wCAAgC,EAAC,IAAI,CAAC,GAAG,EAAE,wBA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
|
|
4
|
-
import { RawSource } from 'webpack-sources'
|
|
5
|
-
import { generateCustomDynamicPackageName } from './utils'
|
|
6
|
-
|
|
7
|
-
export const PLUGIN_NAME = 'InjectStyleComponent'
|
|
8
|
-
|
|
9
|
-
export const InjectStyleComponentName = 'inject-style'
|
|
10
|
-
|
|
11
|
-
type Opt = AsyncPackOpts
|
|
12
|
-
|
|
13
|
-
export class InjectStyleComponentPlugin {
|
|
14
|
-
private readonly opt: Opt
|
|
15
|
-
|
|
16
|
-
private readonly WXmlContent: string = '<block/>'
|
|
17
|
-
|
|
18
|
-
private readonly JsonContent:string = '{"component": true,"styleIsolation": "shared"}'
|
|
19
|
-
|
|
20
|
-
private readonly JsContent: string = 'Component({})'
|
|
21
|
-
|
|
22
|
-
constructor (opt: Opt) {
|
|
23
|
-
this.opt = opt
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
apply (compiler: Compiler) {
|
|
27
|
-
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation: Compilation) => {
|
|
28
|
-
const stage = compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL // 最早阶段,在优化前
|
|
29
|
-
|
|
30
|
-
compilation.hooks.processAssets.tap({ name: PLUGIN_NAME, stage }, (assets) => {
|
|
31
|
-
const { customDynamicPackages } = this.opt
|
|
32
|
-
|
|
33
|
-
customDynamicPackages.forEach(customDynamicPackageItem => {
|
|
34
|
-
if (!customDynamicPackageItem.asyncStyle) return
|
|
35
|
-
|
|
36
|
-
const customDynamicPackageName = generateCustomDynamicPackageName(this.opt, customDynamicPackageItem.name)
|
|
37
|
-
|
|
38
|
-
const styleFileContent = Object.keys(assets).reduce((result, assetPath) => {
|
|
39
|
-
if (!new RegExp(`^${customDynamicPackageName}\\/.*\\.wxss$`).test(assetPath)) return result
|
|
40
|
-
const relativePath = path.posix.relative(customDynamicPackageName, assetPath)
|
|
41
|
-
const code = `@import './${relativePath}';`
|
|
42
|
-
return result + code + '\n'
|
|
43
|
-
}, '')
|
|
44
|
-
|
|
45
|
-
const componentPath = `${customDynamicPackageName}/${InjectStyleComponentName}`
|
|
46
|
-
compilation.assets[`${componentPath}.wxss`] = new RawSource(styleFileContent)
|
|
47
|
-
compilation.assets[`${componentPath}.wxml`] = new RawSource(this.WXmlContent)
|
|
48
|
-
compilation.assets[`${componentPath}.json`] = new RawSource(this.JsonContent)
|
|
49
|
-
compilation.assets[`${componentPath}.js`] = new RawSource(this.JsContent)
|
|
50
|
-
})
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
}
|