Skip to content
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

refactor: use consistent build process across packages #3456

Merged
merged 30 commits into from
May 11, 2023

Conversation

nolanlawson
Copy link
Collaborator

@nolanlawson nolanlawson commented Apr 11, 2023

Details

Partially addresses #3445

Does this pull request introduce a breaking change?

  • ✅ No, it does not introduce a breaking change.

Update: breaking changes have been removed! I've added backwards compat for usages like this:

require('@lwc/synthetic-shadow/dist/synthetic-shadow.js')
require('@lwc/engine-server/dist/engine-server.js')
require('@lwc/compiler/dist/commonjs/transformers/transformer')

I've also added a TODO so that hopefully we can remove this when we're ready to release LWC v3.0.0.

Does this pull request introduce an observable change?

  • ⚠️ Yes, it does include an observable change.

The dist/ files have changed, and some exports have been added, so it may be detectable in certain use cases. But this is not detectable to LWC component authors on core – only to npm package consumers.

Copy link
Collaborator Author

@nolanlawson nolanlawson Apr 11, 2023

Choose a reason for hiding this comment

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

This script is the main magic; everything else is basically just details.

},
"namedInputs": {
"sharedGlobals": ["{workspaceRoot}/scripts/rollup/rollup.config.js"],
"default": ["{projectRoot}/**/*", "sharedGlobals"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This tells NX that when this file changes, it should bust the cache

@@ -29,6 +29,9 @@ export function parse(source: string, config: Config = {}): TemplateParseResult
return parseTemplate(source, state);
}

// Export as a named export as well for easier importing in certain environments (e.g. Jest)
export { compile };

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I needed this to unbreak our Jest tests. Jest is weird about default exports.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if this will break anything else in the DX ecosystem, if they import @lwc/template-compiler directly. I'm fine with this change, but we should keep an eye out for issues that might arise because of this change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm hoping we'll be fine, and that this is just an internal issue with TypeScript+Jest on our source code, but it's impossible to know for sure.

FWIW the current package uses exports.default = compile in the dist CJS file, and the new dist file does the same thing.

"devDependencies": {
"@lwc/engine-core": "2.43.0",
"@lwc/shared": "2.43.0"
},
"lwc": {
"modules": [
{
"name": "wire-service",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Technically I'm breaking import 'wire-service', but we shouldn't support this anyway because of dependency confusion. (Luckily we are not vulnerable because we squatted the package.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also I doubt anybody is using this.

@@ -15,7 +15,7 @@ function createDir(dir) {
}

function getEs6ModuleEntry(pkg) {
const pkgFilePath = require.resolve(`${pkg}/package.json`);
const pkgFilePath = path.resolve(__dirname, '../../../', pkg, './package.json');
Copy link
Collaborator Author

@nolanlawson nolanlawson Apr 11, 2023

Choose a reason for hiding this comment

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

I changed this because originally I was using package exports, which breaks if you try to require an unexposed file. I think it's better to just use path.resolve anyway though.

@nolanlawson nolanlawson marked this pull request as ready for review April 11, 2023 22:59
@nolanlawson nolanlawson marked this pull request as draft April 11, 2023 23:25
@nolanlawson nolanlawson added this to the 3.0.0 milestone Apr 12, 2023
@nolanlawson nolanlawson changed the title fix: use consistent build process across packages fix: use consistent build process across packages (BREAKING CHANGE) Apr 12, 2023
@nolanlawson
Copy link
Collaborator Author

nolanlawson commented Apr 12, 2023

This PR breaks @lwc/jest-preset, looks like we can solve it here.

No more breakages!

@nolanlawson
Copy link
Collaborator Author

nolanlawson commented Apr 12, 2023

Cataloguing some of the errors I'm seeing, so those desperately Googling can wind up on this PR:

Update: no more breakages!

Cannot find module '@lwc/compiler/dist/commonjs/transformers/transformer'
Cannot find module '@lwc/synthetic-shadow/dist/synthetic-shadow.js'
Cannot find module '@lwc/engine-server/dist/engine-server.js'

(Hint: the solution is to update all your deps, and update your require()s/imports to the new location.)

@ekashida
Copy link
Member

A lot of nice changes in here, should we separate out the non-breaking changes from the breaking changes so that the non-breaking ones are not held up here?

@nolanlawson
Copy link
Collaborator Author

I was thinking the same thing, yeah. I can rework this so that there are no breaking changes, and then add TODOs for the breaking changes.

@nolanlawson nolanlawson changed the title fix: use consistent build process across packages (BREAKING CHANGE) refactor: use consistent build process across packages (BREAKING CHANGE) May 9, 2023
@nolanlawson nolanlawson marked this pull request as ready for review May 9, 2023 21:25
@nolanlawson
Copy link
Collaborator Author

154 lines of code removed! I'm happy about this.

Screenshot 2023-05-09 at 2 34 06 PM

@ekashida The breaking changes have been removed – at least, downstream Nucleus tests are passing. Can you take another look?

@nolanlawson nolanlawson removed this from the 3.0.0 milestone May 9, 2023
@@ -11,7 +11,7 @@ yarn add --dev @lwc/template-compiler
## Usage

```js
import compile from '@lwc/template-compiler';
import { compile } from '@lwc/template-compiler';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Named exports are way more compatible across the JS ecosystem than default exports. We should just recommend people to use this, even if we still support the default exports.

Copy link
Contributor

@divmain divmain left a comment

Choose a reason for hiding this comment

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

This is fantastic!

packages/@lwc/compiler/src/transformers/template.ts Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Also cleanup?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The root source file of each package is now named index.js. @lwc/features was inconsistent – its root source file was called flags.ts.

@@ -29,6 +29,9 @@ export function parse(source: string, config: Config = {}): TemplateParseResult
return parseTemplate(source, state);
}

// Export as a named export as well for easier importing in certain environments (e.g. Jest)
export { compile };

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if this will break anything else in the DX ecosystem, if they import @lwc/template-compiler directly. I'm fine with this change, but we should keep an eye out for issues that might arise because of this change.

scripts/rollup/rollup.config.js Outdated Show resolved Hide resolved
@nolanlawson nolanlawson merged commit cfb0424 into master May 11, 2023
@nolanlawson nolanlawson deleted the nolan/package-reform-2 branch May 11, 2023 22:29
@nolanlawson
Copy link
Collaborator Author

Boom! 💥

@ekashida
Copy link
Member

Sorry for the late review. This is awesome, thanks!

"license": "MIT",
"scripts": {
"build": "rollup --config ./scripts/rollup/rollup.config.js",
"postbuild": "node ../../../scripts/tasks/verify-treeshakable.js ./dist/index.js",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Accidentally removed this

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.

3 participants