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

[v5-alpha] workbox-build does not bundle workbox-* packages with rollup #2106

Closed
kaykayehnn opened this issue Jul 11, 2019 · 5 comments
Closed
Assignees

Comments

@kaykayehnn
Copy link
Contributor

Library Affected:
workbox-build

Browser & Platform:
all browsers, tested on macOS and Windows

Issue or Feature Request Description:
In v5 alpha, when workbox-build generates a service worker, it saves it to a temporary directory on the file system in order to pass it to rollup for bundling. The temporary directory only contains sw.js and rollup cannot resolve the imports to workbox-* packages, so it assumes they are external dependencies and leaves them as raw define calls.

Expected behaviour
Workbox-build generates a bundle containing the service worker and its dependencies.

Actual behaviour
Workbox-build generates a bundle containing raw define calls to dependent packages.

Minimal reproducible repo
kaykayehnn/workbox-build-node-resolution-bug

Notes
rollup-plugin-node-resolve, which is used to resolve imports in the service worker bundle, has an option to provide a base directory for resolutions. Setting it to the project root should fix this issue.
I can make a PR for this if that's okay.

@jeffposnick
Copy link
Contributor

Thanks for reporting this. I'm taking a look now. The intended behavior is for workbox-build to figure out an absolute path to the various dependencies, but I guess that's not working as expected:

this.nodeModulesPath = upath.resolve(
__dirname, '..', '..', 'node_modules');

@jeffposnick jeffposnick added Bug An issue with our existing, production codebase. workbox-build and removed Bug An issue with our existing, production codebase. labels Jul 12, 2019
@jeffposnick jeffposnick self-assigned this Jul 12, 2019
@jeffposnick
Copy link
Contributor

Okay, I think I see what's happening: when you use workbox-build "in the real world", then the other Workbox libraries don't end up installed within a node_modules subdirectory of the workbox-build project like they do locally in the Lerna-managed repo.

I'll clean up that logic. Thanks for reporting it!

@kaykayehnn
Copy link
Contributor Author

Sure! I put some notes I took while debugging the issue in a gist in case they might be helpful. You can take a look at the revisions for a diff of the changes.

@jeffposnick
Copy link
Contributor

Thanks for the debugging and proposed solution, @kaykayehnn! I tested out your change and it looks good:

getImportStatements() {
  const workboxModuleImports = [];

  for (const [localName, {moduleName, pkg}] of this.modulesUsed) {
    const pkgJsonPath = require.resolve(`${pkg}/package.json`);
    const pkgRoot = upath.dirname(pkgJsonPath);
    const importStatement = ol`import {${moduleName} as ${localName}} from
      '${pkgRoot}/${moduleName}.mjs';`;

    workboxModuleImports.push(importStatement);
  }

  return workboxModuleImports;
}

The one slightly messy part is that there's a unit test that needs to be updated to work with this new approach. We use proxyquire to mock out various things when running our unit tests, but you can't use it on require.resolve() (which kind of makes sense), so the test case ends up looking a little messy.

If you wanted to open a PR against the v5 branch that includes your modifications to getImportStatements(), I'm happy to force-push an update to the test suite so that you didn't have to worry about it.

@kaykayehnn
Copy link
Contributor Author

Fixed in #2110

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants