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

chore: make terser an optional dependency #8049

Merged
merged 25 commits into from
Jun 8, 2022

Conversation

sapphi-red
Copy link
Member

BREAKING CHANGE: terser must be installed for minification with terser

Description

This PR makes terser an optional dependency since it is not a default option now.
This will reduce package size much.

Result of npm publish --dry-run

Before

npm notice === Tarball Details ===
npm notice name:          vite
npm notice version:       2.9.8
npm notice filename:      vite-2.9.8.tgz
npm notice package size:  1.0 MB
npm notice unpacked size: 4.6 MB
npm notice shasum:        a23ece1600b5dc3d3f1d5cc360b019e5c70d8215
npm notice integrity:     sha512-G/0LVsqqnhzBe[...]ch0ft85Fb/87A==
npm notice total files:   39

After

npm notice === Tarball Details ===
npm notice name:          vite
npm notice version:       2.9.8
npm notice filename:      vite-2.9.8.tgz
npm notice package size:  838.7 kB
npm notice unpacked size: 3.6 MB
npm notice shasum:        8872ab7e4da1c508cf458adedbda0967cdaa5763
npm notice integrity:     sha512-7KiUjzUvu05XM[...]21fevZYCZBn+g==
npm notice total files:   38
before after diff
package size 1.0 MB 838.7 kB -161.3 kB (-19.2%)
unpacked size 4.6 MB 3.6 MB -1.0 MB (-21.8%)

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red mentioned this pull request May 6, 2022
9 tasks
@TrySound
Copy link
Contributor

TrySound commented May 8, 2022

Probably not "chore" but breaking?

@sapphi-red
Copy link
Member Author

https://github.com/vitejs/vite/blob/main/.github/commit-convention.md#tldr
There is no ! or breaking: in the convention. Instead, I added BREAKING CHANGE: footer.

@bluwy bluwy added the p2-to-be-discussed Enhancement under consideration (priority) label May 9, 2022
@sapphi-red sapphi-red force-pushed the chore/terser-optional branch 2 times, most recently from 55c4d94 to 3cbfcfc Compare May 9, 2022 09:14
BREAKING CHANGE: `terser` must be installed for minification with terser
@sapphi-red
Copy link
Member Author

I selected 5.4.0 because #8045 (comment).

docs/config/index.md Outdated Show resolved Hide resolved
sapphi-red and others added 4 commits May 21, 2022 17:09
@sapphi-red
Copy link
Member Author

This commit (925fd58) worked. CI is now happy. 🙌
So the reason why node.js crashes might be because of a race condition when dynamic import is called parallelly.
It might happen with static imports too.
The incomprehensible crash when running Vitest might be sharing the same reason.

