@granite-js/mpack 1.0.21 → 1.0.23
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/CHANGELOG.md +15 -0
- package/dist/bundler/plugins/requireContextPlugin/__fixtures__/preview.require.context.d.ts +3 -0
- package/dist/bundler/plugins/requireContextPlugin/__fixtures__/preview.require.context.js +45 -0
- package/dist/bundler/plugins/requireContextPlugin/requireContextPlugin.js +19 -5
- package/dist/bundler/plugins/requireContextPlugin/scripts.d.ts +10 -37
- package/dist/bundler/plugins/requireContextPlugin/scripts.js +51 -6
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @granite-js/mpack
|
|
2
2
|
|
|
3
|
+
## 1.0.23
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 63217da: fix(mpack): support deep and filter params in require.context(), isolate require context
|
|
8
|
+
- @granite-js/plugin-core@1.0.23
|
|
9
|
+
- @granite-js/utils@1.0.23
|
|
10
|
+
|
|
11
|
+
## 1.0.22
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- @granite-js/plugin-core@1.0.22
|
|
16
|
+
- @granite-js/utils@1.0.22
|
|
17
|
+
|
|
3
18
|
## 1.0.21
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var preview_require_context_exports = {};
|
|
20
|
+
__export(preview_require_context_exports, {
|
|
21
|
+
findPreview: () => findPreview,
|
|
22
|
+
normalizeGroup: () => normalizeGroup,
|
|
23
|
+
previews: () => previews
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(preview_require_context_exports);
|
|
26
|
+
const previewContext = require.context("../../", true, /\.preview\.tsx$/);
|
|
27
|
+
const previews = previewContext.keys().filter((key) => key.startsWith("./")).map((key) => previewContext(key).default).filter(Boolean);
|
|
28
|
+
function findPreview(key) {
|
|
29
|
+
return previews.find((p) => p.key === key);
|
|
30
|
+
}
|
|
31
|
+
function normalizeGroup(group) {
|
|
32
|
+
if (group == null) {
|
|
33
|
+
return ["Ungrouped"];
|
|
34
|
+
}
|
|
35
|
+
if (typeof group === "string") {
|
|
36
|
+
return [group];
|
|
37
|
+
}
|
|
38
|
+
return group.length === 0 ? ["Ungrouped"] : group;
|
|
39
|
+
}
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
findPreview,
|
|
43
|
+
normalizeGroup,
|
|
44
|
+
previews
|
|
45
|
+
});
|
|
@@ -36,13 +36,15 @@ var import_path = __toESM(require("path"));
|
|
|
36
36
|
var import_scripts = require("./scripts");
|
|
37
37
|
var import_constants = require("../../../constants");
|
|
38
38
|
var import_esbuildUtils = require("../../../utils/esbuildUtils");
|
|
39
|
-
async function getFilePaths(rootDir) {
|
|
39
|
+
async function getFilePaths(rootDir, deep = true) {
|
|
40
40
|
const filenames = [];
|
|
41
41
|
const rootFilenames = await import_fs.default.readdirSync(rootDir);
|
|
42
42
|
for await (const filename of rootFilenames) {
|
|
43
43
|
const filePath = import_path.default.join(rootDir, filename);
|
|
44
44
|
if (import_fs.default.lstatSync(filePath).isDirectory()) {
|
|
45
|
-
|
|
45
|
+
if (deep) {
|
|
46
|
+
filenames.push(...await getFilePaths(filePath, deep));
|
|
47
|
+
}
|
|
46
48
|
} else {
|
|
47
49
|
filenames.push(filePath);
|
|
48
50
|
}
|
|
@@ -61,11 +63,21 @@ function requireContextPlugin() {
|
|
|
61
63
|
};
|
|
62
64
|
});
|
|
63
65
|
build.onResolve({ filter: new RegExp(`^${import_constants.REQUIRE_CONTEXT_PROTOCOL}.*`) }, (args) => {
|
|
66
|
+
const rawPath = args.path.slice(import_constants.REQUIRE_CONTEXT_PROTOCOL.length);
|
|
67
|
+
const qIdx = rawPath.indexOf("?");
|
|
68
|
+
const contextPath = qIdx === -1 ? rawPath : rawPath.slice(0, qIdx);
|
|
69
|
+
const params = new URLSearchParams(qIdx === -1 ? "" : rawPath.slice(qIdx + 1));
|
|
70
|
+
const deep = params.get("deep") !== "false";
|
|
71
|
+
const filterSrc = params.get("filterSrc");
|
|
72
|
+
const filterFlags = params.get("filterFlags") ?? "";
|
|
73
|
+
const filter = filterSrc != null ? new RegExp(filterSrc, filterFlags) : void 0;
|
|
64
74
|
return {
|
|
65
75
|
namespace: "require-context",
|
|
66
|
-
path:
|
|
76
|
+
path: contextPath,
|
|
67
77
|
pluginData: {
|
|
68
|
-
importer: args.resolveDir
|
|
78
|
+
importer: args.resolveDir,
|
|
79
|
+
deep,
|
|
80
|
+
filter
|
|
69
81
|
}
|
|
70
82
|
};
|
|
71
83
|
});
|
|
@@ -74,9 +86,11 @@ function requireContextPlugin() {
|
|
|
74
86
|
if (importer == null) {
|
|
75
87
|
throw new Error(`importer\uAC00 \uC8FC\uC5B4\uC838\uC57C \uD569\uB2C8\uB2E4.`);
|
|
76
88
|
}
|
|
89
|
+
const { deep, filter } = args.pluginData;
|
|
77
90
|
const targetDir = import_path.default.resolve(importer, args.path);
|
|
78
91
|
const basePath = import_path.default.join(importer, args.path);
|
|
79
|
-
const
|
|
92
|
+
const allPaths = await getFilePaths(targetDir, deep);
|
|
93
|
+
const filePaths = filter ? allPaths.filter((fp) => filter.test(import_path.default.basename(fp))) : allPaths;
|
|
80
94
|
const requireContextModules = filePaths.map((filePath, index) => {
|
|
81
95
|
const pagePath = import_path.default.relative(basePath, filePath);
|
|
82
96
|
const normalizedPagePath = (0, import_esbuildUtils.normalizePath)(pagePath);
|
|
@@ -7,50 +7,23 @@ interface RequireContextModule {
|
|
|
7
7
|
* require context export 스크립트 반환
|
|
8
8
|
*
|
|
9
9
|
* ```js
|
|
10
|
-
* export const
|
|
11
|
-
* export
|
|
10
|
+
* export const context1 = require.context('../path1'); // A
|
|
11
|
+
* export const context2 = require.context('../path2'); // B
|
|
12
|
+
* export default require.context('../path3'); // C
|
|
12
13
|
* ```
|
|
13
14
|
*
|
|
14
|
-
* 형태의 코드를 아래와 같이 변경
|
|
15
|
+
* 형태의 코드를 아래와 같이 변경 (각 호출마다 고유한 변수로 격리)
|
|
15
16
|
*
|
|
16
17
|
* ```js
|
|
17
|
-
*
|
|
18
|
-
* import
|
|
18
|
+
* import __context_0__ from 'require-context:../path1';
|
|
19
|
+
* import __context_1__ from 'require-context:../path2';
|
|
20
|
+
* import __context_2__ from 'require-context:../path3';
|
|
19
21
|
*
|
|
20
|
-
* export var
|
|
21
|
-
* export
|
|
22
|
+
* export var context1 = __context_0__; // A
|
|
23
|
+
* export var context2 = __context_1__; // B
|
|
24
|
+
* export default __context_2__; // C
|
|
22
25
|
* ```
|
|
23
26
|
*/
|
|
24
27
|
export declare function toRequireContextExportScript(content: string): string;
|
|
25
|
-
/**
|
|
26
|
-
* require context 스크립트
|
|
27
|
-
*
|
|
28
|
-
* ```js
|
|
29
|
-
* // Sample
|
|
30
|
-
* import * as module0 from "/path/to/module-0";
|
|
31
|
-
* import * as module1 from "/path/to/module-1";
|
|
32
|
-
* import * as module2 from "/path/to/module-2";
|
|
33
|
-
*
|
|
34
|
-
* var requireContext = function(key) {
|
|
35
|
-
* var _modules = {};
|
|
36
|
-
*
|
|
37
|
-
* _modules["./relative/path/of/module-0"] = module0;
|
|
38
|
-
* _modules["./relative/path/of/module-1"] = module1;
|
|
39
|
-
* _modules["./relative/path/of/module-2"] = module2;
|
|
40
|
-
*
|
|
41
|
-
* return _modules[key];
|
|
42
|
-
* };
|
|
43
|
-
*
|
|
44
|
-
* requireContext.keys = function() {
|
|
45
|
-
* return [
|
|
46
|
-
* "./relative/path/of/module-0",
|
|
47
|
-
* "./relative/path/of/module-1",
|
|
48
|
-
* "./relative/path/of/module-2",
|
|
49
|
-
* ];
|
|
50
|
-
* };
|
|
51
|
-
*
|
|
52
|
-
* export default requireContext;
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
28
|
export declare function getRequireContextScript(modules: RequireContextModule[]): string;
|
|
56
29
|
export {};
|
|
@@ -24,19 +24,64 @@ __export(scripts_exports, {
|
|
|
24
24
|
module.exports = __toCommonJS(scripts_exports);
|
|
25
25
|
var import_constants = require("../../../constants");
|
|
26
26
|
function toRequireContextExportScript(content) {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
const sources = [];
|
|
28
|
+
let idx = 0;
|
|
29
|
+
const MODULE_BODY = content.replace(
|
|
30
|
+
/require\.context\((['"])(.*?)\1(?:\s*,\s*(true|false))?(?:\s*,\s*(\/.*?\/\w*))?\)/g,
|
|
31
|
+
(_, _quote, sourcePath, deep, filterLiteral) => {
|
|
32
|
+
const filterRegex = filterLiteral ? parseRegExpLiteral(filterLiteral) : void 0;
|
|
33
|
+
sources.push({
|
|
34
|
+
path: sourcePath,
|
|
35
|
+
deep: deep !== "false",
|
|
36
|
+
filterSrc: filterRegex?.source,
|
|
37
|
+
filterFlags: filterRegex?.flags
|
|
38
|
+
});
|
|
39
|
+
return `__context_${idx++}__`;
|
|
40
|
+
}
|
|
41
|
+
).replace(/\b(const|let)\b/g, "var");
|
|
42
|
+
if (sources.length === 0) {
|
|
30
43
|
throw new Error("\uC720\uD6A8\uD558\uC9C0 \uC54A\uC740 require context \uAD6C\uBB38\uC785\uB2C8\uB2E4");
|
|
31
44
|
}
|
|
32
|
-
const
|
|
33
|
-
|
|
45
|
+
for (const source of sources) {
|
|
46
|
+
if (!source.path) {
|
|
47
|
+
throw new Error("\uC720\uD6A8\uD558\uC9C0 \uC54A\uC740 require context \uAD6C\uBB38\uC785\uB2C8\uB2E4");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const IMPORT_STATEMENTS = sources.map((source, i) => {
|
|
51
|
+
const query = buildQueryString(source);
|
|
52
|
+
const protocolPath = `${import_constants.REQUIRE_CONTEXT_PROTOCOL}${source.path}${query}`;
|
|
53
|
+
return `import __context_${i}__ from '${protocolPath}';`;
|
|
54
|
+
}).join("\n");
|
|
34
55
|
return `
|
|
35
|
-
${
|
|
56
|
+
${IMPORT_STATEMENTS}
|
|
36
57
|
|
|
37
58
|
${MODULE_BODY}
|
|
38
59
|
`.trim();
|
|
39
60
|
}
|
|
61
|
+
function parseRegExpLiteral(literal) {
|
|
62
|
+
const match = literal.match(/^\/(.*)\/(\w*)$/);
|
|
63
|
+
if (!match || match[1] == null) {
|
|
64
|
+
throw new Error(`\uC720\uD6A8\uD558\uC9C0 \uC54A\uC740 \uC815\uADDC\uC2DD \uB9AC\uD130\uB7F4: ${literal}`);
|
|
65
|
+
}
|
|
66
|
+
return new RegExp(match[1], match[2] ?? "");
|
|
67
|
+
}
|
|
68
|
+
function buildQueryString(source) {
|
|
69
|
+
const params = new URLSearchParams();
|
|
70
|
+
if (!source.deep) {
|
|
71
|
+
params.set("deep", "false");
|
|
72
|
+
}
|
|
73
|
+
;
|
|
74
|
+
if (source.filterSrc != null) {
|
|
75
|
+
params.set("filterSrc", source.filterSrc);
|
|
76
|
+
}
|
|
77
|
+
;
|
|
78
|
+
if (source.filterFlags != null) {
|
|
79
|
+
params.set("filterFlags", source.filterFlags);
|
|
80
|
+
}
|
|
81
|
+
;
|
|
82
|
+
const qs = params.toString();
|
|
83
|
+
return qs.length > 0 ? `?${qs}` : "";
|
|
84
|
+
}
|
|
40
85
|
function getRequireContextScript(modules) {
|
|
41
86
|
const IMPORT_STATEMENTS = modules.map((module2) => `import * as module${module2.moduleIndex} from ${JSON.stringify(module2.absolutePath)};`).join("\n");
|
|
42
87
|
const ASSIGN_STATEMENTS = modules.map((module2) => `_modules[${JSON.stringify(module2.relativePath)}] = module${module2.moduleIndex};`).join("\n");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@granite-js/mpack",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.23",
|
|
4
4
|
"description": "A bundler for Granite apps",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -92,8 +92,8 @@
|
|
|
92
92
|
"@babel/traverse": "7.28.5",
|
|
93
93
|
"@babel/types": "7.28.5",
|
|
94
94
|
"@fastify/middie": "8.3.0",
|
|
95
|
-
"@granite-js/plugin-core": "1.0.
|
|
96
|
-
"@granite-js/utils": "1.0.
|
|
95
|
+
"@granite-js/plugin-core": "1.0.23",
|
|
96
|
+
"@granite-js/utils": "1.0.23",
|
|
97
97
|
"@react-native-community/cli-plugin-metro": "11.3.7",
|
|
98
98
|
"@react-native-community/cli-server-api": "11.3.7",
|
|
99
99
|
"@react-native-community/cli-tools": "11.3.7",
|