Repository: ProjectEvergreen/greenwood
Pull request #722 (2021-09-10 22:49:17)

better handle the duality of @greenwood node modules resolution
<!--
## Submitting a Pull Request
We love contributions and appreciate any help you can offer!
-->

## Related Issue
related to #557 and fixing an observed issue as part of the v01.16.0-alpha.0 - https://github.com/thegreenhouseio/www.thegreenhouse.io/pull/244

![Screen Shot 2021-09-10 at 4 57 48 PM](https://user-images.githubusercontent.com/895923/132917462-d854d7b7-ff81-405b-a406-e341baf5b01a.png)

> Note: there is a discussion for track refactoring this kind of stuff - #623

## Summary of Changes
1.  Correctly handle @greenwood scope when auto resolving _node_modules_


Patch:
diff --git a/packages/cli/src/plugins/resource/plugin-node-modules.js b/packages/cli/src/plugins/resource/plugin-node-modules.js
index d92291bb..90932322 100644
--- a/packages/cli/src/plugins/resource/plugin-node-modules.js
+++ b/packages/cli/src/plugins/resource/plugin-node-modules.js
@@ -222,7 +222,9 @@ class NodeModulesResource extends ResourceInterface {
       const packageEntryLocation = require.resolve(packageName).replace(/\\/g, '/'); // force / for consistency and path matching
 
       if (packageName.indexOf('@greenwood') === 0) {
-        const subPackage = packageName.split('/')[1];
+        const subPackage = packageEntryLocation.indexOf('@greenwood') > 0
+          ? packageName // we are in the user's node modules
+          : packageName.split('/')[1]; // else we are in our monorepo
         const packageRootPath = packageEntryLocation.indexOf('@greenwood') > 0
           ? packageEntryLocation.split(packageName)[0] // we are in the user's node modules
           : packageEntryLocation.split(subPackage)[0]; // else we are in our monorepo


Test patch:
diff --git a/packages/cli/test/cases/develop.default/develop.default.spec.js b/packages/cli/test/cases/develop.default/develop.default.spec.js
index 9b4d59ce..446aa1bc 100644
--- a/packages/cli/test/cases/develop.default/develop.default.spec.js
+++ b/packages/cli/test/cases/develop.default/develop.default.spec.js
@@ -139,6 +139,11 @@ describe('Develop Greenwood With: ', function() {
         });
       });
 
+      it('should return the correct content type', function(done) {
+        expect(response.headers['content-type']).to.contain('text/html');
+        done();
+      });
+
       it('should return a 200', function(done) {
         expect(response.statusCode).to.equal(200);
 
diff --git a/packages/plugin-graphql/test/cases/develop.default/develop.default.spec.js b/packages/plugin-graphql/test/cases/develop.default/develop.default.spec.js
index b53d1e93..0ea64e0e 100644
--- a/packages/plugin-graphql/test/cases/develop.default/develop.default.spec.js
+++ b/packages/plugin-graphql/test/cases/develop.default/develop.default.spec.js
@@ -25,7 +25,7 @@ const Runner = require('gallinago').Runner;
 const runSmokeTest = require('../../../../../test/smoke-test');
 
 describe('Develop Greenwood With: ', function() {
-  const LABEL = 'GraphQL plugin for resolving .gql files';
+  const LABEL = 'GraphQL plugin for resolving client facing files';
   const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js');
   const outputPath = __dirname;
   const hostname = 'http://localhost';
@@ -55,6 +55,93 @@ describe('Develop Greenwood With: ', function() {
 
     runSmokeTest(['serve'], LABEL);
 
+    describe('Develop command import map for GraphQL', function() {
+      let response = {};
+      let dom;
+
+      before(async function() {
+        return new Promise((resolve, reject) => {
+          request.get({
+            url: `http://127.0.0.1:${port}`,
+            headers: {
+              accept: 'text/html'
+            }
+          }, (err, res, body) => {
+            if (err) {
+              reject();
+            }
+
+            response = res;
+
+            dom = new JSDOM(body);
+            resolve();
+          });
+        });
+      });
+
+      it('should return a 200', function(done) {
+        expect(response.statusCode).to.equal(200);
+
+        done();
+      });
+
+      it('should return the correct content type', function(done) {
+        expect(response.headers['content-type']).to.contain('text/html');
+        done();
+      });
+
+      it('should return an import map shim <script> in the <head> of the document', function(done) {
+        const importMapTag = dom.window.document.querySelectorAll('head > script[type="importmap-shim"]')[0];
+        const importMap = JSON.parse(importMapTag.textContent).imports;
+
+        expect(importMap['@greenwood/plugin-graphql/core/client']).to.equal('/node_modules/@greenwood/plugin-graphql/src/core/client.js');
+        expect(importMap['@greenwood/plugin-graphql/core/common']).to.equal('/node_modules/@greenwood/plugin-graphql/src/core/common.client.js');
+        expect(importMap['@greenwood/plugin-graphql/queries/children']).to.equal('/node_modules/@greenwood/plugin-graphql/src/queries/children.gql');
+        expect(importMap['@greenwood/plugin-graphql/queries/config']).to.equal('/node_modules/@greenwood/plugin-graphql/src/queries/config.gql');
+        expect(importMap['@greenwood/plugin-graphql/queries/graph']).to.equal('/node_modules/@greenwood/plugin-graphql/src/queries/graph.gql');
+        expect(importMap['@greenwood/plugin-graphql/queries/menu']).to.equal('/node_modules/@greenwood/plugin-graphql/src/queries/menu.gql');
+
+        done();
+      });
+    });
+
+    describe('Develop command specific client node_modules resolution', function() {
+      let response = {};
+
+      before(async function() {
+        return new Promise((resolve, reject) => {
+          request.get({
+            url: `http://127.0.0.1:${port}/node_modules/@greenwood/plugin-graphql/src/core/client.js`
+          }, (err, res, body) => {
+            if (err) {
+              reject();
+            }
+
+            response = res;
+            
+            dom = new JSDOM(body);
+            resolve();
+          });
+        });
+      });
+
+      it('should return a 200', function(done) {
+        expect(response.statusCode).to.equal(200);
+
+        done();
+      });
+
+      it('should return the correct content type', function(done) {
+        expect(response.headers['content-type']).to.equal('text/javascript');
+        done();
+      });
+
+      it('should return an ECMASCript module', function(done) {
+        expect(response.body).to.contain('export default client;');
+        done();
+      });
+    });
+
     describe('Develop command specific .gql behaviors', function() {
       let response = {};
 
@@ -68,7 +155,7 @@ describe('Develop Greenwood With: ', function() {
             }
 
             response = res;
-            
+
             dom = new JSDOM(body);
             resolve();
           });