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 npm commands for dev and fix hotreloading #179

Merged
merged 20 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,49 @@
"scripts": {
"preanalyze-bundles": "webpack --config webpack.cdn.config.js --profile --json > stats.json",
"analyze-bundles": "webpack-bundle-analyzer stats.json .",
"prestart": "npm run targets:standalone",
"start": "webpack-dev-server --config webpack.standalone.config.js",
"targets": "run-p targets:*",
"targets:cdn": "run-p targets:cdn:* && run-p targets:cdn:*:*",
"targets:cdn:css:main": "node-sass src/form.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/cdn/happychat.css",
"targets:cdn:css:woo": "node-sass src/ui/css/themes/_woo.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/cdn/woo.css",
"targets:cdn:css:jpop": "node-sass src/ui/css/themes/_jpop.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/cdn/jpop.css",
"targets:cdn:js": "webpack --config webpack.cdn.config.js",
"targets:standalone": "webpack --config webpack.standalone.config.js",
"targets:wordpress": "webpack --config webpack.wordpress.config.js",
"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": "run-p dev:standalone:css:*",
"dev:standalone:css:main": "node-sass src/form.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/standalone/happychat.css",
"dev:standalone:css:woo": "node-sass src/ui/css/themes/_woo.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/standalone/woo.css",
"dev:standalone:css:jpop": "node-sass src/ui/css/themes/_jpop.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/standalone/jpop.css",

"dev:cdn": "run-p dev:cdn:*",
"dev:cdn:js": "NODE_ENV=development webpack-dev-server --config webpack.cdn.config.js",
"dev:cdn:css": "run-p dev:cdn:css:*",
"dev:cdn:css:main": "node-sass src/form.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/cdn/happychat.css",
"dev:cdn:css:woo": "node-sass src/ui/css/themes/_woo.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/cdn/woo.css",
"dev:cdn:css:jpop": "node-sass src/ui/css/themes/_jpop.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/cdn/jpop.css",

"dev:wordpress": "run-p dev:wordpress:*",
"dev:wordpress:js": "NODE_ENV=development webpack-dev-server --config webpack.wordpress.config.js",
"dev:wordpress:css": "run-p dev:wordpress:css:*",
"dev:wordpress:css:main": "node-sass src/form.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/wordpress/happychat.css",
"dev:wordpress:css:woo": "node-sass src/ui/css/themes/_woo.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/wordpress/woo.css",
"dev:wordpress:css:jpop": "node-sass src/ui/css/themes/_jpop.scss | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/wordpress/jpop.css",

"build": "run-p build:*",
"build:cdn": "run-p build:cdn:*",
"build:cdn:js": "NODE_ENV=production webpack -p --config webpack.cdn.config.js",
"build:cdn:css": "run-p build:cdn:css:*",
"build:cdn:css:main": "node-sass src/form.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/cdn/happychat.css",
"build:cdn:css:woo": "node-sass src/ui/css/themes/_woo.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/cdn/woo.css",
"build:cdn:css:jpop": "node-sass src/ui/css/themes/_jpop.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/cdn/jpop.css",

"build:standalone": "run-p build:standalone:*",
"build:standalone:js": "NODE_ENV=production webpack -p --config webpack.standalone.config.js -p",
"build:standalone:css": "run-p build:standalone:css:*",
"build:standalone:css:main": "node-sass src/form.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/standalone/happychat.css",
"build:standalone:css:woo": "node-sass src/ui/css/themes/_woo.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/standalone/woo.css",
"build:standalone:css:jpop": "node-sass src/ui/css/themes/_jpop.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/standalone/jpop.css",

"build:wordpress": "run-p build:wordpress:*",
"build:wordpress:js": "NODE_ENV=production webpack -p --config webpack.wordpress.config.js",
"build:wordpress:css": "run-p build:wordpress:css:*",
"build:wordpress:css:main": "node-sass src/form.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/wordpress/assets/happychat.css",
"build:wordpress:css:woo": "node-sass src/ui/css/themes/_woo.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/wordpress/assets/woo.css",
"build:wordpress:css:jpop": "node-sass src/ui/css/themes/_jpop.scss --output-style compressed | postcss --config postcss.config.json -u autoprefixer -u postcss-custom-properties --no-map -o targets/wordpress/assets/jpop.css",
"test": "jest -c jest.config.json"
},
"devDependencies": {
Expand Down
58 changes: 36 additions & 22 deletions targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,60 @@

This folder contains target apps that embed the Happychat library and we actively maintain. Every target has a companion webpack build config and a npm command.

## targets/cdn
## Developing

There is a command for developing on each target `npm run dev:<target>`. While developing the `NODE_ENV` is set to `development` within the webpack config, the config values are taken from `src/config/development.json`.

Contains the production-ready JavaScript and CSS assets.
**When developing local css will be loaded but for now there is no watch mechanism. Stop and start the
command to rebuild local css. Or just run `npm run dev:<target>:css` in another terminal.**

To build this target, execute:

npm run targets:cdn
## Production builds

It'll build the JS library using the `webpack.cdn.config.js`. By defining `NODE_ENV` to `production` within the webpack config, we leverage some optimizations of our dependencies (slimmer React build, for example) and also take the config values from `src/config/production.json` (which, for example, teach the library to connect to Happychat production server).
`npm run build` - will create production builds for all the targets, you can build a target independenly
by running `npm run build:<target>`.

For all production builds `NODE_ENV` is set to `production` within the webpack config, we leverage some optimizations of our dependencies (slimmer React build, for example) and also take the config values from `src/config/production.json` (which, for example, teach the library to connect to Happychat production server).

It'll build the CSS asset using the `postcss.config.json`. Note that the CSS is automatically requested once the Happychat component is initialized. You never need to embed this manually in your page. It's served from the WordPress.com CDN at https://widgets.wp.com/happychat/happychat.css so any time it's updated it needs to be uploaded there for changes to take effect.

## targets/standalone

Contains a standalone example of Happychat embedded in a bare HTML webpage.
## targets/cdn

To build this target, execute:
Contains the production-ready JavaScript and CSS assets. It'll build the JS library using the `webpack.cdn.config.js`.

npm run targets:standalone
To develop this target (`http://localhost:9000`), execute:

This build uses `webpack.standalone.config.js` which doesn't include some production optimization (uglify, etc) so it's readable for development. By defining the `NODE_ENV` to `development` within the webpack config, the config values are taken from `src/config/development.json` (which, for example, teach the library to connect to Happychat production server).
npm run dev:cdn

It also defines a server for development:
To build this target for production, execute:

npm install
npm run start
npm run build:cdn

And visit `localhost:9000`.

## targets/wordpress
## targets/standalone

Contains a WordPress plugin that exposes a `[happychat]` shortcode, it leverages the library to provide a Happychat experience in any WordPress.
Contains a standalone example of Happychat embedded in a bare HTML webpage. It'll build the JS library using the `webpack.standalone.config.js`.

To develop this target (`http://localhost:9000`), execute:

npm run dev:standalone
// or
npm start

To build this target for production, execute:

npm run build:standalone


## targets/wordpress

To build this target instead, execute:
Contains a WordPress plugin that exposes a `[happychat]` shortcode, it leverages the library to provide a Happychat experience in any WordPress. Because it leverages the browser's `window` object this target will use the same entry point as browser (`targets/browser/index.js`). It'll build the JS library using the `webpack.wordpress.config.js`.

npm run targets:wordpress
To develop this target (`http://localhost:9000`), execute:

This build uses `webpack.wordpress.config.js` which doesn't include some production optimization (uglify, etc) so it's readable for development. By defining the `NODE_ENV` to `development` within the webpack config, the config values are taken from `src/config/development.json` (which, for example, teach the library to connect to Happychat production server).
npm run dev:wordpress

Should you want a production build, execute
To build this target for production, execute:

npm run targets:cdn:js
cp targets/cdn/happychat.js targets/wordpress/assets/happychat.js
npm run build:wordpress
8 changes: 0 additions & 8 deletions targets/dist/index.js

This file was deleted.

27 changes: 22 additions & 5 deletions webpack.cdn.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ const path = require( 'path' );
const webpack = require( 'webpack' );
const UglifyJsPlugin = require( 'uglifyjs-webpack-plugin' );

module.exports = {
const env = process.env.NODE_ENV;

const config = {
entry: './targets/cdn/index.js',
output: {
filename: './targets/cdn/happychat.js',
filename: 'happychat.js',
path: path.resolve( __dirname, 'targets/cdn' ),
},
module: {
rules: [
Expand All @@ -19,13 +22,27 @@ module.exports = {
},
plugins: [
new webpack.DefinePlugin( {
'process.env.NODE_ENV': JSON.stringify( 'production' ),
'process.env.NODE_ENV': JSON.stringify( env ),
} ),
new webpack.optimize.ModuleConcatenationPlugin(),
new UglifyJsPlugin(),
],
resolve: {
extensions: [ '.js', '.jsx' ],
modules: [ path.resolve( __dirname ), path.resolve( __dirname, 'node_modules' ) ],
},
};

switch ( env ) {
case 'development':
config.devtool = 'source-map';
config.devServer = {
port: 9000,
};
break;

case 'production':
config.plugins.push( new webpack.optimize.ModuleConcatenationPlugin() );
config.plugins.push( new UglifyJsPlugin() );
break;
}

module.exports = config;
34 changes: 25 additions & 9 deletions webpack.standalone.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/** @format */
const path = require( 'path' );
const webpack = require( 'webpack' );
const UglifyJsPlugin = require( 'uglifyjs-webpack-plugin' );

module.exports = {
const env = process.env.NODE_ENV;

const config = {
entry: './targets/standalone/index.js',
output: {
filename: './targets/standalone/public/happychat.js',
filename: 'happychat.js',
path: path.resolve( __dirname, 'targets/standalone' ),
},
module: {
rules: [
Expand All @@ -18,17 +22,29 @@ module.exports = {
},
plugins: [
new webpack.DefinePlugin( {
'process.env.NODE_ENV': JSON.stringify( 'development' ),
'process.env.NODE_ENV': JSON.stringify( env ),
} ),
],
resolve: {
extensions: [ '.js', '.jsx' ],
modules: [ path.resolve( __dirname ), path.resolve( __dirname, 'node_modules' ) ],
},
devServer: {
contentBase: './targets/standalone/public',
compress: true,
port: 9000,
hot: true,
},
};

switch ( env ) {
case 'development':
config.devtool = 'source-map';
config.devServer = {
contentBase: path.resolve( __dirname, 'targets/standalone' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️ we should merge this piece right away!

publicPath: '/',
port: 9000,
};
break;

case 'production':
config.plugins.push( new webpack.optimize.ModuleConcatenationPlugin() );
config.plugins.push( new UglifyJsPlugin() );
break;
}

module.exports = config;
26 changes: 23 additions & 3 deletions webpack.wordpress.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/** @format */
const path = require( 'path' );
const webpack = require( 'webpack' );
const UglifyJsPlugin = require( 'uglifyjs-webpack-plugin' );

module.exports = {
const env = process.env.NODE_ENV;

const config = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if I should mention this here as I've already mentioned it in the first PR: I'd prefer that targets were independent of each other. We should have a targets/wordpress/index.js independent from targets/browser/index.js.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you did, and this was fixed in the base PR.

entry: './targets/wordpress/index.js',
output: {
filename: './targets/wordpress/assets/happychat.js',
filename: 'happychat.js',
path: path.resolve( __dirname, 'targets/wordpress/assets' ),
},
module: {
rules: [
Expand All @@ -18,11 +22,27 @@ module.exports = {
},
plugins: [
new webpack.DefinePlugin( {
'process.env.NODE_ENV': JSON.stringify( 'development' ),
'process.env.NODE_ENV': JSON.stringify( env ),
} ),
],
resolve: {
extensions: [ '.js', '.jsx' ],
modules: [ path.resolve( __dirname ), path.resolve( __dirname, 'node_modules' ) ],
},
};

switch ( env ) {
case 'development':
config.devtool = 'source-map';
config.devServer = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we use the development server in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't actually tried it, but it should be helpful to serve local css while developing a wordpress plugin.

port: 9000,
};
break;

case 'production':
config.plugins.push( new webpack.optimize.ModuleConcatenationPlugin() );
config.plugins.push( new UglifyJsPlugin() );
break;
}

module.exports = config;