Visual Studio Code development containers allows you to work with SPFx solutions in containers that Visual Studio will run automatically for you so you don't need to care running docker run
. In this it is also easier to make sure that all members of the project are using the same development environment.
There might be two options of using Visual Studio Code development containers with m365pnp/spfx image:
- Using locally installed Docker, on your desktop/laptop
- Running containers in cloud using GitHub Codespaces (requires a paid GitHub plan)
When you run in a development container in GitHub Codespaces, you do NOT need to install any of the following on your laptop/desktop:
- Node.js
- yeoman
- gulp
- Docker
- git
What you will need locally is only Visual Studio!
For using that option you need to have locally installed the following software:
- Visual Studio Code
- Visual Studio Code extension, Remote - Containers
- Docker Engine
- SPFx solution code, scaffolded in the root directory
This option requires the following:
- Visual Studio Code
- Visual Studio Code extension, Remote - Containers
- Git repository in a GitHub organization with enabled Codespaces
- SPFx solution code, scaffolded in the root directory
-
In the root folder of the source code, create the folder called
.devcontainer
. -
In this folder, create the
devcontainer.json
file with the following contents:
// For more information on how to run this SPFx project in a VS Code Remote Container, please visit https://aka.ms/spfx-devcontainer
{
"name": "SPFx 1.15.0",
"image": "m365pnp/spfx:1.15.0",
// Set *default* container specific settings.json values on container create.
"settings": {},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"editorconfig.editorconfig",
"dbaeumer.vscode-eslint"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [
4321,
35729
],
"portsAttributes": {
"4321": {
"label": "Manifest",
"requireLocalPort": true
},
"35729": {
"label": "LiveReload",
"requireLocalPort": true
}
},
"postCreateCommand": "bash .devcontainer/spfx-startup.sh",
"remoteUser": "node"
}
- In the same folder create the
spfx-startup.sh
file with the following contents:
echo
echo -e "\e[1;94mInstalling Node dependencies\e[0m"
npm install
## commands to create dev certificate and copy it to the root folder of the project
echo
echo -e "\e[1;94mGenerating dev certificate\e[0m"
gulp trust-dev-cert
This is the structure that you should have:
- Open the folder in Visual Studio Code
- Click in the bottom left corner:
- Select
Reopen in Container
:
- Wait until the Node.js modules are installed:
-
Type
gulp serve --nobrowser
in the Visual Studio Code terminal -
Add the SSL certificate as trusted on your system. For example, if you use Windows, run the following PowerShell snippet:
$tcpClient = New-Object -TypeName System.Net.Sockets.TcpClient;
$tcpClient.Connect("localhost", 4321);
$tcpStream = $tcpClient.GetStream();
$callback = { param($sender, $cert, $chain, $errors) return $true };
$sslStream = New-Object -TypeName System.Net.Security.SslStream -ArgumentList @($tcpStream, $true, $callback);
$sslStream.AuthenticateAsClient('');
$certificate = $SslStream.RemoteCertificate;
$x509Certificate = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $certificate
$store = new-object System.Security.Cryptography.X509Certificates.X509Store(
[System.Security.Cryptography.X509Certificates.StoreName]::Root,
"localmachine"
)
$store.open("MaxAllowed");
$store.add($x509Certificate);
$store.close();
- Now you should be able to access the workbench
- Open the project in GitHub site.
- Click
Code
button:
- Click
Create codespace on ...
button:
- Click
Open this codespace in VS Code Desktop
:
- Confirm opening Visual Studio Code:
-
Type
gulp serve --nobrowser
in the Visual Studio Code terminal -
Add the SSL certificate as trusted on your system. For example, if you use Windows, run the following PowerShell snippet:
$tcpClient = New-Object -TypeName System.Net.Sockets.TcpClient;
$tcpClient.Connect("localhost", 4321);
$tcpStream = $tcpClient.GetStream();
$callback = { param($sender, $cert, $chain, $errors) return $true };
$sslStream = New-Object -TypeName System.Net.Security.SslStream -ArgumentList @($tcpStream, $true, $callback);
$sslStream.AuthenticateAsClient('');
$certificate = $SslStream.RemoteCertificate;
$x509Certificate = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $certificate
$store = new-object System.Security.Cryptography.X509Certificates.X509Store(
[System.Security.Cryptography.X509Certificates.StoreName]::Root,
"localmachine"
)
$store.open("MaxAllowed");
$store.add($x509Certificate);
$store.close();
- Now you should be able to access the workbench
Make sure that in Visual Studio Code local ports are allocated correspondingly:
If the local ports are busy with something else before you open the project in the development container, Visual Studio will allocate random ports instead.