vscode-apollo 2.3.3 → 2.3.5
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/.github/workflows/E2E.yml +6 -1
- package/CHANGELOG.md +12 -0
- package/README.md +87 -36
- package/package.json +1 -1
- package/src/__e2e__/runTests.js +1 -0
- package/src/language-server/config/cache-busting-resolver.js +70 -7
- package/src/language-server/config/cache-busting-resolver.types.ts +20 -2
- package/src/language-server/config/loadTsConfig.ts +14 -5
|
@@ -8,8 +8,11 @@ jobs:
|
|
|
8
8
|
test:
|
|
9
9
|
name: Run E2E tests
|
|
10
10
|
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
version: ["1.90.0", "stable", "insiders"]
|
|
11
14
|
steps:
|
|
12
|
-
- run: sudo apt update && sudo apt install -y
|
|
15
|
+
- run: sudo apt update && sudo apt install -y libasound2t64 libgbm1 libgtk-3-0 libnss3 xvfb expect
|
|
13
16
|
- uses: actions/checkout@v4
|
|
14
17
|
- uses: actions/setup-node@v4
|
|
15
18
|
with:
|
|
@@ -25,3 +28,5 @@ jobs:
|
|
|
25
28
|
expect eof
|
|
26
29
|
EOF
|
|
27
30
|
- run: xvfb-run -a npm run test:extension
|
|
31
|
+
env:
|
|
32
|
+
VSCODE_VERSION: "${{ matrix.version }}"
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 2.3.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#226](https://github.com/apollographql/vscode-graphql/pull/226) [`57c51c81`](https://github.com/apollographql/vscode-graphql/commit/57c51c81ec56d68c2226f0b169ed849fcbdaad55) Thanks [@yesmeck](https://github.com/yesmeck)! - Fixes config files being unable to load in old VSCode versions
|
|
8
|
+
|
|
9
|
+
## 2.3.4
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#224](https://github.com/apollographql/vscode-graphql/pull/224) [`b7d300f7`](https://github.com/apollographql/vscode-graphql/commit/b7d300f7d44138ef656bf5e5359dc284d41b78e2) Thanks [@phryneas](https://github.com/phryneas)! - Fix a situation where config files using `require` would not be imported as CommonJS.
|
|
14
|
+
|
|
3
15
|
## 2.3.3
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
</div>
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Thanks to its strongly typed schema and query language, GraphQL has the potential to create incredible developer experiences. The Apollo platform brings these possibilities to life by enhancing your editor with rich metadata from your graph API.
|
|
13
13
|
|
|
14
14
|

