Skip to content

Commit b4b431d

Browse files
refactor: various Browser related changes (#3785)
* fix: package-lock.json not updated Problem: We recently updated the vscode engine in package.json. 'npm install' was not run so the package-lock.json was not updated. Solution: Ran npm install and got the updated package-lock.json Signed-off-by: nkomonen <[email protected]> * npm: install webpack types package Add npm types to stop errors in webpack.browser.config.js Signed-off-by: Nikolas Komonen <[email protected]> * fix imports Signed-off-by: nkomonen <[email protected]> * refactor: only create instance when it will be used This was done due to the instance being created when running in the browser context and failing since the MetadatService of the DefaultEc2MetadataClient was being indicated as not existing. Signed-off-by: Nikolas Komonen <[email protected]> * refactor: update logger utils to use new filesystem class Signed-off-by: nkomonen <[email protected]> * vscode-nls: Fix localize() not working in browser See the long comment in the code for info. Signed-off-by: Nikolas Komonen <[email protected]> * refactor: move promisify call inside class Problem: When this module is loaded the promisify call gets called and it fails in the browser. We are not explicitly using the functionality at the moment. Solution: Move the promisify call to inside the class where it is used. If we do end up using this in the browser we will need to revisit this. But for now it allows things to run in the browser Signed-off-by: nkomonen <[email protected]> * dependency: remove 'filer' module It is not needed anymore. It just existed for testing purposes initially. Signed-off-by: nkomonen <[email protected]> * browser: polyfill 'fs' with 'memfs' temporarily Execution of the extension fails in browser mode partly due to 'fs' not working in the browser. Solution: polyfill 'fs' with 'memfs' TEMPORARILY during the development phase. Eventually we will need to see what still depends on 'fs' and if it is out of our control we may want to remove our dependency from it. Signed-off-by: nkomonen <[email protected]> * tests: setup initial browser tests - launch config to run the browser tests - temporary test the ensure things run - test the isInBrowser function is properly set - The browser extension is activated - We can test that the isInBrowser function has been properly set Signed-off-by: Nikolas Komonen <[email protected]> * feat: isInBrowser function Function that tells us if we are in the browser. Signed-off-by: Nikolas Komonen <[email protected]> * build: remove vscode-nls copyFiles script logic During the copyFiles script we can indicate for nls.metadata files (that are generated by the now disabled loader in the base webpack config) to be copied to 'dist'. Since the files are not generated anymore we disable the copying of these files from being attempted. Signed-off-by: nkomonen <[email protected]> * logger: don't fs.watch log file when in browser This will skip watching the log file for changes when we are in the browser. We will eventually want to remove this function as a whole and instead throw errors on attempts to interact with the file and it is not where we expect it to be. Signed-off-by: nkomonen <[email protected]> --------- Signed-off-by: nkomonen <[email protected]> Signed-off-by: Nikolas Komonen <[email protected]>
1 parent f45a67e commit b4b431d

21 files changed

+573
-110
lines changed

.vscode/launch.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"request": "launch",
3131
"args": ["--extensionDevelopmentPath=${workspaceFolder}", "--extensionDevelopmentKind=web"],
3232
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
33-
"preLaunchTask": "${defaultBuildTask}"
33+
"preLaunchTask": "npm: buildBrowser"
3434
},
3535
{
3636
"name": "Extension (webpack)",
@@ -78,6 +78,21 @@
7878
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
7979
"preLaunchTask": "${defaultBuildTask}"
8080
},
81+
{
82+
"name": "Extension Tests (browser)",
83+
"type": "extensionHost",
84+
"debugWebWorkerHost": true,
85+
"request": "launch",
86+
"args": [
87+
"--disable-extensions",
88+
"--extensionDevelopmentPath=${workspaceFolder}",
89+
"--extensionDevelopmentKind=web",
90+
"--extensionTestsPath=${workspaceFolder}/dist/src/testBrowser/testRunner",
91+
"${workspaceRoot}/dist/src/testFixtures/workspaceFolder"
92+
],
93+
"outFiles": ["${workspaceFolder}/dist/src/**/*.js"],
94+
"preLaunchTask": "npm: buildBrowser"
95+
},
8196
{
8297
"name": "Integration Tests",
8398
"type": "extensionHost",

.vscode/tasks.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
}
3737
}
3838
},
39+
{
40+
"label": "npm: buildBrowser",
41+
"type": "npm",
42+
"script": "buildBrowser",
43+
"dependsOn": ["watch"],
44+
"detail": "Builds the webpacked browser bundle"
45+
},
3946
{
4047
"type": "npm",
4148
"script": "lint",

package-lock.json

Lines changed: 353 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3554,13 +3554,13 @@
35543554
},
35553555
"scripts": {
35563556
"prepare": "ts-node ./scripts/build/prepare.ts",
3557-
"vscode:prepublish": "npm run clean && npm run buildScripts && webpack --mode production && npm run copyFiles -- --webpacked",
3557+
"vscode:prepublish": "npm run clean && npm run buildScripts && webpack --mode production && npm run copyFiles",
35583558
"clean": "ts-node ./scripts/clean.ts dist",
35593559
"reset": "npm run clean -- node_modules && npm install",
35603560
"copyFiles": "ts-node ./scripts/build/copyFiles.ts",
35613561
"buildScripts": "npm run generateClients && npm run generatePackage && npm run generateNonCodeFiles && npm run copyFiles",
3562-
"buildBrowser": "npm run clean && npm run buildScripts && webpack --config webpack.browser.config.js --mode production && npm run copyFiles -- --webpacked",
3563-
"compile": "npm run clean && npm run buildScripts && webpack --mode development && npm run copyFiles -- --webpacked",
3562+
"buildBrowser": "npm run clean && npm run buildScripts && webpack --config webpack.browser.config.js --mode production && npm run copyFiles",
3563+
"compile": "npm run clean && npm run buildScripts && webpack --mode development && npm run copyFiles",
35643564
"watch": "npm run clean && npm run buildScripts && tsc -watch -p ./",
35653565
"postinstall": "npm run generateTelemetry && npm run generateConfigurationAttributes",
35663566
"testCompile": "npm run buildScripts && tsc -p ./ && npm run instrument",
@@ -3607,6 +3607,7 @@
36073607
"@types/uuid": "^9.0.1",
36083608
"@types/vscode": "^1.65.0",
36093609
"@types/vscode-webview": "^1.57.1",
3610+
"@types/webpack-env": "^1.18.1",
36103611
"@types/xml2js": "^0.4.11",
36113612
"@typescript-eslint/eslint-plugin": "^5.59.0",
36123613
"@typescript-eslint/parser": "^5.59.1",

scripts/build/copyFiles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ async function copy(task: CopyTask): Promise<void> {
8585
;(async () => {
8686
const args = process.argv.slice(2)
8787

88+
// To use this something like: "npm run copyFiles -- --webpacked"
8889
if (args.includes('--webpacked')) {
8990
tasks.push(...webpackedTasks)
9091
}

src/browser/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Browser
22

3+
This folder should only contain code which must run in the Browser.
4+
35
For the AWS VS Code Toolkit extension to work in the browser (eg: [vscode.dev](https://vscode.dev/)), we
46
need to ensure that certain functionality which may have previously only been able to
57
run in a desktop environment can additionally run in a browser environment.

src/browser/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
"description": "Browser specific modules to separate concerns from main package.json",
44
"license": "Apache-2.0",
55
"dependencies": {
6-
"filer": "^1.4.1",
6+
"memfs": "^4.2.1",
77
"os-browserify": "^0.3.0",
88
"path-browserify": "^1.0.1",
99
"process": "^0.11.10",
1010
"stream-browserify": "^3.0.0"
11+
},
12+
"devDependencies": {
13+
"assert": "^2.0.0"
1114
}
1215
}

src/common/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Common
2+
3+
This folder is for code that can be used in any environment (eg: Node.js or Browser).
4+
5+
### Examples
6+
7+
Examples of what should **NOT** be in this folder:
8+
9+
- `fs`: only works with Node.js
10+
- `window`: only exists in the Browser

src/common/browserUtils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
let inBrowser = false
7+
/** Set the value of if we are in the browser. Impacts {@link isInBrowser}. */
8+
export function setInBrowser(value: boolean) {
9+
inBrowser = value
10+
}
11+
/** Return true if we are running in the browser, false otherwise. */
12+
export function isInBrowser() {
13+
return inBrowser
14+
}

src/extensionWeb.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
*/
55

66
import * as vscode from 'vscode'
7-
8-
// The following is required so that the copyFiles script does not fail.
9-
// I'm assuming this generates something when run that the script can use.
10-
import * as nls from 'vscode-nls'
11-
nls.loadMessageBundle()
7+
import { setInBrowser } from './common/browserUtils'
128

139
export async function activate(context: vscode.ExtensionContext) {
10+
setInBrowser(true)
11+
1412
vscode.window.showInformationMessage(
1513
'AWS Toolkit: Browser Mode Under Development. No features are currently provided',
16-
{ modal: true }
14+
{ modal: false }
1715
)
1816
}
1917

0 commit comments

Comments
 (0)