mirror of
https://github.com/developit/microbundle.git
synced 2026-01-25 14:06:50 +00:00
Merge pull request #57 from developit/testing-setup
Add a proper testing setup
This commit is contained in:
commit
280cb5855e
14
package.json
14
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"
|
||||
}
|
||||
}
|
||||
|
||||
17
src/index.js
17
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';
|
||||
@ -33,26 +34,32 @@ 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');
|
||||
}
|
||||
}
|
||||
|
||||
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');
|
||||
@ -217,7 +224,7 @@ function createConfig(options, entry, format, writeMeta) {
|
||||
},
|
||||
parser: {
|
||||
plugins: {
|
||||
jsx: require('acorn-jsx')
|
||||
jsx: acornJsx
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
31
test/demo.test.js
Normal file
31
test/demo.test.js
Normal file
@ -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'
|
||||
]);
|
||||
});
|
||||
});
|
||||
3
test/lib/util.js
Normal file
3
test/lib/util.js
Normal file
@ -0,0 +1,3 @@
|
||||
import stripAnsi from 'strip-ansi';
|
||||
|
||||
export const strip = s => stripAnsi(s).replace(/(?:^[\n\s]+|[\n\s]+$|(^|\n)\s+)/gm, '$1');
|
||||
31
test/ts-demo.test.js
Normal file
31
test/ts-demo.test.js
Normal file
@ -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'
|
||||
]);
|
||||
});
|
||||
});
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"name": "ts-demo"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user