@jsenv/plugin-bundling 2.4.2 → 2.5.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.
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.1",
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.1",
30
+ "@jsenv/sourcemap": "1.2.1",
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,96 @@ 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
+ const generatedToShareCode =
269
+ !rollupFileInfo.isEntry &&
270
+ !rollupFileInfo.isDynamicEntry &&
271
+ !rollupFileInfo.isImplicitEntry;
272
+
273
+ return {
274
+ originalUrl,
275
+ type: format === "esm" ? "js_module" : "common_js",
276
+ data: {
277
+ bundlerName: "rollup",
278
+ bundleRelativeUrl: rollupFileInfo.fileName,
279
+ usesImport:
280
+ rollupFileInfo.imports.length > 0 ||
281
+ rollupFileInfo.dynamicImports.length > 0,
282
+ isDynamicEntry: rollupFileInfo.isDynamicEntry,
283
+ generatedToShareCode,
284
+ },
285
+ sourceUrls,
286
+ contentType: "text/javascript",
287
+ content: rollupFileInfo.code,
288
+ sourcemap: rollupFileInfo.map,
289
+ // rollup is generating things like "./file.js"
290
+ // that must be converted back to urls for jsenv
291
+ remapReference:
292
+ specifierToUrlMap.size > 0
293
+ ? (reference) => {
294
+ // rollup generate specifiers only for static and dynamic imports
295
+ // other references (like new URL()) are ignored
296
+ // there is no need to remap them back
297
+ if (reference.type === "js_import") {
298
+ return specifierToUrlMap.get(reference.specifier);
299
+ }
300
+ return reference.specifier;
301
+ }
302
+ : undefined,
303
+ };
304
+ };
241
305
 
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;
306
+ const jsModuleBundleUrlInfos = {};
307
+ const fileNames = Object.keys(rollupResult);
308
+ for (const fileName of fileNames) {
309
+ const rollupFileInfo = rollupResult[fileName];
310
+ // there is 3 types of file: "placeholder", "asset", "chunk"
311
+ if (rollupFileInfo.type === "chunk") {
312
+ const jsModuleInfo = createBundledFileInfo(rollupFileInfo);
313
+ jsModuleBundleUrlInfos[jsModuleInfo.originalUrl] = jsModuleInfo;
260
314
  }
261
- });
315
+ }
316
+
262
317
  resultRef.current = {
263
318
  jsModuleBundleUrlInfos,
264
319
  };
@@ -267,38 +322,18 @@ const rollupPluginJsenv = ({
267
322
  // const sourcemapFile = buildDirectoryUrl
268
323
  Object.assign(outputOptions, {
269
324
  format,
270
- dir: fileUrlConverter.asFilePath(buildDirectoryUrl),
325
+ dir: fileUrlConverter.asFilePath(rootDirectoryUrl),
271
326
  sourcemap: sourcemaps === "file" || sourcemaps === "inline",
272
327
  // sourcemapFile,
273
328
  sourcemapPathTransform: (relativePath) => {
274
- return new URL(relativePath, buildDirectoryUrl).href;
329
+ return new URL(relativePath, rootDirectoryUrl).href;
275
330
  },
276
331
  entryFileNames: () => {
277
332
  return `[name].js`;
278
333
  },
279
334
  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}`;
335
+ return `${chunkInfo.name}.js`;
297
336
  },
298
- // https://rollupjs.org/guide/en/#outputpaths
299
- // paths: (id) => {
300
- // return id
301
- // },
302
337
  });
303
338
  },
304
339
  // https://rollupjs.org/guide/en/#resolvedynamicimport
@@ -339,10 +374,6 @@ const rollupPluginJsenv = ({
339
374
  } else {
340
375
  url = new URL(specifier, importer).href;
341
376
  }
342
- const existingImporter = urlImporters[url];
343
- if (!existingImporter) {
344
- urlImporters[url] = importer;
345
- }
346
377
  if (!url.startsWith("file:")) {
347
378
  return { id: url, external: true };
348
379
  }
@@ -397,31 +428,3 @@ const applyRollupPlugins = async ({
397
428
  const rollupOutputArray = await rollupReturnValue.generate(rollupOutput);
398
429
  return rollupOutputArray;
399
430
  };
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
- };
@@ -1,5 +1,5 @@
1
1
  import { bundleCss } from "./css/css_bundling_lightning_css.js";
2
- import { bundleJsClassic } from "./js_classic/bundle_js_classic.js";
2
+ // import { bundleJsClassic } from "./js_classic/bundle_js_classic.js";
3
3
  import { bundleJsModules } from "./js_module/bundle_js_modules.js";
4
4
 
5
5
  export const jsenvPluginBundling = ({
@@ -15,9 +15,9 @@ export const jsenvPluginBundling = ({
15
15
  };
16
16
  }
17
17
  if (js_classic) {
18
- bundle.js_classic = (jsClassicUrlInfos) => {
19
- return bundleJsClassic(jsClassicUrlInfos);
20
- };
18
+ // bundle.js_classic = (jsClassicUrlInfos) => {
19
+ // return bundleJsClassic(jsClassicUrlInfos, js_classic);
20
+ // };
21
21
  }
22
22
  if (js_module) {
23
23
  if (js_module === true) {