|
|
15
15
|
|
|
@@ -17,8 +17,9 @@ The Apollo GraphQL extension for VS Code brings an all-in-one tooling experience
|
|
|
17
17
|
|
|
18
18
|
- Add [syntax highlighting](#syntax-highlighting) for GraphQL files and gql templates inside JavaScript files
|
|
19
19
|
- Get instant feedback and [intelligent autocomplete](#intelligent-autocomplete) for fields, arguments, types, and variables as you write queries
|
|
20
|
-
- Manage client
|
|
20
|
+
- Manage client-side schema alongside remote schema
|
|
21
21
|
- See [performance information](#performance-insights) inline with your query definitions
|
|
22
|
+
- Extra features to help you with [supergraph editing](#supergraph-editing)
|
|
22
23
|
- Validate field and argument usage in operations
|
|
23
24
|
- [Navigate projects more easily](#navigating-projects) with jump-to and peek-at definitions
|
|
24
25
|
- Manage [client-only](#client-only-schemas) schemas
|
|
@@ -26,24 +27,22 @@ The Apollo GraphQL extension for VS Code brings an all-in-one tooling experience
|
|
|
26
27
|
|
|
27
28
|
<h2 id="getting-started">Getting started</h2>
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
First, create an `apollo.config.json` file at the root of the project.
|
|
30
|
+
The VS Code plugin must be linked to a published or local schema. To do so, create an `apollo.config.json` file at the root of the project.
|
|
32
31
|
Alternatively, you can create a `yaml`, `cjs`, `mjs`, or `ts` file with the same configuration.
|
|
33
32
|
|
|
34
33
|
For the contents of this configuration file, select one of these options:
|
|
35
34
|
|
|
36
|
-
<h3>Configure extension for schemas published to Apollo GraphOS</h3>
|
|
35
|
+
<h3>Configure extension for client development with schemas published to Apollo GraphOS</h3>
|
|
37
36
|
<details>
|
|
38
37
|
<summary>
|
|
39
38
|
<i>Expand for instructions.</i>
|
|
40
39
|
</summary>
|
|
41
40
|
|
|
42
|
-
To get all the benefits of the VS Code experience, it's best to link the schema
|
|
41
|
+
To get all the benefits of the VS Code experience, it's best to link the schema being developed before installing the extension. The best way to do that is by [publishing a schema](https://www.apollographql.com/docs/graphos/delivery/publishing-schemas/) to the [GraphOS schema registry](https://www.apollographql.com/docs/graphos#core-features).
|
|
43
42
|
|
|
44
43
|
After that's done, edit the `apollo.config.json` file to look like this:
|
|
45
44
|
|
|
46
|
-
```
|
|
45
|
+
```jsonc
|
|
47
46
|
{
|
|
48
47
|
"client": {
|
|
49
48
|
"service": "graphos-graph-name"
|
|
@@ -55,31 +54,68 @@ The `service` name is the name of the graph you've created in [GraphOS Studio](h
|
|
|
55
54
|
|
|
56
55
|
See [additional configuration options](#additional-apollo-config-options).
|
|
57
56
|
|
|
58
|
-
To authenticate with GraphOS Studio to pull down your schema, create
|
|
57
|
+
To authenticate with GraphOS Studio to pull down your schema, create an `.env` file in the same directory as the `apollo.config.json` file. The `.env` file should be untracked—that is, don't commit it to Git.
|
|
59
58
|
|
|
60
|
-
Then go to your [User Settings page](https://studio.apollographql.com/user-settings/api-keys?referrer=docs-content) in GraphOS Studio to create a new
|
|
59
|
+
Then, go to your [User Settings page](https://studio.apollographql.com/user-settings/api-keys?referrer=docs-content) in GraphOS Studio to create a new personal API key.
|
|
61
60
|
|
|
62
|
-
> It
|
|
61
|
+
> It's best practice to create a new API key for each team member. API keys should also be named so they're easy to find and revoke if needed.
|
|
63
62
|
|
|
64
|
-
After
|
|
63
|
+
After you've created your API key, add the following line to the `.env` file:
|
|
65
64
|
|
|
66
65
|
```bash showLineNumbers=false
|
|
67
66
|
APOLLO_KEY=<enter copied key here>
|
|
68
67
|
```
|
|
69
68
|
|
|
70
|
-
|
|
69
|
+
Afterward, reload VS Code. The Apollo integration will connect to GraphOS Studio to provide autocomplete, validation, and more.
|
|
71
70
|
|
|
72
71
|
</details>
|
|
73
72
|
|
|
74
|
-
<h3
|
|
73
|
+
<h3>Configure extension for supergraph schema development</h3>
|
|
75
74
|
<details>
|
|
76
75
|
<summary>
|
|
77
76
|
<i>Expand for instructions.</i>
|
|
78
77
|
</summary>
|
|
79
78
|
|
|
80
|
-
|
|
79
|
+
The extension can integrate with the [Rover CLI](https://www.apollographql.com/docs/rover/) to help you design supergraph schemas with additional support for Apollo Federation.
|
|
80
|
+
|
|
81
|
+
Ensure you've [installed](https://www.apollographql.com/docs/rover/getting-started) and [configured](https://www.apollographql.com/docs/rover/configuring) the [latest Rover release](https://github.com/apollographql/rover/releases).
|
|
82
|
+
|
|
83
|
+
Next edit your `apollo.config.json` to look like this:
|
|
84
|
+
|
|
85
|
+
```jsonc
|
|
86
|
+
{
|
|
87
|
+
"rover": {
|
|
88
|
+
// optional, if your rover binary is in PATH it will automatically be detected
|
|
89
|
+
"bin": "/path/to/rover",
|
|
90
|
+
// optional, defaults to `supergraph.yaml` in the folder of the configuration file
|
|
91
|
+
"supergraphConfig": "/path/to/supergraph.yaml",
|
|
92
|
+
// optional, defaults to the Rover default profile
|
|
93
|
+
"profile": ""
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Since all these options are optional, you can specify only the `rover` key to indicate you're using Rover for schema development rather than client development:
|
|
81
99
|
|
|
82
|
-
```
|
|
100
|
+
```jsonc
|
|
101
|
+
{
|
|
102
|
+
"rover": {}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Afterward, reload VS Code. The Apollo extension will start using Rover to help you build your supergraph.
|
|
107
|
+
|
|
108
|
+
</details>
|
|
109
|
+
|
|
110
|
+
<h3 id="local-schemas">Configure extension for client development with introspection from a locally running service</h3>
|
|
111
|
+
<details>
|
|
112
|
+
<summary>
|
|
113
|
+
<i>Expand for instructions.</i>
|
|
114
|
+
</summary>
|
|
115
|
+
|
|
116
|
+
To experiment with designs under active development, you can link the editor to a locally running version of a schema. Link the `apollo.config.json` file to a local service definition like so:
|
|
117
|
+
|
|
118
|
+
```jsonc
|
|
83
119
|
{
|
|
84
120
|
"client": {
|
|
85
121
|
"service": {
|
|
@@ -90,11 +126,11 @@ Sometimes it may make sense to link the editor to a locally running version of a
|
|
|
90
126
|
}
|
|
91
127
|
```
|
|
92
128
|
|
|
93
|
-
Linking to
|
|
129
|
+
Linking to local schemas won't provide all extension features, such as switching graph variants and performance metrics.
|
|
94
130
|
|
|
95
131
|
</details>
|
|
96
132
|
|
|
97
|
-
<h3 id="local-schema-files">Configure extension for local schema files</h3>
|
|
133
|
+
<h3 id="local-schema-files">Configure extension for client development with local schema files</h3>
|
|
98
134
|
<details>
|
|
99
135
|
<summary>
|
|
100
136
|
<i>Expand for instructions.</i>
|
|
@@ -104,7 +140,7 @@ You might not always have a running server to link to, so the extension also sup
|
|
|
104
140
|
This is useful for working on a schema in isolation or for testing out new features.
|
|
105
141
|
To link to a local schema file, add the following to the `apollo.config.json` file:
|
|
106
142
|
|
|
107
|
-
```
|
|
143
|
+
```jsonc
|
|
108
144
|
{
|
|
109
145
|
"client": {
|
|
110
146
|
"service": {
|
|
@@ -117,13 +153,17 @@ To link to a local schema file, add the following to the `apollo.config.json` fi
|
|
|
117
153
|
|
|
118
154
|
</details>
|
|
119
155
|
|
|
120
|
-
<h3 id="client-only-schemas">Adding
|
|
156
|
+
<h3 id="client-only-schemas">Bonus: Adding client-only schemas</h3>
|
|
157
|
+
<details>
|
|
158
|
+
<summary>
|
|
159
|
+
<i>Expand for instructions.</i>
|
|
160
|
+
</summary>
|
|
121
161
|
|
|
122
|
-
One of the best features of the VS Code extension is the automatic merging of remote
|
|
162
|
+
One of the best features of the VS Code extension is the automatic merging of remote and local schemas when using integrated state management with Apollo Client. This happens automatically whenever schema definitions are found within a client project. By default, the VS Code extension will look for all JavaScript, TypeScript, and GraphQL files under `./src` to find both the operations and schema definitions for building a complete schema for the application.
|
|
123
163
|
|
|
124
|
-
Client
|
|
164
|
+
Client-side schema definitions can be spread throughout the client app project and will be merged to create one single schema. You can set the default behavior by adding specifications to the `apollo.config.json`:
|
|
125
165
|
|
|
126
|
-
```
|
|
166
|
+
```jsonc
|
|
127
167
|
{
|
|
128
168
|
"client": {
|
|
129
169
|
// "service": <your service configuration>,
|
|
@@ -135,17 +175,19 @@ Client side schema definitions can be spread throughout the client app project a
|
|
|
135
175
|
}
|
|
136
176
|
```
|
|
137
177
|
|
|
178
|
+
</details>
|
|
179
|
+
|
|
138
180
|
<h3 id="get-the-extension">Get the extension</h3>
|
|
139
181
|
|
|
140
182
|
Once you have a config set up and a schema published, [install the Apollo GraphQL extension](https://marketplace.visualstudio.com/items?itemName=apollographql.vscode-apollo), then try opening a file containing a GraphQL operation.
|
|
141
183
|
|
|
142
|
-
|
|
184
|
+
After opening a file, click the status bar icon to open the output window and see stats about the project associated with that file. This is helpful for confirming that the project is set up properly.
|
|
143
185
|
|
|
144
186
|
<img src="https://raw.githubusercontent.com/apollographql/vscode-graphql/main/images/marketplace/stats.gif" alt="Clicking the status bar icon to open the output pane">
|
|
145
187
|
|
|
146
188
|
<h2 id="features">Features</h2>
|
|
147
189
|
|
|
148
|
-
Apollo for VS Code
|
|
190
|
+
Apollo for VS Code offers a range of useful features for working on GraphQL projects.
|
|
149
191
|
|
|
150
192
|
<h3 id="intelligent-autocomplete">Intelligent autocomplete</h3>
|
|
151
193
|
|
|
@@ -155,24 +197,24 @@ Once configured, VS Code has full knowledge of the schema clients are running op
|
|
|
155
197
|
|
|
156
198
|
<h3 id="errors-and-warnings">Inline errors and warnings</h3>
|
|
157
199
|
|
|
158
|
-
VS Code can use local or published schemas to validate operations before running them. **Syntax errors**, **invalid fields or arguments**, and even **deprecated fields** instantly appear as errors or warnings
|
|
200
|
+
VS Code can use local or published schemas to validate operations before running them. **Syntax errors**, **invalid fields or arguments**, and even **deprecated fields** instantly appear as errors or warnings in your editor, ensuring your entire team is working with the most up-to-date production schemas.
|
|
159
201
|
|
|
160
202
|
<img src="https://raw.githubusercontent.com/apollographql/vscode-graphql/main/images/marketplace/warnings-and-errors.gif" alt="tooltip showing a field deprecation warning and error">
|
|
161
203
|
|
|
162
204
|
<h3 id="field-type-info">Inline field type information</h3>
|
|
163
205
|
|
|
164
|
-
Because of GraphQL's strongly
|
|
206
|
+
Because of GraphQL's strongly typed schema, VS Code knows not only which fields and arguments are valid, but also what types are expected. Hover over any type in a valid GraphQL operation to see what type that field returns and whether or not it can be null.
|
|
165
207
|
|
|
166
208
|
<img src="https://raw.githubusercontent.com/apollographql/vscode-graphql/main/images/marketplace/type-info.png" style="max-width:800px;" alt="a tooltip showing a Boolean type for a field">
|
|
167
209
|
|
|
168
210
|
<h3 id="performance-insights">Performance insights</h3>
|
|
169
211
|
|
|
170
|
-
GraphQL's flexibility can make it difficult to predict the cost of an operation. Without insight into how expensive an operation is, developers can accidentally write queries that place strain on their graph API's underlying backends. Thanks to the Apollo platform's integration with VS Code and our trace warehouse, teams can avoid these performance issues
|
|
212
|
+
GraphQL's flexibility can make it difficult to predict the cost of an operation. Without insight into how expensive an operation is, developers can accidentally write queries that place strain on their graph API's underlying backends. Thanks to the Apollo platform's integration with VS Code and our trace warehouse, teams can avoid these performance issues by instantly seeing the cost of a query in their editor.
|
|
171
213
|
|
|
172
|
-
The VS Code extension will show inline performance diagnostics when connected to a service with reported metrics in GraphOS Studio. As operations are typed, any fields that take longer than 1 ms to respond will be annotated to the right of the field inline
|
|
214
|
+
The VS Code extension will show inline performance diagnostics when connected to a service with reported metrics in GraphOS Studio. As operations are typed, any fields that take longer than 1 ms to respond will be annotated to the right of the field inline. This shows team members how long the operation will take as more and more fields are added to operations or fragments.
|
|
173
215
|
|
|
174
216
|
<img
|
|
175
|
-
src="https://raw.githubusercontent.com/apollographql/vscode-graphql/
|
|
217
|
+
src="https://raw.githubusercontent.com/apollographql/vscode-graphql/main/images/marketplace/perf-annotation.png"
|
|
176
218
|
width="80%"
|
|
177
219
|
style="margin: 5%"
|
|
178
220
|
alt="Performance annotation next to a field"
|
|
@@ -180,11 +222,20 @@ The VS Code extension will show inline performance diagnostics when connected to
|
|
|
180
222
|
|
|
181
223
|
<h3 id="syntax-highlighting">Syntax highlighting</h3>
|
|
182
224
|
|
|
183
|
-
Apollo's editor extension provides syntax highlighting for all things GraphQL, including schema definitions in `.graphql` files, complex queries in TypeScript, and even client-only schema extensions. Syntax highlighting for GraphQL works out-of-the-box
|
|
225
|
+
Apollo's editor extension provides syntax highlighting for all things GraphQL, including schema definitions in `.graphql` files, complex queries in TypeScript, and even client-only schema extensions. Syntax highlighting for GraphQL works out-of-the-box in GraphQL, JavaScript, TypeScript, Python, Lua, Ruby, Dart, Elixir, and ReasonML files.
|
|
226
|
+
|
|
227
|
+
<h3 id="supergraph-editing">supergraph editing</h3>
|
|
228
|
+
|
|
229
|
+
The extension provides features for supergraph editing, such as support for Federation directives, subgraph-spanning go-to-definition, and reporting composition errors directly to the **Problems** panel.
|
|
230
|
+
|
|
231
|
+
<img
|
|
232
|
+
src="https://raw.githubusercontent.com/apollographql/vscode-graphql/main/images/marketplace/federation-directive-hover.png"
|
|
233
|
+
alt="Hover on Federation directive"
|
|
234
|
+
/>
|
|
184
235
|
|
|
185
236
|
<h3 id="navigating-projects">Navigating projects</h3>
|
|
186
237
|
|
|
187
|
-
Navigating large codebases can be difficult, but the Apollo GraphQL extension makes this easier. Right-clicking on any field in operations or schemas
|
|
238
|
+
Navigating large codebases can be difficult, but the Apollo GraphQL extension makes this easier. Right-clicking on any field in operations or schemas allows you to jump to (or peek at) definitions and find any other references to that field in your project.
|
|
188
239
|
|
|
189
240
|
<img src="https://raw.githubusercontent.com/apollographql/vscode-graphql/main/images/marketplace/jump-to-def.gif" alt="Using jump to definition on a fragment">
|
|
190
241
|
|
|
@@ -194,11 +245,11 @@ Apollo supports publishing multiple versions ([variants](https://www.apollograph
|
|
|
194
245
|
|
|
195
246
|
<h2 id="troubleshooting">Troubleshooting</h2>
|
|
196
247
|
|
|
197
|
-
The most common errors are configuration errors, like a missing `.env` file or incorrect service information in the `apollo.config.
|
|
248
|
+
The most common errors are configuration errors, like a missing `.env` file or incorrect service information in the `apollo.config.json` file. Please see [the Apollo config docs](https://www.apollographql.com/docs/devtools/apollo-config/) for more configuration guidance.
|
|
198
249
|
|
|
199
|
-
|
|
250
|
+
An old version of a published schema may cause other errors. To reload a schema, open the **Command Palette** (`cmd + shift + p` on Mac), search for "Apollo." Choose the **Apollo: Reload Schema** option.
|
|
200
251
|
|
|
201
|
-
Sometimes errors will
|
|
252
|
+
Sometimes, errors will appear as a notification at the bottom of your editor. Other, less critical, messages may be shown in the output pane of the editor. To open the output pane and get diagnostic information about the extension and the current service loaded (if working with a client project), click the Apollo GraphQL icon in the status bar at the bottom.
|
|
202
253
|
|
|
203
254
|
<img src="https://raw.githubusercontent.com/apollographql/vscode-graphql/main/images/marketplace/stats.gif" alt="Clicking the status bar icon to open the output pane">
|
|
204
255
|
|
|
@@ -214,7 +265,7 @@ _Optional_ - custom tagged template literal.
|
|
|
214
265
|
|
|
215
266
|
When using GraphQL with JavaScript or TypeScript projects, it is common to use the `gql` tagged template literal to write out operations. Apollo tools look through your files for the `gql` tag to extract your queries, so if you use a different template literal, you can configure it like so:
|
|
216
267
|
|
|
217
|
-
```
|
|
268
|
+
```jsonc
|
|
218
269
|
{
|
|
219
270
|
"client": {
|
|
220
271
|
"tagName": "graphql",
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "vscode-apollo",
|
|
3
3
|
"displayName": "Apollo GraphQL",
|
|
4
4
|
"description": "Rich editor support for GraphQL client and server development that seamlessly integrates with the Apollo platform",
|
|
5
|
-
"version": "2.3.
|
|
5
|
+
"version": "2.3.5",
|
|
6
6
|
"referenceID": "87197759-7617-40d0-b32e-46d378e907c7",
|
|
7
7
|
"author": "Apollo GraphQL <opensource@apollographql.com>",
|
|
8
8
|
"license": "MIT",
|
package/src/__e2e__/runTests.js
CHANGED
|
@@ -28,6 +28,7 @@ async function main() {
|
|
|
28
28
|
const exitCode = await runTests({
|
|
29
29
|
extensionDevelopmentPath,
|
|
30
30
|
extensionTestsPath,
|
|
31
|
+
version: process.env.VSCODE_VERSION || "stable",
|
|
31
32
|
launchArgs: [
|
|
32
33
|
"--disable-extensions",
|
|
33
34
|
`${extensionDevelopmentPath}/sampleWorkspace/sampleWorkspace.code-workspace`,
|
|
@@ -1,7 +1,62 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
const { pathToFileURL } = require("node:url");
|
|
3
3
|
|
|
4
|
-
/** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext } from "./cache-busting-resolver.types" */
|
|
4
|
+
/** @import { ResolveContext, ResolutionResult, LoadResult, ImportContext, ImportAttributes, ImportAssertions, LegacyResolveContext, LegacyImportContext, Format } from "./cache-busting-resolver.types" */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* importAssertions was renamed to importAttributes after following versions of Node.js.
|
|
8
|
+
* Once we hit a minimum of v1.92 of VSCode, we can remove the legacy check and
|
|
9
|
+
* use `importAttributes` directly.
|
|
10
|
+
*
|
|
11
|
+
* - v21.0.0
|
|
12
|
+
* - v20.10.0
|
|
13
|
+
* - v18.19.0
|
|
14
|
+
*
|
|
15
|
+
* @see https://github.com/apollographql/vscode-graphql/issues/225
|
|
16
|
+
* @see https://nodejs.org/docs/latest/api/module.html#resolvespecifier-context-nextresolve
|
|
17
|
+
*
|
|
18
|
+
* @param {ResolveContext|ImportContext|LegacyResolveContext|LegacyImportContext} context
|
|
19
|
+
* @returns {context is ResolveContext|ImportContext}
|
|
20
|
+
*/
|
|
21
|
+
function isImportAttributesAvailable(context) {
|
|
22
|
+
return "importAttributes" in context;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {ResolveContext|ImportContext} context
|
|
27
|
+
* @returns {"importAttributes"|"importAssertions"}
|
|
28
|
+
*/
|
|
29
|
+
function resolveImportAttributesKeyName(context) {
|
|
30
|
+
if (isImportAttributesAvailable(context)) {
|
|
31
|
+
return "importAttributes";
|
|
32
|
+
}
|
|
33
|
+
return "importAssertions";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {ResolveContext|ImportContext|LegacyResolveContext|LegacyImportContext} context
|
|
38
|
+
* @returns {ImportAttributes|ImportAssertions}
|
|
39
|
+
*/
|
|
40
|
+
function resolveImportAttributes(context) {
|
|
41
|
+
if (isImportAttributesAvailable(context)) {
|
|
42
|
+
return context.importAttributes;
|
|
43
|
+
}
|
|
44
|
+
return context.importAssertions;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {ImportAttributes|ImportAssertions} importAttributes
|
|
49
|
+
* @returns {Format|null}
|
|
50
|
+
*/
|
|
51
|
+
function resolveConfigFormat(importAttributes) {
|
|
52
|
+
const [as, format] = importAttributes.as
|
|
53
|
+
? importAttributes.as.split(":")
|
|
54
|
+
: [];
|
|
55
|
+
if (as === "cachebust" && format) {
|
|
56
|
+
return /** @type {Format} */ (format);
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
5
60
|
|
|
6
61
|
/**
|
|
7
62
|
* @param {string} specifier
|
|
@@ -21,14 +76,16 @@ function bustFileName(specifier) {
|
|
|
21
76
|
* @returns {Promise<ResolutionResult>}
|
|
22
77
|
*/
|
|
23
78
|
async function resolve(specifier, context, nextResolve) {
|
|
24
|
-
|
|
79
|
+
const importAttributes = resolveImportAttributes(context);
|
|
80
|
+
const format = resolveConfigFormat(importAttributes);
|
|
81
|
+
if (!format) {
|
|
25
82
|
return nextResolve(specifier, context);
|
|
26
83
|
}
|
|
27
84
|
// no need to resolve at all, we have all necessary information
|
|
28
85
|
return {
|
|
29
86
|
url: bustFileName(specifier),
|
|
30
|
-
format
|
|
31
|
-
|
|
87
|
+
format,
|
|
88
|
+
[resolveImportAttributesKeyName(context)]: importAttributes,
|
|
32
89
|
shortCircuit: true,
|
|
33
90
|
};
|
|
34
91
|
}
|
|
@@ -41,13 +98,19 @@ async function resolve(specifier, context, nextResolve) {
|
|
|
41
98
|
* @returns {Promise<LoadResult>}
|
|
42
99
|
*/
|
|
43
100
|
async function load(url, context, nextLoad) {
|
|
44
|
-
|
|
101
|
+
const importAttributes = resolveImportAttributes(context);
|
|
102
|
+
const format = resolveConfigFormat(importAttributes);
|
|
103
|
+
if (!format) {
|
|
45
104
|
return nextLoad(url, context);
|
|
46
105
|
}
|
|
106
|
+
const contents =
|
|
107
|
+
"contents" in importAttributes
|
|
108
|
+
? importAttributes.contents
|
|
109
|
+
: Object.keys(importAttributes).find((key) => key != "as");
|
|
47
110
|
return {
|
|
48
|
-
format
|
|
111
|
+
format,
|
|
49
112
|
shortCircuit: true,
|
|
50
|
-
source:
|
|
113
|
+
source: /** @type {string} */ (contents),
|
|
51
114
|
};
|
|
52
115
|
}
|
|
53
116
|
|
|
@@ -2,13 +2,20 @@ import { pathToFileURL } from "node:url";
|
|
|
2
2
|
|
|
3
3
|
export type ImportAttributes =
|
|
4
4
|
| {
|
|
5
|
-
as:
|
|
5
|
+
as: `cachebust:${Format}`;
|
|
6
6
|
contents: string;
|
|
7
7
|
format: Format;
|
|
8
8
|
}
|
|
9
9
|
| { as?: undefined };
|
|
10
10
|
|
|
11
|
-
type
|
|
11
|
+
export type ImportAssertions =
|
|
12
|
+
| {
|
|
13
|
+
as: `cachebust:${Format}`;
|
|
14
|
+
[key: string]: string;
|
|
15
|
+
}
|
|
16
|
+
| { as?: undefined };
|
|
17
|
+
|
|
18
|
+
export type Format =
|
|
12
19
|
| "builtin"
|
|
13
20
|
| "commonjs"
|
|
14
21
|
| "json"
|
|
@@ -17,12 +24,23 @@ type Format =
|
|
|
17
24
|
| null
|
|
18
25
|
| undefined;
|
|
19
26
|
|
|
27
|
+
export interface LegacyResolveContext {
|
|
28
|
+
conditions: string[];
|
|
29
|
+
importAssertions: ImportAssertions;
|
|
30
|
+
parentURL?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
20
33
|
export interface ResolveContext {
|
|
21
34
|
conditions: string[];
|
|
22
35
|
importAttributes: ImportAttributes;
|
|
23
36
|
parentURL?: string;
|
|
24
37
|
}
|
|
25
38
|
|
|
39
|
+
export interface LegacyImportContext {
|
|
40
|
+
conditions: string[];
|
|
41
|
+
importAssertions: ImportAssertions;
|
|
42
|
+
format: Format;
|
|
43
|
+
}
|
|
26
44
|
export interface ImportContext {
|
|
27
45
|
conditions: string[];
|
|
28
46
|
importAttributes: ImportAttributes;
|
|
@@ -3,16 +3,19 @@ import { dirname, extname } from "node:path";
|
|
|
3
3
|
import typescript from "typescript";
|
|
4
4
|
import { pathToFileURL } from "node:url";
|
|
5
5
|
import { register } from "node:module";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
ImportAssertions,
|
|
8
|
+
ImportAttributes,
|
|
9
|
+
} from "./cache-busting-resolver.types";
|
|
7
10
|
// implementation based on https://github.com/cosmiconfig/cosmiconfig/blob/a5a842547c13392ebb89a485b9e56d9f37e3cbd3/src/loaders.ts
|
|
8
11
|
// Copyright (c) 2015 David Clark licensed MIT. Full license can be found here:
|
|
9
12
|
// https://github.com/cosmiconfig/cosmiconfig/blob/a5a842547c13392ebb89a485b9e56d9f37e3cbd3/LICENSE
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
try {
|
|
12
15
|
register(
|
|
13
16
|
pathToFileURL(require.resolve("./config/cache-busting-resolver.js")),
|
|
14
17
|
);
|
|
15
|
-
}
|
|
18
|
+
} catch {
|
|
16
19
|
register(pathToFileURL(require.resolve("./cache-busting-resolver.js")));
|
|
17
20
|
}
|
|
18
21
|
|
|
@@ -93,7 +96,8 @@ export const loadJs: Loader = async function loadJs(filepath, contents) {
|
|
|
93
96
|
if (
|
|
94
97
|
error instanceof Error &&
|
|
95
98
|
// [ERROR] ReferenceError: module is not defined in ES module scope
|
|
96
|
-
|
|
99
|
+
// [ERROR] ReferenceError: require is not defined in ES module scope
|
|
100
|
+
error.message.includes("is not defined in ES module scope")
|
|
97
101
|
) {
|
|
98
102
|
return loadCachebustedJs(filepath, contents, "commonjs");
|
|
99
103
|
} else {
|
|
@@ -113,10 +117,15 @@ async function loadCachebustedJs(
|
|
|
113
117
|
// @ts-ignore
|
|
114
118
|
{
|
|
115
119
|
with: {
|
|
116
|
-
as:
|
|
120
|
+
as: `cachebust:${type}`,
|
|
117
121
|
contents,
|
|
118
122
|
format: type,
|
|
119
123
|
} satisfies ImportAttributes,
|
|
124
|
+
assert: {
|
|
125
|
+
as: `cachebust:${type}`,
|
|
126
|
+
contents,
|
|
127
|
+
format: type,
|
|
128
|
+
} satisfies ImportAssertions,
|
|
120
129
|
}
|
|
121
130
|
)
|
|
122
131
|
).default;
|