Skip to content

Commit

Permalink
Build plottable.min.js with webpack (#3318)
Browse files Browse the repository at this point in the history
Use webpack to build plottable.min.js, removing old grunt code and opening the door for more enhancements later (sourcemaps, dev logging that gets wiped in prod, removing comments)

Fixes #3315
  • Loading branch information
hellochar authored and adidahiya committed Mar 30, 2017
1 parent 3be0942 commit 0c2cdad
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 469 deletions.
70 changes: 0 additions & 70 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ module.exports = function(grunt) {
}
};

var bumpConfig = {
options: {
files: ["package.json", "bower.json"],
updateConfigs: ["pkg"],
commit: false,
createTag: false,
push: false,
prereleaseName: "rc"
}
};

var jscsConfig = {
files: ["Gruntfile.js", "quicktests/**/*.js"],
options: {
Expand Down Expand Up @@ -68,54 +57,6 @@ module.exports = function(grunt) {
}
};

var FILES_TO_COMMIT = [
"plottable.d.ts",
"plottable.css",
"plottable.zip",
"bower.json",
"package.json"
];

var gitcommitConfig = {
version: {
options: {
message: "Release version <%= pkg.version %>"
},
files: {
src: FILES_TO_COMMIT
}
},
built: {
options: {
message: "Update built files"
},
files: {
src: FILES_TO_COMMIT
}
}
};

var compressConfig = {
main: {
options: {
archive: "plottable.zip"
},
files: [
{src: "plottable.d.ts", dest: "."},
{src: "plottable.css", dest: "."},
{src: "README.md", dest: "."},
{src: "LICENSE", dest: "."},
{src: ["build/**"], dest: "."}
]
}
};

var uglifyConfig = {
main: {
files: {"plottable.min.js": ["plottable.js"]}
}
};

var saucelabsMochaConfig = {
all: {
options: {
Expand Down Expand Up @@ -145,16 +86,12 @@ module.exports = function(grunt) {

grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
bump: bumpConfig,
jscs: jscsConfig,
eslint: eslintConfig,
exec: execConfig,
watch: watchConfig,
"blanket_mocha": blanketMochaConfig,
connect: connectConfig,
gitcommit: gitcommitConfig,
compress: compressConfig,
uglify: uglifyConfig,
"saucelabs-mocha": saucelabsMochaConfig
});

Expand All @@ -166,13 +103,6 @@ module.exports = function(grunt) {
"update-quicktests"
]);

grunt.registerTask("release:patch", ["bump:patch", "dist-compile", "gitcommit:version"]);
grunt.registerTask("release:minor", ["bump:minor", "dist-compile", "gitcommit:version"]);
grunt.registerTask("release:major", ["bump:major", "dist-compile", "gitcommit:version"]);

grunt.registerTask("dist-compile", ["exec:yarn:build", "exec:yarn:sed-version", "uglify", "compress"]);

grunt.registerTask("commitjs", ["dist-compile", "gitcommit:built"]);
grunt.registerTask("default", ["exec:yarn:start"]);

grunt.registerTask("test", ["dev-compile", "test-local"]);
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ deployment:
tag: /v[0-9.]+(-beta[0-9.]*)?/
commands:
# Confirm we are ready to publish
- yarn dist-compile
- yarn dist
- git diff HEAD..origin/develop --quiet || (echo "dist not built"; exit 1)
- npm run -s check_version || (echo "package.json version not updated"; exit 1)
# Publish
Expand Down
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"scripts": {
"build": "npm-run-all -p lint:tslint build:tsc -s build:webpack",
"build:tsc": "tsc -p test/",
"build:webpack": "webpack --config webpack.config.js",
"build:webpack": "webpack",
"check_version": "echo ${npm_package_version} $(npm view ${npm_package_name} version) | awk '{ if ($1 == $2) { exit 1 }}'",
"dist-compile": "grunt dist-compile",
"dist": "npm-run-all build dist:sed-version dist:build-min",
"dist:build-min": "webpack --config webpackConfig/build.js --optimize-minimize --output-filename plottable.min.js",
"dist:sed-version": "sed -i.bak -e \"s/exports.version = __VERSION__/exports.version = \\\"${npm_package_version}\\\"/\" build/src/core/version.js",
"lint:tslint": "tslint --project tsconfig.json --type-check",
"sed-version": "sed -i.bak -e \"s/exports.version = __VERSION__/exports.version = \\\"${npm_package_version}\\\"/\" build/src/core/version.js",
"start": "npm-run-all -p start:update-quicktests start:watch-tsc start:watch-quicktests start:webpack-dev-server",
"start:update-quicktests": "grunt update-quicktests",
"start:watch-quicktests": "grunt watch-quicktests",
Expand All @@ -34,15 +35,11 @@
"circle-github-bot": "0.3.0",
"grunt": "~0.4.5",
"grunt-blanket-mocha": "0.5.0",
"grunt-bump": "0.7.0",
"grunt-cli": "0.1.13",
"grunt-contrib-compress": "0.14.0",
"grunt-contrib-connect": "0.11.2",
"grunt-contrib-uglify": "0.11.0",
"grunt-contrib-watch": "0.6.1",
"grunt-eslint": "17.3.1",
"grunt-exec": "1.0.1",
"grunt-git": "0.3.7",
"grunt-jscs": "2.6.0",
"grunt-saucelabs": "8.6.2",
"jquery": "2.1.0",
Expand All @@ -55,7 +52,7 @@
"typescript": "2.1.5",
"webpack": "2.2.1",
"webpack-dev-server": "2.3.0",
"write-file-webpack-plugin": "3.4.2"
"webpack-merge": "^4.1.0"
},
"dependencies": {
"@types/d3": "^4.5.0",
Expand Down
2 changes: 1 addition & 1 deletion publishSnapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ then
else
# set package.json version
npm --no-git-tag-version version $GIT_DESCRIBE
yarn dist-compile
yarn dist
npm publish --tag next
fi
123 changes: 9 additions & 114 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,12 @@
var path = require("path");
var webpack = require("webpack");
var WriteFilePlugin = require("write-file-webpack-plugin");
var packageJson = require("./package.json");

var LICENSE_HEADER =
`Plottable ${packageJson.version} (https://github.com/palantir/plottable)
Copyright 2014-2017 Palantir Technologies
Licensed under MIT (https://github.com/palantir/plottable/blob/master/LICENSE)`;
const buildConfig = require("./webpackConfig/build.js");
const testConfig = require("./webpackConfig/test.js");

/**
* Build plottable.js and tests/test by default (e.g. for webpack-dev-server). Both of these
* files included in <script> tags for tests.
* TODO we include Plottable twice this way right now; improve testing/devserver setup to only need one.
*/
module.exports = [
/**
* Builds test.js from sources in /build/test. Requirements:
*
* Loads for side-effect - loading the file immediately invokes many global functions
* such as describe and it().
*
* Look to test/tests.html for the context of how it's used.
*/
{
devtool: "source-map",
devServer: {
compress: true,
port: 9999,
inline: false
},
entry: "./build/test/index.js",
output: {
filename: "test/tests.js"
},
externals: {
"d3": "d3",
"mocha": "mocha",
"chai": "chai"
},
plugins: [
// Resolve require.context calls containing TestSelector to recursively search the directory
// for files matching the /Tests.js$/ regex. we use .js files because webpack runs on compiled
// .js files.
// see https://github.com/webpack/webpack/issues/2783 for info on how this works
new webpack.ContextReplacementPlugin(/TestSelector/, ".", true, /Tests.js$/),
new webpack.DefinePlugin({
"__VERSION__": JSON.stringify(packageJson.version)
})
]
},

/**
* Builds plottable.js from sources in /build/src. Requirements:
*
* Be ready for consumption by bower, a script tag, or requireJS.
* should specify d3 as an external dependency.
*
* Example bower usage:
*
* User runs `bower install plottable --save` in their directory.
* Their bower_components gets plottable and d3. They then add the two
* files in the right order through script tags, or plug it into requireJS,
* and reference Plottable globally.
*
* Example script tag usage:
*
* User adds two script tags to their html page:
*
* <script src="//cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.js"></script>
* <script src="//unpkg.com/plottable/plottable.js"></script>
*
* And then references Plottable globally.
*
* Example RequireJS usage:
*
* User configures their requireJS to point to Plottable, e.g.
*
* require.config( {
* paths: {
* d3: "https://cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.min",
* plottable: "//unpkg.com/plottable/plottable.js"
* },
* shim: {
* "plottable": {
* deps: [ "d3" ]
* }
* }
* } );
*
* User calls define(["plottable"], function (Plottable) { ... })
*
* So there's two ideas:
* (a) Getting the plottable.js file. This is either from bower or from a web url.
* (b) Referencing the Plottable object/code. This is either globally or by declaring
* a dependency on a module named "plottable" (both in AMD and CommonJS).
*/
{
entry: "./build/src/index.js",
output: {
filename: "plottable.js",
// adds the UMD header to allow export to AMD, commonJS, or global
libraryTarget: "umd",
// the name of the AMD/commonJS/global
library: "Plottable"
},
externals: {
// don't bundle d3 but instead it request it externally
"d3": "d3"
},
plugins: [
new webpack.BannerPlugin({
banner: LICENSE_HEADER,
entryOnly: true
}),
new webpack.DefinePlugin({
"__VERSION__": JSON.stringify(packageJson.version)
}),
new WriteFilePlugin()
]
}
testConfig,
buildConfig
];
77 changes: 77 additions & 0 deletions webpackConfig/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const webpack = require("webpack");

const packageJson = require("../package.json");

const LICENSE_HEADER =
`Plottable ${packageJson.version} (https://github.com/palantir/plottable)
Copyright 2014-2017 Palantir Technologies
Licensed under MIT (https://github.com/palantir/plottable/blob/master/LICENSE)`;

/**
* Builds plottable.js from sources in /build/src. Requirements:
*
* Be ready for consumption by bower, a script tag, or requireJS.
* should specify d3 as an external dependency.
*
* Example bower usage:
*
* User runs `bower install plottable --save` in their directory.
* Their bower_components gets plottable and d3. They then add the two
* files in the right order through script tags, or plug it into requireJS,
* and reference Plottable globally.
*
* Example script tag usage:
*
* User adds two script tags to their html page:
*
* <script src="//cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.js"></script>
* <script src="//unpkg.com/plottable/plottable.js"></script>
*
* And then references Plottable globally.
*
* Example RequireJS usage:
*
* User configures their requireJS to point to Plottable, e.g.
*
* require.config( {
* paths: {
* d3: "https://cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.min",
* plottable: "//unpkg.com/plottable/plottable.js"
* },
* shim: {
* "plottable": {
* deps: [ "d3" ]
* }
* }
* } );
*
* User calls define(["plottable"], function (Plottable) { ... })
*
* So there's two ideas:
* (a) Getting the plottable.js file. This is either from bower or from a web url.
* (b) Referencing the Plottable object/code. This is either globally or by declaring
* a dependency on a module named "plottable" (both in AMD and CommonJS).
*/
module.exports = {
entry: "./build/src/index.js",
output: {
filename: "plottable.js",
// adds the UMD header to allow export to AMD, commonJS, or global
libraryTarget: "umd",
// the name of the AMD/commonJS/global
library: "Plottable"
},
externals: {
// don't bundle d3 but instead it request it externally
"d3": "d3"
},
plugins: [
new webpack.BannerPlugin({
banner: LICENSE_HEADER,
entryOnly: true
}),
new webpack.DefinePlugin({
"__VERSION__": JSON.stringify(packageJson.version)
})
]
};
Loading

1 comment on commit 0c2cdad

@blueprint-bot
Copy link

Choose a reason for hiding this comment

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

Build plottable.min.js with webpack (#3318)

Demo: quicktests | fiddle

Please sign in to comment.