@jsenv/core 41.5.8 → 41.5.10

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.
@@ -8520,30 +8520,46 @@ const jsenvPluginImportMetaCss = () => {
8520
8520
  const relativeUrl = packageName
8521
8521
  ? `${packageName}${relativePathInPackage}`
8522
8522
  : relativePathInPackage;
8523
- const { code } = await applyBabelPlugins({
8524
- babelPlugins: [
8525
- [babelPluginRewriteImportMetaCssAssignment, { relativeUrl }],
8526
- ],
8527
- input: urlInfo.content,
8528
- inputIsJsModule: true,
8529
- inputUrl: urlInfo.originalUrl,
8530
- outputUrl: urlInfo.generatedUrl,
8531
- // the map would be dropped: the content sent back carries none
8532
- options: { sourceMaps: false },
8523
+ // wrap each `import.meta.css = X` right-hand side into
8524
+ // [X, relativeUrl]: two insertions around the existing expression,
8525
+ // so content and sourcemap stay untouched everywhere else
8526
+ const assignmentsToWrap = [];
8527
+ visitJsAst(urlInfo.contentAst, {
8528
+ AssignmentExpression: (node) => {
8529
+ if (node.operator !== "=") {
8530
+ return;
8531
+ }
8532
+ if (getImportMetaPropertyName(node.left) !== "css") {
8533
+ return;
8534
+ }
8535
+ if (node.right.type === "ArrayExpression") {
8536
+ // already in array form (pre-built file)
8537
+ return;
8538
+ }
8539
+ assignmentsToWrap.push(node);
8540
+ },
8533
8541
  });
8534
- if (code === urlInfo.content) {
8535
- // all assignments were already in array form (pre-built file) — nothing to do
8542
+ if (assignmentsToWrap.length === 0) {
8536
8543
  return null;
8537
8544
  }
8545
+ const magicSource = createMagicSource(urlInfo.content);
8546
+ for (const assignment of assignmentsToWrap) {
8547
+ const { right } = assignment;
8548
+ magicSource.insert({ position: right.start, text: "[" });
8549
+ magicSource.insert({
8550
+ position: right.end,
8551
+ text: `, ${JSON.stringify(relativeUrl)}]`,
8552
+ });
8553
+ }
8538
8554
  return injectImportMetaCss(urlInfo, {
8539
- content: code,
8555
+ magicSource,
8540
8556
  importFrom: importMetaCssBuildClientFileUrl,
8541
8557
  importName: "installImportMetaCssBuild",
8542
8558
  importAs: "__installImportMetaCssBuild__",
8543
8559
  });
8544
8560
  }
8545
8561
  return injectImportMetaCss(urlInfo, {
8546
- content: urlInfo.content,
8562
+ magicSource: createMagicSource(urlInfo.content),
8547
8563
  importFrom: importMetaCssDevClientFileUrl,
8548
8564
  importName: "installImportMetaCssDev",
8549
8565
  importAs: "__installImportMetaCssDev__",
@@ -8576,44 +8592,9 @@ const hasModuleScopeAssignment = (ast) => {
8576
8592
  return false;
8577
8593
  };
8578
8594
 
8579
- const babelPluginRewriteImportMetaCssAssignment = (
8580
- { types: t },
8581
- { relativeUrl },
8582
- ) => {
8583
- return {
8584
- name: "rewrite-import-meta-css-assignment",
8585
- visitor: {
8586
- AssignmentExpression(path) {
8587
- const { left, right } = path.node;
8588
- if (left.type !== "MemberExpression") {
8589
- return;
8590
- }
8591
- const { object, property } = left;
8592
- if (object.type !== "MetaProperty") {
8593
- return;
8594
- }
8595
- if (object.meta.name !== "import" || object.property.name !== "meta") {
8596
- return;
8597
- }
8598
- if (property.name !== "css") {
8599
- return;
8600
- }
8601
- // already transformed (e.g. pre-built file): leave as-is
8602
- if (right.type === "ArrayExpression") {
8603
- return;
8604
- }
8605
- path.node.right = t.arrayExpression([
8606
- right,
8607
- t.stringLiteral(relativeUrl),
8608
- ]);
8609
- },
8610
- },
8611
- };
8612
- };
8613
-
8614
8595
  const injectImportMetaCss = (
8615
8596
  urlInfo,
8616
- { content, importFrom, importName, importAs, hot },
8597
+ { magicSource, importFrom, importName, importAs, hot },
8617
8598
  ) => {
8618
8599
  const importMetaCssClientFileReference = urlInfo.dependencies.inject({
8619
8600
  parentUrl: urlInfo.url,
@@ -8648,9 +8629,8 @@ ${importVariableName}(import.meta);
8648
8629
 
8649
8630
  `;
8650
8631
 
8651
- return {
8652
- content: `${prelude.replace(/\n/g, "")}${content}`,
8653
- };
8632
+ magicSource.prepend(prelude);
8633
+ return magicSource.toContentAndSourcemap();
8654
8634
  };
8655
8635
 
8656
8636
  // Some "smart" default applied to decide what should hot reload / fullreload:
@@ -6245,30 +6245,46 @@ const jsenvPluginImportMetaCss = () => {
6245
6245
  const relativeUrl = packageName
6246
6246
  ? `${packageName}${relativePathInPackage}`
6247
6247
  : relativePathInPackage;
6248
- const { code } = await applyBabelPlugins({
6249
- babelPlugins: [
6250
- [babelPluginRewriteImportMetaCssAssignment, { relativeUrl }],
6251
- ],
6252
- input: urlInfo.content,
6253
- inputIsJsModule: true,
6254
- inputUrl: urlInfo.originalUrl,
6255
- outputUrl: urlInfo.generatedUrl,
6256
- // the map would be dropped: the content sent back carries none
6257
- options: { sourceMaps: false },
6248
+ // wrap each `import.meta.css = X` right-hand side into
6249
+ // [X, relativeUrl]: two insertions around the existing expression,
6250
+ // so content and sourcemap stay untouched everywhere else
6251
+ const assignmentsToWrap = [];
6252
+ visitJsAst(urlInfo.contentAst, {
6253
+ AssignmentExpression: (node) => {
6254
+ if (node.operator !== "=") {
6255
+ return;
6256
+ }
6257
+ if (getImportMetaPropertyName(node.left) !== "css") {
6258
+ return;
6259
+ }
6260
+ if (node.right.type === "ArrayExpression") {
6261
+ // already in array form (pre-built file)
6262
+ return;
6263
+ }
6264
+ assignmentsToWrap.push(node);
6265
+ },
6258
6266
  });
6259
- if (code === urlInfo.content) {
6260
- // all assignments were already in array form (pre-built file) — nothing to do
6267
+ if (assignmentsToWrap.length === 0) {
6261
6268
  return null;
6262
6269
  }
6270
+ const magicSource = createMagicSource(urlInfo.content);
6271
+ for (const assignment of assignmentsToWrap) {
6272
+ const { right } = assignment;
6273
+ magicSource.insert({ position: right.start, text: "[" });
6274
+ magicSource.insert({
6275
+ position: right.end,
6276
+ text: `, ${JSON.stringify(relativeUrl)}]`,
6277
+ });
6278
+ }
6263
6279
  return injectImportMetaCss(urlInfo, {
6264
- content: code,
6280
+ magicSource,
6265
6281
  importFrom: importMetaCssBuildClientFileUrl,
6266
6282
  importName: "installImportMetaCssBuild",
6267
6283
  importAs: "__installImportMetaCssBuild__",
6268
6284
  });
6269
6285
  }
6270
6286
  return injectImportMetaCss(urlInfo, {
6271
- content: urlInfo.content,
6287
+ magicSource: createMagicSource(urlInfo.content),
6272
6288
  importFrom: importMetaCssDevClientFileUrl,
6273
6289
  importName: "installImportMetaCssDev",
6274
6290
  importAs: "__installImportMetaCssDev__",
@@ -6301,44 +6317,9 @@ const hasModuleScopeAssignment = (ast) => {
6301
6317
  return false;
6302
6318
  };
6303
6319
 
6304
- const babelPluginRewriteImportMetaCssAssignment = (
6305
- { types: t },
6306
- { relativeUrl },
6307
- ) => {
6308
- return {
6309
- name: "rewrite-import-meta-css-assignment",
6310
- visitor: {
6311
- AssignmentExpression(path) {
6312
- const { left, right } = path.node;
6313
- if (left.type !== "MemberExpression") {
6314
- return;
6315
- }
6316
- const { object, property } = left;
6317
- if (object.type !== "MetaProperty") {
6318
- return;
6319
- }
6320
- if (object.meta.name !== "import" || object.property.name !== "meta") {
6321
- return;
6322
- }
6323
- if (property.name !== "css") {
6324
- return;
6325
- }
6326
- // already transformed (e.g. pre-built file): leave as-is
6327
- if (right.type === "ArrayExpression") {
6328
- return;
6329
- }
6330
- path.node.right = t.arrayExpression([
6331
- right,
6332
- t.stringLiteral(relativeUrl),
6333
- ]);
6334
- },
6335
- },
6336
- };
6337
- };
6338
-
6339
6320
  const injectImportMetaCss = (
6340
6321
  urlInfo,
6341
- { content, importFrom, importName, importAs, hot },
6322
+ { magicSource, importFrom, importName, importAs, hot },
6342
6323
  ) => {
6343
6324
  const importMetaCssClientFileReference = urlInfo.dependencies.inject({
6344
6325
  parentUrl: urlInfo.url,
@@ -6373,9 +6354,8 @@ ${importVariableName}(import.meta);
6373
6354
 
6374
6355
  `;
6375
6356
 
6376
- return {
6377
- content: `${prelude.replace(/\n/g, "")}${content}`,
6378
- };
6357
+ magicSource.prepend(prelude);
6358
+ return magicSource.toContentAndSourcemap();
6379
6359
  };
6380
6360
 
6381
6361
  // Some "smart" default applied to decide what should hot reload / fullreload:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/core",
3
- "version": "41.5.8",
3
+ "version": "41.5.10",
4
4
  "type": "module",
5
5
  "description": "Tool to develop, test and build js projects",
6
6
  "repository": {
@@ -73,14 +73,14 @@
73
73
  "test:snapshot_clear": "npx @jsenv/filesystem clear **/tests/**/side_effects/"
74
74
  },
75
75
  "dependencies": {
76
- "@jsenv/ast": "6.10.1",
77
- "@jsenv/js-module-fallback": "1.6.4",
78
- "@jsenv/plugin-bundling": "2.10.23",
79
- "@jsenv/plugin-minification": "1.7.17",
80
- "@jsenv/plugin-supervisor": "1.8.19",
81
- "@jsenv/plugin-transpilation": "1.7.4",
76
+ "@jsenv/ast": "6.10.2",
77
+ "@jsenv/js-module-fallback": "1.6.6",
78
+ "@jsenv/plugin-bundling": "2.10.25",
79
+ "@jsenv/plugin-minification": "1.7.19",
80
+ "@jsenv/plugin-supervisor": "1.8.21",
81
+ "@jsenv/plugin-transpilation": "1.7.6",
82
82
  "@jsenv/server": "17.6.3",
83
- "@jsenv/sourcemap": "1.4.5",
83
+ "@jsenv/sourcemap": "1.4.7",
84
84
  "react-table": "7.8.0"
85
85
  },
86
86
  "devDependencies": {
@@ -30,10 +30,11 @@
30
30
  */
31
31
 
32
32
  import {
33
- applyBabelPlugins,
34
33
  getImportMetaPropertyName,
34
+ visitJsAst,
35
35
  visitJsAstUntil,
36
36
  } from "@jsenv/ast";
37
+ import { createMagicSource } from "@jsenv/sourcemap";
37
38
 
38
39
  export const jsenvPluginImportMetaCss = () => {
39
40
  const importMetaCssDevClientFileUrl = import.meta
@@ -79,30 +80,46 @@ export const jsenvPluginImportMetaCss = () => {
79
80
  const relativeUrl = packageName
80
81
  ? `${packageName}${relativePathInPackage}`
81
82
  : relativePathInPackage;
82
- const { code } = await applyBabelPlugins({
83
- babelPlugins: [
84
- [babelPluginRewriteImportMetaCssAssignment, { relativeUrl }],
85
- ],
86
- input: urlInfo.content,
87
- inputIsJsModule: true,
88
- inputUrl: urlInfo.originalUrl,
89
- outputUrl: urlInfo.generatedUrl,
90
- // the map would be dropped: the content sent back carries none
91
- options: { sourceMaps: false },
83
+ // wrap each `import.meta.css = X` right-hand side into
84
+ // [X, relativeUrl]: two insertions around the existing expression,
85
+ // so content and sourcemap stay untouched everywhere else
86
+ const assignmentsToWrap = [];
87
+ visitJsAst(urlInfo.contentAst, {
88
+ AssignmentExpression: (node) => {
89
+ if (node.operator !== "=") {
90
+ return;
91
+ }
92
+ if (getImportMetaPropertyName(node.left) !== "css") {
93
+ return;
94
+ }
95
+ if (node.right.type === "ArrayExpression") {
96
+ // already in array form (pre-built file)
97
+ return;
98
+ }
99
+ assignmentsToWrap.push(node);
100
+ },
92
101
  });
93
- if (code === urlInfo.content) {
94
- // all assignments were already in array form (pre-built file) — nothing to do
102
+ if (assignmentsToWrap.length === 0) {
95
103
  return null;
96
104
  }
105
+ const magicSource = createMagicSource(urlInfo.content);
106
+ for (const assignment of assignmentsToWrap) {
107
+ const { right } = assignment;
108
+ magicSource.insert({ position: right.start, text: "[" });
109
+ magicSource.insert({
110
+ position: right.end,
111
+ text: `, ${JSON.stringify(relativeUrl)}]`,
112
+ });
113
+ }
97
114
  return injectImportMetaCss(urlInfo, {
98
- content: code,
115
+ magicSource,
99
116
  importFrom: importMetaCssBuildClientFileUrl,
100
117
  importName: "installImportMetaCssBuild",
101
118
  importAs: "__installImportMetaCssBuild__",
102
119
  });
103
120
  }
104
121
  return injectImportMetaCss(urlInfo, {
105
- content: urlInfo.content,
122
+ magicSource: createMagicSource(urlInfo.content),
106
123
  importFrom: importMetaCssDevClientFileUrl,
107
124
  importName: "installImportMetaCssDev",
108
125
  importAs: "__installImportMetaCssDev__",
@@ -135,44 +152,9 @@ const hasModuleScopeAssignment = (ast) => {
135
152
  return false;
136
153
  };
137
154
 
138
- const babelPluginRewriteImportMetaCssAssignment = (
139
- { types: t },
140
- { relativeUrl },
141
- ) => {
142
- return {
143
- name: "rewrite-import-meta-css-assignment",
144
- visitor: {
145
- AssignmentExpression(path) {
146
- const { left, right } = path.node;
147
- if (left.type !== "MemberExpression") {
148
- return;
149
- }
150
- const { object, property } = left;
151
- if (object.type !== "MetaProperty") {
152
- return;
153
- }
154
- if (object.meta.name !== "import" || object.property.name !== "meta") {
155
- return;
156
- }
157
- if (property.name !== "css") {
158
- return;
159
- }
160
- // already transformed (e.g. pre-built file): leave as-is
161
- if (right.type === "ArrayExpression") {
162
- return;
163
- }
164
- path.node.right = t.arrayExpression([
165
- right,
166
- t.stringLiteral(relativeUrl),
167
- ]);
168
- },
169
- },
170
- };
171
- };
172
-
173
155
  const injectImportMetaCss = (
174
156
  urlInfo,
175
- { content, importFrom, importName, importAs, hot },
157
+ { magicSource, importFrom, importName, importAs, hot },
176
158
  ) => {
177
159
  const importMetaCssClientFileReference = urlInfo.dependencies.inject({
178
160
  parentUrl: urlInfo.url,
@@ -207,7 +189,6 @@ ${importVariableName}(import.meta);
207
189
 
208
190
  `;
209
191
 
210
- return {
211
- content: `${prelude.replace(/\n/g, "")}${content}`,
212
- };
192
+ magicSource.prepend(prelude);
193
+ return magicSource.toContentAndSourcemap();
213
194
  };