patak-dev
patak-dev previously approved these changes May 24, 2022
const loadTerserPath = (root: string) => {
if (terserPath) return terserPath
try {
terserPath = requireResolveFromRootWithFallback(root, 'terser')
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Actually the one used in the CSS plugin should be changed to createRequire too

Copy link
Member Author

Choose a reason for hiding this comment

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

Is it safe to remove resolving from root? I now found these related things: ddfcbce, #3988, #2612.

Copy link
Member

Choose a reason for hiding this comment

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

Instead of

const resolved = require.resolve(name, [root, ...fallbackPaths]);
require(resolved)

I think it's safer to use:

const requireFromRoot = createRequire(path.resolve(root, 'index.cjs'))
requireFromRoot(name)

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Wait 🤔 This problem is more complicated than I thought…

To clarify, I'm concerned about the require method used here is not "clean" (would be interfered with by require.cache, etc.).
Both require and require.resolve should be created from the root path so that we can get a fresh instance of terser, sass, etc. The point is that require should be returned from createRequire(some-file-in-the-root-dir) too.

Here comes the complexity:

Copy link
Member

Choose a reason for hiding this comment

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

Anyway, my original suggestion is to make requireResolveFromRootWithFallback requireFromRootWithFallback (that is, put the require part into the function too).

It's a nice-to-have.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm leaving this for now because I tried now but I didn't make it work.

Copy link
Member

Choose a reason for hiding this comment

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

@sodatea do you think we could merge this then? And then work on your request as an enhancement? Or is this still blocking the PR?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah the PR looks good to me. It's not a blocking change.

@sapphi-red
Copy link
Member Author

CI become unhappy now. 😢
It's failing 12 times in a row, several times with "Test serve", most times with "Test build".

Mostly it is failing with this error.

FATAL ERROR: v8::FromJust Maybe value is Nothing.
 1: 00007FF70DEE7A1F v8::internal::CodeObjectRegistry::~CodeObjectRegistry+114207
 2: 00007FF70DE76096 DSA_meth_get_flags+65542
 3: 00007FF70DE76F4D node::OnFatalError+301
 4: 00007FF70E795CD5 v8::V8::FromJustIsNothing+53
 5: 00007FF70DD92B7A v8::internal::MicrotaskQueue::microtasks_policy+22170
 6: 00007FF70DD929ED v8::internal::MicrotaskQueue::microtasks_policy+21773
 7: 00007FF70DD8CD10 v8::internal::OSROptimizedCodeCache::OSROptimizedCodeCache+42848
 8: 00007FF70DF31235 uv_tcp_close_reset+405
 9: 00007FF70DF46A73 uv_run+739
10: 00007FF70DF156C5 node::SpinEventLoop+309
11: 00007FF70DDAE740 v8::internal::wasm::SignatureMap::Freeze+35776
12: 00007FF70DDA9D98 v8::internal::wasm::SignatureMap::Freeze+16920
13: 00007FF70DF36F2D uv_poll_stop+557
14: 00007FF70ED50200 v8::internal::compiler::RepresentationChanger::Uint32OverflowOperatorFor+146416
15: 00007FFCEBEC4ED0 BaseThreadInitThunk+16
16: 00007FFCEC8AE39B RtlUserThreadStart+43
 ELIFECYCLE  Command failed with exit code 134.

This happened once.

Administrator: Windows PowerShell[5500]: c:\ws\src\node_file-inl.h:160: Assertion `finished_' failed.
 1: 00007FF6B2407A1F v8::internal::CodeObjectRegistry::~CodeObjectRegistry+114207
 2: 00007FF6B2396096 DSA_meth_get_flags+65542
 3: 00007FF6B2396451 DSA_meth_get_flags+66497
 4: 00007FF6B237F83E v8::base::CPU::has_sse+33486
 5: 00007FF6B2380D66 v8::base::CPU::has_sse+38902
 6: 00007FF6B246A997 uv_timer_stop+1207
 7: 00007FF6B2466F2B uv_async_send+331
 8: 00007FF6B24666BC uv_loop_init+1292
 9: 00007FF6B246685A uv_run+202
10: 00007FF6B24356C5 node::SpinEventLoop+309
11: 00007FF6B22CE740 v8::internal::wasm::SignatureMap::Freeze+35776
12: 00007FF6B22C9D98 v8::internal::wasm::SignatureMap::Freeze+16920
13: 00007FF6B2456F2D uv_poll_stop+557
14: 00007FF6B3270200 v8::internal::compiler::RepresentationChanger::Uint32OverflowOperatorFor+146416
15: 00007FFBD91B4ED0 BaseThreadInitThunk+16
16: 00007FFBD9E8E39B RtlUserThreadStart+43
 ELIFECYCLE  Command failed with exit code 134.

@sapphi-red
Copy link
Member Author

Rerunning all CI worked. I'm not sure why rerunning only the failed test didn't work 🤔.

haoqunjiang
haoqunjiang previously approved these changes Jun 7, 2022
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Minor docs nitpick, but this looks great

docs/config/build-options.md Show resolved Hide resolved
packages/plugin-legacy/README.md Show resolved Hide resolved
bluwy
bluwy previously approved these changes Jun 8, 2022
@patak-dev
Copy link
Member

@sapphi-red would you check the merge conflicts?

@sapphi-red sapphi-red dismissed stale reviews from bluwy and haoqunjiang via 92113ec June 8, 2022 08:25
@patak-dev patak-dev merged commit 164f528 into vitejs:main Jun 8, 2022
@sapphi-red sapphi-red deleted the chore/terser-optional branch June 8, 2022 14:54
sapphi-red added a commit to sapphi-red/vite that referenced this pull request Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change p2-to-be-discussed Enhancement under consideration (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

6 participants