@jsenv/plugin-bundling 2.4.1 → 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.1",
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,11 +24,10 @@ export const bundleJsModules = async (
24
24
  signal,
25
25
  logger,
26
26
  rootDirectoryUrl,
27
- assetsDirectory,
28
27
  runtimeCompat,
29
28
  sourcemaps,
30
- minification,
31
29
  isSupportedOnCurrentClients,
30
+ getPluginMeta,
32
31
  } = jsModuleUrlInfos[0].context;
33
32
  const graph = jsModuleUrlInfos[0].graph;
34
33
  if (buildDirectoryUrl === undefined) {
@@ -59,6 +58,7 @@ export const bundleJsModules = async (
59
58
  }
60
59
 
61
60
  const resultRef = { current: null };
61
+ const willMinifyJsModule = Boolean(getPluginMeta("willMinifyJsModule"));
62
62
  try {
63
63
  await applyRollupPlugins({
64
64
  rollup,
@@ -69,7 +69,6 @@ export const bundleJsModules = async (
69
69
  logger,
70
70
  rootDirectoryUrl,
71
71
  buildDirectoryUrl,
72
- assetsDirectory,
73
72
  graph,
74
73
  jsModuleUrlInfos,
75
74
 
@@ -97,8 +96,8 @@ export const bundleJsModules = async (
97
96
  ...rollupInput,
98
97
  },
99
98
  rollupOutput: {
100
- compact: minification,
101
- minifyInternalExports: minification,
99
+ compact: willMinifyJsModule,
100
+ minifyInternalExports: willMinifyJsModule,
102
101
  generatedCode: {
103
102
  arrowFunctions: isSupportedOnCurrentClients("arrow_function"),
104
103
  constBindings: isSupportedOnCurrentClients("const_bindings"),
@@ -127,8 +126,6 @@ export const bundleJsModules = async (
127
126
  const rollupPluginJsenv = ({
128
127
  // logger,
129
128
  rootDirectoryUrl,
130
- buildDirectoryUrl,
131
- assetsDirectory,
132
129
  graph,
133
130
  jsModuleUrlInfos,
134
131
  sourcemaps,
@@ -144,7 +141,7 @@ const rollupPluginJsenv = ({
144
141
  throw new Error("not implemented");
145
142
  };
146
143
  const format = jsModuleUrlInfos.some((jsModuleUrlInfo) =>
147
- jsModuleUrlInfo.filename.endsWith(".cjs"),
144
+ jsModuleUrlInfo.filenameHint.endsWith(".cjs"),
148
145
  )
149
146
  ? "cjs"
150
147
  : "esm";
@@ -164,7 +161,19 @@ const rollupPluginJsenv = ({
164
161
  return URL_META.applyAssociations({ url, associations }).bundle;
165
162
  };
166
163
  }
167
- 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
+ };
168
177
 
169
178
  return {
170
179
  name: "jsenv",
@@ -215,49 +224,90 @@ const rollupPluginJsenv = ({
215
224
  async generateBundle(outputOptions, rollupResult) {
216
225
  _rollupEmitFile = (...args) => this.emitFile(...args);
217
226
 
218
- const jsModuleBundleUrlInfos = {};
219
- Object.keys(rollupResult).forEach((fileName) => {
220
- const rollupFileInfo = rollupResult[fileName];
221
- // there is 3 types of file: "placeholder", "asset", "chunk"
222
- if (rollupFileInfo.type === "chunk") {
223
- const sourceUrls = Object.keys(rollupFileInfo.modules).map((id) =>
224
- fileUrlConverter.asFileUrl(id),
225
- );
227
+ const createBundledFileInfo = (rollupFileInfo) => {
228
+ const originalUrl = getOriginalUrl(rollupFileInfo);
229
+ const sourceUrls = Object.keys(rollupFileInfo.modules).map((id) =>
230
+ fileUrlConverter.asFileUrl(id),
231
+ );
226
232
 
227
- let url;
228
- let originalUrl;
229
- if (rollupFileInfo.facadeModuleId) {
230
- url = fileUrlConverter.asFileUrl(rollupFileInfo.facadeModuleId);
231
- originalUrl = url;
232
- } else {
233
- url = new URL(rollupFileInfo.fileName, buildDirectoryUrl).href;
234
- if (rollupFileInfo.isDynamicEntry) {
235
- originalUrl = sourceUrls[sourceUrls.length - 1];
236
- } else {
237
- 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;
238
242
  }
243
+ const importUrl = getOriginalUrl(importRollupFileInfo);
244
+ const rollupSpecifier = `./${importRollupFileInfo.fileName}`;
245
+ specifierToUrlMap.set(rollupSpecifier, importUrl);
239
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
+ };
240
299
 
241
- const jsModuleBundleUrlInfo = {
242
- url,
243
- originalUrl,
244
- type: format === "esm" ? "js_module" : "common_js",
245
- data: {
246
- bundlerName: "rollup",
247
- bundleRelativeUrl: rollupFileInfo.fileName,
248
- usesImport:
249
- rollupFileInfo.imports.length > 0 ||
250
- rollupFileInfo.dynamicImports.length > 0,
251
- isDynamicEntry: rollupFileInfo.isDynamicEntry,
252
- },
253
- sourceUrls,
254
- contentType: "text/javascript",
255
- content: rollupFileInfo.code,
256
- sourcemap: rollupFileInfo.map,
257
- };
258
- 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;
259
308
  }
260
- });
309
+ }
310
+
261
311
  resultRef.current = {
262
312
  jsModuleBundleUrlInfos,
263
313
  };
@@ -266,38 +316,18 @@ const rollupPluginJsenv = ({
266
316
  // const sourcemapFile = buildDirectoryUrl
267
317
  Object.assign(outputOptions, {
268
318
  format,
269
- dir: fileUrlConverter.asFilePath(buildDirectoryUrl),
319
+ dir: fileUrlConverter.asFilePath(rootDirectoryUrl),
270
320
  sourcemap: sourcemaps === "file" || sourcemaps === "inline",
271
321
  // sourcemapFile,
272
322
  sourcemapPathTransform: (relativePath) => {
273
- return new URL(relativePath, buildDirectoryUrl).href;
323
+ return new URL(relativePath, rootDirectoryUrl).href;
274
324
  },
275
325
  entryFileNames: () => {
276
326
  return `[name].js`;
277
327
  },
278
328
  chunkFileNames: (chunkInfo) => {
279
- const insideJs = willBeInsideJsDirectory({
280
- chunkInfo,
281
- fileUrlConverter,
282
- jsModuleUrlInfos,
283
- });
284
- let nameFromUrlInfo;
285
- if (chunkInfo.facadeModuleId) {
286
- const url = fileUrlConverter.asFileUrl(chunkInfo.facadeModuleId);
287
- const urlInfo = jsModuleUrlInfos.find(
288
- (jsModuleUrlInfo) => jsModuleUrlInfo.url === url,
289
- );
290
- if (urlInfo) {
291
- nameFromUrlInfo = urlInfo.filename;
292
- }
293
- }
294
- const name = nameFromUrlInfo || `${chunkInfo.name}.js`;
295
- return insideJs ? `${assetsDirectory}js/${name}` : `${name}`;
329
+ return `${chunkInfo.name}.js`;
296
330
  },
297
- // https://rollupjs.org/guide/en/#outputpaths
298
- // paths: (id) => {
299
- // return id
300
- // },
301
331
  });
302
332
  },
303
333
  // https://rollupjs.org/guide/en/#resolvedynamicimport
@@ -338,10 +368,6 @@ const rollupPluginJsenv = ({
338
368
  } else {
339
369
  url = new URL(specifier, importer).href;
340
370
  }
341
- const existingImporter = urlImporters[url];
342
- if (!existingImporter) {
343
- urlImporters[url] = importer;
344
- }
345
371
  if (!url.startsWith("file:")) {
346
372
  return { id: url, external: true };
347
373
  }
@@ -396,31 +422,3 @@ const applyRollupPlugins = async ({
396
422
  const rollupOutputArray = await rollupReturnValue.generate(rollupOutput);
397
423
  return rollupOutputArray;
398
424
  };
399
-
400
- const willBeInsideJsDirectory = ({
401
- chunkInfo,
402
- fileUrlConverter,
403
- jsModuleUrlInfos,
404
- }) => {
405
- // if the chunk is generated dynamically by rollup
406
- // for an entry point jsenv will put that file inside js/ directory
407
- // if it's generated dynamically for a file already in js/ directory
408
- // both will be inside the js/ directory
409
- if (!chunkInfo.facadeModuleId) {
410
- // generated by rollup
411
- return true;
412
- }
413
- const url = fileUrlConverter.asFileUrl(chunkInfo.facadeModuleId);
414
- const jsModuleUrlInfo = jsModuleUrlInfos.find(
415
- (jsModuleUrlInfo) => jsModuleUrlInfo.url === url,
416
- );
417
- if (!jsModuleUrlInfo) {
418
- // generated by rollup
419
- return true;
420
- }
421
- if (!jsModuleUrlInfo.isEntryPoint) {
422
- // not an entry point, jsenv will put it inside js/ directory
423
- return true;
424
- }
425
- return false;
426
- };