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

Add support for npm package #184

Merged
merged 30 commits into from
Apr 2, 2018
Merged

Add support for npm package #184

merged 30 commits into from
Apr 2, 2018

Conversation

unDemian
Copy link
Contributor

@unDemian unDemian commented Mar 27, 2018

Built on top of #183

Summary

This PR prepares happychat-client to be exposed as a npm package. Paths are resolved using babel-plugin-module-resolver and configuration json files are inlined using babel-plugin-inline-json.

Testing

  1. Switch calypso to try/happychat-client branch (Use happychat-client npm package wp-calypso#23630)
  2. Link this package to wp-calypso, by following these steps
  • run npm link in this folder
  • run npm link happychat-client in calypso folder
  1. Run npm install in this branch
  2. Run npm run dev:standalone in this repo
  3. Grab your oauth token from a wpcom request made by the client (http://localhost:9000) it is under Authorization header after Bearer
  4. Add the token to calypso https://github.com/Automattic/wp-calypso/pull/23630/files#diff-56de7193249f7b0107d6033c1a7737acR22
  5. Run npm dev:npm
  6. Access http://calypso.localhost:3000/me/chat
  7. It should load the happychat-client iframe

Copy link

@lamosty lamosty left a comment

Choose a reason for hiding this comment

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

If I don't have development.local.json, it won't compile and complain: http://cld.wthms.co/K5ttNz. I thought having it is optional. Or not?

@lamosty
Copy link

lamosty commented Mar 28, 2018

What's the command npm dev:npm? There isn't one like that in wp-calypso. When I run npm start, it complains that "Module not found: Error: Can't resolve 'happychat-client'"

@unDemian
Copy link
Contributor Author

If I don't have development.local.json, it won't compile and complain: http://cld.wthms.co/K5ttNz. I thought having it is optional. Or not?

Nice catch, it should be optional but webpack will fail at build time because it resolves the requires bypassing the try/catch. I added a postinstall script that creates an empty local config file if it is missing.

What's the command npm dev:npm? There isn't one like that in wp-calypso. When I run npm start, it complains that "Module not found: Error: Can't resolve 'happychat-client'"

dev:npm command will run a babel process with watch to compile the files into targets/npm and also run a webpack-dev-server that will serve the local css on port 9000.

Most likely the error appears because the happychat-client is not linked. The Calypso PR adds the happychat-client to the package.json however it is not published to npm which will make calypso npm install to fail. For development purposes you can link a local npm package to your project (calypso) by following step 2 in the description. That will basically create a symlink from calypso/node_modules/happychat-client to your local copy of this repo.

package.json Outdated
"start": "run-p dev:standalone",
"dev:standalone": "run-p dev:standalone:*",
"dev:standalone:js": "NODE_ENV=development webpack-dev-server --config webpack.standalone.config.js",
"dev:standalone:css": "node-sass src/form.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties -o targets/standalone/happychat.css",
"dev:npm": "run-p dev:npm:*",
"dev:npm:js": "NODE_ENV=development ./node_modules/babel-cli/bin/babel.js --watch src --out-dir targets/npm",
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't babel in node_modules/.bin/? If so, you can actually do NODE_ENV=development babel --watch src --out-dir targets/npm because the scripts will search in node_module/.bin path for "binaries".

merge( configFile, localConfig );
} catch ( e ) {}
// check if we have local git ignored config and use it to override the development.json
const localConfig = require( 'src/config/development.local.json' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of creating a fake development.local.json file, wouldn't it be better to check whether it exists here?

case 'production':
config.plugins.push( new webpack.optimize.ModuleConcatenationPlugin() );
config.plugins.push( new UglifyJsPlugin() );
config.plugins.push( new LodashModuleReplacementPlugin( {
Copy link
Contributor

Choose a reason for hiding this comment

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

We no longer use the lodash-webpack-plugin.

@oandregal
Copy link
Contributor

Tried and it works! 🎉

As far as I know, /me/chat was thought as an escape hatch for mobile users, so it used the whole "main content" space to display. By using the iframe as it is, that is no longer true. One potential solution for this is to expose the iframe height / width through the API. This will solve the /me/chat issue and will actually allow any consumer to declare the size they want Happychat to use. This is not hassle-free, though, and it raises some questions: for example, whether we should meet the minimum size requirements declared in the iframe -no matter what the consumer wants- as to prevent issues when the entry point is the contact form, not the chat form.

@unDemian
Copy link
Contributor Author

unDemian commented Apr 2, 2018

As far as I know, /me/chat was thought as an escape hatch for mobile users, so it used the whole "main content" space to display.

That is correct but the Calypso branch was just an experiment, and for that me/chat was easier to refactor.

By using the iframe as it is, that is no longer true. One potential solution for this is to expose the iframe height / width through the API. This will solve the /me/chat issue and will actually allow any consumer to declare the size they want Happychat to use. This is not hassle-free, though, and it raises some questions: for example, whether we should meet the minimum size requirements declared in the iframe -no matter what the consumer wants- as to prevent issues when the entry point is the contact form, not the chat form.

Currently working on this :)

@unDemian
Copy link
Contributor Author

unDemian commented Apr 2, 2018

Thanks for taking the time 🏅

@unDemian unDemian changed the base branch from add/css-file-config to master April 2, 2018 10:22
@unDemian unDemian merged commit aad8f66 into master Apr 2, 2018
@unDemian unDemian mentioned this pull request Apr 2, 2018
16 tasks
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