From 5baf00a35b062b7fb9ae4e71b144bbcb38c4fbe3 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 23 Jan 2018 14:51:38 -0500 Subject: [PATCH 1/4] Add a proper test harness (via Jest) --- package.json | 14 ++++++++--- test/demo.test.js | 31 ++++++++++++++++++++++++ test/{ => fixtures}/demo/src/index.js | 0 test/{ => fixtures}/demo/src/two.js | 0 test/{ => fixtures}/ts-demo/src/car.ts | 0 test/{ => fixtures}/ts-demo/src/index.ts | 0 test/lib/util.js | 3 +++ test/ts-demo.test.js | 31 ++++++++++++++++++++++++ test/ts-demo/package.json | 3 --- 9 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 test/demo.test.js rename test/{ => fixtures}/demo/src/index.js (100%) rename test/{ => fixtures}/demo/src/two.js (100%) rename test/{ => fixtures}/ts-demo/src/car.ts (100%) rename test/{ => fixtures}/ts-demo/src/index.ts (100%) create mode 100644 test/lib/util.js create mode 100644 test/ts-demo.test.js delete mode 100644 test/ts-demo/package.json diff --git a/package.json b/package.json index 852e13e..75c2ea3 100644 --- a/package.json +++ b/package.json @@ -12,15 +12,18 @@ "prepare": "npm run -s build", "prepare:babel": "babel --presets env src/*.js -d dist && npm t", "lint": "eslint src", - "test:build": "node dist/cli.js --no-compress --cwd test/demo", - "test:build:ts": "node dist/cli.js --no-compress --cwd test/ts-demo --entry=src/index.ts", - "test": "npm run -s lint && npm run -s build && npm run -s test:build && npm run -s test:build:ts", + "test": "npm run -s lint && npm run -s build && jest", "release": "npm run -s prepare && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" }, "repository": "developit/microbundle", "eslintConfig": { "extends": "eslint-config-developit" }, + "babel": { + "presets": [ + "env" + ] + }, "keywords": [ "bundle", "rollup", @@ -66,6 +69,9 @@ "babel-cli": "^6.26.0", "babel-preset-env": "^1.6.1", "eslint": "^4.15.0", - "eslint-config-developit": "^1.1.1" + "eslint-config-developit": "^1.1.1", + "fs-extra": "^5.0.0", + "jest": "^22.1.4", + "strip-ansi": "^4.0.0" } } diff --git a/test/demo.test.js b/test/demo.test.js new file mode 100644 index 0000000..4365d22 --- /dev/null +++ b/test/demo.test.js @@ -0,0 +1,31 @@ +import path from 'path'; +import fs from 'fs-extra'; +import { strip } from './lib/util'; +import microbundle from '../src/index'; + +describe('demo', () => { + it('should produce build files', async () => { + let output = await microbundle({ + cwd: path.resolve(__dirname, 'fixtures/demo'), + formats: 'es,cjs,umd' + }); + + expect(strip(output)).toEqual(strip(` + Build output to dist: + 225 B: demo.js + 225 B: demo.m.js + 295 B: demo.umd.js + `)); + + let dist = await fs.readdir(path.resolve(__dirname, 'fixtures/demo/dist')); + + expect(dist).toEqual([ + 'demo.js', + 'demo.js.map', + 'demo.m.js', + 'demo.m.js.map', + 'demo.umd.js', + 'demo.umd.js.map' + ]); + }); +}); diff --git a/test/demo/src/index.js b/test/fixtures/demo/src/index.js similarity index 100% rename from test/demo/src/index.js rename to test/fixtures/demo/src/index.js diff --git a/test/demo/src/two.js b/test/fixtures/demo/src/two.js similarity index 100% rename from test/demo/src/two.js rename to test/fixtures/demo/src/two.js diff --git a/test/ts-demo/src/car.ts b/test/fixtures/ts-demo/src/car.ts similarity index 100% rename from test/ts-demo/src/car.ts rename to test/fixtures/ts-demo/src/car.ts diff --git a/test/ts-demo/src/index.ts b/test/fixtures/ts-demo/src/index.ts similarity index 100% rename from test/ts-demo/src/index.ts rename to test/fixtures/ts-demo/src/index.ts diff --git a/test/lib/util.js b/test/lib/util.js new file mode 100644 index 0000000..46debd9 --- /dev/null +++ b/test/lib/util.js @@ -0,0 +1,3 @@ +import stripAnsi from 'strip-ansi'; + +export const strip = s => stripAnsi(s).replace(/(?:^[\n\s]+|[\n\s]+$|(^|\n)\s+)/gm, '$1'); diff --git a/test/ts-demo.test.js b/test/ts-demo.test.js new file mode 100644 index 0000000..445a027 --- /dev/null +++ b/test/ts-demo.test.js @@ -0,0 +1,31 @@ +import path from 'path'; +import fs from 'fs-extra'; +import { strip } from './lib/util'; +import microbundle from '../src/index'; + +describe('ts-demo', () => { + it('should produce build files', async () => { + let output = await microbundle({ + cwd: path.resolve(__dirname, 'fixtures/ts-demo'), + formats: 'es,cjs,umd' + }); + + expect(strip(output)).toEqual(strip(` + Build output to dist: + 106 B: ts-demo.js + 106 B: ts-demo.m.js + 175 B: ts-demo.umd.js + `)); + + let dist = await fs.readdir(path.resolve(__dirname, 'fixtures/ts-demo/dist')); + + expect(dist).toEqual([ + 'ts-demo.js', + 'ts-demo.js.map', + 'ts-demo.m.js', + 'ts-demo.m.js.map', + 'ts-demo.umd.js', + 'ts-demo.umd.js.map' + ]); + }); +}); diff --git a/test/ts-demo/package.json b/test/ts-demo/package.json deleted file mode 100644 index 3a020c0..0000000 --- a/test/ts-demo/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "name": "ts-demo" -} From 29c6621fab7bba97d16158e2077b1bf12ef43061 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 23 Jan 2018 14:52:07 -0500 Subject: [PATCH 2/4] Don't bother avoiding importing `acorn-jsx` (likely cause of [this failure](https://travis-ci.org/developit/karmatic/builds/332444062)) --- src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index effcde5..42031ef 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ import promisify from 'es6-promisify'; import glob from 'glob'; import autoprefixer from 'autoprefixer'; import { rollup, watch } from 'rollup'; +import acornJsx from 'acorn-jsx'; import nodent from 'rollup-plugin-nodent'; import commonjs from 'rollup-plugin-commonjs'; import nodeResolve from 'rollup-plugin-node-resolve'; @@ -217,7 +218,7 @@ function createConfig(options, entry, format, writeMeta) { }, parser: { plugins: { - jsx: require('acorn-jsx') + jsx: acornJsx } } }), From 7db4d5c2e7ff3ccfe63325336c6dd53b244ca674 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 23 Jan 2018 14:52:29 -0500 Subject: [PATCH 3/4] Don't warn about missing "name" field in `package.json` if we just warned about a missing package.json. --- src/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 42031ef..27003c3 100644 --- a/src/index.js +++ b/src/index.js @@ -34,21 +34,25 @@ const WATCH_OPTS = { }; export default async function microbundle(options) { - let cwd = options.cwd = resolve(process.cwd(), options.cwd); + let cwd = options.cwd = resolve(process.cwd(), options.cwd), + hasPackageJson = true; try { options.pkg = JSON.parse(await readFile(resolve(cwd, 'package.json'), 'utf8')); } catch (err) { - console.warn(chalk.yellow(`${chalk.yellow.inverse('WARN')} no package.json found.`)); + process.stderr.write(chalk.yellow(`${chalk.yellow.inverse('WARN')} no package.json found. Assuming a name of "${basename(options.cwd)}".`)+'\n'); let msg = String(err.message || err); if (!msg.match(/ENOENT/)) console.warn(` ${chalk.red.dim(msg)}`); options.pkg = {}; + hasPackageJson = false; } if (!options.pkg.name) { options.pkg.name = basename(options.cwd); - console.warn(chalk.yellow(`${chalk.yellow.inverse('WARN')} missing package.json "name" field. Assuming "${options.pkg.name}".`)); + if (hasPackageJson) { + process.stderr.write(chalk.yellow(`${chalk.yellow.inverse('WARN')} missing package.json "name" field. Assuming "${options.pkg.name}".`)+'\n'); + } } options.input = []; From 236fa3f4de808c1980c61a697e2d16cb2ffd6333 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 23 Jan 2018 14:52:55 -0500 Subject: [PATCH 4/4] Automatically detect and use `index.ts` & `src/index.ts` entry rather than requiring it to be manually specified. --- src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 27003c3..b395106 100644 --- a/src/index.js +++ b/src/index.js @@ -55,9 +55,11 @@ export default async function microbundle(options) { } } + const jsOrTs = async filename => resolve(cwd, `${filename}${await isFile(resolve(cwd, filename+'.ts')) ? '.ts' : '.js'}`); + options.input = []; [].concat( - options.entries && options.entries.length ? options.entries : options.pkg.source || (await isDir(resolve(cwd, 'src')) && 'src/index.js') || (await isFile(resolve(cwd, 'index.js')) && 'index.js') || options.pkg.module + options.entries && options.entries.length ? options.entries : options.pkg.source || (await isDir(resolve(cwd, 'src')) && await jsOrTs('src/index')) || await jsOrTs('index') || options.pkg.module ).map( file => glob.sync(resolve(cwd, file)) ).forEach( file => options.input.push(...file) ); let main = resolve(cwd, options.output || options.pkg.main || 'dist');