@jsenv/plugin-bundling 2.4.2 → 2.5.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/plugin-bundling",
3
- "version": "2.4.2",
3
+ "version": "2.5.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,11 +26,11 @@
26
26
  "dependencies": {
27
27
  "@jsenv/log": "3.4.0",
28
28
  "@jsenv/url-meta": "8.1.0",
29
- "@jsenv/urls": "2.1.0",
30
- "@jsenv/sourcemap": "1.1.0",
29
+ "@jsenv/urls": "2.2.0",
30
+ "@jsenv/sourcemap": "1.2.0",
31
31
  "acorn-import-assertions": "1.9.0",
32
32
  "lightningcss": "1.21.5",
33
- "rollup": "3.26.3"
33
+ "rollup": "3.27.0"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@jsenv/core": "*"
@@ -24,7 +24,6 @@ export const bundleJsModules = async (
24
24
  signal,
25
25
  logger,
26
26
  rootDirectoryUrl,
27
- assetsDirectory,
28
27
  runtimeCompat,
29
28
  sourcemaps,
30
29
  isSupportedOnCurrentClients,
@@ -70,7 +69,6 @@ export const bundleJsModules = async (
70
69
  logger,
71
70
  rootDirectoryUrl,
72
71
  buildDirectoryUrl,
73
- assetsDirectory,
74
72
  graph,
75
73
  jsModuleUrlInfos,
76
74
 
@@ -128,8 +126,6 @@ export const bundleJsModules = async (
128
126
  const rollupPluginJsenv = ({
129
127
  // logger,
130
128
  rootDirectoryUrl,
131
- buildDirectoryUrl,
132
- assetsDirectory,
133
129
  graph,
134
130
  jsModuleUrlInfos,
135
131
  sourcemaps,
@@ -145,7 +141,7 @@ const rollupPluginJsenv = ({
145
141
  throw new Error("not implemented");
146
142
  };
147
143
  const format = jsModuleUrlInfos.some((jsModuleUrlInfo) =>
148
- jsModuleUrlInfo.filename.endsWith(".cjs"),
144
+ jsModuleUrlInfo.filenameHint.endsWith(".cjs"),
149
145
  )
150
146
  ? "cjs"
151
147
  : "esm";
@@ -165,7 +161,19 @@ const rollupPluginJsenv = ({
165
161
  return URL_META.applyAssociations({ url, associations }).bundle;
166
162
  };
167
163
  }
168
- const urlImporters = {};
164
+
165
+ const getOriginalUrl = (rollupFileInfo) => {
166
+ const { facadeModuleId } = rollupFileInfo;
167
+ if (facadeModuleId) {
168
+ return fileUrlConverter.asFileUrl(facadeModuleId);
169
+ }
170
+ if (rollupFileInfo.isDynamicEntry) {
171
+ const { moduleIds } = rollupFileInfo;
172
+ const lastModuleId = moduleIds[moduleIds.length - 1];
173
+ return fileUrlConverter.asFileUrl(lastModuleId);
174
+ }
175
+ return new URL(rollupFileInfo.fileName, rootDirectoryUrl).href;
176
+ };
169
177
 
170
178
  return {
171
179
  name: "jsenv",
@@ -216,49 +224,90 @@ const rollupPluginJsenv = ({
216
224
  async generateBundle(outputOptions, rollupResult) {
217
225
  _rollupEmitFile = (...args) => this.emitFile(...args);
218
226
 
219
- const jsModuleBundleUrlInfos = {};
220
- Object.keys(rollupResult).forEach((fileName) => {
221
- const rollupFileInfo = rollupResult[fileName];
222
- // there is 3 types of file: "placeholder", "asset", "chunk"
223
- if (rollupFileInfo.type === "chunk") {
224
- const sourceUrls = Object.keys(rollupFileInfo.modules).map((id) =>
225
- fileUrlConverter.asFileUrl(id),
226
- );
227
+ const createBundledFileInfo = (rollupFileInfo) => {
228
+ const originalUrl = getOriginalUrl(rollupFileInfo);
229
+ const sourceUrls = Object.keys(rollupFileInfo.modules).map((id) =>
230
+ fileUrlConverter.asFileUrl(id),
231
+ );
227
232
 
228
- let url;
229
- let originalUrl;
230
- if (rollupFileInfo.facadeModuleId) {
231
- url = fileUrlConverter.asFileUrl(rollupFileInfo.facadeModuleId);
232
- originalUrl = url;
233
- } else {
234
- url = new URL(rollupFileInfo.fileName, buildDirectoryUrl).href;
235
- if (rollupFileInfo.isDynamicEntry) {
236
- originalUrl = sourceUrls[sourceUrls.length - 1];
237
- } else {
238
- originalUrl = url;
233
+ const specifierToUrlMap = new Map();
234
+ const { imports, dynamicImports } = rollupFileInfo;
235
+ for (const importFileName of imports) {
236
+ if (!importFileName.startsWith("file:")) {
237
+ const importRollupFileInfo = rollupResult[importFileName];
238
+ if (!importRollupFileInfo) {
239
+ // happens for external import, like "ignore:" or anything marked as external
240
+ specifierToUrlMap.set(importFileName, importFileName);
241
+ continue;
239
242
  }
243
+ const importUrl = getOriginalUrl(importRollupFileInfo);
244
+ const rollupSpecifier = `./${importRollupFileInfo.fileName}`;
245
+ specifierToUrlMap.set(rollupSpecifier, importUrl);
240
246
  }
247
+ }
248
+ for (const dynamicImportFileName of dynamicImports) {
249
+ if (!dynamicImportFileName.startsWith("file:")) {
250
+ const dynamicImportRollupFileInfo =
251
+ rollupResult[dynamicImportFileName];
252
+ if (!dynamicImportRollupFileInfo) {
253
+ // happens for external import, like "ignore:" or anything marked as external
254
+ specifierToUrlMap.set(
255
+ dynamicImportFileName,
256
+ dynamicImportFileName,
257
+ );
258
+ continue;
259
+ }
260
+ const dynamicImportUrl = getOriginalUrl(
261
+ dynamicImportRollupFileInfo,
262
+ );
263
+ const rollupSpecifier = `./${dynamicImportRollupFileInfo.fileName}`;
264
+ specifierToUrlMap.set(rollupSpecifier, dynamicImportUrl);
265
+ }
266
+ }
267
+
268
+ return {
269
+ originalUrl,
270
+ type: format === "esm" ? "js_module" : "common_js",
271
+ data: {
272
+ bundlerName: "rollup",
273
+ bundleRelativeUrl: rollupFileInfo.fileName,
274
+ usesImport:
275
+ rollupFileInfo.imports.length > 0 ||
276
+ rollupFileInfo.dynamicImports.length > 0,
277
+ isDynamicEntry: rollupFileInfo.isDynamicEntry,
278
+ },
279
+ sourceUrls,
280
+ contentType: "text/javascript",
281
+ content: rollupFileInfo.code,
282
+ sourcemap: rollupFileInfo.map,
283
+ // rollup is generating things like "./file.js"
284
+ // that must be converted back to urls for jsenv
285
+ remapReference:
286
+ specifierToUrlMap.size > 0
287
+ ? (reference) => {
288
+ // rollup generate specifiers only for static and dynamic imports
289
+ // other references (like new URL()) are ignored
290
+ // there is no need to remap them back
291
+ if (reference.type === "js_import") {
292
+ return specifierToUrlMap.get(reference.specifier);
293
+ }
294
+ return reference.specifier;
295
+ }
296
+ : undefined,
297
+ };
298
+ };
241
299
 
242
- const jsModuleBundleUrlInfo = {
243
- url,
244
- originalUrl,
245
- type: format === "esm" ? "js_module" : "common_js",
246
- data: {
247
- bundlerName: "rollup",
248
- bundleRelativeUrl: rollupFileInfo.fileName,
249
- usesImport:
250
- rollupFileInfo.imports.length > 0 ||
251
- rollupFileInfo.dynamicImports.length > 0,
252
- isDynamicEntry: rollupFileInfo.isDynamicEntry,
253
- },
254
- sourceUrls,
255
- contentType: "text/javascript",
256
- content: rollupFileInfo.code,
257
- sourcemap: rollupFileInfo.map,
258
- };
259
- jsModuleBundleUrlInfos[url] = jsModuleBundleUrlInfo;
300
+ const jsModuleBundleUrlInfos = {};
301
+ const fileNames = Object.keys(rollupResult);
302
+ for (const fileName of fileNames) {
303
+ const rollupFileInfo = rollupResult[fileName];
304
+ // there is 3 types of file: "placeholder", "asset", "chunk"
305
+ if (rollupFileInfo.type === "chunk") {
306
+ const jsModuleInfo = createBundledFileInfo(rollupFileInfo);
307
+ jsModuleBundleUrlInfos[jsModuleInfo.originalUrl] = jsModuleInfo;
260
308
  }
261
- });
309
+ }
310
+
262
311
  resultRef.current = {
263
312
  jsModuleBundleUrlInfos,
264
313
  };
@@ -267,38 +316,18 @@ const rollupPluginJsenv = ({
267
316
  // const sourcemapFile = buildDirectoryUrl
268
317
  Object.assign(outputOptions, {
269
318
  format,
270
- dir: fileUrlConverter.asFilePath(buildDirectoryUrl),
319
+ dir: fileUrlConverter.asFilePath(rootDirectoryUrl),
271
320
  sourcemap: sourcemaps === "file" || sourcemaps === "inline",
272
321
  // sourcemapFile,
273
322
  sourcemapPathTransform: (relativePath) => {
274
- return new URL(relativePath, buildDirectoryUrl).href;
323
+ return new URL(relativePath, rootDirectoryUrl).href;
275
324
  },
276
325
  entryFileNames: () => {
277
326
  return `[name].js`;
278
327
  },
279
328
  chunkFileNames: (chunkInfo) => {
280
- const insideJs = willBeInsideJsDirectory({
281
- chunkInfo,
282
- fileUrlConverter,
283
- jsModuleUrlInfos,
284
- });
285
- let nameFromUrlInfo;
286
- if (chunkInfo.facadeModuleId) {
287
- const url = fileUrlConverter.asFileUrl(chunkInfo.facadeModuleId);
288
- const urlInfo = jsModuleUrlInfos.find(
289
- (jsModuleUrlInfo) => jsModuleUrlInfo.url === url,
290
- );
291
- if (urlInfo) {
292
- nameFromUrlInfo = urlInfo.filename;
293
- }
294
- }
295
- const name = nameFromUrlInfo || `${chunkInfo.name}.js`;
296
- return insideJs ? `${assetsDirectory}js/${name}` : `${name}`;
329
+ return `${chunkInfo.name}.js`;
297
330
  },
298
- // https://rollupjs.org/guide/en/#outputpaths
299
- // paths: (id) => {
300
- // return id
301
- // },
302
331
  });
303
332
  },
304
333
  // https://rollupjs.org/guide/en/#resolvedynamicimport
@@ -339,10 +368,6 @@ const rollupPluginJsenv = ({
339
368
  } else {
340
369
  url = new URL(specifier, importer).href;
341
370
  }
342
- const existingImporter = urlImporters[url];
343
- if (!existingImporter) {
344
- urlImporters[url] = importer;
345
- }
346
371
  if (!url.startsWith("file:")) {
347
372
  return { id: url, external: true };
348
373
  }
@@ -397,31 +422,3 @@ const applyRollupPlugins = async ({
397
422
  const rollupOutputArray = await rollupReturnValue.generate(rollupOutput);
398
423
  return rollupOutputArray;
399
424
  };
400
-
401
- const willBeInsideJsDirectory = ({
402
- chunkInfo,
403
- fileUrlConverter,
404
- jsModuleUrlInfos,
405
- }) => {
406
- // if the chunk is generated dynamically by rollup
407
- // for an entry point jsenv will put that file inside js/ directory
408
- // if it's generated dynamically for a file already in js/ directory
409
- // both will be inside the js/ directory
410
- if (!chunkInfo.facadeModuleId) {
411
- // generated by rollup
412
- return true;
413
- }
414
- const url = fileUrlConverter.asFileUrl(chunkInfo.facadeModuleId);
415
- const jsModuleUrlInfo = jsModuleUrlInfos.find(
416
- (jsModuleUrlInfo) => jsModuleUrlInfo.url === url,
417
- );
418
- if (!jsModuleUrlInfo) {
419
- // generated by rollup
420
- return true;
421
- }
422
- if (!jsModuleUrlInfo.isEntryPoint) {
423
- // not an entry point, jsenv will put it inside js/ directory
424
- return true;
425
- }
426
- return false;
427
- };