@ecrindigital/facetpack 0.1.1 → 0.1.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/__tests__/cache.test.d.ts +2 -0
- package/dist/__tests__/cache.test.d.ts.map +1 -0
- package/dist/__tests__/index.test.d.ts +2 -0
- package/dist/__tests__/index.test.d.ts.map +1 -0
- package/dist/__tests__/minifier.test.d.ts +2 -0
- package/dist/__tests__/minifier.test.d.ts.map +1 -0
- package/dist/__tests__/resolver.test.d.ts +2 -0
- package/dist/__tests__/resolver.test.d.ts.map +1 -0
- package/dist/__tests__/serializer.test.d.ts +2 -0
- package/dist/__tests__/serializer.test.d.ts.map +1 -0
- package/dist/__tests__/transformer.test.d.ts +2 -0
- package/dist/__tests__/transformer.test.d.ts.map +1 -0
- package/dist/__tests__/withFacetpack.test.d.ts +2 -0
- package/dist/__tests__/withFacetpack.test.d.ts.map +1 -0
- package/dist/index.cjs +223 -12
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +218 -1
- package/dist/minifier.cjs +68 -0
- package/dist/minifier.d.ts +22 -0
- package/dist/minifier.d.ts.map +1 -0
- package/dist/minifier.js +41 -0
- package/dist/serializer.cjs +196 -0
- package/dist/serializer.d.ts +59 -0
- package/dist/serializer.d.ts.map +1 -0
- package/dist/serializer.js +172 -0
- package/dist/transformer.cjs +3 -1
- package/dist/transformer.d.ts.map +1 -1
- package/dist/transformer.js +3 -1
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/withFacetpack.d.ts.map +1 -1
- package/package.json +14 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/cache.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/index.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"minifier.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/minifier.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolver.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/resolver.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serializer.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/serializer.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transformer.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/transformer.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withFacetpack.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/withFacetpack.test.ts"],"names":[],"mappings":""}
|
package/dist/index.cjs
CHANGED
|
@@ -32,17 +32,20 @@ __export(exports_src, {
|
|
|
32
32
|
withFacetpack: () => withFacetpack,
|
|
33
33
|
transform: () => transform,
|
|
34
34
|
setTransformerOptions: () => setTransformerOptions,
|
|
35
|
-
resolveSync: () =>
|
|
35
|
+
resolveSync: () => import_facetpack_native4.resolveSync,
|
|
36
|
+
minifyCode: () => minifyCode,
|
|
37
|
+
minify: () => minify,
|
|
36
38
|
getStoredOptions: () => getStoredOptions,
|
|
37
39
|
getCacheStats: () => getCacheStats,
|
|
38
40
|
createTransformer: () => createTransformer,
|
|
39
41
|
createResolver: () => createResolver,
|
|
42
|
+
createFacetpackSerializer: () => createFacetpackSerializer,
|
|
40
43
|
clearCache: () => clearCache
|
|
41
44
|
});
|
|
42
45
|
module.exports = __toCommonJS(exports_src);
|
|
43
46
|
|
|
44
47
|
// src/withFacetpack.ts
|
|
45
|
-
var
|
|
48
|
+
var import_facetpack_native2 = require("@ecrindigital/facetpack-native");
|
|
46
49
|
|
|
47
50
|
// src/cache.ts
|
|
48
51
|
var resolutionCache = new Map;
|
|
@@ -79,6 +82,168 @@ function getCacheStats() {
|
|
|
79
82
|
return { files: resolutionCache.size, resolutions };
|
|
80
83
|
}
|
|
81
84
|
|
|
85
|
+
// src/serializer.ts
|
|
86
|
+
var import_facetpack_native = require("@ecrindigital/facetpack-native");
|
|
87
|
+
function createFacetpackSerializer(existingSerializer, config = {}) {
|
|
88
|
+
return async (entryPoint, preModules, graph, options) => {
|
|
89
|
+
if (options.dev || config.treeShake === false) {
|
|
90
|
+
if (existingSerializer) {
|
|
91
|
+
return existingSerializer(entryPoint, preModules, graph, options);
|
|
92
|
+
}
|
|
93
|
+
return defaultSerialize(entryPoint, preModules, graph, options);
|
|
94
|
+
}
|
|
95
|
+
const analyses = new Map;
|
|
96
|
+
for (const [path, module2] of graph.dependencies) {
|
|
97
|
+
if (path.includes("node_modules")) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
const code = module2.output[0]?.data?.code ?? "";
|
|
102
|
+
const analysis = import_facetpack_native.analyzeSync(path, code);
|
|
103
|
+
analyses.set(path, analysis);
|
|
104
|
+
} catch {
|
|
105
|
+
analyses.set(path, {
|
|
106
|
+
exports: [],
|
|
107
|
+
imports: [],
|
|
108
|
+
hasSideEffects: true
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const usedExports = computeUsedExports(entryPoint, analyses, graph);
|
|
113
|
+
const shakenModules = new Map;
|
|
114
|
+
let totalRemoved = 0;
|
|
115
|
+
for (const [path, module2] of graph.dependencies) {
|
|
116
|
+
if (path.includes("node_modules")) {
|
|
117
|
+
const code = module2.output[0]?.data?.code ?? "";
|
|
118
|
+
shakenModules.set(path, { code });
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const used = usedExports.get(path);
|
|
122
|
+
const analysis = analyses.get(path);
|
|
123
|
+
if ((!used || used.size === 0) && analysis && !analysis.hasSideEffects) {
|
|
124
|
+
totalRemoved++;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
const code = module2.output[0]?.data?.code ?? "";
|
|
129
|
+
const usedArray = used ? Array.from(used) : ["*"];
|
|
130
|
+
const result = import_facetpack_native.shakeSync(path, code, usedArray);
|
|
131
|
+
shakenModules.set(path, { code: result.code, map: result.map ?? undefined });
|
|
132
|
+
totalRemoved += result.removedExports.length;
|
|
133
|
+
} catch {
|
|
134
|
+
const code = module2.output[0]?.data?.code ?? "";
|
|
135
|
+
shakenModules.set(path, { code });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (totalRemoved > 0) {
|
|
139
|
+
console.log(`[facetpack] Tree-shaking removed ${totalRemoved} unused exports`);
|
|
140
|
+
}
|
|
141
|
+
if (existingSerializer) {
|
|
142
|
+
const shakenGraph = createShakenGraph(graph, shakenModules);
|
|
143
|
+
return existingSerializer(entryPoint, preModules, shakenGraph, options);
|
|
144
|
+
}
|
|
145
|
+
return defaultSerialize(entryPoint, preModules, graph, options, shakenModules);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function computeUsedExports(entryPoint, analyses, graph) {
|
|
149
|
+
const used = new Map;
|
|
150
|
+
const visited = new Set;
|
|
151
|
+
function visit(modulePath, importedNames) {
|
|
152
|
+
let moduleUsed = used.get(modulePath);
|
|
153
|
+
if (!moduleUsed) {
|
|
154
|
+
moduleUsed = new Set;
|
|
155
|
+
used.set(modulePath, moduleUsed);
|
|
156
|
+
}
|
|
157
|
+
for (const name of importedNames) {
|
|
158
|
+
moduleUsed.add(name);
|
|
159
|
+
}
|
|
160
|
+
if (visited.has(modulePath)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
visited.add(modulePath);
|
|
164
|
+
const analysis = analyses.get(modulePath);
|
|
165
|
+
if (!analysis) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
for (const imp of analysis.imports) {
|
|
169
|
+
const module2 = graph.dependencies.get(modulePath);
|
|
170
|
+
if (!module2)
|
|
171
|
+
continue;
|
|
172
|
+
const resolvedPath = module2.dependencies.get(imp.source);
|
|
173
|
+
if (resolvedPath) {
|
|
174
|
+
visit(resolvedPath, imp.specifiers);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
for (const exp of analysis.exports) {
|
|
178
|
+
if (exp.isReexport && exp.source) {
|
|
179
|
+
const module2 = graph.dependencies.get(modulePath);
|
|
180
|
+
if (!module2)
|
|
181
|
+
continue;
|
|
182
|
+
const resolvedPath = module2.dependencies.get(exp.source);
|
|
183
|
+
if (resolvedPath && moduleUsed.has(exp.name)) {
|
|
184
|
+
visit(resolvedPath, [exp.name === "*" ? "*" : exp.name]);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
visit(entryPoint, ["*"]);
|
|
190
|
+
return used;
|
|
191
|
+
}
|
|
192
|
+
function createShakenGraph(originalGraph, shakenModules) {
|
|
193
|
+
const newDependencies = new Map;
|
|
194
|
+
for (const [path, module2] of originalGraph.dependencies) {
|
|
195
|
+
const shaken = shakenModules.get(path);
|
|
196
|
+
if (shaken) {
|
|
197
|
+
newDependencies.set(path, {
|
|
198
|
+
...module2,
|
|
199
|
+
output: [
|
|
200
|
+
{
|
|
201
|
+
data: {
|
|
202
|
+
code: shaken.code,
|
|
203
|
+
map: shaken.map
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
]
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
dependencies: newDependencies,
|
|
212
|
+
entryPoints: originalGraph.entryPoints
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function defaultSerialize(entryPoint, preModules, graph, options, shakenModules) {
|
|
216
|
+
const modules = [];
|
|
217
|
+
let moduleId = 0;
|
|
218
|
+
const moduleIds = new Map;
|
|
219
|
+
for (const module2 of preModules) {
|
|
220
|
+
const code = module2.output[0]?.data?.code ?? "";
|
|
221
|
+
const id = options.createModuleId?.(module2.path) ?? moduleId++;
|
|
222
|
+
moduleIds.set(module2.path, id);
|
|
223
|
+
modules.push(wrapModule(id, code));
|
|
224
|
+
}
|
|
225
|
+
for (const [path] of graph.dependencies) {
|
|
226
|
+
const shaken = shakenModules?.get(path);
|
|
227
|
+
const module2 = graph.dependencies.get(path);
|
|
228
|
+
const code = shaken?.code ?? module2?.output[0]?.data?.code ?? "";
|
|
229
|
+
const id = options.createModuleId?.(path) ?? moduleId++;
|
|
230
|
+
moduleIds.set(path, id);
|
|
231
|
+
modules.push(wrapModule(id, code));
|
|
232
|
+
}
|
|
233
|
+
const entryId = moduleIds.get(entryPoint) ?? 0;
|
|
234
|
+
const runStatement = options.getRunModuleStatement?.(entryId) ?? `__r(${entryId});`;
|
|
235
|
+
const bundleCode = modules.join(`
|
|
236
|
+
`) + `
|
|
237
|
+
` + runStatement;
|
|
238
|
+
return {
|
|
239
|
+
code: bundleCode,
|
|
240
|
+
map: '{"version":3,"sources":[],"mappings":""}'
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function wrapModule(id, code) {
|
|
244
|
+
return `__d(function(g,r,i,a,m,e,d){${code}},${id});`;
|
|
245
|
+
}
|
|
246
|
+
|
|
82
247
|
// src/withFacetpack.ts
|
|
83
248
|
var import_path = require("path");
|
|
84
249
|
var import_url = require("url");
|
|
@@ -88,12 +253,20 @@ var __dirname2 = import_path.dirname(__filename2);
|
|
|
88
253
|
function withFacetpack(config, options = {}) {
|
|
89
254
|
const sourceExts = options.sourceExts ?? DEFAULT_SOURCE_EXTS;
|
|
90
255
|
const transformerPath = import_path.join(__dirname2, "transformer.js");
|
|
256
|
+
const useMinifier = options.minifier !== false;
|
|
257
|
+
const minifierPath = useMinifier ? import_path.join(__dirname2, "minifier.js") : config.transformer?.minifierPath;
|
|
258
|
+
const minifierConfig = typeof options.minifier === "object" ? options.minifier : {};
|
|
259
|
+
const useTreeShake = options.treeShake !== false;
|
|
260
|
+
const existingSerializer = config.serializer?.customSerializer;
|
|
261
|
+
const customSerializer = useTreeShake ? createFacetpackSerializer(existingSerializer, { treeShake: true }) : existingSerializer;
|
|
91
262
|
storeTransformerOptions(options);
|
|
92
263
|
return {
|
|
93
264
|
...config,
|
|
94
265
|
transformer: {
|
|
95
266
|
...config.transformer,
|
|
96
267
|
babelTransformerPath: transformerPath,
|
|
268
|
+
minifierPath,
|
|
269
|
+
minifierConfig,
|
|
97
270
|
getTransformOptions: async (entryPoints, opts, getDepsOf) => {
|
|
98
271
|
const baseOptions = await config.transformer?.getTransformOptions?.(entryPoints, opts, getDepsOf);
|
|
99
272
|
return {
|
|
@@ -126,7 +299,7 @@ function withFacetpack(config, options = {}) {
|
|
|
126
299
|
return context.resolveRequest(context, moduleName, platform);
|
|
127
300
|
}
|
|
128
301
|
const directory = context.originModulePath.substring(0, context.originModulePath.lastIndexOf("/"));
|
|
129
|
-
const result =
|
|
302
|
+
const result = import_facetpack_native2.resolveSync(directory, moduleName, {
|
|
130
303
|
extensions: [...sourceExts.map((ext) => `.${ext}`), ".json"],
|
|
131
304
|
mainFields: ["react-native", "browser", "main"],
|
|
132
305
|
conditionNames: ["react-native", "import", "require"]
|
|
@@ -136,6 +309,10 @@ function withFacetpack(config, options = {}) {
|
|
|
136
309
|
}
|
|
137
310
|
return context.resolveRequest(context, moduleName, platform);
|
|
138
311
|
}
|
|
312
|
+
},
|
|
313
|
+
serializer: {
|
|
314
|
+
...config.serializer,
|
|
315
|
+
customSerializer
|
|
139
316
|
}
|
|
140
317
|
};
|
|
141
318
|
}
|
|
@@ -152,7 +329,7 @@ function getStoredOptions() {
|
|
|
152
329
|
return {};
|
|
153
330
|
}
|
|
154
331
|
// src/transformer.ts
|
|
155
|
-
var
|
|
332
|
+
var import_facetpack_native3 = require("@ecrindigital/facetpack-native");
|
|
156
333
|
var import_parser = require("@babel/parser");
|
|
157
334
|
var IMPORT_REGEX = /(?:import|export)\s+(?:[\s\S]*?\s+from\s+)?['"]([^'"]+)['"]/g;
|
|
158
335
|
var REQUIRE_REGEX = /require\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
@@ -174,7 +351,7 @@ function preResolveImports(filename, code, sourceExts) {
|
|
|
174
351
|
if (specifiers.length === 0)
|
|
175
352
|
return;
|
|
176
353
|
const directory = filename.substring(0, filename.lastIndexOf("/"));
|
|
177
|
-
const results =
|
|
354
|
+
const results = import_facetpack_native3.resolveBatchSync(directory, specifiers, {
|
|
178
355
|
extensions: [...sourceExts.map((ext) => `.${ext}`), ".json"],
|
|
179
356
|
mainFields: ["react-native", "browser", "main"],
|
|
180
357
|
conditionNames: ["react-native", "import", "require"]
|
|
@@ -195,7 +372,9 @@ var defaultOptions = {
|
|
|
195
372
|
jsxPragma: "React.createElement",
|
|
196
373
|
jsxPragmaFrag: "React.Fragment",
|
|
197
374
|
typescript: true,
|
|
198
|
-
sourceExts: ["ts", "tsx", "js", "jsx", "mjs", "cjs"]
|
|
375
|
+
sourceExts: ["ts", "tsx", "js", "jsx", "mjs", "cjs"],
|
|
376
|
+
minifier: true,
|
|
377
|
+
treeShake: true
|
|
199
378
|
};
|
|
200
379
|
var globalOptions = {};
|
|
201
380
|
var fallbackTransformer = null;
|
|
@@ -254,9 +433,9 @@ function transform(params) {
|
|
|
254
433
|
}
|
|
255
434
|
try {
|
|
256
435
|
const isClassic = opts.jsxRuntime === "classic";
|
|
257
|
-
const result =
|
|
436
|
+
const result = import_facetpack_native3.transformSync(filename, src, {
|
|
258
437
|
jsx: opts.jsx,
|
|
259
|
-
jsxRuntime: isClassic ?
|
|
438
|
+
jsxRuntime: isClassic ? import_facetpack_native3.JsxRuntime.Classic : import_facetpack_native3.JsxRuntime.Automatic,
|
|
260
439
|
...isClassic ? { jsxPragma: opts.jsxPragma, jsxPragmaFrag: opts.jsxPragmaFrag } : { jsxImportSource: opts.jsxImportSource },
|
|
261
440
|
typescript: opts.typescript,
|
|
262
441
|
sourcemap: metroOptions.dev
|
|
@@ -298,9 +477,9 @@ function createTransformer(options = {}) {
|
|
|
298
477
|
return getFallbackTransformer().transform(params);
|
|
299
478
|
}
|
|
300
479
|
const isClassic = opts.jsxRuntime === "classic";
|
|
301
|
-
const result =
|
|
480
|
+
const result = import_facetpack_native3.transformSync(filename, src, {
|
|
302
481
|
jsx: opts.jsx,
|
|
303
|
-
jsxRuntime: isClassic ?
|
|
482
|
+
jsxRuntime: isClassic ? import_facetpack_native3.JsxRuntime.Classic : import_facetpack_native3.JsxRuntime.Automatic,
|
|
304
483
|
...isClassic ? { jsxPragma: opts.jsxPragma, jsxPragmaFrag: opts.jsxPragmaFrag } : { jsxImportSource: opts.jsxImportSource },
|
|
305
484
|
typescript: opts.typescript,
|
|
306
485
|
sourcemap: metroOptions.dev
|
|
@@ -318,13 +497,45 @@ ${result.errors.join(`
|
|
|
318
497
|
};
|
|
319
498
|
}
|
|
320
499
|
// src/resolver.ts
|
|
321
|
-
var
|
|
500
|
+
var import_facetpack_native4 = require("@ecrindigital/facetpack-native");
|
|
322
501
|
function createResolver(options) {
|
|
323
502
|
return {
|
|
324
503
|
resolve(originModulePath, moduleName) {
|
|
325
504
|
const directory = originModulePath.substring(0, originModulePath.lastIndexOf("/"));
|
|
326
|
-
const result =
|
|
505
|
+
const result = import_facetpack_native4.resolveSync(directory, moduleName, options);
|
|
327
506
|
return result.path ?? null;
|
|
328
507
|
}
|
|
329
508
|
};
|
|
330
509
|
}
|
|
510
|
+
// src/minifier.ts
|
|
511
|
+
var import_facetpack_native5 = require("@ecrindigital/facetpack-native");
|
|
512
|
+
function minify(input) {
|
|
513
|
+
const options = {
|
|
514
|
+
compress: input.config.compress ?? true,
|
|
515
|
+
mangle: input.config.mangle ?? true,
|
|
516
|
+
keepFnames: input.config.keep_fnames ?? false,
|
|
517
|
+
dropConsole: input.config.drop_console ?? false,
|
|
518
|
+
dropDebugger: input.config.drop_debugger ?? true,
|
|
519
|
+
sourcemap: input.map !== undefined
|
|
520
|
+
};
|
|
521
|
+
const result = import_facetpack_native5.minifySync(input.code, input.filename, options);
|
|
522
|
+
return {
|
|
523
|
+
code: result.code,
|
|
524
|
+
map: result.map ?? undefined
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function minifyCode(code, filename, options) {
|
|
528
|
+
const nativeOptions = {
|
|
529
|
+
compress: options?.compress ?? true,
|
|
530
|
+
mangle: options?.mangle ?? true,
|
|
531
|
+
keepFnames: options?.keep_fnames ?? false,
|
|
532
|
+
dropConsole: options?.drop_console ?? false,
|
|
533
|
+
dropDebugger: options?.drop_debugger ?? true,
|
|
534
|
+
sourcemap: false
|
|
535
|
+
};
|
|
536
|
+
const result = import_facetpack_native5.minifySync(code, filename, nativeOptions);
|
|
537
|
+
return {
|
|
538
|
+
code: result.code,
|
|
539
|
+
map: result.map ?? undefined
|
|
540
|
+
};
|
|
541
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,5 +2,8 @@ export { withFacetpack, getStoredOptions } from './withFacetpack';
|
|
|
2
2
|
export { transform, createTransformer, setTransformerOptions } from './transformer';
|
|
3
3
|
export { createResolver, resolveSync } from './resolver';
|
|
4
4
|
export { clearCache, getCacheStats } from './cache';
|
|
5
|
-
export
|
|
5
|
+
export { minify, minifyCode } from './minifier';
|
|
6
|
+
export { createFacetpackSerializer } from './serializer';
|
|
7
|
+
export type { FacetpackOptions, MetroConfig, TransformParams, TransformOptions, TransformResult, MinifierConfig, } from './types';
|
|
8
|
+
export type { CustomSerializer, FacetpackSerializerConfig, SerializerModule, SerializerGraph, SerializerOptions, } from './serializer';
|
|
6
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AACjE,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACnF,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACnD,YAAY,EACV,gBAAgB,EAChB,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,eAAe,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AACjE,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACnF,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACnD,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAC/C,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAA;AACxD,YAAY,EACV,gBAAgB,EAChB,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,cAAc,GACf,MAAM,SAAS,CAAA;AAChB,YAAY,EACV,gBAAgB,EAChB,yBAAyB,EACzB,gBAAgB,EAChB,eAAe,EACf,iBAAiB,GAClB,MAAM,cAAc,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,172 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
3
3
|
|
|
4
|
+
// src/serializer.ts
|
|
5
|
+
import {
|
|
6
|
+
analyzeSync,
|
|
7
|
+
shakeSync
|
|
8
|
+
} from "@ecrindigital/facetpack-native";
|
|
9
|
+
function createFacetpackSerializer(existingSerializer, config = {}) {
|
|
10
|
+
return async (entryPoint, preModules, graph, options) => {
|
|
11
|
+
if (options.dev || config.treeShake === false) {
|
|
12
|
+
if (existingSerializer) {
|
|
13
|
+
return existingSerializer(entryPoint, preModules, graph, options);
|
|
14
|
+
}
|
|
15
|
+
return defaultSerialize(entryPoint, preModules, graph, options);
|
|
16
|
+
}
|
|
17
|
+
const analyses = new Map;
|
|
18
|
+
for (const [path, module] of graph.dependencies) {
|
|
19
|
+
if (path.includes("node_modules")) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const code = module.output[0]?.data?.code ?? "";
|
|
24
|
+
const analysis = analyzeSync(path, code);
|
|
25
|
+
analyses.set(path, analysis);
|
|
26
|
+
} catch {
|
|
27
|
+
analyses.set(path, {
|
|
28
|
+
exports: [],
|
|
29
|
+
imports: [],
|
|
30
|
+
hasSideEffects: true
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const usedExports = computeUsedExports(entryPoint, analyses, graph);
|
|
35
|
+
const shakenModules = new Map;
|
|
36
|
+
let totalRemoved = 0;
|
|
37
|
+
for (const [path, module] of graph.dependencies) {
|
|
38
|
+
if (path.includes("node_modules")) {
|
|
39
|
+
const code = module.output[0]?.data?.code ?? "";
|
|
40
|
+
shakenModules.set(path, { code });
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const used = usedExports.get(path);
|
|
44
|
+
const analysis = analyses.get(path);
|
|
45
|
+
if ((!used || used.size === 0) && analysis && !analysis.hasSideEffects) {
|
|
46
|
+
totalRemoved++;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const code = module.output[0]?.data?.code ?? "";
|
|
51
|
+
const usedArray = used ? Array.from(used) : ["*"];
|
|
52
|
+
const result = shakeSync(path, code, usedArray);
|
|
53
|
+
shakenModules.set(path, { code: result.code, map: result.map ?? undefined });
|
|
54
|
+
totalRemoved += result.removedExports.length;
|
|
55
|
+
} catch {
|
|
56
|
+
const code = module.output[0]?.data?.code ?? "";
|
|
57
|
+
shakenModules.set(path, { code });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (totalRemoved > 0) {
|
|
61
|
+
console.log(`[facetpack] Tree-shaking removed ${totalRemoved} unused exports`);
|
|
62
|
+
}
|
|
63
|
+
if (existingSerializer) {
|
|
64
|
+
const shakenGraph = createShakenGraph(graph, shakenModules);
|
|
65
|
+
return existingSerializer(entryPoint, preModules, shakenGraph, options);
|
|
66
|
+
}
|
|
67
|
+
return defaultSerialize(entryPoint, preModules, graph, options, shakenModules);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function computeUsedExports(entryPoint, analyses, graph) {
|
|
71
|
+
const used = new Map;
|
|
72
|
+
const visited = new Set;
|
|
73
|
+
function visit(modulePath, importedNames) {
|
|
74
|
+
let moduleUsed = used.get(modulePath);
|
|
75
|
+
if (!moduleUsed) {
|
|
76
|
+
moduleUsed = new Set;
|
|
77
|
+
used.set(modulePath, moduleUsed);
|
|
78
|
+
}
|
|
79
|
+
for (const name of importedNames) {
|
|
80
|
+
moduleUsed.add(name);
|
|
81
|
+
}
|
|
82
|
+
if (visited.has(modulePath)) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
visited.add(modulePath);
|
|
86
|
+
const analysis = analyses.get(modulePath);
|
|
87
|
+
if (!analysis) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
for (const imp of analysis.imports) {
|
|
91
|
+
const module = graph.dependencies.get(modulePath);
|
|
92
|
+
if (!module)
|
|
93
|
+
continue;
|
|
94
|
+
const resolvedPath = module.dependencies.get(imp.source);
|
|
95
|
+
if (resolvedPath) {
|
|
96
|
+
visit(resolvedPath, imp.specifiers);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
for (const exp of analysis.exports) {
|
|
100
|
+
if (exp.isReexport && exp.source) {
|
|
101
|
+
const module = graph.dependencies.get(modulePath);
|
|
102
|
+
if (!module)
|
|
103
|
+
continue;
|
|
104
|
+
const resolvedPath = module.dependencies.get(exp.source);
|
|
105
|
+
if (resolvedPath && moduleUsed.has(exp.name)) {
|
|
106
|
+
visit(resolvedPath, [exp.name === "*" ? "*" : exp.name]);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
visit(entryPoint, ["*"]);
|
|
112
|
+
return used;
|
|
113
|
+
}
|
|
114
|
+
function createShakenGraph(originalGraph, shakenModules) {
|
|
115
|
+
const newDependencies = new Map;
|
|
116
|
+
for (const [path, module] of originalGraph.dependencies) {
|
|
117
|
+
const shaken = shakenModules.get(path);
|
|
118
|
+
if (shaken) {
|
|
119
|
+
newDependencies.set(path, {
|
|
120
|
+
...module,
|
|
121
|
+
output: [
|
|
122
|
+
{
|
|
123
|
+
data: {
|
|
124
|
+
code: shaken.code,
|
|
125
|
+
map: shaken.map
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
dependencies: newDependencies,
|
|
134
|
+
entryPoints: originalGraph.entryPoints
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function defaultSerialize(entryPoint, preModules, graph, options, shakenModules) {
|
|
138
|
+
const modules = [];
|
|
139
|
+
let moduleId = 0;
|
|
140
|
+
const moduleIds = new Map;
|
|
141
|
+
for (const module of preModules) {
|
|
142
|
+
const code = module.output[0]?.data?.code ?? "";
|
|
143
|
+
const id = options.createModuleId?.(module.path) ?? moduleId++;
|
|
144
|
+
moduleIds.set(module.path, id);
|
|
145
|
+
modules.push(wrapModule(id, code));
|
|
146
|
+
}
|
|
147
|
+
for (const [path] of graph.dependencies) {
|
|
148
|
+
const shaken = shakenModules?.get(path);
|
|
149
|
+
const module = graph.dependencies.get(path);
|
|
150
|
+
const code = shaken?.code ?? module?.output[0]?.data?.code ?? "";
|
|
151
|
+
const id = options.createModuleId?.(path) ?? moduleId++;
|
|
152
|
+
moduleIds.set(path, id);
|
|
153
|
+
modules.push(wrapModule(id, code));
|
|
154
|
+
}
|
|
155
|
+
const entryId = moduleIds.get(entryPoint) ?? 0;
|
|
156
|
+
const runStatement = options.getRunModuleStatement?.(entryId) ?? `__r(${entryId});`;
|
|
157
|
+
const bundleCode = modules.join(`
|
|
158
|
+
`) + `
|
|
159
|
+
` + runStatement;
|
|
160
|
+
return {
|
|
161
|
+
code: bundleCode,
|
|
162
|
+
map: '{"version":3,"sources":[],"mappings":""}'
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function wrapModule(id, code) {
|
|
166
|
+
return `__d(function(g,r,i,a,m,e,d){${code}},${id});`;
|
|
167
|
+
}
|
|
168
|
+
var serializer_default = createFacetpackSerializer;
|
|
169
|
+
|
|
4
170
|
// src/transformer.ts
|
|
5
171
|
import { transformSync, JsxRuntime, resolveBatchSync } from "@ecrindigital/facetpack-native";
|
|
6
172
|
import { parse } from "@babel/parser";
|
|
@@ -82,7 +248,9 @@ var defaultOptions = {
|
|
|
82
248
|
jsxPragma: "React.createElement",
|
|
83
249
|
jsxPragmaFrag: "React.Fragment",
|
|
84
250
|
typescript: true,
|
|
85
|
-
sourceExts: ["ts", "tsx", "js", "jsx", "mjs", "cjs"]
|
|
251
|
+
sourceExts: ["ts", "tsx", "js", "jsx", "mjs", "cjs"],
|
|
252
|
+
minifier: true,
|
|
253
|
+
treeShake: true
|
|
86
254
|
};
|
|
87
255
|
var globalOptions = {};
|
|
88
256
|
var fallbackTransformer = null;
|
|
@@ -205,6 +373,40 @@ ${result.errors.join(`
|
|
|
205
373
|
};
|
|
206
374
|
}
|
|
207
375
|
|
|
376
|
+
// src/minifier.ts
|
|
377
|
+
import { minifySync } from "@ecrindigital/facetpack-native";
|
|
378
|
+
function minify(input) {
|
|
379
|
+
const options = {
|
|
380
|
+
compress: input.config.compress ?? true,
|
|
381
|
+
mangle: input.config.mangle ?? true,
|
|
382
|
+
keepFnames: input.config.keep_fnames ?? false,
|
|
383
|
+
dropConsole: input.config.drop_console ?? false,
|
|
384
|
+
dropDebugger: input.config.drop_debugger ?? true,
|
|
385
|
+
sourcemap: input.map !== undefined
|
|
386
|
+
};
|
|
387
|
+
const result = minifySync(input.code, input.filename, options);
|
|
388
|
+
return {
|
|
389
|
+
code: result.code,
|
|
390
|
+
map: result.map ?? undefined
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
function minifyCode(code, filename, options) {
|
|
394
|
+
const nativeOptions = {
|
|
395
|
+
compress: options?.compress ?? true,
|
|
396
|
+
mangle: options?.mangle ?? true,
|
|
397
|
+
keepFnames: options?.keep_fnames ?? false,
|
|
398
|
+
dropConsole: options?.drop_console ?? false,
|
|
399
|
+
dropDebugger: options?.drop_debugger ?? true,
|
|
400
|
+
sourcemap: false
|
|
401
|
+
};
|
|
402
|
+
const result = minifySync(code, filename, nativeOptions);
|
|
403
|
+
return {
|
|
404
|
+
code: result.code,
|
|
405
|
+
map: result.map ?? undefined
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
var minifier_default = minify;
|
|
409
|
+
|
|
208
410
|
// src/withFacetpack.ts
|
|
209
411
|
import { resolveSync } from "@ecrindigital/facetpack-native";
|
|
210
412
|
import { dirname, join } from "path";
|
|
@@ -215,12 +417,20 @@ var __dirname2 = dirname(__filename2);
|
|
|
215
417
|
function withFacetpack(config, options = {}) {
|
|
216
418
|
const sourceExts = options.sourceExts ?? DEFAULT_SOURCE_EXTS;
|
|
217
419
|
const transformerPath = join(__dirname2, "transformer.js");
|
|
420
|
+
const useMinifier = options.minifier !== false;
|
|
421
|
+
const minifierPath = useMinifier ? join(__dirname2, "minifier.js") : config.transformer?.minifierPath;
|
|
422
|
+
const minifierConfig = typeof options.minifier === "object" ? options.minifier : {};
|
|
423
|
+
const useTreeShake = options.treeShake !== false;
|
|
424
|
+
const existingSerializer = config.serializer?.customSerializer;
|
|
425
|
+
const customSerializer = useTreeShake ? createFacetpackSerializer(existingSerializer, { treeShake: true }) : existingSerializer;
|
|
218
426
|
storeTransformerOptions(options);
|
|
219
427
|
return {
|
|
220
428
|
...config,
|
|
221
429
|
transformer: {
|
|
222
430
|
...config.transformer,
|
|
223
431
|
babelTransformerPath: transformerPath,
|
|
432
|
+
minifierPath,
|
|
433
|
+
minifierConfig,
|
|
224
434
|
getTransformOptions: async (entryPoints, opts, getDepsOf) => {
|
|
225
435
|
const baseOptions = await config.transformer?.getTransformOptions?.(entryPoints, opts, getDepsOf);
|
|
226
436
|
return {
|
|
@@ -263,6 +473,10 @@ function withFacetpack(config, options = {}) {
|
|
|
263
473
|
}
|
|
264
474
|
return context.resolveRequest(context, moduleName, platform);
|
|
265
475
|
}
|
|
476
|
+
},
|
|
477
|
+
serializer: {
|
|
478
|
+
...config.serializer,
|
|
479
|
+
customSerializer
|
|
266
480
|
}
|
|
267
481
|
};
|
|
268
482
|
}
|
|
@@ -294,9 +508,12 @@ export {
|
|
|
294
508
|
transform,
|
|
295
509
|
setTransformerOptions,
|
|
296
510
|
resolveSync2 as resolveSync,
|
|
511
|
+
minifyCode,
|
|
512
|
+
minify,
|
|
297
513
|
getStoredOptions,
|
|
298
514
|
getCacheStats,
|
|
299
515
|
createTransformer,
|
|
300
516
|
createResolver,
|
|
517
|
+
createFacetpackSerializer,
|
|
301
518
|
clearCache
|
|
302
519
|
};
|