Skip to content

feat(dev-server): integrate with preview app command #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 11, 2024

Conversation

rax-it
Copy link
Contributor

@rax-it rax-it commented Jun 4, 2024

What does this PR do?

Add flow to start lwc-dev-server when running sf lightning preview app

@W-15044710@

}

function createLWCServerConfig(source: string, logger: Logger): ServerConfig {
const rootDir = path.resolve(source, 'force-app/main/default');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the config be placed elsewhere? My guess is this path reflects a Salesforce VSCode project, as shown in the attached screenshot. The config is for LWR server and LWC server, right? Then, would the root folder be a more appropriate place for the config file to be created?

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the config file will be at the root folder. The value for rootDir usually indicates to the server as to where the components can be found.

And looking at line 58 I see that process.cwd() is being used as the base path (which I think would be the same as the root directory for an SFDX project)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's resolve-ing to force-app/main/default so the config file is getting attempted to be created there instead of root of the project?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lwc-dev-server supports multiple workspaces and the config is used to figure out where does the components live, sfdx project [workspace=sfcli in this case] follows an opinionated folder structure and isn't required to have lwr.config or either lwc.config. But let me know if my understanding is incorrect

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I'm reading this code is that it is creating a ServerConfig object in <root dir>/force-app/main/default. I assume the object that's created inside this method is eventually going to be replaced by values that are read from lwc.config.json in that folder.

You say in your comment that lwc.config(.json) isn't required in the folder. Yes, the code is resolve-ing it, implying that there exists a json there.

Here's what I'm trying to do for myself, @rax-it . I'm working on this repo too and I'm trying to figure out where the lwc.config.json exists. As for where it should exist, that, I don't know. So when I found your draft PR and read the code I naturally thought <root dir>/force-app/main/default is the folder and trying to confirm with you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfdctaka I think I may have understood the root of your confusion here. There are 2 concepts of root directory, each serving a different purpose.

For an SFDX project, the root directory of the entire SFDX project is the folder where the file sfdx-project.json is located at. lwr.config.json should also be placed at this same location.

Now inside the lwr.config.json there are various configuration flags/values. One of those flags is rootDir and it means a different thing. rootDir tells the dev server where to look for when resolving modules/components.

Suppose you have an SFDX project located at ~/Desktop/MyProject and that this project has an lwc component called helloWorld (which would be located at ~/Desktop/MyProject/force-app/main/default/lwc/helloWorld.

By setting rootDir = ~/Desktop/MyProject/force-app/main/default you are telling the dev server that, when requests come in to serve helloWorld (and any other components/modules that it may be referencing) then the dev server should start looking at the location defined by rootDir to search for the components.

If you have further questions on this, feel free to reach out to me on slack (so we don't pollute the comments/feed of this PR)

@rax-it The assumption that for SFDX projects, the modules/components will always be under force-app/main/default is not true. We would need to look at the content of sfdx-project.json and process packageDirectories where it would tell us where the packages are. I've seen code for processing sfdx-project.json a long while ago and can't remember off the top of my head what it would involve. I'll look around to see if I can find it and will post link(s) here if I do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maliroteh-sf I added sfdx config handling as per what information was available in developer docs.
Let me know if you find more info about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that sounds about right. I also was looking at sfdxPackageDirsPattern which is subsequently used for example by getModulesDirs or by componentEntries

@rax-it rax-it marked this pull request as ready for review June 6, 2024 00:35
@rax-it rax-it requested a review from a team as a code owner June 6, 2024 00:35
@rax-it rax-it requested a review from ravijayaramappa June 6, 2024 00:47
import { LWCServer, LogLevel, ServerConfig, Workspace, startLwcDevServer } from '@lwc/lwc-dev-server';
import { Logger } from '@salesforce/core';

const DEV_SERVER_PORT = 8081;
Copy link
Collaborator

@ravijayaramappa ravijayaramappa Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The port needs to be configurable. This a pretty popular port number and we should anticipate that it might not be available. Can we add a new flag to sf lightning preview app command?

Copy link
Contributor

@maliroteh-sf maliroteh-sf Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ravijayaramappa we may need to run it by Marcelino to discuss this. For example:

  • Should it be required or optional flag
  • If it is optional and omitted what should we default to
  • What if the user uses a --port flag to pass in a port but lwc.config.json also has a port defined in it. Should we update the content of the config file and overwrite with the new flag?
  • etc.

Maybe we should utilize our slack channel to discuss further with him and the devs?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea of just putting it in the lwc.config.json. So that the user can add it once and forget it. Created W-15962937 to track.

Copy link
Collaborator

@ravijayaramappa ravijayaramappa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a few minor suggestions and testcase request

getLevel: () => 10,
} as Logger;

describe('lwc-dev-server', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a test which can use a sample sfdx project and start the lwc dev server and stop it successfully? Like a sanity end-2-end test.

@rax-it rax-it merged commit fa4af8b into main Jun 11, 2024
8 of 9 checks passed
@rax-it rax-it deleted the rave/integrate-dev-server branch June 11, 2024 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants