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

feat: enforce module scope plugin #12

Merged
merged 4 commits into from
Jan 29, 2022
Merged

Conversation

Aghassi
Copy link
Owner

@Aghassi Aghassi commented Jan 23, 2022

  • feat: add ModuleScopePlugin
  • chore: expose the exec path to webpack plugin
  • refactor: add src/ folder to execpath
  • refactor: ensure the plugin is in the resolve part of webpack config

I opened a discussion over here regarding customer error handling facebook/create-react-app#11977 and created a draft PR for it here facebook/create-react-app#11978

Example of successful build today

bazel-module-federation on  main [$!] via ⬢ v16.13.0 on 🐳 v20.10.11 took 5s
➜ bazel build //src/client/host
INFO: Analyzed target //src/client/host:host (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
INFO: From Action src/client/host/host_build:
assets by chunk 1.04 MiB (id hint: vendors)
  asset app.vendors-node_modules_react-dom_index_js.js 905 KiB [emitted] (id hint: vendors) 1 related asset
  asset app.vendors-node_modules_react_index_js.js 73.7 KiB [emitted] (id hint: vendors) 1 related asset
  asset app.vendors-node_modules_react-router_index_js.js 36.4 KiB [emitted] (id hint: vendors) 1 related asset
  asset app.vendors-node_modules_react-router-dom_index_js.js 26 KiB [emitted] (id hint: vendors) 1 related asset
  asset app.vendors-node_modules_regenerator-runtime_runtime_js.js 24.7 KiB [emitted] (id hint: vendors) 1 related asset
asset app.main.js 29 KiB [emitted] (name: main) 1 related asset
asset app.bazel-out_darwin_arm64-fastbuild_bin_src_client_host_bootstrap_js.js 18.2 KiB [emitted] 1 related asset
asset app.node_modules_history_index_js.js 9.39 KiB [emitted] 1 related asset
runtime modules 21 KiB 12 modules
built modules 1.04 MiB (javascript) 252 bytes (consume-shared) 210 bytes (share-init) [built]
  cacheable modules 1.04 MiB
    modules by path ./node_modules/ 1.03 MiB
      modules by path ./node_modules/scheduler/ 26.3 KiB 4 modules
      modules by path ./node_modules/react-dom/ 875 KiB 2 modules
      modules by path ./node_modules/react/ 70.6 KiB 2 modules
    modules by path ./bazel-out/darwin_arm64-fastbuild/bin/src/client/host/ 8.93 KiB 5 modules
  consume-shared-module modules 252 bytes 6 modules
  provide-module modules 210 bytes
    provide shared module (default) [email protected] = ./node_modules/react-dom/index.js 42 bytes [built] [code generated]
    provide shared module (default) [email protected] = ./node_modules/react-router-dom/index.js 42 bytes [built] [code generated]
    provide shared module (default) [email protected] = ./node_modules/react-router/index.js 42 bytes [built] [code generated]
    provide shared module (default) [email protected] = ./node_modules/react/index.js 42 bytes [built] [code generated]
    provide shared module (default) [email protected] = ./node_modules/regenerator-runtime/runtime.js 42 bytes [built] [code generated]

LOG from webpack.FileSystemInfo
<w> Managed item /private/var/tmp/_bazel_davidaghassi/bb7da84b9ecf678e9823437c8f5a423f/sandbox/darwin-sandbox/51/execroot/bazel_module_federation/node_modules/@carto/utils isn't a directory or doesn't contain a package.json
+ 11 hidden lines

webpack 5.60.0 compiled successfully in 486 ms
Target //src/client/host:host up-to-date:
  bazel-bin/src/client/host/host
INFO: Elapsed time: 3.210s, Critical Path: 2.94s
INFO: 2 processes: 1 internal, 1 darwin-sandbox.
INFO: Build completed successfully, 2 total actions

Example of failure build by having host depend on data provided by a file from routes

bazel-module-federation on  main [$!] via ⬢ v16.13.0 on 🐳 v20.10.11
➜ bazel build //src/client/host
INFO: Analyzed target //src/client/host:host (2 packages loaded, 13 targets configured).
INFO: Found 1 target...
ERROR: /Users/davidaghassi/git/bazel-module-federation/src/client/host/BUILD.bazel:4:11: Action src/client/host/host_build failed: (Exit 1): webpack-cli.sh failed: error executing command bazel-out/host/bin/external/npm/webpack-cli/bin/webpack-cli.sh --env 'name=host' --env 'entry=./bazel-out/darwin_arm64-fastbuild/bin/src/client/host/host.js' --env ... (remaining 6 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
assets by chunk 1.04 MiB (id hint: vendors)
  asset app.vendors-node_modules_react-dom_index_js.js 905 KiB [emitted] (id hint: vendors) 1 related asset
  asset app.vendors-node_modules_react_index_js.js 73.7 KiB [emitted] (id hint: vendors) 1 related asset
  asset app.vendors-node_modules_react-router_index_js.js 36.4 KiB [emitted] (id hint: vendors) 1 related asset
  asset app.vendors-node_modules_react-router-dom_index_js.js 26 KiB [emitted] (id hint: vendors) 1 related asset
  asset app.vendors-node_modules_regenerator-runtime_runtime_js.js 24.7 KiB [emitted] (id hint: vendors) 1 related asset
asset app.main.js 29.2 KiB [emitted] (name: main) 1 related asset
asset app.bazel-out_darwin_arm64-fastbuild_bin_src_client_host_bootstrap_js.js 18.2 KiB [emitted] 1 related asset
asset app.node_modules_history_index_js.js 9.39 KiB [emitted] 1 related asset
runtime modules 21 KiB 12 modules
built modules 1.04 MiB (javascript) 252 bytes (consume-shared) 210 bytes (share-init) [built]
  cacheable modules 1.04 MiB
    modules by path ./node_modules/ 1.03 MiB
      modules by path ./node_modules/scheduler/ 26.3 KiB 4 modules
      modules by path ./node_modules/react-dom/ 875 KiB 2 modules
      modules by path ./node_modules/react/ 70.6 KiB 2 modules
    modules by path ./bazel-out/darwin_arm64-fastbuild/bin/src/client/host/ 8.96 KiB 5 modules
  consume-shared-module modules 252 bytes 6 modules
  provide-module modules 210 bytes
    provide shared module (default) [email protected] = ./node_modules/react-dom/index.js 42 bytes [built] [code generated]
    provide shared module (default) [email protected] = ./node_modules/react-router-dom/index.js 42 bytes [built] [code generated]
    provide shared module (default) [email protected] = ./node_modules/react-router/index.js 42 bytes [built] [code generated]
    provide shared module (default) [email protected] = ./node_modules/react/index.js 42 bytes [built] [code generated]
    provide shared module (default) [email protected] = ./node_modules/regenerator-runtime/runtime.js 42 bytes [built] [code generated]

LOG from webpack.FileSystemInfo
<w> Managed item /private/var/tmp/_bazel_davidaghassi/bb7da84b9ecf678e9823437c8f5a423f/sandbox/darwin-sandbox/53/execroot/bazel_module_federation/node_modules/@carto/utils isn't a directory or doesn't contain a package.json
+ 11 hidden lines

ERROR in ./bazel-out/darwin_arm64-fastbuild/bin/src/client/host/host.js 2:0-27
Module not found: Error: You attempted to import ../routes/default which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/.

webpack 5.60.0 compiled with 1 error in 491 ms
Target //src/client/host:host failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 3.888s, Critical Path: 3.48s
INFO: 3 processes: 2 internal, 1 darwin-sandbox.
FAILED: Build did NOT complete successfully

Obviously the error messaging needs some workshopping but still.

Aghassi and others added 4 commits January 19, 2022 20:04
Requiring this will require routes and hosts to only
import from things in their source tree and node_modules
This will force end users to have to create shared
node_modules packages with rules_nodejs instead of
doing a relative path import.

This both enforces the DAG in bazel, and enforces clean
boundaries between routes
This also gets the path used for the files being passed into src
properly using path.dirname
@Aghassi Aghassi requested a review from sumwatshade January 23, 2022 06:21
@Aghassi Aghassi marked this pull request as ready for review January 25, 2022 05:11
@Aghassi
Copy link
Owner Author

Aghassi commented Jan 29, 2022

@sumwatshade I'm gonna land this so I can continue to iterate this weekend, but we can work on iterating on this over time. I'm thinking of just moving the plugin into this repo long term and updating it as CRA does, but keeping my patch so that I can customize the error.

@Aghassi Aghassi merged commit 6895a72 into main Jan 29, 2022
@Aghassi Aghassi deleted the feat/enforce-module-scope-plugin branch January 29, 2022 06:47
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.

1 participant