From a4b2f979dbf6503d0cca404ff412f872414a0cf9 Mon Sep 17 00:00:00 2001 From: Corin Lawson Date: Sun, 5 Feb 2017 12:37:29 +1100 Subject: [PATCH 1/2] Target the browser; add local web server and Karma. --- .eslintrc | 4 +- .travis.yml | 6 +++ index.html | 12 +++++ karma.conf.js | 98 +++++++++++++++++++++++++++++++++++++++ lib/index.js | 2 + package.json | 16 +++++-- rollup.config.js | 6 +-- test/index_test.js | 2 +- test/istanbul.reporter.js | 17 ------- test/mocha.opts | 1 - 10 files changed, 135 insertions(+), 29 deletions(-) create mode 100644 index.html create mode 100644 karma.conf.js delete mode 100644 test/istanbul.reporter.js delete mode 100644 test/mocha.opts diff --git a/.eslintrc b/.eslintrc index bf88a35..b50e89e 100644 --- a/.eslintrc +++ b/.eslintrc @@ -2,7 +2,7 @@ "parser": "babel-eslint", "extends": "eslint:recommended", "env": { - "node": true, + "browser": true, "mocha": true } -} \ No newline at end of file +} diff --git a/.travis.yml b/.travis.yml index d96bef9..986b314 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,3 +2,9 @@ language: node_js node_js: - "4.0" - "5.0" +cache: + directories: + - node_modules +env: + global: + - NPM_CONFIG_PROGRESS="false" diff --git a/index.html b/index.html new file mode 100644 index 0000000..b5b97c3 --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + + Sample project for packages built with rollup + + +
+ +
+ + diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..9739444 --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,98 @@ +// Karma configuration +// Generated on Sun Feb 05 2017 11:15:12 GMT+1100 (AEDT) +var babel = require('rollup-plugin-babel'); +var babelrc = require('babelrc-rollup').default; +var globals = require('rollup-plugin-node-globals'); +var builtins = require('rollup-plugin-node-builtins'); +var resolve = require('rollup-plugin-node-resolve'); +var istanbul = require('rollup-plugin-istanbul'); + +var pkg = require('./package.json'); +var external = Object.keys(pkg.dependencies); +var testFiles = [ + 'test/**/*_test.js' +]; + +module.exports = function(config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: '', + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ['mocha'], + + client: { mocha: { reporter: './istanbul.reporter.js' } }, + + // list of files / patterns to load in the browser + files: testFiles, + + + // list of files to exclude + exclude: [ + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + 'test/**/*_test.js': ['rollup'] + }, + + rollupPreprocessor: { + plugins: [ + babel(babelrc()), + globals(), + builtins(), + resolve({ + jsnext: true + }), + istanbul({ + exclude: testFiles.concat([ 'node_modules/**/*' ]) + }) + ], + external: external, + format: 'iife', + sourceMap: 'inline' + }, + + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['progress', 'coverage'], + + + // web server port + port: 9876, + + + // enable / disable colors in the output (reporters and logs) + colors: true, + + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: false, + + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: ['PhantomJS'], + + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: true, + + // Concurrency level + // how many browser should be started simultaneous + concurrency: Infinity + }) +} diff --git a/lib/index.js b/lib/index.js index bd702c4..dd820bf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -37,3 +37,5 @@ export function multiply(n, m, negative=false) { } return negative ? -result : result; } + +document.write(multiply(11, 9)); diff --git a/package.json b/package.json index 2adcb40..300c7c8 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "build": "rollup -c", "watch": "rollup -c -w", "pretest": "npm run build", - "test": "mocha", - "prepublish": "npm test" + "test": "karma start", + "prepublish": "npm test", + "prestart": "npm install", + "start": "ws" }, "repository": "rollup/rollup-starter-project", "keywords": [ @@ -39,11 +41,19 @@ "babel-register": "^6.18.0", "babelrc-rollup": "^3.0.0", "eslint": "^3.12.2", - "istanbul": "^0.4.5", + "karma": "^1.4.1", + "karma-coverage": "^1.1.1", + "karma-mocha": "^1.3.0", + "karma-phantomjs-launcher": "^1.0.2", + "karma-rollup-plugin": "^0.2.4", + "local-web-server": "^1.2.6", "mocha": "^3.2.0", "rollup": "^0.37.0", "rollup-plugin-babel": "^2.7.1", "rollup-plugin-istanbul": "^1.1.0", + "rollup-plugin-node-builtins": "^2.0.0", + "rollup-plugin-node-globals": "^1.1.0", + "rollup-plugin-node-resolve": "^2.0.0", "rollup-watch": "^2.5.0" } } diff --git a/rollup.config.js b/rollup.config.js index 056d1a8..e5357b8 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,6 +1,5 @@ import babel from 'rollup-plugin-babel'; import babelrc from 'babelrc-rollup'; -import istanbul from 'rollup-plugin-istanbul'; let pkg = require('./package.json'); let external = Object.keys(pkg.dependencies); @@ -8,10 +7,7 @@ let external = Object.keys(pkg.dependencies); export default { entry: 'lib/index.js', plugins: [ - babel(babelrc()), - istanbul({ - exclude: ['test/**/*', 'node_modules/**/*'] - }) + babel(babelrc()) ], external: external, targets: [ diff --git a/test/index_test.js b/test/index_test.js index 13cc432..e041bfc 100644 --- a/test/index_test.js +++ b/test/index_test.js @@ -1,4 +1,4 @@ -import { multiply } from '../'; +import { multiply } from '../lib/index.js'; import { strictEqual } from 'assert'; describe('multiply', () => { diff --git a/test/istanbul.reporter.js b/test/istanbul.reporter.js deleted file mode 100644 index 5022b61..0000000 --- a/test/istanbul.reporter.js +++ /dev/null @@ -1,17 +0,0 @@ -const instanbul = require('istanbul'); -const MochaSpecReporter = require('mocha/lib/reporters/spec'); - -module.exports = function (runner) { - const collector = new instanbul.Collector(); - const reporter = new instanbul.Reporter(); - reporter.addAll(['lcov', 'json']); - new MochaSpecReporter(runner); - - runner.on('end', function () { - collector.add(global.__coverage__); - - reporter.write(collector, true, function () { - process.stdout.write('report generated'); - }); - }); -}; diff --git a/test/mocha.opts b/test/mocha.opts deleted file mode 100644 index d17f083..0000000 --- a/test/mocha.opts +++ /dev/null @@ -1 +0,0 @@ ---reporter test/istanbul.reporter.js --recursive --compilers js:babel-register From 74b0bf3ac7ba4345bfc0d331355467e5dc20b38f Mon Sep 17 00:00:00 2001 From: Corin Lawson Date: Sun, 5 Feb 2017 13:17:45 +1100 Subject: [PATCH 2/2] Add Uglify. --- index.html | 2 +- package.json | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index b5b97c3..ebbbe84 100644 --- a/index.html +++ b/index.html @@ -6,7 +6,7 @@
- +
diff --git a/package.json b/package.json index 300c7c8..cc749a3 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "watch": "rollup -c -w", "pretest": "npm run build", "test": "karma start", - "prepublish": "npm test", + "preuglify": "npm test", + "uglify": "cd dist && uglifyjs rollup-starter-project.js --output rollup-starter-project.min.js --source-map rollup-starter-project.min.js.map --in-source-map rollup-starter-project.js.map --compress --mangle", + "prepublish": "npm run uglify", "prestart": "npm install", "start": "ws" }, @@ -54,6 +56,7 @@ "rollup-plugin-node-builtins": "^2.0.0", "rollup-plugin-node-globals": "^1.1.0", "rollup-plugin-node-resolve": "^2.0.0", - "rollup-watch": "^2.5.0" + "rollup-watch": "^2.5.0", + "uglify-js": "^2.7.5" } }