@into-mini/sfc-split-plugin 0.1.2 → 0.4.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/helper/index.mjs DELETED
@@ -1,62 +0,0 @@
1
- export const COMPONENT_ROOT = 'as-components';
2
-
3
- function unique(...arr) {
4
- return [...new Set(arr)];
5
- }
6
-
7
- export function getAllPages(config) {
8
- const { entryPagePath, pages, subPackages, tabBar } = config ?? {};
9
-
10
- const { custom = false, list = [] } = tabBar ?? {};
11
-
12
- return unique(
13
- entryPagePath,
14
- ...(pages ?? []),
15
- ...list.map(({ pagePath }) => pagePath),
16
- ...(subPackages ?? []).flatMap(
17
- (subPackage) =>
18
- (subPackage.pages || []).map((page) => `${subPackage.root}/${page}`) ||
19
- [],
20
- ),
21
- custom === true ? 'custom-tab-bar/index' : '',
22
- ).filter(Boolean);
23
- }
24
-
25
- export function patchConfig(json) {
26
- const object = structuredClone(json ?? {});
27
-
28
- object.pages ??= [];
29
-
30
- if (object.tabBar?.list?.length > 0) {
31
- for (const tab of object.tabBar.list) {
32
- if (tab.pagePath && !object.pages.includes(tab.pagePath)) {
33
- object.pages.push(tab.pagePath);
34
- }
35
- }
36
- }
37
-
38
- object.subPackages ??= [];
39
- object.preloadRule ??= {};
40
-
41
- for (const page of object.pages) {
42
- object.preloadRule[page] ??= {};
43
-
44
- object.preloadRule[page].network = 'all';
45
- object.preloadRule[page].packages ??= [];
46
-
47
- if (!object.preloadRule[page].packages.includes(COMPONENT_ROOT)) {
48
- object.preloadRule[page].packages.push(COMPONENT_ROOT);
49
- }
50
- }
51
-
52
- if (
53
- !object.subPackages.some((subPackage) => subPackage.root === COMPONENT_ROOT)
54
- ) {
55
- object.subPackages.push({
56
- root: COMPONENT_ROOT,
57
- pages: ['fake'],
58
- });
59
- }
60
-
61
- return object;
62
- }
package/helper/read.mjs DELETED
@@ -1,37 +0,0 @@
1
- import { readFileSync } from 'node:fs';
2
- import { resolve } from 'node:path';
3
-
4
- import { parse as yamlParse } from 'yaml';
5
-
6
- function tryReadFileWithParsers(base, name, ext, parser) {
7
- const filePath = resolve(base, `${name}${ext}`);
8
-
9
- try {
10
- const content = readFileSync(filePath, 'utf8');
11
-
12
- return {
13
- filePath,
14
- config: (parser ? parser(content) : content) || {},
15
- };
16
- } catch {
17
- return false;
18
- }
19
- }
20
-
21
- const candidates = [
22
- { ext: '.yaml', parser: yamlParse },
23
- { ext: '.yml', parser: yamlParse },
24
- { ext: '.json', parser: JSON.parse },
25
- ];
26
-
27
- export function readConfig(base, name) {
28
- for (const { ext, parser } of candidates) {
29
- const result = tryReadFileWithParsers(base, name, ext, parser);
30
-
31
- if (result !== false) {
32
- return result;
33
- }
34
- }
35
-
36
- return false;
37
- }
@@ -1,39 +0,0 @@
1
- // @ts-expect-error -------------------
2
- import babel from '@babel/core';
3
-
4
- export function transformJS(input, absoluteFrom) {
5
- const minified = process.env.NODE_ENV === 'production';
6
-
7
- if (!absoluteFrom.endsWith('.js')) {
8
- return input;
9
- }
10
-
11
- const result = babel.transformSync(input, {
12
- presets: [
13
- [
14
- 'babel-preset-evergreen',
15
- {
16
- usage: 'pure',
17
- mini: true,
18
- },
19
- ],
20
- ],
21
- plugins: [
22
- ['@babel/plugin-transform-modules-commonjs', { importInterop: 'none' }],
23
- ],
24
- targets: 'ios 12, chrome 86',
25
- configFile: false,
26
- babelrc: false,
27
- filename: 'a.mjs',
28
- sourceType: 'commonjs',
29
- compact: !minified,
30
- retainLines: !minified,
31
- envName: process.env.NODE_ENV,
32
- minified,
33
- });
34
-
35
- return (result?.code || input).replace(
36
- ';Object.defineProperty(exports,"__esModule",{value:true});',
37
- ';',
38
- );
39
- }
package/helper/utils.mjs DELETED
@@ -1,6 +0,0 @@
1
- export const configKeys = {
2
- app: 'app-json',
3
- projectPrivate: 'project.private.config',
4
- project: 'project.config',
5
- hack: 'hack.entry',
6
- };
@@ -1,140 +0,0 @@
1
- import { createHash } from 'node:crypto';
2
- import { join, relative, resolve } from 'node:path';
3
- import { fileURLToPath } from 'node:url';
4
-
5
- import { toJSONString } from '@into-mini/sfc-transformer/utils.mjs';
6
- import slash from 'slash';
7
-
8
- function createShortHash(input) {
9
- return createHash('sha256').update(input).digest('hex').slice(0, 8);
10
- }
11
-
12
- function reach(path) {
13
- return fileURLToPath(import.meta.resolve(path));
14
- }
15
-
16
- function handleImport({
17
- toThis,
18
- addSmartEntry,
19
- componentRoot,
20
- context,
21
- rootContext,
22
- maps,
23
- callback,
24
- }) {
25
- if (Object.keys(maps).length > 0) {
26
- for (const [name, path] of Object.entries(maps)) {
27
- if (path.endsWith('.vue') && !path.startsWith('plugin://')) {
28
- try {
29
- const absolutePath = slash(
30
- path.startsWith('.') ? resolve(context, path) : reach(path),
31
- );
32
- const relativePath = slash(relative(rootContext, absolutePath));
33
- const hack = relativePath.startsWith('..');
34
- const entryName = hack
35
- ? [
36
- componentRoot,
37
- absolutePath
38
- .split('/')
39
- .slice(-2)
40
- .join('/')
41
- .replace(/\.vue$/, ''),
42
- createShortHash(slash(relativePath)),
43
- ].join('/')
44
- : relativePath.replace(/\.vue$/, '');
45
- const placer = toThis(entryName);
46
- callback({
47
- name,
48
- placer,
49
- });
50
-
51
- const entryPath = relativePath.startsWith('..')
52
- ? absolutePath
53
- : `./${relativePath}`;
54
-
55
- this.addDependency(resolve(absolutePath));
56
- this.addMissingDependency(resolve(absolutePath));
57
- addSmartEntry({
58
- name: entryName,
59
- path: entryPath,
60
- });
61
- } catch (error) {
62
- console.error(error);
63
- }
64
- }
65
- }
66
- }
67
- }
68
-
69
- export default function loader(source, map, meta) {
70
- this.cacheable();
71
- const callback = this.async();
72
- const { componentRoot } = this.getOptions();
73
- const { entryName: thisEntryName } = this;
74
- const resourcePath = slash(this.resourcePath);
75
- const { paths, config, script } = this.processSfcFile({
76
- source,
77
- resourcePath,
78
- });
79
- const { rootContext, context } = this;
80
-
81
- for (const path of paths) {
82
- const filePath = join(rootContext, path);
83
- this.addDependency(filePath);
84
- this.addMissingDependency(filePath);
85
- }
86
-
87
- function toThis(entryName) {
88
- return slash(relative(`/${thisEntryName}/..`, `/${entryName}`));
89
- }
90
-
91
- const addSmartEntry = (io) => {
92
- this.addSmartEntry(io);
93
- };
94
-
95
- if (config?.usingComponents) {
96
- handleImport.bind(this)({
97
- toThis,
98
- addSmartEntry,
99
- componentRoot,
100
- context,
101
- rootContext,
102
- maps: config.usingComponents,
103
- callback({ name, placer }) {
104
- config.usingComponents[name] = placer;
105
-
106
- if (placer.includes(componentRoot)) {
107
- config.componentPlaceholder ??= {};
108
- config.componentPlaceholder[name] = 'view';
109
- }
110
- },
111
- });
112
- }
113
-
114
- if (config?.componentGenerics) {
115
- handleImport.bind(this)({
116
- toThis,
117
- addSmartEntry,
118
- componentRoot,
119
- context,
120
- rootContext,
121
- maps: Object.fromEntries(
122
- Object.entries(config.componentGenerics)
123
- .filter(([_, item]) => item?.default)
124
- .map(([key, item]) => [key, item.default]),
125
- ),
126
- callback({ name, placer }) {
127
- config.componentGenerics[name].default = placer;
128
- },
129
- });
130
- }
131
-
132
- const file = [
133
- ...paths
134
- .map((path) => relative(`${resourcePath}/..`, path))
135
- .map((path) => `import "./${path}";`),
136
- script,
137
- ].join('\n');
138
- this.emitFile(`${thisEntryName}.json`, toJSONString(config));
139
- callback(null, file, map, meta);
140
- }
@@ -1,5 +0,0 @@
1
- export default function loader() {
2
- console.log(this._compiler.__entries__);
3
-
4
- return '';
5
- }
@@ -1,85 +0,0 @@
1
- export class AddEntryPlugin {
2
- PLUGIN_NAME = 'AddEntryPlugin';
3
-
4
- #addSmartEntry({ name, path, layer }) {
5
- if (this.compiler.__entries__.get(name) !== path) {
6
- this.compiler.__entries__.set(name, { path, layer });
7
- }
8
- }
9
-
10
- #addEntries(compilation) {
11
- const { compiler } = this;
12
-
13
- const { createDependency } = compiler.webpack.EntryPlugin;
14
-
15
- compilation.hooks.buildModule.tap(this.PLUGIN_NAME, () => {
16
- for (const [name, { path, layer }] of compiler.__entries__.entries()) {
17
- compilation.addEntry(
18
- compiler.context,
19
- createDependency(path, { name }),
20
- {
21
- name,
22
- import: [path],
23
- layer,
24
- },
25
- (err) => {
26
- if (err) {
27
- throw err;
28
- } else {
29
- compilation.fileDependencies.add(path);
30
- }
31
- },
32
- );
33
- }
34
- });
35
- }
36
-
37
- #expose(compiler) {
38
- this.compiler = compiler;
39
-
40
- const { PLUGIN_NAME } = this;
41
-
42
- const {
43
- NormalModule: { getCompilationHooks },
44
- } = compiler.webpack;
45
-
46
- Object.defineProperty(compiler, 'addSmartEntry', {
47
- enumerable: true,
48
- configurable: false,
49
- value: (options) => {
50
- this.#addSmartEntry(options);
51
- },
52
- });
53
-
54
- compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
55
- getCompilationHooks(compilation).loader.tap(
56
- PLUGIN_NAME,
57
- (loaderContext) => {
58
- Object.defineProperty(loaderContext, 'addSmartEntry', {
59
- enumerable: true,
60
- configurable: false,
61
- value: (options) => {
62
- this.#addSmartEntry(options);
63
- },
64
- });
65
- },
66
- );
67
- });
68
- }
69
-
70
- apply(compiler) {
71
- this.#expose(compiler);
72
-
73
- const { PLUGIN_NAME } = this;
74
-
75
- compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
76
- this.#addEntries(compilation);
77
- });
78
- compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
79
- this.#addEntries(compilation);
80
- });
81
- compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => {
82
- this.#addEntries(compilation);
83
- });
84
- }
85
- }
@@ -1,62 +0,0 @@
1
- import { readFileSync } from 'node:fs';
2
- import { extname, join, relative } from 'node:path';
3
- import { fileURLToPath } from 'node:url';
4
-
5
- import { CLSX_PLACEHOLDER } from '@into-mini/sfc-transformer/utils.mjs';
6
- import slash from 'slash';
7
-
8
- // WXS文件输出路径
9
- const WXS_FILENAME = 'wxs/clsx.wxs';
10
-
11
- /**
12
- * 将clsx.wxs文件添加到编译结果中,并替换WXML文件中的占位符。
13
- * 只在发现CLSX_PLACEHOLDER时添加wxs文件,且只添加一次。
14
- */
15
- export class AddWxsPlugin {
16
- PLUGIN_NAME = 'AddWxsPlugin';
17
-
18
- apply(compiler) {
19
- const { RawSource } = compiler.webpack.sources;
20
-
21
- compiler.hooks.compilation.tap(this.PLUGIN_NAME, (compilation) => {
22
- compilation.hooks.processAssets.tap(
23
- {
24
- name: this.PLUGIN_NAME,
25
- stage: compilation.constructor.PROCESS_ASSETS_STAGE_ADDITIONAL,
26
- },
27
- (assets) => this.#processAssets(assets, compilation, RawSource),
28
- );
29
- });
30
- }
31
-
32
- #processAssets(assets, compilation, RawSource) {
33
- // 处理所有wxml文件
34
- for (const [filename, source] of Object.entries(assets)) {
35
- if (extname(filename) === '.wxml') {
36
- const content = source.source().toString();
37
-
38
- if (content.includes(CLSX_PLACEHOLDER)) {
39
- this.#addWxsFile(compilation, RawSource);
40
-
41
- this.#replaceSource(compilation, RawSource, {
42
- filename,
43
- content,
44
- });
45
- }
46
- }
47
- }
48
- }
49
-
50
- #replaceSource(compilation, RawSource, { filename, content }) {
51
- const relativePath = slash(relative(join(filename, '..'), WXS_FILENAME));
52
- const newContent = content.replace(CLSX_PLACEHOLDER, relativePath);
53
- compilation.updateAsset(filename, new RawSource(newContent));
54
- }
55
-
56
- #addWxsFile(compilation, RawSource) {
57
- const wxsPath = import.meta.resolve('@into-mini/clsx/index.wxs');
58
- const wxsContent = readFileSync(fileURLToPath(wxsPath), 'utf8');
59
-
60
- compilation.emitAsset(WXS_FILENAME, new RawSource(wxsContent));
61
- }
62
- }
@@ -1,84 +0,0 @@
1
- import { fileURLToPath } from 'node:url';
2
-
3
- import { patchConfig } from '../helper/index.mjs';
4
- import { configKeys } from '../helper/utils.mjs';
5
-
6
- function reach(path) {
7
- return fileURLToPath(import.meta.resolve(path));
8
- }
9
-
10
- const emptyJSON = reach('../helper/empty.json');
11
- const yamlLoader = reach('yaml-patch-loader');
12
-
13
- export class CopyConfigPlugin {
14
- constructor({ type = false } = {}) {
15
- this.type = type;
16
- }
17
-
18
- addConfigSmartEntry({
19
- layer,
20
- from = layer,
21
- name = layer,
22
- filename = from,
23
- options,
24
- }) {
25
- const path = `./${from}.yaml`;
26
-
27
- this.compiler.options.entry[name] = {
28
- import: [path],
29
- layer,
30
- runtime: false,
31
- filename,
32
- };
33
-
34
- this.compiler.options.resolve.fallback[path] = emptyJSON;
35
-
36
- this.compiler.options.module.rules.push({
37
- issuerLayer: layer,
38
- loader: yamlLoader,
39
- type: 'asset/resource',
40
- generator: {
41
- filename: `${filename}.json`,
42
- },
43
- options,
44
- });
45
- }
46
-
47
- apply(compiler) {
48
- const { type } = this;
49
- this.compiler = compiler;
50
-
51
- if (type) {
52
- this.addConfigSmartEntry({
53
- layer: configKeys.project,
54
- options: {
55
- modify: (json) => ({
56
- srcMiniprogramRoot: '',
57
- miniprogramRoot: '',
58
- pluginRoot: '',
59
- ...json,
60
- compileType: type,
61
- }),
62
- },
63
- });
64
-
65
- this.addConfigSmartEntry({
66
- layer: configKeys.projectPrivate,
67
- });
68
-
69
- if (this.type === 'miniprogram') {
70
- this.addConfigSmartEntry({
71
- layer: configKeys.app,
72
- from: 'app',
73
- options: {
74
- modify: patchConfig,
75
- },
76
- });
77
-
78
- this.compiler.options.entry.app = {
79
- import: ['./app'],
80
- };
81
- }
82
- }
83
- }
84
- }
@@ -1,35 +0,0 @@
1
- import { COMPONENT_ROOT } from '../helper/index.mjs';
2
-
3
- const files = {
4
- '/fake.json': '{}',
5
- '/fake.js': '/**用于创建分包的假页面**/',
6
- '/fake.wxml': '<!--用于创建分包的假页面-->',
7
- };
8
-
9
- export class EmitFakePlugin {
10
- PLUGIN_NAME = 'EmitFakePlugin';
11
-
12
- apply(compiler) {
13
- const {
14
- sources: { RawSource },
15
- Compilation: { PROCESS_ASSETS_STAGE_ADDITIONAL },
16
- } = compiler.webpack;
17
-
18
- compiler.hooks.make.tap(this.PLUGIN_NAME, (compilation) => {
19
- compilation.hooks.processAssets.tap(
20
- {
21
- name: this.PLUGIN_NAME,
22
- stage: PROCESS_ASSETS_STAGE_ADDITIONAL,
23
- },
24
- () => {
25
- for (const [path, content] of Object.entries(files)) {
26
- compilation.emitAsset(
27
- COMPONENT_ROOT + path,
28
- new RawSource(content),
29
- );
30
- }
31
- },
32
- );
33
- });
34
- }
35
- }
@@ -1,109 +0,0 @@
1
- /* eslint-disable no-continue */
2
- import path from 'node:path';
3
-
4
- const pluginName = 'EntryRenamePlugin';
5
-
6
- export class EntryRenamePlugin {
7
- options;
8
-
9
- constructor(options = {}) {
10
- this.options = options;
11
- }
12
-
13
- getIssuerPath(issuerModule) {
14
- return (
15
- issuerModule?.nameForCondition?.() ??
16
- issuerModule?.resource ??
17
- issuerModule?.identifier?.()
18
- );
19
- }
20
-
21
- matchIssuer(issuerPath) {
22
- const { issuer } = this.options;
23
-
24
- if (!issuer) {
25
- return true;
26
- }
27
-
28
- if (!issuerPath) {
29
- return false;
30
- }
31
-
32
- if (issuer instanceof RegExp) {
33
- return issuer.test(issuerPath);
34
- }
35
-
36
- if (typeof issuer === 'function') {
37
- return issuer(issuerPath);
38
- }
39
-
40
- return issuerPath.includes(issuer);
41
- }
42
-
43
- matchTest(filename) {
44
- const { test } = this.options;
45
-
46
- if (!test) {
47
- return true;
48
- }
49
-
50
- if (test instanceof RegExp) {
51
- return test.test(filename);
52
- }
53
-
54
- if (typeof test === 'function') {
55
- return test(filename);
56
- }
57
-
58
- return filename.includes(test);
59
- }
60
-
61
- apply(compiler) {
62
- compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
63
- const { Compilation } = compiler.webpack;
64
- compilation.hooks.processAssets.tap(
65
- { name: pluginName, stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS },
66
- () => {
67
- for (const module of compilation.modules) {
68
- const filename = module.buildInfo?.filename;
69
-
70
- if (!filename) {
71
- continue;
72
- }
73
-
74
- if (!this.matchTest(filename)) {
75
- continue;
76
- }
77
-
78
- const issuerModule = compilation.moduleGraph.getIssuer(module);
79
- const issuerPath = this.getIssuerPath(issuerModule);
80
-
81
- if (!this.matchIssuer(issuerPath)) {
82
- continue;
83
- }
84
-
85
- const chunks = compilation.chunkGraph.getModuleChunks(module);
86
- const entryChunk = [...chunks].find((chunk) => chunk.name);
87
-
88
- if (!entryChunk?.name) {
89
- continue;
90
- }
91
-
92
- const ext = path.extname(filename);
93
- const newName = `${entryChunk.name}${ext}`;
94
-
95
- if (newName === filename) {
96
- continue;
97
- }
98
-
99
- if (compilation.getAsset(newName)) {
100
- continue;
101
- }
102
-
103
- compilation.renameAsset(filename, newName);
104
- }
105
- },
106
- );
107
- });
108
- }
109
- }