jiek 2.3.1 → 2.3.3
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/dist/.internal/bin/common.cjs +7 -7
- package/dist/.internal/bin/common.d.cts +2 -0
- package/dist/.internal/bin/common.d.ts +2 -0
- package/dist/.internal/bin/common.js +7 -7
- package/dist/.internal/rollup/base.d.cts +46 -0
- package/dist/.internal/rollup/base.d.ts +46 -0
- package/dist/rollup/index.cjs +135 -3
- package/dist/rollup/index.js +136 -5
- package/dist/rollup-plugin-utils.cjs +4135 -0
- package/dist/rollup-plugin-utils.d.cts +10 -0
- package/dist/rollup-plugin-utils.d.ts +10 -0
- package/dist/rollup-plugin-utils.js +4127 -0
- package/package.json +11 -4
- package/rollup-plugin-utils/package.json +1 -0
- package/src/rollup/base.ts +15 -0
- package/src/rollup/index.ts +11 -3
- package/src/rollup/plugins/replace.ts +185 -0
- package/src/rollup-plugin-utils.ts +32 -0
@@ -37,7 +37,7 @@ var Koa__default = /*#__PURE__*/_interopDefault(Koa);
|
|
37
37
|
|
38
38
|
var name = "jiek";
|
39
39
|
var type = "module";
|
40
|
-
var version = "2.3.
|
40
|
+
var version = "2.3.2";
|
41
41
|
var description$1 = "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.";
|
42
42
|
var author = "YiJie <yijie4188@gmail.com>";
|
43
43
|
var homepage = "https://github.com/NWYLZW/jiek/tree/master/packages/jiek#readme";
|
@@ -63,7 +63,8 @@ var exports$1 = {
|
|
63
63
|
require: "./bin-helper/index.cjs",
|
64
64
|
"default": "./bin-helper/index.js"
|
65
65
|
},
|
66
|
-
"./rollup": "./src/rollup/index.ts"
|
66
|
+
"./rollup": "./src/rollup/index.ts",
|
67
|
+
"./rollup-plugin-utils": "./src/rollup-plugin-utils.ts"
|
67
68
|
};
|
68
69
|
var imports = {
|
69
70
|
"#~/*": "./src/*.ts"
|
@@ -88,6 +89,7 @@ var scripts = {
|
|
88
89
|
test: "vitest run"
|
89
90
|
};
|
90
91
|
var peerDependencies = {
|
92
|
+
"@ast-grep/napi": "^0.32.3",
|
91
93
|
"@pnpm/filter-workspace-packages": "^7.2.13||^8.0.0||^9.0.0||^10.0.0||>=1000.0.0",
|
92
94
|
"@rollup/plugin-terser": "^0.4.4",
|
93
95
|
"esbuild-register": "^3.5.0",
|
@@ -113,9 +115,11 @@ var dependencies = {
|
|
113
115
|
"js-yaml": "^4.1.0",
|
114
116
|
"jsonc-parser": "^3.2.1",
|
115
117
|
koa: "^2.15.3",
|
118
|
+
"magic-string": "^0.30.17",
|
116
119
|
rollup: "^4.0.0"
|
117
120
|
};
|
118
121
|
var devDependencies = {
|
122
|
+
"@ast-grep/napi": "^0.32.3",
|
119
123
|
"@npm/types": "^1.0.2",
|
120
124
|
"@pnpm/filter-workspace-packages": "^7.2.13",
|
121
125
|
"@pnpm/workspace.pkgs-graph": "^2.0.15",
|
@@ -136,9 +140,6 @@ var devDependencies = {
|
|
136
140
|
"rollup-plugin-swc3": "^0.12.1",
|
137
141
|
"vite-bundle-analyzer": "0.16.0-beta.3"
|
138
142
|
};
|
139
|
-
var publishConfig = {
|
140
|
-
directory: "./dist/.internal"
|
141
|
-
};
|
142
143
|
var pkg = {
|
143
144
|
name: name,
|
144
145
|
type: type,
|
@@ -156,8 +157,7 @@ var pkg = {
|
|
156
157
|
scripts: scripts,
|
157
158
|
peerDependencies: peerDependencies,
|
158
159
|
dependencies: dependencies,
|
159
|
-
devDependencies: devDependencies
|
160
|
-
publishConfig: publishConfig
|
160
|
+
devDependencies: devDependencies
|
161
161
|
};
|
162
162
|
|
163
163
|
const entriesDescription = `
|
@@ -30,7 +30,7 @@ import 'node:stream/promises';
|
|
30
30
|
|
31
31
|
var name = "jiek";
|
32
32
|
var type = "module";
|
33
|
-
var version = "2.3.
|
33
|
+
var version = "2.3.2";
|
34
34
|
var description$1 = "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.";
|
35
35
|
var author = "YiJie <yijie4188@gmail.com>";
|
36
36
|
var homepage = "https://github.com/NWYLZW/jiek/tree/master/packages/jiek#readme";
|
@@ -56,7 +56,8 @@ var exports = {
|
|
56
56
|
require: "./bin-helper/index.cjs",
|
57
57
|
"default": "./bin-helper/index.js"
|
58
58
|
},
|
59
|
-
"./rollup": "./src/rollup/index.ts"
|
59
|
+
"./rollup": "./src/rollup/index.ts",
|
60
|
+
"./rollup-plugin-utils": "./src/rollup-plugin-utils.ts"
|
60
61
|
};
|
61
62
|
var imports = {
|
62
63
|
"#~/*": "./src/*.ts"
|
@@ -81,6 +82,7 @@ var scripts = {
|
|
81
82
|
test: "vitest run"
|
82
83
|
};
|
83
84
|
var peerDependencies = {
|
85
|
+
"@ast-grep/napi": "^0.32.3",
|
84
86
|
"@pnpm/filter-workspace-packages": "^7.2.13||^8.0.0||^9.0.0||^10.0.0||>=1000.0.0",
|
85
87
|
"@rollup/plugin-terser": "^0.4.4",
|
86
88
|
"esbuild-register": "^3.5.0",
|
@@ -106,9 +108,11 @@ var dependencies = {
|
|
106
108
|
"js-yaml": "^4.1.0",
|
107
109
|
"jsonc-parser": "^3.2.1",
|
108
110
|
koa: "^2.15.3",
|
111
|
+
"magic-string": "^0.30.17",
|
109
112
|
rollup: "^4.0.0"
|
110
113
|
};
|
111
114
|
var devDependencies = {
|
115
|
+
"@ast-grep/napi": "^0.32.3",
|
112
116
|
"@npm/types": "^1.0.2",
|
113
117
|
"@pnpm/filter-workspace-packages": "^7.2.13",
|
114
118
|
"@pnpm/workspace.pkgs-graph": "^2.0.15",
|
@@ -129,9 +133,6 @@ var devDependencies = {
|
|
129
133
|
"rollup-plugin-swc3": "^0.12.1",
|
130
134
|
"vite-bundle-analyzer": "0.16.0-beta.3"
|
131
135
|
};
|
132
|
-
var publishConfig = {
|
133
|
-
directory: "./dist/.internal"
|
134
|
-
};
|
135
136
|
var pkg = {
|
136
137
|
name: name,
|
137
138
|
type: type,
|
@@ -149,8 +150,7 @@ var pkg = {
|
|
149
150
|
scripts: scripts,
|
150
151
|
peerDependencies: peerDependencies,
|
151
152
|
dependencies: dependencies,
|
152
|
-
devDependencies: devDependencies
|
153
|
-
publishConfig: publishConfig
|
153
|
+
devDependencies: devDependencies
|
154
154
|
};
|
155
155
|
|
156
156
|
const entriesDescription = `
|
@@ -2,6 +2,38 @@ import * as _rollup_plugin_terser from '@rollup/plugin-terser';
|
|
2
2
|
import * as rollup_plugin_swc3 from 'rollup-plugin-swc3';
|
3
3
|
import * as rollup_plugin_esbuild from 'rollup-plugin-esbuild';
|
4
4
|
import { OutputOptions, InputPluginOption } from 'rollup';
|
5
|
+
import { SgNode, Lang } from '@ast-grep/napi';
|
6
|
+
import { FilterOptions } from 'jiek/rollup-plugin-utils';
|
7
|
+
|
8
|
+
type Mode = 'string' | 'ast-grep';
|
9
|
+
type ReplacementFuncCtx = {
|
10
|
+
type: 'transform' | 'renderChunk';
|
11
|
+
id: string;
|
12
|
+
code: string;
|
13
|
+
mode: Mode;
|
14
|
+
} & ({
|
15
|
+
mode?: 'string';
|
16
|
+
start: number;
|
17
|
+
end: number;
|
18
|
+
} | {
|
19
|
+
mode?: 'ast-grep';
|
20
|
+
$: ((name: string) => string | undefined) & ((template: {
|
21
|
+
raw: readonly string[];
|
22
|
+
}) => string | undefined);
|
23
|
+
node: SgNode;
|
24
|
+
lang: Lang;
|
25
|
+
});
|
26
|
+
type Falsy = false | null | undefined;
|
27
|
+
type ReplacementFunc = (ctx: ReplacementFuncCtx) => string | Falsy;
|
28
|
+
type Replacements = Record<string, string | Falsy | ReplacementFunc>;
|
29
|
+
type ReplaceOptions = FilterOptions & {
|
30
|
+
/**
|
31
|
+
* @default 'string'
|
32
|
+
*/
|
33
|
+
mode?: Mode;
|
34
|
+
sourcemap?: boolean;
|
35
|
+
values?: Replacements;
|
36
|
+
};
|
5
37
|
|
6
38
|
type Mapping2ROO<K extends keyof OutputOptions> = OutputOptions[K] | {
|
7
39
|
js?: OutputOptions[K];
|
@@ -114,6 +146,20 @@ interface TemplateOptions {
|
|
114
146
|
* ```
|
115
147
|
*/
|
116
148
|
injects?: Record<string, string | [string, string]>;
|
149
|
+
/**
|
150
|
+
* Replace the specified content in the code.
|
151
|
+
*
|
152
|
+
* @example
|
153
|
+
* ```js
|
154
|
+
* {
|
155
|
+
* 'process.env.DEBUG': 'false',
|
156
|
+
* 'process.env.NODE_ENV': JSON.stringify('production'),
|
157
|
+
* 'process.env.BUILD_PATH': ctx => JSON.stringify(ctx.id)
|
158
|
+
* }
|
159
|
+
* ```
|
160
|
+
*/
|
161
|
+
replacements?: Replacements;
|
162
|
+
replacementsOptions?: Pick<ReplaceOptions, 'mode' | 'include' | 'exclude'>;
|
117
163
|
}
|
118
164
|
|
119
165
|
export { BUILDER_TYPES, BUILDER_TYPE_PACKAGE_NAME_MAP, type ConfigGenerateContext, type Mapping2ROO, type OutputControl, type TemplateOptions };
|
@@ -2,6 +2,38 @@ import * as _rollup_plugin_terser from '@rollup/plugin-terser';
|
|
2
2
|
import * as rollup_plugin_swc3 from 'rollup-plugin-swc3';
|
3
3
|
import * as rollup_plugin_esbuild from 'rollup-plugin-esbuild';
|
4
4
|
import { OutputOptions, InputPluginOption } from 'rollup';
|
5
|
+
import { SgNode, Lang } from '@ast-grep/napi';
|
6
|
+
import { FilterOptions } from 'jiek/rollup-plugin-utils';
|
7
|
+
|
8
|
+
type Mode = 'string' | 'ast-grep';
|
9
|
+
type ReplacementFuncCtx = {
|
10
|
+
type: 'transform' | 'renderChunk';
|
11
|
+
id: string;
|
12
|
+
code: string;
|
13
|
+
mode: Mode;
|
14
|
+
} & ({
|
15
|
+
mode?: 'string';
|
16
|
+
start: number;
|
17
|
+
end: number;
|
18
|
+
} | {
|
19
|
+
mode?: 'ast-grep';
|
20
|
+
$: ((name: string) => string | undefined) & ((template: {
|
21
|
+
raw: readonly string[];
|
22
|
+
}) => string | undefined);
|
23
|
+
node: SgNode;
|
24
|
+
lang: Lang;
|
25
|
+
});
|
26
|
+
type Falsy = false | null | undefined;
|
27
|
+
type ReplacementFunc = (ctx: ReplacementFuncCtx) => string | Falsy;
|
28
|
+
type Replacements = Record<string, string | Falsy | ReplacementFunc>;
|
29
|
+
type ReplaceOptions = FilterOptions & {
|
30
|
+
/**
|
31
|
+
* @default 'string'
|
32
|
+
*/
|
33
|
+
mode?: Mode;
|
34
|
+
sourcemap?: boolean;
|
35
|
+
values?: Replacements;
|
36
|
+
};
|
5
37
|
|
6
38
|
type Mapping2ROO<K extends keyof OutputOptions> = OutputOptions[K] | {
|
7
39
|
js?: OutputOptions[K];
|
@@ -114,6 +146,20 @@ interface TemplateOptions {
|
|
114
146
|
* ```
|
115
147
|
*/
|
116
148
|
injects?: Record<string, string | [string, string]>;
|
149
|
+
/**
|
150
|
+
* Replace the specified content in the code.
|
151
|
+
*
|
152
|
+
* @example
|
153
|
+
* ```js
|
154
|
+
* {
|
155
|
+
* 'process.env.DEBUG': 'false',
|
156
|
+
* 'process.env.NODE_ENV': JSON.stringify('production'),
|
157
|
+
* 'process.env.BUILD_PATH': ctx => JSON.stringify(ctx.id)
|
158
|
+
* }
|
159
|
+
* ```
|
160
|
+
*/
|
161
|
+
replacements?: Replacements;
|
162
|
+
replacementsOptions?: Pick<ReplaceOptions, 'mode' | 'include' | 'exclude'>;
|
117
163
|
}
|
118
164
|
|
119
165
|
export { BUILDER_TYPES, BUILDER_TYPE_PACKAGE_NAME_MAP, type ConfigGenerateContext, type Mapping2ROO, type OutputControl, type TemplateOptions };
|
package/dist/rollup/index.cjs
CHANGED
@@ -19,6 +19,8 @@ var loadConfig = require('#~/utils/loadConfig');
|
|
19
19
|
var recursiveListFiles = require('#~/utils/recursiveListFiles');
|
20
20
|
var resolveExports = require('#~/utils/resolveExports');
|
21
21
|
var ts = require('#~/utils/ts');
|
22
|
+
var rollupPluginUtils = require('jiek/rollup-plugin-utils');
|
23
|
+
var MagicString = require('magic-string');
|
22
24
|
var node_module = require('node:module');
|
23
25
|
|
24
26
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
@@ -29,6 +31,7 @@ var commonjs__default = /*#__PURE__*/_interopDefault(commonjs);
|
|
29
31
|
var inject__default = /*#__PURE__*/_interopDefault(inject);
|
30
32
|
var json__default = /*#__PURE__*/_interopDefault(json);
|
31
33
|
var ts__default = /*#__PURE__*/_interopDefault(ts$1);
|
34
|
+
var MagicString__default = /*#__PURE__*/_interopDefault(MagicString);
|
32
35
|
|
33
36
|
const CREATE_REQUIRE_VIRTUAL_MODULE_NAME = "jiek:create-require";
|
34
37
|
const INSERT_STR = (isESM) => `
|
@@ -62,6 +65,130 @@ var progress = (options = {}) => {
|
|
62
65
|
};
|
63
66
|
};
|
64
67
|
|
68
|
+
var require$1 = require;
|
69
|
+
|
70
|
+
var replace = rollupPluginUtils.definePlugin((options = {}) => {
|
71
|
+
const {
|
72
|
+
include = [/\.[cm]?[tj]sx?$/],
|
73
|
+
exclude = [/node_modules/],
|
74
|
+
values = {},
|
75
|
+
sourcemap
|
76
|
+
} = options;
|
77
|
+
let { mode = "string" } = options;
|
78
|
+
const allValues = { ...values };
|
79
|
+
const allKeys = Object.keys(allValues);
|
80
|
+
const filter = rollupPluginUtils.createFilter({ include, exclude });
|
81
|
+
const replaceAll = async (ctx, code) => {
|
82
|
+
const ms = new MagicString__default.default(code);
|
83
|
+
if (mode === "string") {
|
84
|
+
allKeys.forEach((key) => {
|
85
|
+
const reg = new RegExp(key, "g");
|
86
|
+
let match;
|
87
|
+
while (match = reg.exec(code)) {
|
88
|
+
const start = match.index;
|
89
|
+
const end = start + key.length;
|
90
|
+
const value = typeof allValues[key] === "function" ? allValues[key]({
|
91
|
+
...ctx,
|
92
|
+
code,
|
93
|
+
start,
|
94
|
+
end,
|
95
|
+
mode: "string"
|
96
|
+
}) : allValues[key];
|
97
|
+
if ([null, void 0, false].includes(value)) continue;
|
98
|
+
ms.overwrite(
|
99
|
+
match.index,
|
100
|
+
match.index + key.length,
|
101
|
+
value
|
102
|
+
);
|
103
|
+
}
|
104
|
+
});
|
105
|
+
} else if (mode === "ast-grep") {
|
106
|
+
const ext = node_path.extname(ctx.id);
|
107
|
+
const { parse, Lang } = await import('@ast-grep/napi');
|
108
|
+
let lang;
|
109
|
+
if (/[cm]?tsx?/.test(ext)) {
|
110
|
+
lang = Lang.TypeScript;
|
111
|
+
}
|
112
|
+
if (/[cm]?jsx?/.test(ext)) {
|
113
|
+
lang = Lang.JavaScript;
|
114
|
+
}
|
115
|
+
if (/json?/.test(ext)) {
|
116
|
+
lang = Lang.Json;
|
117
|
+
}
|
118
|
+
if (lang == null) return;
|
119
|
+
const root = parse(lang, code).root();
|
120
|
+
allKeys.forEach((key) => {
|
121
|
+
root.findAll(key).forEach((node) => {
|
122
|
+
const { start, end } = node.range();
|
123
|
+
const newValue = typeof allValues[key] === "function" ? allValues[key]({
|
124
|
+
...ctx,
|
125
|
+
code,
|
126
|
+
mode: "ast-grep",
|
127
|
+
node,
|
128
|
+
lang,
|
129
|
+
$: (input) => {
|
130
|
+
if (typeof input === "string") {
|
131
|
+
return node.getMatch(input)?.text();
|
132
|
+
}
|
133
|
+
if ("raw" in input) {
|
134
|
+
return node.getMatch(input.raw[0])?.text();
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}) : allValues[key];
|
138
|
+
if ([null, void 0, false].includes(newValue)) return;
|
139
|
+
ms.overwrite(
|
140
|
+
start.index,
|
141
|
+
end.index,
|
142
|
+
newValue
|
143
|
+
);
|
144
|
+
});
|
145
|
+
});
|
146
|
+
}
|
147
|
+
return ms;
|
148
|
+
};
|
149
|
+
return {
|
150
|
+
name: "jiek:replace",
|
151
|
+
buildStart() {
|
152
|
+
if (mode === "ast-grep") {
|
153
|
+
try {
|
154
|
+
require$1.resolve("@ast-grep/napi");
|
155
|
+
this.warn(
|
156
|
+
"You are using `ast-grep` mode, please make sure you have installed `@ast-grep/napi`"
|
157
|
+
);
|
158
|
+
} catch {
|
159
|
+
mode = "string";
|
160
|
+
}
|
161
|
+
}
|
162
|
+
},
|
163
|
+
transform: {
|
164
|
+
order: "pre",
|
165
|
+
async handler(code, id) {
|
166
|
+
if (allKeys.length === 0) return;
|
167
|
+
if (filter(id)) return;
|
168
|
+
const ms = await replaceAll({ type: "transform", id }, code);
|
169
|
+
if (ms == null) return;
|
170
|
+
return {
|
171
|
+
code: ms.toString(),
|
172
|
+
map: sourcemap ? ms.generateMap({ hires: true }) : null
|
173
|
+
};
|
174
|
+
}
|
175
|
+
},
|
176
|
+
renderChunk: {
|
177
|
+
order: "post",
|
178
|
+
async handler(code, { fileName: id }) {
|
179
|
+
if (allKeys.length === 0) return;
|
180
|
+
if (filter(id)) return;
|
181
|
+
const ms = await replaceAll({ type: "renderChunk", id }, code);
|
182
|
+
if (ms == null) return;
|
183
|
+
return {
|
184
|
+
code: ms.toString(),
|
185
|
+
map: sourcemap ? ms.generateMap({ hires: true }) : null
|
186
|
+
};
|
187
|
+
}
|
188
|
+
}
|
189
|
+
};
|
190
|
+
});
|
191
|
+
|
65
192
|
var skip = (options = {}) => ({
|
66
193
|
name: "skip",
|
67
194
|
// skip the specified files by `options.patterns`
|
@@ -360,7 +487,12 @@ const generateConfigs = (context, {
|
|
360
487
|
const tsOutputSuffix = jsOutputSuffix.replace(/(\.[cm]?)js$/, ".d$1ts");
|
361
488
|
const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output);
|
362
489
|
const rollupOptions = [];
|
363
|
-
const commonPlugins = [
|
490
|
+
const commonPlugins = ({ sourcemap }) => [
|
491
|
+
replace({
|
492
|
+
...build.replacementsOptions,
|
493
|
+
sourcemap: sourcemap === "hidden" ? false : !!sourcemap,
|
494
|
+
values: build.replacements
|
495
|
+
}),
|
364
496
|
...inputCommonPlugins,
|
365
497
|
withExternal(),
|
366
498
|
!disableCollectInternalModule && {
|
@@ -471,7 +603,7 @@ const generateConfigs = (context, {
|
|
471
603
|
)
|
472
604
|
],
|
473
605
|
plugins: [
|
474
|
-
...commonPlugins,
|
606
|
+
...commonPlugins({ sourcemap }),
|
475
607
|
nodeResolvePluginInstance,
|
476
608
|
import('rollup-plugin-postcss').then(
|
477
609
|
({ default: postcss }) => postcss({
|
@@ -514,7 +646,7 @@ const generateConfigs = (context, {
|
|
514
646
|
}
|
515
647
|
],
|
516
648
|
plugins: [
|
517
|
-
...commonPlugins,
|
649
|
+
...commonPlugins({ sourcemap }),
|
518
650
|
skip({ patterns: [STYLE_REGEXP] }),
|
519
651
|
rollupPluginDts.dts({
|
520
652
|
respectExternal: true,
|
package/dist/rollup/index.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import fs from 'node:fs';
|
2
|
-
import { relative, resolve, dirname
|
2
|
+
import { extname, relative, resolve, dirname } from 'node:path';
|
3
3
|
import process from 'node:process';
|
4
4
|
import { getAllLeafs } from '@jiek/pkger/entrypoints';
|
5
5
|
import { dts } from '@jiek/rollup-plugin-dts';
|
@@ -17,7 +17,9 @@ import { loadConfig } from '#~/utils/loadConfig';
|
|
17
17
|
import { recursiveListFiles } from '#~/utils/recursiveListFiles';
|
18
18
|
import { getOutDirs, resolveExports } from '#~/utils/resolveExports';
|
19
19
|
import { getCompilerOptionsByFilePath } from '#~/utils/ts';
|
20
|
-
import { builtinModules } from 'node:module';
|
20
|
+
import { createRequire as createRequire$1, builtinModules } from 'node:module';
|
21
|
+
import { definePlugin, createFilter } from 'jiek/rollup-plugin-utils';
|
22
|
+
import MagicString from 'magic-string';
|
21
23
|
|
22
24
|
const CREATE_REQUIRE_VIRTUAL_MODULE_NAME = "jiek:create-require";
|
23
25
|
const INSERT_STR = (isESM) => `
|
@@ -51,6 +53,130 @@ var progress = (options = {}) => {
|
|
51
53
|
};
|
52
54
|
};
|
53
55
|
|
56
|
+
var require = /* @__PURE__ */ createRequire$1(import.meta.url);
|
57
|
+
|
58
|
+
var replace = definePlugin((options = {}) => {
|
59
|
+
const {
|
60
|
+
include = [/\.[cm]?[tj]sx?$/],
|
61
|
+
exclude = [/node_modules/],
|
62
|
+
values = {},
|
63
|
+
sourcemap
|
64
|
+
} = options;
|
65
|
+
let { mode = "string" } = options;
|
66
|
+
const allValues = { ...values };
|
67
|
+
const allKeys = Object.keys(allValues);
|
68
|
+
const filter = createFilter({ include, exclude });
|
69
|
+
const replaceAll = async (ctx, code) => {
|
70
|
+
const ms = new MagicString(code);
|
71
|
+
if (mode === "string") {
|
72
|
+
allKeys.forEach((key) => {
|
73
|
+
const reg = new RegExp(key, "g");
|
74
|
+
let match;
|
75
|
+
while (match = reg.exec(code)) {
|
76
|
+
const start = match.index;
|
77
|
+
const end = start + key.length;
|
78
|
+
const value = typeof allValues[key] === "function" ? allValues[key]({
|
79
|
+
...ctx,
|
80
|
+
code,
|
81
|
+
start,
|
82
|
+
end,
|
83
|
+
mode: "string"
|
84
|
+
}) : allValues[key];
|
85
|
+
if ([null, void 0, false].includes(value)) continue;
|
86
|
+
ms.overwrite(
|
87
|
+
match.index,
|
88
|
+
match.index + key.length,
|
89
|
+
value
|
90
|
+
);
|
91
|
+
}
|
92
|
+
});
|
93
|
+
} else if (mode === "ast-grep") {
|
94
|
+
const ext = extname(ctx.id);
|
95
|
+
const { parse, Lang } = await import('@ast-grep/napi');
|
96
|
+
let lang;
|
97
|
+
if (/[cm]?tsx?/.test(ext)) {
|
98
|
+
lang = Lang.TypeScript;
|
99
|
+
}
|
100
|
+
if (/[cm]?jsx?/.test(ext)) {
|
101
|
+
lang = Lang.JavaScript;
|
102
|
+
}
|
103
|
+
if (/json?/.test(ext)) {
|
104
|
+
lang = Lang.Json;
|
105
|
+
}
|
106
|
+
if (lang == null) return;
|
107
|
+
const root = parse(lang, code).root();
|
108
|
+
allKeys.forEach((key) => {
|
109
|
+
root.findAll(key).forEach((node) => {
|
110
|
+
const { start, end } = node.range();
|
111
|
+
const newValue = typeof allValues[key] === "function" ? allValues[key]({
|
112
|
+
...ctx,
|
113
|
+
code,
|
114
|
+
mode: "ast-grep",
|
115
|
+
node,
|
116
|
+
lang,
|
117
|
+
$: (input) => {
|
118
|
+
if (typeof input === "string") {
|
119
|
+
return node.getMatch(input)?.text();
|
120
|
+
}
|
121
|
+
if ("raw" in input) {
|
122
|
+
return node.getMatch(input.raw[0])?.text();
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}) : allValues[key];
|
126
|
+
if ([null, void 0, false].includes(newValue)) return;
|
127
|
+
ms.overwrite(
|
128
|
+
start.index,
|
129
|
+
end.index,
|
130
|
+
newValue
|
131
|
+
);
|
132
|
+
});
|
133
|
+
});
|
134
|
+
}
|
135
|
+
return ms;
|
136
|
+
};
|
137
|
+
return {
|
138
|
+
name: "jiek:replace",
|
139
|
+
buildStart() {
|
140
|
+
if (mode === "ast-grep") {
|
141
|
+
try {
|
142
|
+
require.resolve("@ast-grep/napi");
|
143
|
+
this.warn(
|
144
|
+
"You are using `ast-grep` mode, please make sure you have installed `@ast-grep/napi`"
|
145
|
+
);
|
146
|
+
} catch {
|
147
|
+
mode = "string";
|
148
|
+
}
|
149
|
+
}
|
150
|
+
},
|
151
|
+
transform: {
|
152
|
+
order: "pre",
|
153
|
+
async handler(code, id) {
|
154
|
+
if (allKeys.length === 0) return;
|
155
|
+
if (filter(id)) return;
|
156
|
+
const ms = await replaceAll({ type: "transform", id }, code);
|
157
|
+
if (ms == null) return;
|
158
|
+
return {
|
159
|
+
code: ms.toString(),
|
160
|
+
map: sourcemap ? ms.generateMap({ hires: true }) : null
|
161
|
+
};
|
162
|
+
}
|
163
|
+
},
|
164
|
+
renderChunk: {
|
165
|
+
order: "post",
|
166
|
+
async handler(code, { fileName: id }) {
|
167
|
+
if (allKeys.length === 0) return;
|
168
|
+
if (filter(id)) return;
|
169
|
+
const ms = await replaceAll({ type: "renderChunk", id }, code);
|
170
|
+
if (ms == null) return;
|
171
|
+
return {
|
172
|
+
code: ms.toString(),
|
173
|
+
map: sourcemap ? ms.generateMap({ hires: true }) : null
|
174
|
+
};
|
175
|
+
}
|
176
|
+
}
|
177
|
+
};
|
178
|
+
});
|
179
|
+
|
54
180
|
var skip = (options = {}) => ({
|
55
181
|
name: "skip",
|
56
182
|
// skip the specified files by `options.patterns`
|
@@ -349,7 +475,12 @@ const generateConfigs = (context, {
|
|
349
475
|
const tsOutputSuffix = jsOutputSuffix.replace(/(\.[cm]?)js$/, ".d$1ts");
|
350
476
|
const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output);
|
351
477
|
const rollupOptions = [];
|
352
|
-
const commonPlugins = [
|
478
|
+
const commonPlugins = ({ sourcemap }) => [
|
479
|
+
replace({
|
480
|
+
...build.replacementsOptions,
|
481
|
+
sourcemap: sourcemap === "hidden" ? false : !!sourcemap,
|
482
|
+
values: build.replacements
|
483
|
+
}),
|
353
484
|
...inputCommonPlugins,
|
354
485
|
withExternal(),
|
355
486
|
!disableCollectInternalModule && {
|
@@ -460,7 +591,7 @@ const generateConfigs = (context, {
|
|
460
591
|
)
|
461
592
|
],
|
462
593
|
plugins: [
|
463
|
-
...commonPlugins,
|
594
|
+
...commonPlugins({ sourcemap }),
|
464
595
|
nodeResolvePluginInstance,
|
465
596
|
import('rollup-plugin-postcss').then(
|
466
597
|
({ default: postcss }) => postcss({
|
@@ -503,7 +634,7 @@ const generateConfigs = (context, {
|
|
503
634
|
}
|
504
635
|
],
|
505
636
|
plugins: [
|
506
|
-
...commonPlugins,
|
637
|
+
...commonPlugins({ sourcemap }),
|
507
638
|
skip({ patterns: [STYLE_REGEXP] }),
|
508
639
|
dts({
|
509
640
|
respectExternal: true,
|