Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Dec 1, 2016
0 parents commit 30d7b68
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
git:
depth: 1
branches:
except: /^v\d/
language: node_js
node_js: node
script: npm run-script pretest && npm run-script coverage
after_script:
- npm install istanbul-coveralls
- node node_modules/.bin/istanbul-coveralls
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2016 Shinnosuke Watanabe

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# metalsmith-buble

[![NPM version](https://img.shields.io/npm/v/metalsmith-buble.svg)](https://www.npmjs.com/package/metalsmith-buble)
[![Build Status](https://travis-ci.org/shinnn/metalsmith-buble.svg?branch=master)](https://travis-ci.org/shinnn/metalsmith-buble)
[![Build status](https://ci.appveyor.com/api/projects/status/nevjeddyndcl5ubo/branch/master?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/metalsmith-buble/branch/master)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/metalsmith-buble.svg)](https://coveralls.io/r/shinnn/metalsmith-buble)
[![dependencies Status](https://david-dm.org/shinnn/metalsmith-buble/status.svg)](https://david-dm.org/shinnn/metalsmith-buble)
[![devDependencies Status](https://david-dm.org/shinnn/metalsmith-buble/dev-status.svg)](https://david-dm.org/shinnn/metalsmith-buble?type=dev)

[Bublé](https://buble.surge.sh/guide/) plugin for [Metalsmith](http://www.metalsmith.io/)

## Installation

[Use npm](https://docs.npmjs.com/cli/install):

```
npm install metalsmith-buble
```

## Usage

### [CLI](https://github.com/metalsmith/metalsmith#cli)

Add the `metalsmith-buble` field to your `metalsmith.json`.

```javascript
{
"plugins": {
"metalsmith-buble": {
"transforms": {
"modules": false,
"dangerousForOf": true
},
"sourceMap": "inline"
}
}
}
```

### [API](https://github.com/metalsmith/metalsmith#api)

```javascript
const Metalsmith = require('metalsmith');
const buble = require('metalsmith-buble');

new Metalsmith('./source')
.use(buble({
sourceMap: true
}))
.build((err, files) => {
if (err) {
throw err;
}

console.log('Completed.');
});
```

### Options

All [Bublé options](https://buble.surge.sh/guide/#using-the-javascript-api) are available except for `file` and `source` that will be automatically set.

In addition the following option is supported:

#### options.sourceMap

Value: `true`, `false` or `'inline'`
Default: `false`

* `true` generates a separate source map file with `.map` extension, for exmaple `script.js.map` along with `script.js`.
* `'inline'` appends an inline source map to the transformed file.

## License

Copyright (c) 2016 [Shinnosuke Watanabe](https://github.com/shinnn)

Licensed under [the MIT License](./LICENSE).
8 changes: 8 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
shallow_clone: true
version: '{build}'
skip_tags: true
install:
- ps: Install-Product node Stable
- npm install
build: off
test_script: node --strong_mode test.js
61 changes: 61 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*!
* metalsmith-babel | MIT (c) Shinnosuke Watanabe
* https://github.com/babel/metalsmith-babel
*/
'use strict';

const path = require('path');
const util = require('util');

const buble = require('buble');
const sourceMapToComment = require('source-map-to-comment');
const toFastProperties = require('to-fast-properties');

module.exports = function metalsmithBuble(options) {
options = Object.assign({sourceMap: false}, options);

if (typeof options.sourceMap !== 'boolean' && options.sourceMap !== 'inline') {
throw new TypeError(
'`sourceMap` option must be true, false or \'inline\', but got ' +
util.inspect(options.sourceMap) +
'.'
);
}

return function metalsmithBublePlugin(files, metalsmith) {
Object.keys(files).forEach(originalFilename => {
const ext = path.extname(originalFilename).toLowerCase();
if (ext !== '.js' && ext !== '.jsx') {
return;
}

const filename = originalFilename.replace(/\.jsx$/i, '.js');

if (originalFilename !== filename) {
files[filename] = files[originalFilename];
delete files[originalFilename];
toFastProperties(files);
}

const result = buble.transform(files[filename].contents.toString(), Object.assign({}, options, {
source: path.join(metalsmith.directory(), metalsmith.source(), originalFilename),
file: path.join(metalsmith.directory(), metalsmith.destination(), filename)
}));

if (options.sourceMap === true) {
const sourcemapPath = `${filename}.map`;
files[sourcemapPath] = {
contents: new Buffer(JSON.stringify(result.map))
};

result.code += `\n//# sourceMappingURL=${
path.relative(path.dirname(filename), sourcemapPath).replace(/\\/g, '/')
}\n`;
} else if (options.sourceMap === 'inline') {
result.code += `\n${sourceMapToComment(result.map)}`;
}

files[filename].contents = new Buffer(result.code);
});
};
};
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "metalsmith-buble",
"version": "1.0.0",
"description": "Bublé plugin for Metalsmith",
"repository": "shinnn/metalsmith-buble",
"author": "Shinnosuke Watanabe (https://github.com/shinnn)",
"scripts": {
"pretest": "eslint --fix --format=codeframe index.js test.js",
"test": "node test.js",
"coverage": "istanbul cover --print=both test.js"
},
"license": "MIT",
"files": [
"index.js"
],
"keywords": [
"metalsmith",
"metalsmith-plugin",
"buble",
"ecmascript",
"script",
"transform",
"compile",
"compiler",
"transpile",
"transpiler",
"language",
"esnext",
"future",
"harmony",
"jsx"
],
"dependencies": {
"buble": "^0.14.2",
"source-map-to-comment": "^1.1.0",
"to-fast-properties": "^1.0.2"
},
"devDependencies": {
"@shinnn/eslint-config-node": "^3.0.0",
"eslint": "^3.11.1",
"istanbul": "^0.4.5",
"metalsmith": "^2.3.0",
"tape": "^4.6.3"
},
"eslintConfig": {
"extends": "@shinnn/node"
}
}
82 changes: 82 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use strict';

const Metalsmith = require('metalsmith');
const buble = require('.');
const test = require('tape');

test('metalsmith-buble', t => {
t.plan(11);

new Metalsmith('.')
.use(buble())
.run({
'source.js': {contents: Buffer.from('() => 1')},
'non-js.txt': {contents: Buffer.from('Hi')}
}, (err, files) => {
t.strictEqual(err, null, 'should be used as a metalsmith plugin.');
t.strictEqual(
String(files['source.js'].contents),
'(function() { return 1; })',
'should transform JavaScript files.'
);
t.strictEqual(
String(files['non-js.txt'].contents),
'Hi',
'should not transform non-JavaScript files.'
);
});

new Metalsmith('.')
.use(buble({sourceMap: true}))
.run({
'dir/source.jsx': {contents: Buffer.from('<div />')}
}, (err, files) => {
t.strictEqual(err, null, 'should support JSX.');
t.strictEqual(
String(files['dir/source.js'].contents),
'React.createElement( \'div\', null )\n//# sourceMappingURL=source.js.map\n',
'should append a URL to the bottom of code when `sourceMap` option is true.'
);
t.strictEqual(
String(files['dir/source.js.map'].contents),
JSON.stringify({
version: 3,
file: 'source.js',
sources: ['../../src/dir/source.jsx'],
sourcesContent: ['<div />'],
names: [],
mappings: 'AAAA,qBAAC,CAAA,GAAG,OAAA'
}),
'should create a source map file.'
);
t.notOk(Object.keys(files).includes('dir/source.jsx'), 'should rename .jsx file to .js.');
});

new Metalsmith('.')
.use(buble({
target: {firefox: 43},
transforms: {arrow: true},
sourceMap: 'inline'
}))
.run({'☺️.js': {contents: Buffer.from('let x = y => `z`')}}, (err, files) => {
t.strictEqual(err, null, 'should support non-ASCII filename.');
t.strictEqual(
String(files['☺️.js'].contents),
`var x = function (y) { return \`z\`; }
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoi4pi677iPLmpzIiwic291cmNlcyI6WyIuLi9zcmMv4pi677iPLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImxldCB4ID0geSA9PiBgemAiXSwibmFtZXMiOlsibGV0Il0sIm1hcHBpbmdzIjoiQUFBQUEsR0FBRyxDQUFDLENBQUMsR0FBRyxVQUFBLENBQUMsQ0FBQSxDQUFDLEFBQUcsU0FBQSxDQUFDLENBQUMsQ0FBQyJ9`,
'should support `target` and `transforms` options.'
);
});

new Metalsmith('.')
.use(buble({sourceMap: false}))
.run({'FOO.JS': {contents: Buffer.from('1=a')}}, err => {
t.ok(err instanceof SyntaxError, 'should fail when Buble cannot transpile the code.');
});

t.throws(
() => buble({sourceMap: [1, 2, 3]}),
/^TypeError.* `sourceMap` option must be true, false or 'inline', but got \[ 1, 2, 3 ]\./,
'should throw a type error when it takes an invalid `sourceMap` option value.'
);
});

0 comments on commit 30d7b68

Please sign in to comment.