@ms-cloudpack/bundler-utilities 0.3.9 → 0.4.1
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.
|
@@ -1,20 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* Don't externalize inlined packages, node builtins, absolute paths, relative paths,
|
|
4
|
-
* or dependencies of inlined dependencies except if they are explicitly marked as external.
|
|
5
|
-
*
|
|
6
|
-
* @param params - The parameters for the externalization check, e.g.
|
|
7
|
-
* `{
|
|
8
|
-
* inputPath: "/app",
|
|
9
|
-
* id: "/app/src/about.tsx",
|
|
10
|
-
* parent: "/app",
|
|
11
|
-
* inlined: ["foo"],
|
|
12
|
-
* external: ["bar", "baz"],
|
|
13
|
-
* polyfills: new Set(["global"]),
|
|
14
|
-
* shouldInlineNodeBuiltins: false,
|
|
15
|
-
* };`
|
|
16
|
-
*/
|
|
17
|
-
export declare function shouldExternalizePackage(params: {
|
|
1
|
+
import type { BundleMode, TargetEnvironment } from '@ms-cloudpack/common-types';
|
|
2
|
+
export interface ShouldExternalizePackageOptions {
|
|
18
3
|
/**
|
|
19
4
|
* The absolute input path for the entry points to be bundled.
|
|
20
5
|
*/
|
|
@@ -41,14 +26,47 @@ export declare function shouldExternalizePackage(params: {
|
|
|
41
26
|
*/
|
|
42
27
|
external?: string[];
|
|
43
28
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
29
|
+
* Additional polyfill package names that should be inlined instead of externalized
|
|
30
|
+
* if `shouldInlineNodeBuiltins` is true. This does NOT need to include Node.js built-ins.
|
|
46
31
|
*/
|
|
47
32
|
polyfills?: Set<string>;
|
|
48
33
|
/**
|
|
49
34
|
* Whether Node.js built-in modules should be inlined (with polyfills) rather than externalized.
|
|
50
35
|
* When true, modules like 'fs' or 'path' will be replaced with browser-compatible versions.
|
|
36
|
+
*
|
|
37
|
+
* If `targetEnvironment` is `'node'`, this will always be ignored since built-ins should always be externalized.
|
|
51
38
|
*/
|
|
52
39
|
shouldInlineNodeBuiltins: boolean;
|
|
53
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Where the bundled code will run.
|
|
42
|
+
*/
|
|
43
|
+
targetEnvironment: TargetEnvironment;
|
|
44
|
+
/**
|
|
45
|
+
* Bundle mode. In production/development mode, all packages will be inlined, except Node built-ins if
|
|
46
|
+
* `targetEnvironment` is `'node'`. (You can skip calling this function and just inline everything for
|
|
47
|
+
* `targetEnvironment: 'browser'`.)
|
|
48
|
+
*/
|
|
49
|
+
mode: BundleMode;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Determines if a package should be externalized from the bundle.
|
|
53
|
+
* Don't externalize inlined packages, node builtins, absolute paths, relative paths,
|
|
54
|
+
* or dependencies of inlined dependencies except if they are explicitly marked as external.
|
|
55
|
+
*
|
|
56
|
+
* Example params (`<root>` is the absolute repo root path):
|
|
57
|
+
* ```ts
|
|
58
|
+
* {
|
|
59
|
+
* inputPath: "<root>/app",
|
|
60
|
+
* id: "<root>/app/src/about.tsx",
|
|
61
|
+
* parent: "<root>/app/src/index.tsx",
|
|
62
|
+
* inlined: ["foo"],
|
|
63
|
+
* external: ["bar", "baz"],
|
|
64
|
+
* polyfills: new Set(["global"]),
|
|
65
|
+
* shouldInlineNodeBuiltins: false,
|
|
66
|
+
* targetEnvironment: "browser",
|
|
67
|
+
* mode: "library"
|
|
68
|
+
* )
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function shouldExternalizePackage(params: ShouldExternalizePackageOptions): boolean;
|
|
54
72
|
//# sourceMappingURL=shouldExternalizePackage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shouldExternalizePackage.d.ts","sourceRoot":"","sources":["../src/shouldExternalizePackage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"shouldExternalizePackage.d.ts","sourceRoot":"","sources":["../src/shouldExternalizePackage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAKhF,MAAM,WAAW,+BAA+B;IAC9C;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB;;;OAGG;IACH,SAAS,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACxB;;;;;OAKG;IACH,wBAAwB,EAAE,OAAO,CAAC;IAClC;;OAEG;IACH,iBAAiB,EAAE,iBAAiB,CAAC;IACrC;;;;OAIG;IACH,IAAI,EAAE,UAAU,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,+BAA+B,GAAG,OAAO,CA+BzF"}
|
|
@@ -6,31 +6,45 @@ import path from 'path';
|
|
|
6
6
|
* Don't externalize inlined packages, node builtins, absolute paths, relative paths,
|
|
7
7
|
* or dependencies of inlined dependencies except if they are explicitly marked as external.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
9
|
+
* Example params (`<root>` is the absolute repo root path):
|
|
10
|
+
* ```ts
|
|
11
|
+
* {
|
|
12
|
+
* inputPath: "<root>/app",
|
|
13
|
+
* id: "<root>/app/src/about.tsx",
|
|
14
|
+
* parent: "<root>/app/src/index.tsx",
|
|
14
15
|
* inlined: ["foo"],
|
|
15
16
|
* external: ["bar", "baz"],
|
|
16
17
|
* polyfills: new Set(["global"]),
|
|
17
18
|
* shouldInlineNodeBuiltins: false,
|
|
18
|
-
*
|
|
19
|
+
* targetEnvironment: "browser",
|
|
20
|
+
* mode: "library"
|
|
21
|
+
* )
|
|
22
|
+
* ```
|
|
19
23
|
*/
|
|
20
24
|
export function shouldExternalizePackage(params) {
|
|
21
|
-
const { inputPath, id, parent, inlined, external, polyfills, shouldInlineNodeBuiltins } = params;
|
|
25
|
+
const { inputPath, id, parent, inlined, external, polyfills, shouldInlineNodeBuiltins, targetEnvironment, mode } = params;
|
|
26
|
+
if (targetEnvironment === 'node' && isBuiltin(id)) {
|
|
27
|
+
// For Node.js, always externalize built-in modules, even in production mode.
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
if (mode !== 'library') {
|
|
31
|
+
// For production/development mode, don't externalize anything (except as above).
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
22
34
|
const isExternalPackage = isExternal(id, external);
|
|
23
|
-
return !(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
35
|
+
return !((targetEnvironment === 'browser' &&
|
|
36
|
+
// For browser bundles, we should inline node builtins that are handled by the nodePolyfills plugin.
|
|
37
|
+
// Except if the dependency is listed as external.
|
|
38
|
+
!isExternalPackage &&
|
|
39
|
+
shouldInlineNodeBuiltins &&
|
|
40
|
+
isInlinedNodeBuiltin(id, polyfills)) ||
|
|
27
41
|
path.isAbsolute(id) ||
|
|
28
42
|
isRelative(id) ||
|
|
29
43
|
// The length check ensures that parseImportString is only called when there are inlined deps
|
|
30
44
|
(inlined?.length && inlined.includes(parseImportString(id).packageName)) ||
|
|
31
45
|
// A file outside of the package is doing this import, so we should inline the nested dependencies.
|
|
32
46
|
// Except if the dependency is listed as external.
|
|
33
|
-
(isNestedDependency(inputPath, parent) && !isExternalPackage))
|
|
47
|
+
(isNestedDependency(inputPath, parent) && !isExternalPackage));
|
|
34
48
|
}
|
|
35
49
|
function isInlinedNodeBuiltin(id, polyfills) {
|
|
36
50
|
return isBuiltin(id) || !!polyfills?.has(id);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shouldExternalizePackage.js","sourceRoot":"","sources":["../src/shouldExternalizePackage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"shouldExternalizePackage.js","sourceRoot":"","sources":["../src/shouldExternalizePackage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,IAAI,MAAM,MAAM,CAAC;AAoDxB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAAuC;IAC9E,MAAM,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,IAAI,EAAE,GAC9G,MAAM,CAAC;IAET,IAAI,iBAAiB,KAAK,MAAM,IAAI,SAAS,CAAC,EAAE,CAAC,EAAE,CAAC;QAClD,6EAA6E;QAC7E,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,iFAAiF;QACjF,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,iBAAiB,GAAG,UAAU,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;IAEnD,OAAO,CAAC,CACN,CAAC,iBAAiB,KAAK,SAAS;QAC9B,oGAAoG;QACpG,kDAAkD;QAClD,CAAC,iBAAiB;QAClB,wBAAwB;QACxB,oBAAoB,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC;QACtC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QACnB,UAAU,CAAC,EAAE,CAAC;QACd,6FAA6F;QAC7F,CAAC,OAAO,EAAE,MAAM,IAAI,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC,WAAW,CAAC,CAAC;QACxE,mGAAmG;QACnG,kDAAkD;QAClD,CAAC,kBAAkB,CAAC,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAC9D,CAAC;AACJ,CAAC;AAED,SAAS,oBAAoB,CAAC,EAAU,EAAE,SAAkC;IAC1E,OAAO,SAAS,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;AAC/C,CAAC;AAED,SAAS,UAAU,CAAC,EAAU;IAC5B,OAAO,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,KAAK,IAAI,CAAC;AACxG,CAAC;AAED,wDAAwD;AACxD,SAAS,kBAAkB,CAAC,SAAiB,EAAE,MAA0B;IACvE,MAAM,kBAAkB,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACjF,OAAO,CAAC,CAAC,CAAC,kBAAkB,EAAE,UAAU,CAAC,IAAI,CAAC,IAAI,kBAAkB,EAAE,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC;AAClG,CAAC;AAED,SAAS,UAAU,CAAC,EAAU,EAAE,QAA8B;IAC5D,OAAO,CAAC,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import type { BundleMode, TargetEnvironment } from '@ms-cloudpack/common-types';\nimport { parseImportString } from '@ms-cloudpack/path-string-parsing';\nimport { isBuiltin } from 'module';\nimport path from 'path';\n\nexport interface ShouldExternalizePackageOptions {\n /**\n * The absolute input path for the entry points to be bundled.\n */\n inputPath: string;\n /**\n * The module ID (import path) being evaluated for externalization.\n * This can be a package name or a relative/absolute path.\n */\n id: string;\n /**\n * The absolute file path of the module importing this dependency.\n * Used to determine if the import is coming from outside the package.\n * If it is undefined, the import is considered to be from the same package (this is common in Rollup).\n */\n parent?: string;\n /**\n * List of dependencies which should be explicitly inlined rather than externalized.\n * These will always be bundled with the package regardless of other settings.\n */\n inlined?: string[];\n /**\n * List of dependencies which should be explicitly externalized.\n * These will never be bundled with the package.\n */\n external?: string[];\n /**\n * Additional polyfill package names that should be inlined instead of externalized\n * if `shouldInlineNodeBuiltins` is true. This does NOT need to include Node.js built-ins.\n */\n polyfills?: Set<string>;\n /**\n * Whether Node.js built-in modules should be inlined (with polyfills) rather than externalized.\n * When true, modules like 'fs' or 'path' will be replaced with browser-compatible versions.\n *\n * If `targetEnvironment` is `'node'`, this will always be ignored since built-ins should always be externalized.\n */\n shouldInlineNodeBuiltins: boolean;\n /**\n * Where the bundled code will run.\n */\n targetEnvironment: TargetEnvironment;\n /**\n * Bundle mode. In production/development mode, all packages will be inlined, except Node built-ins if\n * `targetEnvironment` is `'node'`. (You can skip calling this function and just inline everything for\n * `targetEnvironment: 'browser'`.)\n */\n mode: BundleMode;\n}\n\n/**\n * Determines if a package should be externalized from the bundle.\n * Don't externalize inlined packages, node builtins, absolute paths, relative paths,\n * or dependencies of inlined dependencies except if they are explicitly marked as external.\n *\n * Example params (`<root>` is the absolute repo root path):\n * ```ts\n * {\n * inputPath: \"<root>/app\",\n * id: \"<root>/app/src/about.tsx\",\n * parent: \"<root>/app/src/index.tsx\",\n * inlined: [\"foo\"],\n * external: [\"bar\", \"baz\"],\n * polyfills: new Set([\"global\"]),\n * shouldInlineNodeBuiltins: false,\n * targetEnvironment: \"browser\",\n * mode: \"library\"\n * )\n * ```\n */\nexport function shouldExternalizePackage(params: ShouldExternalizePackageOptions): boolean {\n const { inputPath, id, parent, inlined, external, polyfills, shouldInlineNodeBuiltins, targetEnvironment, mode } =\n params;\n\n if (targetEnvironment === 'node' && isBuiltin(id)) {\n // For Node.js, always externalize built-in modules, even in production mode.\n return true;\n }\n\n if (mode !== 'library') {\n // For production/development mode, don't externalize anything (except as above).\n return false;\n }\n\n const isExternalPackage = isExternal(id, external);\n\n return !(\n (targetEnvironment === 'browser' &&\n // For browser bundles, we should inline node builtins that are handled by the nodePolyfills plugin.\n // Except if the dependency is listed as external.\n !isExternalPackage &&\n shouldInlineNodeBuiltins &&\n isInlinedNodeBuiltin(id, polyfills)) ||\n path.isAbsolute(id) ||\n isRelative(id) ||\n // The length check ensures that parseImportString is only called when there are inlined deps\n (inlined?.length && inlined.includes(parseImportString(id).packageName)) ||\n // A file outside of the package is doing this import, so we should inline the nested dependencies.\n // Except if the dependency is listed as external.\n (isNestedDependency(inputPath, parent) && !isExternalPackage)\n );\n}\n\nfunction isInlinedNodeBuiltin(id: string, polyfills: Set<string> | undefined): boolean {\n return isBuiltin(id) || !!polyfills?.has(id);\n}\n\nfunction isRelative(id: string): boolean {\n return id.startsWith('/') || id.startsWith('./') || id.startsWith('../') || id === '.' || id === '..';\n}\n\n// Check if the import comes from an inlined dependency.\nfunction isNestedDependency(inputPath: string, parent: string | undefined): boolean {\n const relativeParentPath = parent ? path.relative(inputPath, parent) : undefined;\n return !!(relativeParentPath?.startsWith('..') || relativeParentPath?.includes('node_modules'));\n}\n\nfunction isExternal(id: string, external: string[] | undefined): boolean {\n return !!external?.find((ex) => id.startsWith(ex));\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/bundler-utilities",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Utilities used by multiple bundlers.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -15,13 +15,14 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@ms-cloudpack/json-utilities": "^0.1.10",
|
|
18
|
-
"@ms-cloudpack/package-utilities": "^12.3.
|
|
18
|
+
"@ms-cloudpack/package-utilities": "^12.3.16",
|
|
19
19
|
"@ms-cloudpack/path-string-parsing": "^1.2.7",
|
|
20
|
-
"@ms-cloudpack/path-utilities": "^3.1.
|
|
20
|
+
"@ms-cloudpack/path-utilities": "^3.1.10",
|
|
21
21
|
"get-tsconfig": "^4.7.2",
|
|
22
22
|
"tsconfig-to-swcconfig": "^2.7.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
+
"@ms-cloudpack/common-types": "^0.26.0",
|
|
25
26
|
"@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
|
|
26
27
|
"@ms-cloudpack/scripts": "^0.0.1",
|
|
27
28
|
"@ms-cloudpack/test-utilities": "^0.5.0",
|