rolldown-require 1.0.2 → 1.0.4
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/README.md +1 -2
- package/dist/index.cjs +206 -162
- package/dist/index.mjs +195 -151
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -15,7 +15,6 @@ Previously, [vite](https://vitejs.dev/) introduced the [bundle-require](https://
|
|
|
15
15
|
## How it works
|
|
16
16
|
|
|
17
17
|
- Bundles your file using `rolldown`, excluding `node_modules` (since bundling those can cause problems)
|
|
18
|
-
|
|
19
18
|
- `__filename`, `__dirname`, and `import.meta.url` are replaced with the source file’s value rather than the temporary output file’s value.
|
|
20
19
|
|
|
21
20
|
- Outputs the file in `esm` format if possible (for `.ts` and `.js` inputs).
|
|
@@ -24,7 +23,7 @@ Previously, [vite](https://vitejs.dev/) introduced the [bundle-require](https://
|
|
|
24
23
|
|
|
25
24
|
## Install
|
|
26
25
|
|
|
27
|
-
```
|
|
26
|
+
```sh
|
|
28
27
|
npm i rolldown-require rolldown
|
|
29
28
|
```
|
|
30
29
|
|
package/dist/index.cjs
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
6
|
});
|
|
7
7
|
|
|
8
|
-
// ../../node_modules/.pnpm/tsup@8.5.0_@swc+core@1.
|
|
8
|
+
// ../../node_modules/.pnpm/tsup@8.5.0_@swc+core@1.13.19_jiti@2.6.1_postcss@8.5.6_tsx@4.20.6_typescript@5.9.3_yaml@2.8.1/node_modules/tsup/assets/cjs_shims.js
|
|
9
9
|
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
|
|
10
10
|
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
11
11
|
|
|
@@ -28,8 +28,6 @@ var _rolldown = require('rolldown');
|
|
|
28
28
|
|
|
29
29
|
// src/utils.ts
|
|
30
30
|
var _child_process = require('child_process');
|
|
31
|
-
var _crypto = require('crypto'); var _crypto2 = _interopRequireDefault(_crypto);
|
|
32
|
-
|
|
33
31
|
|
|
34
32
|
|
|
35
33
|
|
|
@@ -60,12 +58,8 @@ function splitFileAndPostfix(path6) {
|
|
|
60
58
|
const file = cleanUrl(path6);
|
|
61
59
|
return { file, postfix: path6.slice(file.length) };
|
|
62
60
|
}
|
|
63
|
-
var AsyncFunction = async function() {
|
|
64
|
-
}.constructor;
|
|
65
61
|
|
|
66
62
|
// src/utils.ts
|
|
67
|
-
|
|
68
|
-
var _filter = require('rolldown/filter');
|
|
69
63
|
var createFilter = _pluginutils.createFilter;
|
|
70
64
|
var NODE_BUILTIN_NAMESPACE = "node:";
|
|
71
65
|
var NPM_BUILTIN_NAMESPACE = "npm:";
|
|
@@ -113,16 +107,6 @@ function isOptimizable(id, optimizeDeps) {
|
|
|
113
107
|
}
|
|
114
108
|
var bareImportRE = /^(?![a-z]:)[\w@](?!.*:\/\/)/i;
|
|
115
109
|
var deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//;
|
|
116
|
-
var _require = _module.createRequire.call(void 0, importMetaUrl);
|
|
117
|
-
var _dirname = _path2.default.dirname(_url.fileURLToPath.call(void 0, importMetaUrl));
|
|
118
|
-
var urlCanParse = _nullishCoalesce(_url.URL.canParse, () => ( ((path6, base) => {
|
|
119
|
-
try {
|
|
120
|
-
new (0, _url.URL)(path6, base);
|
|
121
|
-
return true;
|
|
122
|
-
} catch (e2) {
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
|
-
})));
|
|
126
110
|
function normalizePath(id) {
|
|
127
111
|
return _path2.default.posix.normalize(isWindows ? slash(id) : id);
|
|
128
112
|
}
|
|
@@ -140,7 +124,7 @@ function isObject(value) {
|
|
|
140
124
|
function tryStatSync(file) {
|
|
141
125
|
try {
|
|
142
126
|
return _fs2.default.statSync(file, { throwIfNoEntry: false });
|
|
143
|
-
} catch (
|
|
127
|
+
} catch (e2) {
|
|
144
128
|
}
|
|
145
129
|
}
|
|
146
130
|
function isFilePathESM(filePath, packageCache) {
|
|
@@ -152,12 +136,15 @@ function isFilePathESM(filePath, packageCache) {
|
|
|
152
136
|
try {
|
|
153
137
|
const pkg = findNearestPackageData(_path2.default.dirname(filePath), packageCache);
|
|
154
138
|
return _optionalChain([pkg, 'optionalAccess', _3 => _3.data, 'access', _4 => _4.type]) === "module";
|
|
155
|
-
} catch (
|
|
139
|
+
} catch (e3) {
|
|
156
140
|
return false;
|
|
157
141
|
}
|
|
158
142
|
}
|
|
159
143
|
}
|
|
160
|
-
var
|
|
144
|
+
var currentSafeRealpathSync = isWindows ? windowsSafeRealPathSync : _fs2.default.realpathSync.native;
|
|
145
|
+
function safeRealpathSync(filePath) {
|
|
146
|
+
return currentSafeRealpathSync(filePath);
|
|
147
|
+
}
|
|
161
148
|
var windowsNetworkMap = /* @__PURE__ */ new Map();
|
|
162
149
|
function windowsMappedRealpathSync(path6) {
|
|
163
150
|
const realPath = _fs2.default.realpathSync.native(path6);
|
|
@@ -182,14 +169,14 @@ function windowsSafeRealPathSync(path6) {
|
|
|
182
169
|
function optimizeSafeRealPathSync() {
|
|
183
170
|
const nodeVersion = _process2.default.versions.node.split(".").map(Number);
|
|
184
171
|
if (nodeVersion[0] < 18 || nodeVersion[0] === 18 && nodeVersion[1] < 10) {
|
|
185
|
-
|
|
172
|
+
currentSafeRealpathSync = _fs2.default.realpathSync;
|
|
186
173
|
return;
|
|
187
174
|
}
|
|
188
175
|
try {
|
|
189
176
|
_fs2.default.realpathSync.native(_path2.default.resolve("./"));
|
|
190
177
|
} catch (error) {
|
|
191
178
|
if (error.message.includes("EISDIR: illegal operation on a directory")) {
|
|
192
|
-
|
|
179
|
+
currentSafeRealpathSync = _fs2.default.realpathSync;
|
|
193
180
|
return;
|
|
194
181
|
}
|
|
195
182
|
}
|
|
@@ -204,14 +191,9 @@ function optimizeSafeRealPathSync() {
|
|
|
204
191
|
windowsNetworkMap.set(m[2], m[1]);
|
|
205
192
|
}
|
|
206
193
|
}
|
|
207
|
-
|
|
208
|
-
safeRealpathSync = _fs2.default.realpathSync.native;
|
|
209
|
-
} else {
|
|
210
|
-
safeRealpathSync = windowsMappedRealpathSync;
|
|
211
|
-
}
|
|
194
|
+
currentSafeRealpathSync = windowsNetworkMap.size === 0 ? _fs2.default.realpathSync.native : windowsMappedRealpathSync;
|
|
212
195
|
});
|
|
213
196
|
}
|
|
214
|
-
var hash = _nullishCoalesce(_crypto2.default.hash, () => ( ((algorithm, data, outputEncoding) => _crypto2.default.createHash(algorithm).update(data).digest(outputEncoding))));
|
|
215
197
|
function stripBomTag(content) {
|
|
216
198
|
if (content.charCodeAt(0) === 65279) {
|
|
217
199
|
return content.slice(1);
|
|
@@ -230,7 +212,7 @@ function getNpmPackageName(importPath) {
|
|
|
230
212
|
}
|
|
231
213
|
}
|
|
232
214
|
var dynamicImport = async (id, { format }) => {
|
|
233
|
-
const fn = format === "esm" ? (file) => Promise.resolve().then(() => _interopRequireWildcard(require(file))) : false ?
|
|
215
|
+
const fn = format === "esm" ? (file) => Promise.resolve().then(() => _interopRequireWildcard(require(file))) : false ? createRequire(importMetaUrl) : __require;
|
|
234
216
|
return fn(id);
|
|
235
217
|
};
|
|
236
218
|
|
|
@@ -239,7 +221,7 @@ var pnp;
|
|
|
239
221
|
if (_process2.default.versions.pnp) {
|
|
240
222
|
try {
|
|
241
223
|
pnp = _module.createRequire.call(void 0, importMetaUrl)("pnpapi");
|
|
242
|
-
} catch (
|
|
224
|
+
} catch (e4) {
|
|
243
225
|
}
|
|
244
226
|
}
|
|
245
227
|
function resolvePackageData(pkgName, basedir, preserveSymlinks = false, packageCache) {
|
|
@@ -258,7 +240,7 @@ function resolvePackageData(pkgName, basedir, preserveSymlinks = false, packageC
|
|
|
258
240
|
const pkgData = loadPackageData(_path2.default.join(pkg, "package.json"));
|
|
259
241
|
_optionalChain([packageCache, 'optionalAccess', _7 => _7.set, 'call', _8 => _8(cacheKey, pkgData)]);
|
|
260
242
|
return pkgData;
|
|
261
|
-
} catch (
|
|
243
|
+
} catch (e5) {
|
|
262
244
|
return null;
|
|
263
245
|
}
|
|
264
246
|
}
|
|
@@ -293,7 +275,7 @@ function resolvePackageData(pkgName, basedir, preserveSymlinks = false, packageC
|
|
|
293
275
|
}
|
|
294
276
|
return pkgData;
|
|
295
277
|
}
|
|
296
|
-
} catch (
|
|
278
|
+
} catch (e6) {
|
|
297
279
|
}
|
|
298
280
|
const nextBasedir = _path2.default.dirname(basedir);
|
|
299
281
|
if (nextBasedir === basedir) {
|
|
@@ -320,7 +302,7 @@ function findNearestPackageData(basedir, packageCache) {
|
|
|
320
302
|
setFnpdCache(packageCache, pkgData, basedir, originalBasedir);
|
|
321
303
|
}
|
|
322
304
|
return pkgData;
|
|
323
|
-
} catch (
|
|
305
|
+
} catch (e7) {
|
|
324
306
|
}
|
|
325
307
|
}
|
|
326
308
|
const nextBasedir = _path2.default.dirname(basedir);
|
|
@@ -854,34 +836,51 @@ var configDefaults = Object.freeze({
|
|
|
854
836
|
var defaultGetOutputFile = (filepath, _format) => {
|
|
855
837
|
return filepath;
|
|
856
838
|
};
|
|
857
|
-
|
|
839
|
+
var cachedModuleSyncCondition;
|
|
840
|
+
var moduleSyncConditionPromise;
|
|
841
|
+
async function getModuleSyncConditionEnabled() {
|
|
842
|
+
if (cachedModuleSyncCondition !== void 0) {
|
|
843
|
+
return cachedModuleSyncCondition;
|
|
844
|
+
}
|
|
845
|
+
if (!moduleSyncConditionPromise) {
|
|
846
|
+
moduleSyncConditionPromise = Promise.resolve().then(() => _interopRequireWildcard(require(
|
|
847
|
+
// @ts-ignore
|
|
848
|
+
"#module-sync-enabled"
|
|
849
|
+
))).then((mod) => Boolean(_optionalChain([mod, 'optionalAccess', _38 => _38.default]))).catch(() => false).then((result) => {
|
|
850
|
+
cachedModuleSyncCondition = result;
|
|
851
|
+
return result;
|
|
852
|
+
}).finally(() => {
|
|
853
|
+
moduleSyncConditionPromise = void 0;
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
return moduleSyncConditionPromise;
|
|
857
|
+
}
|
|
858
|
+
function collectReferencedModules(bundle, fileName, allModules, analyzedModules = /* @__PURE__ */ new Set()) {
|
|
858
859
|
if (analyzedModules.has(fileName)) {
|
|
859
860
|
return;
|
|
860
861
|
}
|
|
861
862
|
analyzedModules.add(fileName);
|
|
862
863
|
const chunk = bundle[fileName];
|
|
864
|
+
if (!chunk) {
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
863
867
|
for (const mod of chunk.moduleIds) {
|
|
864
868
|
allModules.add(mod);
|
|
865
869
|
}
|
|
866
|
-
for (const
|
|
867
|
-
analyzedModules
|
|
868
|
-
collectAllModules(bundle, i, allModules, analyzedModules);
|
|
870
|
+
for (const imported of chunk.imports) {
|
|
871
|
+
collectReferencedModules(bundle, imported, allModules, analyzedModules);
|
|
869
872
|
}
|
|
870
|
-
for (const
|
|
871
|
-
analyzedModules
|
|
872
|
-
collectAllModules(bundle, i, allModules, analyzedModules);
|
|
873
|
+
for (const imported of chunk.dynamicImports) {
|
|
874
|
+
collectReferencedModules(bundle, imported, allModules, analyzedModules);
|
|
873
875
|
}
|
|
874
876
|
}
|
|
875
877
|
async function bundleFile(fileName, options) {
|
|
876
878
|
const { isESM } = options;
|
|
877
|
-
const
|
|
878
|
-
// @ts-ignore
|
|
879
|
-
"#module-sync-enabled"
|
|
880
|
-
)))).default;
|
|
879
|
+
const moduleSyncEnabled = await getModuleSyncConditionEnabled();
|
|
881
880
|
const dirnameVarName = "__vite_injected_original_dirname";
|
|
882
881
|
const filenameVarName = "__vite_injected_original_filename";
|
|
883
882
|
const importMetaUrlVarName = "__vite_injected_original_import_meta_url";
|
|
884
|
-
const rolldownInputOptions = _optionalChain([options, 'optionalAccess',
|
|
883
|
+
const rolldownInputOptions = _optionalChain([options, 'optionalAccess', _39 => _39.rolldownOptions, 'optionalAccess', _40 => _40.input]) || {};
|
|
885
884
|
const bundle = await _rolldown.rolldown.call(void 0, {
|
|
886
885
|
...rolldownInputOptions,
|
|
887
886
|
input: fileName,
|
|
@@ -901,98 +900,21 @@ async function bundleFile(fileName, options) {
|
|
|
901
900
|
// disable treeshake to include files that is not sideeffectful to `moduleIds`
|
|
902
901
|
treeshake: false,
|
|
903
902
|
plugins: [
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
conditions: [
|
|
915
|
-
"node",
|
|
916
|
-
...isModuleSyncConditionEnabled ? ["module-sync"] : []
|
|
917
|
-
],
|
|
918
|
-
externalConditions: [],
|
|
919
|
-
external: [],
|
|
920
|
-
noExternal: [],
|
|
921
|
-
dedupe: [],
|
|
922
|
-
extensions: configDefaults.resolve.extensions,
|
|
923
|
-
preserveSymlinks: false,
|
|
924
|
-
packageCache,
|
|
925
|
-
isRequire,
|
|
926
|
-
builtins: nodeLikeBuiltins
|
|
927
|
-
}), 'optionalAccess', _41 => _41.id]);
|
|
928
|
-
};
|
|
929
|
-
return {
|
|
930
|
-
name: "externalize-deps",
|
|
931
|
-
resolveId: {
|
|
932
|
-
filter: { id: /^[^.#].*/ },
|
|
933
|
-
async handler(id, importer, { kind }) {
|
|
934
|
-
if (!importer || _path2.default.isAbsolute(id) || isNodeBuiltin(id)) {
|
|
935
|
-
return;
|
|
936
|
-
}
|
|
937
|
-
if (isNodeLikeBuiltin(id)) {
|
|
938
|
-
return { id, external: true };
|
|
939
|
-
}
|
|
940
|
-
const isImport = isESM || kind === "dynamic-import";
|
|
941
|
-
let idFsPath;
|
|
942
|
-
try {
|
|
943
|
-
idFsPath = resolveByViteResolver(id, importer, !isImport);
|
|
944
|
-
} catch (e) {
|
|
945
|
-
if (!isImport) {
|
|
946
|
-
let canResolveWithImport = false;
|
|
947
|
-
try {
|
|
948
|
-
canResolveWithImport = !!resolveByViteResolver(
|
|
949
|
-
id,
|
|
950
|
-
importer,
|
|
951
|
-
false
|
|
952
|
-
);
|
|
953
|
-
} catch (e9) {
|
|
954
|
-
}
|
|
955
|
-
if (canResolveWithImport) {
|
|
956
|
-
throw new Error(
|
|
957
|
-
`Failed to resolve ${JSON.stringify(
|
|
958
|
-
id
|
|
959
|
-
)}. This package is ESM only but it was tried to load by \`require\`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`
|
|
960
|
-
);
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
throw e;
|
|
964
|
-
}
|
|
965
|
-
if (!idFsPath) {
|
|
966
|
-
return;
|
|
967
|
-
}
|
|
968
|
-
if (idFsPath.endsWith(".json")) {
|
|
969
|
-
return idFsPath;
|
|
970
|
-
}
|
|
971
|
-
if (idFsPath && isImport) {
|
|
972
|
-
idFsPath = _url.pathToFileURL.call(void 0, idFsPath).href;
|
|
973
|
-
}
|
|
974
|
-
return { id: idFsPath, external: true };
|
|
975
|
-
}
|
|
976
|
-
}
|
|
977
|
-
};
|
|
978
|
-
})(),
|
|
979
|
-
{
|
|
980
|
-
name: "inject-file-scope-variables",
|
|
981
|
-
transform: {
|
|
982
|
-
filter: { id: /\.[cm]?[jt]s$/ },
|
|
983
|
-
async handler(code, id) {
|
|
984
|
-
const injectValues = `const ${dirnameVarName} = ${JSON.stringify(_path2.default.dirname(id))};const ${filenameVarName} = ${JSON.stringify(id)};const ${importMetaUrlVarName} = ${JSON.stringify(
|
|
985
|
-
_url.pathToFileURL.call(void 0, id).href
|
|
986
|
-
)};`;
|
|
987
|
-
return { code: injectValues + code, map: null };
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
}
|
|
903
|
+
createExternalizeDepsPlugin({
|
|
904
|
+
entryFile: fileName,
|
|
905
|
+
isESM,
|
|
906
|
+
moduleSyncEnabled
|
|
907
|
+
}),
|
|
908
|
+
createFileScopeVariablesPlugin({
|
|
909
|
+
dirnameVarName,
|
|
910
|
+
filenameVarName,
|
|
911
|
+
importMetaUrlVarName
|
|
912
|
+
})
|
|
991
913
|
],
|
|
992
914
|
external: options.external
|
|
993
915
|
// preserveEntrySignatures: 'exports-only'
|
|
994
916
|
});
|
|
995
|
-
const rolldownOutputOptions = _optionalChain([options, 'optionalAccess',
|
|
917
|
+
const rolldownOutputOptions = _optionalChain([options, 'optionalAccess', _41 => _41.rolldownOptions, 'optionalAccess', _42 => _42.output]) || {};
|
|
996
918
|
const result = await bundle.generate({
|
|
997
919
|
...rolldownOutputOptions,
|
|
998
920
|
format: options.format,
|
|
@@ -1011,14 +933,14 @@ async function bundleFile(fileName, options) {
|
|
|
1011
933
|
result.output.flatMap((c) => c.type === "chunk" ? [[c.fileName, c]] : [])
|
|
1012
934
|
);
|
|
1013
935
|
const allModules = /* @__PURE__ */ new Set();
|
|
1014
|
-
|
|
936
|
+
collectReferencedModules(bundleChunks, entryChunk.fileName, allModules);
|
|
1015
937
|
allModules.delete(fileName);
|
|
1016
938
|
return {
|
|
1017
939
|
code: entryChunk.code,
|
|
1018
940
|
dependencies: [...allModules]
|
|
1019
941
|
};
|
|
1020
942
|
}
|
|
1021
|
-
var
|
|
943
|
+
var _require = _module.createRequire.call(void 0, importMetaUrl);
|
|
1022
944
|
async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
1023
945
|
const { isESM } = options;
|
|
1024
946
|
if (isESM) {
|
|
@@ -1036,11 +958,11 @@ async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
|
1036
958
|
}
|
|
1037
959
|
}
|
|
1038
960
|
}
|
|
1039
|
-
const
|
|
961
|
+
const hash = `timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
1040
962
|
const tempFileName = nodeModulesDir ? _path2.default.resolve(
|
|
1041
963
|
nodeModulesDir,
|
|
1042
|
-
`.vite-temp/${_path2.default.basename(fileName)}.${
|
|
1043
|
-
) : `${fileName}.${
|
|
964
|
+
`.vite-temp/${_path2.default.basename(fileName)}.${hash}.${isESM ? "mjs" : "cjs"}`
|
|
965
|
+
) : `${fileName}.${hash}.mjs`;
|
|
1044
966
|
const getOutputFile = options.getOutputFile || defaultGetOutputFile;
|
|
1045
967
|
const outfile = getOutputFile(tempFileName, options.format);
|
|
1046
968
|
await _promises2.default.writeFile(outfile, bundledCode);
|
|
@@ -1053,7 +975,7 @@ async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
|
1053
975
|
);
|
|
1054
976
|
return mod;
|
|
1055
977
|
} finally {
|
|
1056
|
-
if (!_optionalChain([options, 'optionalAccess',
|
|
978
|
+
if (!_optionalChain([options, 'optionalAccess', _43 => _43.preserveTemporaryFile])) {
|
|
1057
979
|
_fs2.default.unlink(outfile, () => {
|
|
1058
980
|
});
|
|
1059
981
|
}
|
|
@@ -1061,9 +983,9 @@ async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
|
1061
983
|
} else {
|
|
1062
984
|
const extension = _path2.default.extname(fileName);
|
|
1063
985
|
const realFileName = await promisifiedRealpath(fileName);
|
|
1064
|
-
const loaderExt = extension in
|
|
1065
|
-
const defaultLoader =
|
|
1066
|
-
|
|
986
|
+
const loaderExt = extension in _require.extensions ? extension : ".js";
|
|
987
|
+
const defaultLoader = _require.extensions[loaderExt];
|
|
988
|
+
const compileLoader = (module, filename) => {
|
|
1067
989
|
if (filename === realFileName) {
|
|
1068
990
|
;
|
|
1069
991
|
module._compile(bundledCode, filename);
|
|
@@ -1071,29 +993,20 @@ async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
|
1071
993
|
defaultLoader(module, filename);
|
|
1072
994
|
}
|
|
1073
995
|
};
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
996
|
+
try {
|
|
997
|
+
_require.extensions[loaderExt] = compileLoader;
|
|
998
|
+
delete _require.cache[_require.resolve(fileName)];
|
|
999
|
+
const raw = _require(fileName);
|
|
1000
|
+
return raw.__esModule ? raw.default : raw;
|
|
1001
|
+
} finally {
|
|
1002
|
+
_require.extensions[loaderExt] = defaultLoader;
|
|
1003
|
+
}
|
|
1078
1004
|
}
|
|
1079
1005
|
}
|
|
1080
1006
|
async function bundleRequire(options) {
|
|
1081
|
-
const resolvedPath =
|
|
1082
|
-
const isESM =
|
|
1083
|
-
|
|
1084
|
-
options.tsconfig = _nullishCoalesce(_nullishCoalesce(options.tsconfig, () => ( _optionalChain([_gettsconfig.getTsconfig.call(void 0, options.cwd, "tsconfig.json"), 'optionalAccess', _45 => _45.path]))), () => ( void 0));
|
|
1085
|
-
} else {
|
|
1086
|
-
options.tsconfig = void 0;
|
|
1087
|
-
}
|
|
1088
|
-
if (!options.format) {
|
|
1089
|
-
options.format = isESM ? "esm" : "cjs";
|
|
1090
|
-
}
|
|
1091
|
-
const internalOptions = {
|
|
1092
|
-
...options,
|
|
1093
|
-
isESM,
|
|
1094
|
-
format: options.format,
|
|
1095
|
-
tsconfig: options.tsconfig
|
|
1096
|
-
};
|
|
1007
|
+
const resolvedPath = resolveEntryFilepath(options);
|
|
1008
|
+
const isESM = detectModuleType(resolvedPath);
|
|
1009
|
+
const internalOptions = createInternalOptions(options, isESM);
|
|
1097
1010
|
const bundled = await bundleFile(
|
|
1098
1011
|
resolvedPath,
|
|
1099
1012
|
internalOptions
|
|
@@ -1108,6 +1021,137 @@ async function bundleRequire(options) {
|
|
|
1108
1021
|
dependencies: bundled.dependencies
|
|
1109
1022
|
};
|
|
1110
1023
|
}
|
|
1024
|
+
function createExternalizeDepsPlugin({
|
|
1025
|
+
entryFile,
|
|
1026
|
+
isESM,
|
|
1027
|
+
moduleSyncEnabled
|
|
1028
|
+
}) {
|
|
1029
|
+
const packageCache = /* @__PURE__ */ new Map();
|
|
1030
|
+
const resolveByViteResolver = (id, importer, isRequire) => {
|
|
1031
|
+
return _optionalChain([tryNodeResolve, 'call', _44 => _44(id, importer, {
|
|
1032
|
+
root: _path2.default.dirname(entryFile),
|
|
1033
|
+
isBuild: true,
|
|
1034
|
+
isProduction: true,
|
|
1035
|
+
preferRelative: false,
|
|
1036
|
+
tryIndex: true,
|
|
1037
|
+
mainFields: [],
|
|
1038
|
+
conditions: [
|
|
1039
|
+
"node",
|
|
1040
|
+
...moduleSyncEnabled ? ["module-sync"] : []
|
|
1041
|
+
],
|
|
1042
|
+
externalConditions: [],
|
|
1043
|
+
external: [],
|
|
1044
|
+
noExternal: [],
|
|
1045
|
+
dedupe: [],
|
|
1046
|
+
extensions: configDefaults.resolve.extensions,
|
|
1047
|
+
preserveSymlinks: false,
|
|
1048
|
+
packageCache,
|
|
1049
|
+
isRequire,
|
|
1050
|
+
builtins: nodeLikeBuiltins
|
|
1051
|
+
}), 'optionalAccess', _45 => _45.id]);
|
|
1052
|
+
};
|
|
1053
|
+
return {
|
|
1054
|
+
name: "externalize-deps",
|
|
1055
|
+
resolveId: {
|
|
1056
|
+
filter: { id: /^[^.#].*/ },
|
|
1057
|
+
async handler(id, importer, { kind }) {
|
|
1058
|
+
if (!importer || _path2.default.isAbsolute(id) || isNodeBuiltin(id)) {
|
|
1059
|
+
return;
|
|
1060
|
+
}
|
|
1061
|
+
if (isNodeLikeBuiltin(id)) {
|
|
1062
|
+
return { id, external: true };
|
|
1063
|
+
}
|
|
1064
|
+
const isImport = isESM || kind === "dynamic-import";
|
|
1065
|
+
let idFsPath;
|
|
1066
|
+
try {
|
|
1067
|
+
idFsPath = resolveByViteResolver(id, importer, !isImport);
|
|
1068
|
+
} catch (e) {
|
|
1069
|
+
if (!isImport) {
|
|
1070
|
+
let canResolveWithImport = false;
|
|
1071
|
+
try {
|
|
1072
|
+
canResolveWithImport = !!resolveByViteResolver(
|
|
1073
|
+
id,
|
|
1074
|
+
importer,
|
|
1075
|
+
false
|
|
1076
|
+
);
|
|
1077
|
+
} catch (e8) {
|
|
1078
|
+
}
|
|
1079
|
+
if (canResolveWithImport) {
|
|
1080
|
+
throw new Error(
|
|
1081
|
+
`Failed to resolve ${JSON.stringify(
|
|
1082
|
+
id
|
|
1083
|
+
)}. This package is ESM only but it was tried to load by \`require\`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
throw e;
|
|
1088
|
+
}
|
|
1089
|
+
if (!idFsPath) {
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1092
|
+
if (idFsPath.endsWith(".json")) {
|
|
1093
|
+
return idFsPath;
|
|
1094
|
+
}
|
|
1095
|
+
if (idFsPath && isImport) {
|
|
1096
|
+
idFsPath = _url.pathToFileURL.call(void 0, idFsPath).href;
|
|
1097
|
+
}
|
|
1098
|
+
return { id: idFsPath, external: true };
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
function createFileScopeVariablesPlugin({
|
|
1104
|
+
dirnameVarName,
|
|
1105
|
+
filenameVarName,
|
|
1106
|
+
importMetaUrlVarName
|
|
1107
|
+
}) {
|
|
1108
|
+
return {
|
|
1109
|
+
name: "inject-file-scope-variables",
|
|
1110
|
+
transform: {
|
|
1111
|
+
filter: { id: /\.[cm]?[jt]s$/ },
|
|
1112
|
+
async handler(code, id) {
|
|
1113
|
+
const injectValues = `const ${dirnameVarName} = ${JSON.stringify(_path2.default.dirname(id))};const ${filenameVarName} = ${JSON.stringify(id)};const ${importMetaUrlVarName} = ${JSON.stringify(
|
|
1114
|
+
_url.pathToFileURL.call(void 0, id).href
|
|
1115
|
+
)};`;
|
|
1116
|
+
return { code: injectValues + code, map: null };
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
function resolveEntryFilepath(options) {
|
|
1122
|
+
if (_path2.default.isAbsolute(options.filepath)) {
|
|
1123
|
+
return options.filepath;
|
|
1124
|
+
}
|
|
1125
|
+
const cwd = options.cwd ? _path2.default.resolve(options.cwd) : _process2.default.cwd();
|
|
1126
|
+
return _path2.default.resolve(cwd, options.filepath);
|
|
1127
|
+
}
|
|
1128
|
+
function detectModuleType(resolvedPath) {
|
|
1129
|
+
return typeof _process2.default.versions.deno === "string" || isFilePathESM(resolvedPath);
|
|
1130
|
+
}
|
|
1131
|
+
function createInternalOptions(userOptions, isESM) {
|
|
1132
|
+
const {
|
|
1133
|
+
filepath: _filepath,
|
|
1134
|
+
cwd: _cwd,
|
|
1135
|
+
...rest
|
|
1136
|
+
} = userOptions;
|
|
1137
|
+
const tsconfig = resolveTsconfigPath(userOptions);
|
|
1138
|
+
const format = _nullishCoalesce(userOptions.format, () => ( (isESM ? "esm" : "cjs")));
|
|
1139
|
+
return {
|
|
1140
|
+
...rest,
|
|
1141
|
+
isESM,
|
|
1142
|
+
format,
|
|
1143
|
+
tsconfig
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
function resolveTsconfigPath(options) {
|
|
1147
|
+
if (options.tsconfig === false) {
|
|
1148
|
+
return void 0;
|
|
1149
|
+
}
|
|
1150
|
+
if (typeof options.tsconfig === "string") {
|
|
1151
|
+
return options.tsconfig;
|
|
1152
|
+
}
|
|
1153
|
+
return _nullishCoalesce(_optionalChain([_gettsconfig.getTsconfig.call(void 0, options.cwd, "tsconfig.json"), 'optionalAccess', _46 => _46.path]), () => ( void 0));
|
|
1154
|
+
}
|
|
1111
1155
|
|
|
1112
1156
|
|
|
1113
1157
|
|
package/dist/index.mjs
CHANGED
|
@@ -17,12 +17,10 @@ import process3 from "process";
|
|
|
17
17
|
|
|
18
18
|
// src/utils.ts
|
|
19
19
|
import { exec } from "child_process";
|
|
20
|
-
import crypto from "crypto";
|
|
21
20
|
import fs from "fs";
|
|
22
21
|
import { builtinModules, createRequire } from "module";
|
|
23
22
|
import path from "path";
|
|
24
23
|
import process2 from "process";
|
|
25
|
-
import { fileURLToPath, URL as URL2 } from "url";
|
|
26
24
|
import { createFilter as _createFilter } from "@rollup/pluginutils";
|
|
27
25
|
|
|
28
26
|
// src/constants.ts
|
|
@@ -49,12 +47,8 @@ function splitFileAndPostfix(path6) {
|
|
|
49
47
|
const file = cleanUrl(path6);
|
|
50
48
|
return { file, postfix: path6.slice(file.length) };
|
|
51
49
|
}
|
|
52
|
-
var AsyncFunction = async function() {
|
|
53
|
-
}.constructor;
|
|
54
50
|
|
|
55
51
|
// src/utils.ts
|
|
56
|
-
import { VERSION } from "rolldown";
|
|
57
|
-
import { withFilter } from "rolldown/filter";
|
|
58
52
|
var createFilter = _createFilter;
|
|
59
53
|
var NODE_BUILTIN_NAMESPACE = "node:";
|
|
60
54
|
var NPM_BUILTIN_NAMESPACE = "npm:";
|
|
@@ -102,16 +96,6 @@ function isOptimizable(id, optimizeDeps) {
|
|
|
102
96
|
}
|
|
103
97
|
var bareImportRE = /^(?![a-z]:)[\w@](?!.*:\/\/)/i;
|
|
104
98
|
var deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//;
|
|
105
|
-
var _require = createRequire(import.meta.url);
|
|
106
|
-
var _dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
107
|
-
var urlCanParse = URL2.canParse ?? ((path6, base) => {
|
|
108
|
-
try {
|
|
109
|
-
new URL2(path6, base);
|
|
110
|
-
return true;
|
|
111
|
-
} catch {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
99
|
function normalizePath(id) {
|
|
116
100
|
return path.posix.normalize(isWindows ? slash(id) : id);
|
|
117
101
|
}
|
|
@@ -146,7 +130,10 @@ function isFilePathESM(filePath, packageCache) {
|
|
|
146
130
|
}
|
|
147
131
|
}
|
|
148
132
|
}
|
|
149
|
-
var
|
|
133
|
+
var currentSafeRealpathSync = isWindows ? windowsSafeRealPathSync : fs.realpathSync.native;
|
|
134
|
+
function safeRealpathSync(filePath) {
|
|
135
|
+
return currentSafeRealpathSync(filePath);
|
|
136
|
+
}
|
|
150
137
|
var windowsNetworkMap = /* @__PURE__ */ new Map();
|
|
151
138
|
function windowsMappedRealpathSync(path6) {
|
|
152
139
|
const realPath = fs.realpathSync.native(path6);
|
|
@@ -171,14 +158,14 @@ function windowsSafeRealPathSync(path6) {
|
|
|
171
158
|
function optimizeSafeRealPathSync() {
|
|
172
159
|
const nodeVersion = process2.versions.node.split(".").map(Number);
|
|
173
160
|
if (nodeVersion[0] < 18 || nodeVersion[0] === 18 && nodeVersion[1] < 10) {
|
|
174
|
-
|
|
161
|
+
currentSafeRealpathSync = fs.realpathSync;
|
|
175
162
|
return;
|
|
176
163
|
}
|
|
177
164
|
try {
|
|
178
165
|
fs.realpathSync.native(path.resolve("./"));
|
|
179
166
|
} catch (error) {
|
|
180
167
|
if (error.message.includes("EISDIR: illegal operation on a directory")) {
|
|
181
|
-
|
|
168
|
+
currentSafeRealpathSync = fs.realpathSync;
|
|
182
169
|
return;
|
|
183
170
|
}
|
|
184
171
|
}
|
|
@@ -193,14 +180,9 @@ function optimizeSafeRealPathSync() {
|
|
|
193
180
|
windowsNetworkMap.set(m[2], m[1]);
|
|
194
181
|
}
|
|
195
182
|
}
|
|
196
|
-
|
|
197
|
-
safeRealpathSync = fs.realpathSync.native;
|
|
198
|
-
} else {
|
|
199
|
-
safeRealpathSync = windowsMappedRealpathSync;
|
|
200
|
-
}
|
|
183
|
+
currentSafeRealpathSync = windowsNetworkMap.size === 0 ? fs.realpathSync.native : windowsMappedRealpathSync;
|
|
201
184
|
});
|
|
202
185
|
}
|
|
203
|
-
var hash = crypto.hash ?? ((algorithm, data, outputEncoding) => crypto.createHash(algorithm).update(data).digest(outputEncoding));
|
|
204
186
|
function stripBomTag(content) {
|
|
205
187
|
if (content.charCodeAt(0) === 65279) {
|
|
206
188
|
return content.slice(1);
|
|
@@ -843,30 +825,47 @@ var configDefaults = Object.freeze({
|
|
|
843
825
|
var defaultGetOutputFile = (filepath, _format) => {
|
|
844
826
|
return filepath;
|
|
845
827
|
};
|
|
846
|
-
|
|
828
|
+
var cachedModuleSyncCondition;
|
|
829
|
+
var moduleSyncConditionPromise;
|
|
830
|
+
async function getModuleSyncConditionEnabled() {
|
|
831
|
+
if (cachedModuleSyncCondition !== void 0) {
|
|
832
|
+
return cachedModuleSyncCondition;
|
|
833
|
+
}
|
|
834
|
+
if (!moduleSyncConditionPromise) {
|
|
835
|
+
moduleSyncConditionPromise = import(
|
|
836
|
+
// @ts-ignore
|
|
837
|
+
"#module-sync-enabled"
|
|
838
|
+
).then((mod) => Boolean(mod?.default)).catch(() => false).then((result) => {
|
|
839
|
+
cachedModuleSyncCondition = result;
|
|
840
|
+
return result;
|
|
841
|
+
}).finally(() => {
|
|
842
|
+
moduleSyncConditionPromise = void 0;
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
return moduleSyncConditionPromise;
|
|
846
|
+
}
|
|
847
|
+
function collectReferencedModules(bundle, fileName, allModules, analyzedModules = /* @__PURE__ */ new Set()) {
|
|
847
848
|
if (analyzedModules.has(fileName)) {
|
|
848
849
|
return;
|
|
849
850
|
}
|
|
850
851
|
analyzedModules.add(fileName);
|
|
851
852
|
const chunk = bundle[fileName];
|
|
853
|
+
if (!chunk) {
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
852
856
|
for (const mod of chunk.moduleIds) {
|
|
853
857
|
allModules.add(mod);
|
|
854
858
|
}
|
|
855
|
-
for (const
|
|
856
|
-
analyzedModules
|
|
857
|
-
collectAllModules(bundle, i, allModules, analyzedModules);
|
|
859
|
+
for (const imported of chunk.imports) {
|
|
860
|
+
collectReferencedModules(bundle, imported, allModules, analyzedModules);
|
|
858
861
|
}
|
|
859
|
-
for (const
|
|
860
|
-
analyzedModules
|
|
861
|
-
collectAllModules(bundle, i, allModules, analyzedModules);
|
|
862
|
+
for (const imported of chunk.dynamicImports) {
|
|
863
|
+
collectReferencedModules(bundle, imported, allModules, analyzedModules);
|
|
862
864
|
}
|
|
863
865
|
}
|
|
864
866
|
async function bundleFile(fileName, options) {
|
|
865
867
|
const { isESM } = options;
|
|
866
|
-
const
|
|
867
|
-
// @ts-ignore
|
|
868
|
-
"#module-sync-enabled"
|
|
869
|
-
)).default;
|
|
868
|
+
const moduleSyncEnabled = await getModuleSyncConditionEnabled();
|
|
870
869
|
const dirnameVarName = "__vite_injected_original_dirname";
|
|
871
870
|
const filenameVarName = "__vite_injected_original_filename";
|
|
872
871
|
const importMetaUrlVarName = "__vite_injected_original_import_meta_url";
|
|
@@ -890,93 +889,16 @@ async function bundleFile(fileName, options) {
|
|
|
890
889
|
// disable treeshake to include files that is not sideeffectful to `moduleIds`
|
|
891
890
|
treeshake: false,
|
|
892
891
|
plugins: [
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
conditions: [
|
|
904
|
-
"node",
|
|
905
|
-
...isModuleSyncConditionEnabled ? ["module-sync"] : []
|
|
906
|
-
],
|
|
907
|
-
externalConditions: [],
|
|
908
|
-
external: [],
|
|
909
|
-
noExternal: [],
|
|
910
|
-
dedupe: [],
|
|
911
|
-
extensions: configDefaults.resolve.extensions,
|
|
912
|
-
preserveSymlinks: false,
|
|
913
|
-
packageCache,
|
|
914
|
-
isRequire,
|
|
915
|
-
builtins: nodeLikeBuiltins
|
|
916
|
-
})?.id;
|
|
917
|
-
};
|
|
918
|
-
return {
|
|
919
|
-
name: "externalize-deps",
|
|
920
|
-
resolveId: {
|
|
921
|
-
filter: { id: /^[^.#].*/ },
|
|
922
|
-
async handler(id, importer, { kind }) {
|
|
923
|
-
if (!importer || path5.isAbsolute(id) || isNodeBuiltin(id)) {
|
|
924
|
-
return;
|
|
925
|
-
}
|
|
926
|
-
if (isNodeLikeBuiltin(id)) {
|
|
927
|
-
return { id, external: true };
|
|
928
|
-
}
|
|
929
|
-
const isImport = isESM || kind === "dynamic-import";
|
|
930
|
-
let idFsPath;
|
|
931
|
-
try {
|
|
932
|
-
idFsPath = resolveByViteResolver(id, importer, !isImport);
|
|
933
|
-
} catch (e) {
|
|
934
|
-
if (!isImport) {
|
|
935
|
-
let canResolveWithImport = false;
|
|
936
|
-
try {
|
|
937
|
-
canResolveWithImport = !!resolveByViteResolver(
|
|
938
|
-
id,
|
|
939
|
-
importer,
|
|
940
|
-
false
|
|
941
|
-
);
|
|
942
|
-
} catch {
|
|
943
|
-
}
|
|
944
|
-
if (canResolveWithImport) {
|
|
945
|
-
throw new Error(
|
|
946
|
-
`Failed to resolve ${JSON.stringify(
|
|
947
|
-
id
|
|
948
|
-
)}. This package is ESM only but it was tried to load by \`require\`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`
|
|
949
|
-
);
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
throw e;
|
|
953
|
-
}
|
|
954
|
-
if (!idFsPath) {
|
|
955
|
-
return;
|
|
956
|
-
}
|
|
957
|
-
if (idFsPath.endsWith(".json")) {
|
|
958
|
-
return idFsPath;
|
|
959
|
-
}
|
|
960
|
-
if (idFsPath && isImport) {
|
|
961
|
-
idFsPath = pathToFileURL(idFsPath).href;
|
|
962
|
-
}
|
|
963
|
-
return { id: idFsPath, external: true };
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
};
|
|
967
|
-
})(),
|
|
968
|
-
{
|
|
969
|
-
name: "inject-file-scope-variables",
|
|
970
|
-
transform: {
|
|
971
|
-
filter: { id: /\.[cm]?[jt]s$/ },
|
|
972
|
-
async handler(code, id) {
|
|
973
|
-
const injectValues = `const ${dirnameVarName} = ${JSON.stringify(path5.dirname(id))};const ${filenameVarName} = ${JSON.stringify(id)};const ${importMetaUrlVarName} = ${JSON.stringify(
|
|
974
|
-
pathToFileURL(id).href
|
|
975
|
-
)};`;
|
|
976
|
-
return { code: injectValues + code, map: null };
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
}
|
|
892
|
+
createExternalizeDepsPlugin({
|
|
893
|
+
entryFile: fileName,
|
|
894
|
+
isESM,
|
|
895
|
+
moduleSyncEnabled
|
|
896
|
+
}),
|
|
897
|
+
createFileScopeVariablesPlugin({
|
|
898
|
+
dirnameVarName,
|
|
899
|
+
filenameVarName,
|
|
900
|
+
importMetaUrlVarName
|
|
901
|
+
})
|
|
980
902
|
],
|
|
981
903
|
external: options.external
|
|
982
904
|
// preserveEntrySignatures: 'exports-only'
|
|
@@ -1000,14 +922,14 @@ async function bundleFile(fileName, options) {
|
|
|
1000
922
|
result.output.flatMap((c) => c.type === "chunk" ? [[c.fileName, c]] : [])
|
|
1001
923
|
);
|
|
1002
924
|
const allModules = /* @__PURE__ */ new Set();
|
|
1003
|
-
|
|
925
|
+
collectReferencedModules(bundleChunks, entryChunk.fileName, allModules);
|
|
1004
926
|
allModules.delete(fileName);
|
|
1005
927
|
return {
|
|
1006
928
|
code: entryChunk.code,
|
|
1007
929
|
dependencies: [...allModules]
|
|
1008
930
|
};
|
|
1009
931
|
}
|
|
1010
|
-
var
|
|
932
|
+
var _require = createRequire3(import.meta.url);
|
|
1011
933
|
async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
1012
934
|
const { isESM } = options;
|
|
1013
935
|
if (isESM) {
|
|
@@ -1025,11 +947,11 @@ async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
|
1025
947
|
}
|
|
1026
948
|
}
|
|
1027
949
|
}
|
|
1028
|
-
const
|
|
950
|
+
const hash = `timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
1029
951
|
const tempFileName = nodeModulesDir ? path5.resolve(
|
|
1030
952
|
nodeModulesDir,
|
|
1031
|
-
`.vite-temp/${path5.basename(fileName)}.${
|
|
1032
|
-
) : `${fileName}.${
|
|
953
|
+
`.vite-temp/${path5.basename(fileName)}.${hash}.${isESM ? "mjs" : "cjs"}`
|
|
954
|
+
) : `${fileName}.${hash}.mjs`;
|
|
1033
955
|
const getOutputFile = options.getOutputFile || defaultGetOutputFile;
|
|
1034
956
|
const outfile = getOutputFile(tempFileName, options.format);
|
|
1035
957
|
await fsp.writeFile(outfile, bundledCode);
|
|
@@ -1050,9 +972,9 @@ async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
|
1050
972
|
} else {
|
|
1051
973
|
const extension = path5.extname(fileName);
|
|
1052
974
|
const realFileName = await promisifiedRealpath(fileName);
|
|
1053
|
-
const loaderExt = extension in
|
|
1054
|
-
const defaultLoader =
|
|
1055
|
-
|
|
975
|
+
const loaderExt = extension in _require.extensions ? extension : ".js";
|
|
976
|
+
const defaultLoader = _require.extensions[loaderExt];
|
|
977
|
+
const compileLoader = (module, filename) => {
|
|
1056
978
|
if (filename === realFileName) {
|
|
1057
979
|
;
|
|
1058
980
|
module._compile(bundledCode, filename);
|
|
@@ -1060,29 +982,20 @@ async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
|
1060
982
|
defaultLoader(module, filename);
|
|
1061
983
|
}
|
|
1062
984
|
};
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
985
|
+
try {
|
|
986
|
+
_require.extensions[loaderExt] = compileLoader;
|
|
987
|
+
delete _require.cache[_require.resolve(fileName)];
|
|
988
|
+
const raw = _require(fileName);
|
|
989
|
+
return raw.__esModule ? raw.default : raw;
|
|
990
|
+
} finally {
|
|
991
|
+
_require.extensions[loaderExt] = defaultLoader;
|
|
992
|
+
}
|
|
1067
993
|
}
|
|
1068
994
|
}
|
|
1069
995
|
async function bundleRequire(options) {
|
|
1070
|
-
const resolvedPath =
|
|
1071
|
-
const isESM =
|
|
1072
|
-
|
|
1073
|
-
options.tsconfig = options.tsconfig ?? getTsconfig(options.cwd, "tsconfig.json")?.path ?? void 0;
|
|
1074
|
-
} else {
|
|
1075
|
-
options.tsconfig = void 0;
|
|
1076
|
-
}
|
|
1077
|
-
if (!options.format) {
|
|
1078
|
-
options.format = isESM ? "esm" : "cjs";
|
|
1079
|
-
}
|
|
1080
|
-
const internalOptions = {
|
|
1081
|
-
...options,
|
|
1082
|
-
isESM,
|
|
1083
|
-
format: options.format,
|
|
1084
|
-
tsconfig: options.tsconfig
|
|
1085
|
-
};
|
|
996
|
+
const resolvedPath = resolveEntryFilepath(options);
|
|
997
|
+
const isESM = detectModuleType(resolvedPath);
|
|
998
|
+
const internalOptions = createInternalOptions(options, isESM);
|
|
1086
999
|
const bundled = await bundleFile(
|
|
1087
1000
|
resolvedPath,
|
|
1088
1001
|
internalOptions
|
|
@@ -1097,6 +1010,137 @@ async function bundleRequire(options) {
|
|
|
1097
1010
|
dependencies: bundled.dependencies
|
|
1098
1011
|
};
|
|
1099
1012
|
}
|
|
1013
|
+
function createExternalizeDepsPlugin({
|
|
1014
|
+
entryFile,
|
|
1015
|
+
isESM,
|
|
1016
|
+
moduleSyncEnabled
|
|
1017
|
+
}) {
|
|
1018
|
+
const packageCache = /* @__PURE__ */ new Map();
|
|
1019
|
+
const resolveByViteResolver = (id, importer, isRequire) => {
|
|
1020
|
+
return tryNodeResolve(id, importer, {
|
|
1021
|
+
root: path5.dirname(entryFile),
|
|
1022
|
+
isBuild: true,
|
|
1023
|
+
isProduction: true,
|
|
1024
|
+
preferRelative: false,
|
|
1025
|
+
tryIndex: true,
|
|
1026
|
+
mainFields: [],
|
|
1027
|
+
conditions: [
|
|
1028
|
+
"node",
|
|
1029
|
+
...moduleSyncEnabled ? ["module-sync"] : []
|
|
1030
|
+
],
|
|
1031
|
+
externalConditions: [],
|
|
1032
|
+
external: [],
|
|
1033
|
+
noExternal: [],
|
|
1034
|
+
dedupe: [],
|
|
1035
|
+
extensions: configDefaults.resolve.extensions,
|
|
1036
|
+
preserveSymlinks: false,
|
|
1037
|
+
packageCache,
|
|
1038
|
+
isRequire,
|
|
1039
|
+
builtins: nodeLikeBuiltins
|
|
1040
|
+
})?.id;
|
|
1041
|
+
};
|
|
1042
|
+
return {
|
|
1043
|
+
name: "externalize-deps",
|
|
1044
|
+
resolveId: {
|
|
1045
|
+
filter: { id: /^[^.#].*/ },
|
|
1046
|
+
async handler(id, importer, { kind }) {
|
|
1047
|
+
if (!importer || path5.isAbsolute(id) || isNodeBuiltin(id)) {
|
|
1048
|
+
return;
|
|
1049
|
+
}
|
|
1050
|
+
if (isNodeLikeBuiltin(id)) {
|
|
1051
|
+
return { id, external: true };
|
|
1052
|
+
}
|
|
1053
|
+
const isImport = isESM || kind === "dynamic-import";
|
|
1054
|
+
let idFsPath;
|
|
1055
|
+
try {
|
|
1056
|
+
idFsPath = resolveByViteResolver(id, importer, !isImport);
|
|
1057
|
+
} catch (e) {
|
|
1058
|
+
if (!isImport) {
|
|
1059
|
+
let canResolveWithImport = false;
|
|
1060
|
+
try {
|
|
1061
|
+
canResolveWithImport = !!resolveByViteResolver(
|
|
1062
|
+
id,
|
|
1063
|
+
importer,
|
|
1064
|
+
false
|
|
1065
|
+
);
|
|
1066
|
+
} catch {
|
|
1067
|
+
}
|
|
1068
|
+
if (canResolveWithImport) {
|
|
1069
|
+
throw new Error(
|
|
1070
|
+
`Failed to resolve ${JSON.stringify(
|
|
1071
|
+
id
|
|
1072
|
+
)}. This package is ESM only but it was tried to load by \`require\`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`
|
|
1073
|
+
);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
throw e;
|
|
1077
|
+
}
|
|
1078
|
+
if (!idFsPath) {
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
if (idFsPath.endsWith(".json")) {
|
|
1082
|
+
return idFsPath;
|
|
1083
|
+
}
|
|
1084
|
+
if (idFsPath && isImport) {
|
|
1085
|
+
idFsPath = pathToFileURL(idFsPath).href;
|
|
1086
|
+
}
|
|
1087
|
+
return { id: idFsPath, external: true };
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
function createFileScopeVariablesPlugin({
|
|
1093
|
+
dirnameVarName,
|
|
1094
|
+
filenameVarName,
|
|
1095
|
+
importMetaUrlVarName
|
|
1096
|
+
}) {
|
|
1097
|
+
return {
|
|
1098
|
+
name: "inject-file-scope-variables",
|
|
1099
|
+
transform: {
|
|
1100
|
+
filter: { id: /\.[cm]?[jt]s$/ },
|
|
1101
|
+
async handler(code, id) {
|
|
1102
|
+
const injectValues = `const ${dirnameVarName} = ${JSON.stringify(path5.dirname(id))};const ${filenameVarName} = ${JSON.stringify(id)};const ${importMetaUrlVarName} = ${JSON.stringify(
|
|
1103
|
+
pathToFileURL(id).href
|
|
1104
|
+
)};`;
|
|
1105
|
+
return { code: injectValues + code, map: null };
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
function resolveEntryFilepath(options) {
|
|
1111
|
+
if (path5.isAbsolute(options.filepath)) {
|
|
1112
|
+
return options.filepath;
|
|
1113
|
+
}
|
|
1114
|
+
const cwd = options.cwd ? path5.resolve(options.cwd) : process4.cwd();
|
|
1115
|
+
return path5.resolve(cwd, options.filepath);
|
|
1116
|
+
}
|
|
1117
|
+
function detectModuleType(resolvedPath) {
|
|
1118
|
+
return typeof process4.versions.deno === "string" || isFilePathESM(resolvedPath);
|
|
1119
|
+
}
|
|
1120
|
+
function createInternalOptions(userOptions, isESM) {
|
|
1121
|
+
const {
|
|
1122
|
+
filepath: _filepath,
|
|
1123
|
+
cwd: _cwd,
|
|
1124
|
+
...rest
|
|
1125
|
+
} = userOptions;
|
|
1126
|
+
const tsconfig = resolveTsconfigPath(userOptions);
|
|
1127
|
+
const format = userOptions.format ?? (isESM ? "esm" : "cjs");
|
|
1128
|
+
return {
|
|
1129
|
+
...rest,
|
|
1130
|
+
isESM,
|
|
1131
|
+
format,
|
|
1132
|
+
tsconfig
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
function resolveTsconfigPath(options) {
|
|
1136
|
+
if (options.tsconfig === false) {
|
|
1137
|
+
return void 0;
|
|
1138
|
+
}
|
|
1139
|
+
if (typeof options.tsconfig === "string") {
|
|
1140
|
+
return options.tsconfig;
|
|
1141
|
+
}
|
|
1142
|
+
return getTsconfig(options.cwd, "tsconfig.json")?.path ?? void 0;
|
|
1143
|
+
}
|
|
1100
1144
|
export {
|
|
1101
1145
|
bundleFile,
|
|
1102
1146
|
bundleRequire,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rolldown-require",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.4",
|
|
5
5
|
"description": "bundle and require a file using rolldown!",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -48,12 +48,12 @@
|
|
|
48
48
|
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"rolldown": "
|
|
51
|
+
"rolldown": ">=1.0.0-beta.33"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@rollup/pluginutils": "^5.
|
|
55
|
-
"get-tsconfig": "^4.
|
|
56
|
-
"mlly": "^1.
|
|
54
|
+
"@rollup/pluginutils": "^5.3.0",
|
|
55
|
+
"get-tsconfig": "^4.12.0",
|
|
56
|
+
"mlly": "^1.8.0",
|
|
57
57
|
"resolve.exports": "^2.0.3"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|