@module-federation/esbuild 0.0.0-chore-bump-node-22-20260710161714
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/LICENSE +21 -0
- package/README.md +113 -0
- package/dist/adapters/lib/collect-exports.d.ts +3 -0
- package/dist/adapters/lib/collect-exports.js +153 -0
- package/dist/adapters/lib/collect-exports.mjs +80 -0
- package/dist/adapters/lib/commonjs.d.ts +101 -0
- package/dist/adapters/lib/commonjs.js +258 -0
- package/dist/adapters/lib/commonjs.mjs +182 -0
- package/dist/adapters/lib/containerPlugin.d.ts +6 -0
- package/dist/adapters/lib/containerPlugin.js +233 -0
- package/dist/adapters/lib/containerPlugin.mjs +179 -0
- package/dist/adapters/lib/containerReference.d.ts +6 -0
- package/dist/adapters/lib/containerReference.js +257 -0
- package/dist/adapters/lib/containerReference.mjs +188 -0
- package/dist/adapters/lib/lexer.d.ts +9 -0
- package/dist/adapters/lib/lexer.js +187 -0
- package/dist/adapters/lib/lexer.mjs +136 -0
- package/dist/adapters/lib/linkRemotesPlugin.d.ts +6 -0
- package/dist/adapters/lib/linkRemotesPlugin.js +117 -0
- package/dist/adapters/lib/linkRemotesPlugin.mjs +48 -0
- package/dist/adapters/lib/manifest.d.ts +2 -0
- package/dist/adapters/lib/manifest.js +227 -0
- package/dist/adapters/lib/manifest.mjs +159 -0
- package/dist/adapters/lib/plugin.d.ts +8 -0
- package/dist/adapters/lib/plugin.js +340 -0
- package/dist/adapters/lib/plugin.mjs +247 -0
- package/dist/adapters/lib/react-replacements.d.ts +9 -0
- package/dist/adapters/lib/react-replacements.js +86 -0
- package/dist/adapters/lib/react-replacements.mjs +34 -0
- package/dist/adapters/lib/transform.d.ts +8 -0
- package/dist/adapters/lib/transform.js +171 -0
- package/dist/adapters/lib/transform.mjs +116 -0
- package/dist/adapters/lib/utils.d.ts +3 -0
- package/dist/adapters/lib/utils.js +87 -0
- package/dist/adapters/lib/utils.mjs +31 -0
- package/dist/build.d.ts +5 -0
- package/dist/build.js +140 -0
- package/dist/build.mjs +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +55 -0
- package/dist/index.mjs +3 -0
- package/dist/lib/config/configuration-context.d.ts +7 -0
- package/dist/lib/config/configuration-context.js +73 -0
- package/dist/lib/config/configuration-context.mjs +18 -0
- package/dist/lib/config/federation-config.d.ts +30 -0
- package/dist/lib/config/federation-config.js +29 -0
- package/dist/lib/config/federation-config.mjs +3 -0
- package/dist/lib/config/share-utils.d.ts +29 -0
- package/dist/lib/config/share-utils.js +340 -0
- package/dist/lib/config/share-utils.mjs +245 -0
- package/dist/lib/config/with-native-federation.d.ts +23 -0
- package/dist/lib/config/with-native-federation.js +120 -0
- package/dist/lib/config/with-native-federation.mjs +66 -0
- package/dist/lib/core/build-adapter.d.ts +4 -0
- package/dist/lib/core/build-adapter.js +72 -0
- package/dist/lib/core/build-adapter.mjs +16 -0
- package/dist/lib/core/createContainerTemplate.d.ts +1 -0
- package/dist/lib/core/createContainerTemplate.js +235 -0
- package/dist/lib/core/createContainerTemplate.mjs +183 -0
- package/dist/lib/core/default-skip-list.d.ts +12 -0
- package/dist/lib/core/default-skip-list.js +103 -0
- package/dist/lib/core/default-skip-list.mjs +45 -0
- package/dist/lib/core/federation-options.d.ts +10 -0
- package/dist/lib/core/federation-options.js +29 -0
- package/dist/lib/core/federation-options.mjs +3 -0
- package/dist/lib/core/get-externals.d.ts +2 -0
- package/dist/lib/core/get-externals.js +68 -0
- package/dist/lib/core/get-externals.mjs +17 -0
- package/dist/lib/core/load-federation-config.d.ts +6 -0
- package/dist/lib/core/load-federation-config.js +71 -0
- package/dist/lib/core/load-federation-config.mjs +16 -0
- package/dist/lib/core/write-federation-info.d.ts +9 -0
- package/dist/lib/core/write-federation-info.js +67 -0
- package/dist/lib/core/write-federation-info.mjs +12 -0
- package/dist/lib/utils/logger.d.ts +11 -0
- package/dist/lib/utils/logger.js +113 -0
- package/dist/lib/utils/logger.mjs +44 -0
- package/dist/lib/utils/mapped-paths.d.ts +10 -0
- package/dist/lib/utils/mapped-paths.js +95 -0
- package/dist/lib/utils/mapped-paths.mjs +40 -0
- package/dist/lib/utils/normalize.d.ts +1 -0
- package/dist/lib/utils/normalize.js +66 -0
- package/dist/lib/utils/normalize.mjs +15 -0
- package/dist/lib/utils/package-info.d.ts +18 -0
- package/dist/lib/utils/package-info.js +325 -0
- package/dist/lib/utils/package-info.mjs +259 -0
- package/dist/plugin.d.ts +1 -0
- package/dist/plugin.js +96 -0
- package/dist/plugin.mjs +4 -0
- package/dist/resolve/esm-resolver.mjs +19 -0
- package/dist/resolve/package.json +3 -0
- package/dist/rslib-runtime.mjs +31 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present zhouxiao(zhoushaw)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# @module-federation/esbuild
|
|
2
|
+
|
|
3
|
+
This package provides an esbuild plugin for Module Federation, enabling you to easily share code between independently built and deployed applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the package using npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @module-federation/esbuild
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
To use the Module Federation plugin with esbuild, add it to your esbuild configuration:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
const esbuild = require('esbuild');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
const { moduleFederationPlugin } = require('@module-federation/esbuild/plugin');
|
|
21
|
+
const federationConfig = require('./federation.config.js');
|
|
22
|
+
|
|
23
|
+
async function buildApp() {
|
|
24
|
+
const tsConfig = 'tsconfig.json';
|
|
25
|
+
const outputPath = path.join('dist', 'host');
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
await esbuild.build({
|
|
29
|
+
entryPoints: [path.join('host', 'main.ts')],
|
|
30
|
+
outdir: outputPath,
|
|
31
|
+
bundle: true,
|
|
32
|
+
platform: 'browser',
|
|
33
|
+
format: 'esm',
|
|
34
|
+
mainFields: ['es2020', 'browser', 'module', 'main'],
|
|
35
|
+
conditions: ['es2020', 'es2015', 'module'],
|
|
36
|
+
resolveExtensions: ['.ts', '.tsx', '.mjs', '.js'],
|
|
37
|
+
tsconfig: tsConfig,
|
|
38
|
+
splitting: true,
|
|
39
|
+
plugins: [moduleFederationPlugin(federationConfig)],
|
|
40
|
+
});
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.error(err);
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
buildApp();
|
|
48
|
+
|
|
49
|
+
// Example of federation.config.js
|
|
50
|
+
|
|
51
|
+
const { withFederation, shareAll } = require('@module-federation/esbuild/build');
|
|
52
|
+
|
|
53
|
+
module.exports = withFederation({
|
|
54
|
+
name: 'host',
|
|
55
|
+
filename: 'remoteEntry.js',
|
|
56
|
+
exposes: {
|
|
57
|
+
'./Component': './src/Component',
|
|
58
|
+
},
|
|
59
|
+
shared: {
|
|
60
|
+
react: {
|
|
61
|
+
singleton: true,
|
|
62
|
+
version: '^18.2.0',
|
|
63
|
+
},
|
|
64
|
+
'react-dom': {
|
|
65
|
+
singleton: true,
|
|
66
|
+
version: '^18.2.0',
|
|
67
|
+
},
|
|
68
|
+
rxjs: {
|
|
69
|
+
singleton: true,
|
|
70
|
+
version: '^7.8.1',
|
|
71
|
+
},
|
|
72
|
+
...shareAll({
|
|
73
|
+
singleton: true,
|
|
74
|
+
strictVersion: true,
|
|
75
|
+
requiredVersion: 'auto',
|
|
76
|
+
includeSecondaries: false,
|
|
77
|
+
}),
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The `moduleFederationPlugin` accepts a configuration object with the following properties:
|
|
83
|
+
|
|
84
|
+
- `name` (string): The name of the host application.
|
|
85
|
+
- `filename` (string, optional): The name of the remote entry file. Defaults to `'remoteEntry.js'`.
|
|
86
|
+
- `remotes` (object, optional): An object specifying the remote applications and their entry points.
|
|
87
|
+
- `exposes` (object, optional): An object specifying the modules to be exposed by the host application.
|
|
88
|
+
- `shared` (array, optional): An array of package names to be shared between the host and remote applications.
|
|
89
|
+
|
|
90
|
+
## Plugin Features
|
|
91
|
+
|
|
92
|
+
The `moduleFederationPlugin` includes the following features:
|
|
93
|
+
|
|
94
|
+
- **Virtual Share Module**: Creates a virtual module for sharing dependencies between the host and remote applications.
|
|
95
|
+
- **Virtual Remote Module**: Creates a virtual module for importing exposed modules from remote applications.
|
|
96
|
+
- **CommonJS to ESM Transformation**: Transforms CommonJS modules to ESM format for compatibility with Module Federation.
|
|
97
|
+
- **Shared Dependencies Linking**: Links shared dependencies between the host and remote applications.
|
|
98
|
+
- **Manifest Generation**: Generates a manifest file containing information about the exposed modules and their exports.
|
|
99
|
+
|
|
100
|
+
## API
|
|
101
|
+
|
|
102
|
+
### `moduleFederationPlugin(config)`
|
|
103
|
+
|
|
104
|
+
Creates an esbuild plugin for Module Federation.
|
|
105
|
+
|
|
106
|
+
- `config` (object): The Module Federation configuration.
|
|
107
|
+
- `name` (string): The name of the host application.
|
|
108
|
+
- `filename` (string, optional): The name of the remote entry file. Defaults to `'remoteEntry.js'`.
|
|
109
|
+
- `remotes` (object, optional): An object specifying the remote applications and their entry points.
|
|
110
|
+
- `exposes` (object, optional): An object specifying the modules to be exposed by the host application.
|
|
111
|
+
- `shared` (array, optional): An array of package names to be shared between the host and remote applications.
|
|
112
|
+
|
|
113
|
+
Returns an esbuild plugin instance.
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const resolve: (arg1: string, arg2: string) => Promise<string | false | undefined>;
|
|
2
|
+
export declare const resolvePackageJson: (packageName: string, callback: (err: Error | null, result?: string) => void) => Promise<void>;
|
|
3
|
+
export declare function getExports(modulePath: string): Promise<string[]>;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
|
|
3
|
+
return typeof document === 'undefined'
|
|
4
|
+
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
|
|
5
|
+
: (document.currentScript && document.currentScript.src) ||
|
|
6
|
+
new URL('main.js', document.baseURI).href;
|
|
7
|
+
})();
|
|
8
|
+
;
|
|
9
|
+
// The require scope
|
|
10
|
+
var __webpack_require__ = {};
|
|
11
|
+
|
|
12
|
+
// webpack/runtime/compat_get_default_export
|
|
13
|
+
(() => {
|
|
14
|
+
// getDefaultExport function for compatibility with non-ESM modules
|
|
15
|
+
__webpack_require__.n = (module) => {
|
|
16
|
+
var getter = module && module.__esModule ?
|
|
17
|
+
() => (module['default']) :
|
|
18
|
+
() => (module);
|
|
19
|
+
__webpack_require__.d(getter, { a: getter });
|
|
20
|
+
return getter;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
})();
|
|
24
|
+
// webpack/runtime/define_property_getters
|
|
25
|
+
(() => {
|
|
26
|
+
__webpack_require__.d = (exports, getters, values) => {
|
|
27
|
+
var define = (defs, kind) => {
|
|
28
|
+
for(var key in defs) {
|
|
29
|
+
if(__webpack_require__.o(defs, key) && !__webpack_require__.o(exports, key)) {
|
|
30
|
+
Object.defineProperty(exports, key, { enumerable: true, [kind]: defs[key] });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
define(getters, "get");
|
|
35
|
+
define(values, "value");
|
|
36
|
+
};
|
|
37
|
+
})();
|
|
38
|
+
// webpack/runtime/has_own_property
|
|
39
|
+
(() => {
|
|
40
|
+
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
41
|
+
})();
|
|
42
|
+
// webpack/runtime/make_namespace_object
|
|
43
|
+
(() => {
|
|
44
|
+
// define __esModule on exports
|
|
45
|
+
__webpack_require__.r = (exports) => {
|
|
46
|
+
if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
47
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
48
|
+
}
|
|
49
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
50
|
+
};
|
|
51
|
+
})();
|
|
52
|
+
var __webpack_exports__ = {};
|
|
53
|
+
// ESM COMPAT FLAG
|
|
54
|
+
__webpack_require__.r(__webpack_exports__);
|
|
55
|
+
|
|
56
|
+
// EXPORTS
|
|
57
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
58
|
+
getExports: () => (/* binding */ getExports),
|
|
59
|
+
resolve: () => (/* binding */ resolve),
|
|
60
|
+
resolvePackageJson: () => (/* binding */ resolvePackageJson)
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
;// CONCATENATED MODULE: external "es-module-lexer"
|
|
64
|
+
const external_es_module_lexer_namespaceObject = require("es-module-lexer");
|
|
65
|
+
;// CONCATENATED MODULE: external "cjs-module-lexer"
|
|
66
|
+
const external_cjs_module_lexer_namespaceObject = require("cjs-module-lexer");
|
|
67
|
+
;// CONCATENATED MODULE: external "util"
|
|
68
|
+
const external_util_namespaceObject = require("util");
|
|
69
|
+
;// CONCATENATED MODULE: external "enhanced-resolve"
|
|
70
|
+
const external_enhanced_resolve_namespaceObject = require("enhanced-resolve");
|
|
71
|
+
var external_enhanced_resolve_default = /*#__PURE__*/__webpack_require__.n(external_enhanced_resolve_namespaceObject);
|
|
72
|
+
;// CONCATENATED MODULE: external "fs"
|
|
73
|
+
const external_fs_namespaceObject = require("fs");
|
|
74
|
+
var external_fs_default = /*#__PURE__*/__webpack_require__.n(external_fs_namespaceObject);
|
|
75
|
+
;// CONCATENATED MODULE: external "path"
|
|
76
|
+
const external_path_namespaceObject = require("path");
|
|
77
|
+
var external_path_default = /*#__PURE__*/__webpack_require__.n(external_path_namespaceObject);
|
|
78
|
+
;// CONCATENATED MODULE: ./src/adapters/lib/collect-exports.ts
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
const resolve = (0,external_util_namespaceObject.promisify)(external_enhanced_resolve_default().create({
|
|
86
|
+
mainFields: [
|
|
87
|
+
'browser',
|
|
88
|
+
'module',
|
|
89
|
+
'main'
|
|
90
|
+
]
|
|
91
|
+
}));
|
|
92
|
+
const resolvePackageJson = async (packageName, callback)=>{
|
|
93
|
+
try {
|
|
94
|
+
const filepath = await resolve(__dirname, packageName);
|
|
95
|
+
if (typeof filepath !== 'string') {
|
|
96
|
+
return callback(new Error('Failed to resolve package path'));
|
|
97
|
+
}
|
|
98
|
+
// Resolve the path to the package.json file
|
|
99
|
+
const packageJsonPath = external_path_default().join(filepath, 'package.json');
|
|
100
|
+
if (external_fs_default().existsSync(packageJsonPath)) {
|
|
101
|
+
callback(null, packageJsonPath);
|
|
102
|
+
} else {
|
|
103
|
+
callback(new Error(`package.json not found for package: ${packageName}`));
|
|
104
|
+
}
|
|
105
|
+
} catch (err) {
|
|
106
|
+
callback(err);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
async function getExports(modulePath) {
|
|
110
|
+
await external_es_module_lexer_namespaceObject.init;
|
|
111
|
+
await external_cjs_module_lexer_namespaceObject.init;
|
|
112
|
+
try {
|
|
113
|
+
const exports = [];
|
|
114
|
+
const paths = [];
|
|
115
|
+
const resolvedPath = await resolve(process.cwd(), modulePath);
|
|
116
|
+
if (typeof resolvedPath === 'string') {
|
|
117
|
+
paths.push(resolvedPath);
|
|
118
|
+
}
|
|
119
|
+
while(paths.length > 0){
|
|
120
|
+
const currentPath = paths.pop();
|
|
121
|
+
if (currentPath) {
|
|
122
|
+
const content = await external_fs_default().promises.readFile(currentPath, 'utf8');
|
|
123
|
+
try {
|
|
124
|
+
const { exports: cjsExports } = (0,external_cjs_module_lexer_namespaceObject.parse)(content);
|
|
125
|
+
exports.push(...cjsExports);
|
|
126
|
+
} catch {
|
|
127
|
+
const [, esExports] = (0,external_es_module_lexer_namespaceObject.parse)(content);
|
|
128
|
+
exports.push(...esExports.map((exp)=>exp.n));
|
|
129
|
+
}
|
|
130
|
+
// TODO: Handle re-exports
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!exports.includes('default')) {
|
|
134
|
+
exports.push('default');
|
|
135
|
+
}
|
|
136
|
+
return exports;
|
|
137
|
+
} catch (e) {
|
|
138
|
+
console.log(e);
|
|
139
|
+
return [
|
|
140
|
+
'default'
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
exports.getExports = __webpack_exports__.getExports;
|
|
146
|
+
exports.resolve = __webpack_exports__.resolve;
|
|
147
|
+
exports.resolvePackageJson = __webpack_exports__.resolvePackageJson;
|
|
148
|
+
for(var __rspack_i in __webpack_exports__) {
|
|
149
|
+
if(["getExports","resolve","resolvePackageJson"].indexOf(__rspack_i) === -1) {
|
|
150
|
+
exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { init, parse } from "es-module-lexer";
|
|
2
|
+
import { init as external_cjs_module_lexer_init, parse as external_cjs_module_lexer_parse } from "cjs-module-lexer";
|
|
3
|
+
import { promisify } from "util";
|
|
4
|
+
import enhanced_resolve from "enhanced-resolve";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
const resolve = promisify(enhanced_resolve.create({
|
|
21
|
+
mainFields: [
|
|
22
|
+
'browser',
|
|
23
|
+
'module',
|
|
24
|
+
'main'
|
|
25
|
+
]
|
|
26
|
+
}));
|
|
27
|
+
const resolvePackageJson = async (packageName, callback)=>{
|
|
28
|
+
try {
|
|
29
|
+
const filepath = await resolve(__dirname, packageName);
|
|
30
|
+
if (typeof filepath !== 'string') {
|
|
31
|
+
return callback(new Error('Failed to resolve package path'));
|
|
32
|
+
}
|
|
33
|
+
// Resolve the path to the package.json file
|
|
34
|
+
const packageJsonPath = path.join(filepath, 'package.json');
|
|
35
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
36
|
+
callback(null, packageJsonPath);
|
|
37
|
+
} else {
|
|
38
|
+
callback(new Error(`package.json not found for package: ${packageName}`));
|
|
39
|
+
}
|
|
40
|
+
} catch (err) {
|
|
41
|
+
callback(err);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
async function getExports(modulePath) {
|
|
45
|
+
await init;
|
|
46
|
+
await external_cjs_module_lexer_init;
|
|
47
|
+
try {
|
|
48
|
+
const exports = [];
|
|
49
|
+
const paths = [];
|
|
50
|
+
const resolvedPath = await resolve(process.cwd(), modulePath);
|
|
51
|
+
if (typeof resolvedPath === 'string') {
|
|
52
|
+
paths.push(resolvedPath);
|
|
53
|
+
}
|
|
54
|
+
while(paths.length > 0){
|
|
55
|
+
const currentPath = paths.pop();
|
|
56
|
+
if (currentPath) {
|
|
57
|
+
const content = await fs.promises.readFile(currentPath, 'utf8');
|
|
58
|
+
try {
|
|
59
|
+
const { exports: cjsExports } = external_cjs_module_lexer_parse(content);
|
|
60
|
+
exports.push(...cjsExports);
|
|
61
|
+
} catch {
|
|
62
|
+
const [, esExports] = parse(content);
|
|
63
|
+
exports.push(...esExports.map((exp)=>exp.n));
|
|
64
|
+
}
|
|
65
|
+
// TODO: Handle re-exports
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (!exports.includes('default')) {
|
|
69
|
+
exports.push('default');
|
|
70
|
+
}
|
|
71
|
+
return exports;
|
|
72
|
+
} catch (e) {
|
|
73
|
+
console.log(e);
|
|
74
|
+
return [
|
|
75
|
+
'default'
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export { getExports, resolve, resolvePackageJson };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Plugin } from 'esbuild';
|
|
2
|
+
export interface CommonJSOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The regexp passed to onLoad() to match commonjs files.
|
|
5
|
+
*
|
|
6
|
+
* @default /\.c?js$/
|
|
7
|
+
*/
|
|
8
|
+
filter?: RegExp;
|
|
9
|
+
/**
|
|
10
|
+
* _Experimental_: Transform commonjs to es modules. You have to install
|
|
11
|
+
* `cjs-module-lexer` to let it work.
|
|
12
|
+
*
|
|
13
|
+
* When `true`, the plugin tries to wrap the commonjs module into:
|
|
14
|
+
*
|
|
15
|
+
* ```js
|
|
16
|
+
* var exports = {}, module = { exports };
|
|
17
|
+
* {
|
|
18
|
+
* // ... original content ...
|
|
19
|
+
* }
|
|
20
|
+
* exports = module.exports;
|
|
21
|
+
* // the exported names are extracted by cjs-module-lexer
|
|
22
|
+
* export default exports;
|
|
23
|
+
* var { something, "a-b" as a_b } = exports;
|
|
24
|
+
* export { something, a_b as "a-b" };
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @default false
|
|
28
|
+
*/
|
|
29
|
+
transform?: boolean | ((path: string) => boolean | TransformConfig | null | void);
|
|
30
|
+
/**
|
|
31
|
+
* _Experimental_: This options acts as a fallback of the `transform` option above.
|
|
32
|
+
*/
|
|
33
|
+
transformConfig?: Pick<TransformConfig, 'behavior' | 'sideEffects'>;
|
|
34
|
+
/**
|
|
35
|
+
* Controls which style of import should be used. By default, it transforms:
|
|
36
|
+
*
|
|
37
|
+
* ```js
|
|
38
|
+
* // input
|
|
39
|
+
* const foo = require("foo")
|
|
40
|
+
* // output
|
|
41
|
+
* import foo from "foo"
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* The above case is often correct when 'foo' is also a commonjs module.
|
|
45
|
+
* But if 'foo' has es module exports, it is better to use:
|
|
46
|
+
*
|
|
47
|
+
* ```js
|
|
48
|
+
* // output
|
|
49
|
+
* import * as foo from "foo"
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* In which case you can set `requireReturnsDefault` to `false` to get the above output.
|
|
53
|
+
* Or use the callback style to control the behavior for each module.
|
|
54
|
+
*
|
|
55
|
+
* @default true
|
|
56
|
+
*/
|
|
57
|
+
requireReturnsDefault?: boolean | ((path: string) => boolean);
|
|
58
|
+
/**
|
|
59
|
+
* Don't replace require("ignored-modules"). Note that this will cause
|
|
60
|
+
* esbuild generates the __require() wrapper which throw error at runtime.
|
|
61
|
+
*/
|
|
62
|
+
ignore?: string[] | ((path: string) => boolean);
|
|
63
|
+
}
|
|
64
|
+
export interface TransformConfig {
|
|
65
|
+
/**
|
|
66
|
+
* If `"babel"`, it will check if there be `exports.__esModule`,
|
|
67
|
+
* then export `exports.default`. i.e. The wrapper code becomes:
|
|
68
|
+
*
|
|
69
|
+
* ```js
|
|
70
|
+
* export default exports.__esModule ? exports.default : exports;
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @default "node"
|
|
74
|
+
*/
|
|
75
|
+
behavior?: 'babel' | 'node';
|
|
76
|
+
/**
|
|
77
|
+
* Also include these named exports if they aren't recognized automatically.
|
|
78
|
+
*
|
|
79
|
+
* @example ["something"]
|
|
80
|
+
*/
|
|
81
|
+
exports?: string[];
|
|
82
|
+
/**
|
|
83
|
+
* If `false`, slightly change the result to make it side-effect free.
|
|
84
|
+
* But it doesn't actually remove many code. So you maybe not need this.
|
|
85
|
+
*
|
|
86
|
+
* ```js
|
|
87
|
+
* var mod;
|
|
88
|
+
* var exports = /\*#__PURE__*\/ ((exports, module) => {
|
|
89
|
+
* // ... original content ...
|
|
90
|
+
* return module.exports;
|
|
91
|
+
* })((mod = { exports: {} }).exports, mod);
|
|
92
|
+
* export default exports;
|
|
93
|
+
* var a_b = /\*#__PURE__*\/ (() => exports['a-b'])();
|
|
94
|
+
* var something = /\*#__PURE__*\/ (() => exports.something)();
|
|
95
|
+
* export { a_b as "a-b", something };
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
sideEffects?: boolean;
|
|
99
|
+
}
|
|
100
|
+
export declare function commonjs({ filter, transform, transformConfig, requireReturnsDefault, ignore, }?: CommonJSOptions): Plugin;
|
|
101
|
+
export default commonjs;
|