Skip to content

Add comprehensive webpack plugin tests and update plugin hooks#20

Open
exo-nikita wants to merge 2 commits into
masterfrom
claude/add-webpack-loader-functionality
Open

Add comprehensive webpack plugin tests and update plugin hooks#20
exo-nikita wants to merge 2 commits into
masterfrom
claude/add-webpack-loader-functionality

Conversation

@exo-nikita
Copy link
Copy Markdown
Collaborator

Summary

This PR adds extensive test coverage for the Stasis webpack plugin and modernizes the plugin implementation to use webpack's current hooks API.

Key Changes

Plugin Implementation (src/webpack.js)

  • Updated from deprecated plugin() API to modern hooks.tap() API for webpack 5+ compatibility
  • Changed assert import to use node:assert/strict for stricter equality checks
  • Refactored normalModuleFactory hook to use afterResolve tap instead of callback-based approach
  • Enhanced file tracking to distinguish entry points from imported modules via isEntry flag
  • Improved import recording with parent URL and raw request information
  • Removed debug logging and commented-out code

Test Infrastructure

  • Added tests/webpack.test.js: 304-line comprehensive test suite covering:
    • Lock file operations (add, frozen, replace, ignore modes)
    • Bundle creation and tampering detection
    • Full scope (source files) and node_modules scope tracking
    • Configuration conflict detection
    • Idempotency and state consistency
  • Added tests/webpack-run.helper.js: Test harness that spawns webpack builds with the plugin in isolated processes
  • Created two fixture projects:
    • webpack-full: Tests full scope with local source file tracking
    • webpack-nm: Tests node_modules scope with package dependency tracking

Test Coverage

The test suite validates:

  • Lockfile generation and idempotency
  • Frozen mode enforcement with change detection
  • Bundle creation with brotli compression
  • Tamper detection against both disk and bundled sources
  • Scope-specific behavior (full vs node_modules)
  • Configuration validation and conflict resolution
  • Stale entry cleanup in replace mode

Notable Implementation Details

  • Tests use spawnSync to ensure each test gets a fresh State singleton
  • Environment variables are cleaned between tests to prevent state leakage
  • Fixtures include committed lockfiles to test idempotency and frozen mode
  • Bundle tampering tests verify cryptographic integrity checks work correctly

https://claude.ai/code/session_01QqHtVH44DmBSMTa4B8ysPv

Migrate StasisWebpack to the .hooks API and fill in the previously
TODO import-recording path: each after-resolve event now calls
state.addImport(issuer, rawRequest, resource) for non-entry resolves,
and state.addFile(url, { isEntry: !issuer }) marks the entry on its
first sighting.

Mirror the esbuild test suite: tests/webpack-run.helper.js drives a
webpack build with the StasisWebpack plugin against a fixture cwd,
then writes state. New fixtures webpack-full and webpack-nm cover
lock=add/frozen/replace/ignore in full and node_modules scope,
package.json metadata mismatch, brand-new entries under lock=frozen,
hash mismatch (src and node_modules), bundle=add/replace including
tamper detection, and env-vs-config scope conflict.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a comprehensive node:test suite for the Stasis webpack plugin (lock add/frozen/replace/ignore, bundle add/replace, scope variants, config conflicts, tamper detection) along with two fixture projects, and modernizes src/webpack.js to use the hooks.tap() API with node:assert/strict, recording imports via state.addImport and tagging entries via addFile(..., { isEntry }).

Changes:

  • Rewrite src/webpack.js to use compiler.hooks.normalModuleFactory / nmf.hooks.afterResolve taps, track isEntry, and call state.addImport(parentURL, rawRequest, url).
  • Add tests/webpack.test.js and tests/webpack-run.helper.js running webpack in a child process with a fresh State per test.
  • Add webpack-full and webpack-nm fixtures (with committed stasis.config.json and stasis.lock.json).

Reviewed changes

Copilot reviewed 13 out of 17 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/webpack.js Switch from legacy plugin() callbacks to hooks.tap(), record imports and entry flag
tests/webpack.test.js New 300+ line test suite covering lock modes, bundle, tampering, scopes, conflicts
tests/webpack-run.helper.js Child-process helper that constructs State, runs webpack with the plugin, then writes state
tests/fixtures/webpack-full/* Full-scope fixture: entry, hello, package.json, committed config & lockfile, pnpm-workspace marker
tests/fixtures/webpack-nm/* node_modules-scope fixture: entry/helper, fake-cjs-pkg under node_modules, committed config & lockfile

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Config now reads env vars at construction time and accepts a
plain-object options bag with the same shape as the CLI flags. When
both env and an option set the same key they must match, otherwise
'Plugin options can not override stasis env' is raised. State
forwards options through to Config.

Both StasisWebpack and StasisEsbuild now take a constructor options
argument and support every CLI mode (lock=add|replace|frozen|ignore|none,
bundle=add|replace|load|ignore|none, plus bundleFile/scope/debug).
If State.instance already exists (preload path) the plugin asserts
its config matches the options; otherwise the plugin constructs
State with them.

Tests: parallel option-driven coverage for both plugins via a
STASIS_TEST_PLUGIN_OPTIONS env var the helpers forward to the plugin
constructor. Covers every lock and bundle mode, bundleFile,
env-vs-option conflict, unknown option, and invalid value.
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