Skip to content

Commit d0b63f8

Browse files
author
Jaeho Lee
authored
Merge pull request #38 from mrTimofey/master
Code for Node 6+, webpack and buble dependency version
2 parents 126e591 + 12b6534 commit d0b63f8

File tree

9 files changed

+96
-136
lines changed

9 files changed

+96
-136
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules/
22
test/temp
33
*.log
4-
4+
.idea
5+
package-lock.json
6+
yarn.lock

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
*.log
22
node_modules
33
test/
4-
4+
.idea
5+
package-lock.json
6+
yarn.lock

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22

33
This package allows you to transpile ES2015 source using [buble](https://github.com/Rich-Harris/buble). See [Buble project website](https://buble.surge.sh/guide/) to check out supported features.
44

5-
note: This is almost-working prototype. Everything is immature. If you want something to be fixed, freely send PR or post issues!
6-
75
## Installation
86
```bash
9-
npm install --save-dev buble-loader buble
7+
npm i -S buble-loader buble
108
```
119
## Usage
1210

13-
Add this to your `webpack.config.js`
11+
Add something like this to your `webpack.config.js`
1412
```js
1513
module: {
16-
loaders: [
14+
rules: [
1715
{
1816
test: /\.js$/,
19-
loaders: 'buble-loader',
17+
loader: 'buble-loader',
2018
include: path.join(__dirname, 'src'),
21-
query: {
19+
options: {
2220
objectAssign: 'Object.assign'
2321
}
2422
}

index.js

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,35 @@
1-
'use strict';
2-
3-
var buble = require('buble');
4-
var path = require('path');
5-
var loaderUtils = require('loader-utils');
6-
7-
function BubleError (err) {
8-
Error.call(this);
9-
Error.captureStackTrace(this, BubleError);
10-
11-
this.name = 'BubleLoaderError';
12-
this.message = ['', err.snippet, err.message].join('\n');
13-
this.hideStack = true;
14-
this.error = err;
1+
/* eslint-disable no-invalid-this */
2+
const buble = require('buble'),
3+
path = require('path'),
4+
loaderUtils = require('loader-utils');
5+
6+
class BubleError extends Error {
7+
constructor(err) {
8+
super();
9+
Error.captureStackTrace(this, this.constructor);
10+
this.name = 'BubleLoaderError';
11+
this.message = ['', err.snippet, err.message].join('\n');
12+
this.error = err;
13+
}
1514
}
1615

17-
BubleError.prototype = Object.create(Error.prototype);
18-
BubleError.prototype.constructor = BubleError;
19-
20-
function handleError (err) {
21-
if (err.name === 'CompileError' || err.name === 'SyntaxError') {
22-
throw new BubleError(err);
23-
} else {
24-
throw err;
25-
}
26-
}
27-
28-
module.exports = function BubleLoader(source, inputSourceMap) {
29-
var loaderOptions = loaderUtils.getOptions(this);
30-
var transformed;
31-
try {
32-
transformed = buble.transform(source, Object.assign({
33-
transforms: {
34-
modules: false
35-
}
36-
}, loaderOptions));
37-
} catch (err) {
38-
handleError(err);
39-
}
40-
41-
var resourcePath = this.resourcePath;
42-
43-
transformed.map.file = resourcePath;
44-
transformed.map.sources[0] = path.relative(process.cwd(), resourcePath);
45-
transformed.map.sourceRoot = process.cwd();
46-
47-
this.cacheable && this.cacheable();
48-
this.callback(null, transformed.code, transformed.map);
16+
module.exports = function(source) {
17+
const config = loaderUtils.getOptions(this);
18+
config.transforms = Object.assign({}, config.transforms || {}, { modules: false });
19+
let output;
20+
try {
21+
output = buble.transform(source, config);
22+
}
23+
catch (err) {
24+
throw err.name === 'CompileError' || err.name === 'SyntaxError' ? new BubleError(err) : err;
25+
}
26+
27+
const resourcePath = this.resourcePath;
28+
29+
output.map.file = resourcePath;
30+
output.map.sources[0] = path.relative(process.cwd(), resourcePath);
31+
output.map.sourceRoot = process.cwd();
32+
33+
if (this.cacheable) this.cacheable();
34+
this.callback(null, output.code, output.map);
4935
};

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "buble-loader",
3-
"version": "0.4.1",
3+
"version": "0.5.0",
44
"description": "lightweight ES2015 source loader for Webpack using Buble",
55
"main": "index.js",
66
"scripts": {
@@ -16,19 +16,21 @@
1616
"webpack",
1717
"buble",
1818
"ES2015",
19-
"ES6"
19+
"ES6",
20+
"loader"
2021
],
2122
"bugs": {
2223
"url": "https://github.com/sairion/buble-loader/issues"
2324
},
25+
"dependencies": {
26+
"loader-utils": "^1.1.0"
27+
},
2428
"devDependencies": {
25-
"object-assign": "^4.0.1"
29+
"buble": "*",
30+
"webpack": "*"
2631
},
2732
"peerDependencies": {
28-
"buble": "^0.15.0",
33+
"buble": "*",
2934
"webpack": "*"
30-
},
31-
"dependencies": {
32-
"loader-utils": "^1.1.0"
3335
}
3436
}

test/fixtures/SyntaxCheck.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
{
1515
let a = 2;
1616
}
17-
console.assert(a === 1);
17+
console.assert(a === 1);
1818

1919
const b = 10;
2020
// b = 100; // throws error

test/fixtures/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var SyntaxCheck = require('./SyntaxCheck');
1+
const SyntaxCheck = require('./SyntaxCheck');
22

33
console.log(SyntaxCheck.objectDestructuring());
44
console.log(SyntaxCheck.blockScopingBindings());

test/loader-test.js

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,46 @@
1-
'use strict';
1+
const path = require('path'),
2+
webpack = require('webpack'),
3+
assert = require('assert');
24

3-
var path = require('path');
4-
var webpack = require('webpack');
5-
var assert = require('assert')
6-
7-
var tempDir = path.resolve(__dirname, './temp/');
8-
var loader = path.resolve(__dirname, '../');
9-
10-
var baseConfig = {
11-
entry: './test/fixtures/index.js',
12-
output: {
13-
path: tempDir,
14-
filename: '[id].js',
15-
},
16-
devtool: 'source-map',
17-
module: {
18-
loaders: [
19-
{
20-
test: /\.js$/,
21-
loader: loader,
22-
query: {
23-
objectAssign: 'Object.assign'
24-
},
25-
exclude: /node_modules/,
26-
},
27-
],
28-
},
5+
const baseConfig = {
6+
output: {
7+
path: path.resolve(__dirname, './temp/'),
8+
filename: '[id].js'
9+
},
10+
devtool: 'source-map',
11+
module: {
12+
rules: [
13+
{
14+
test: /\.js$/,
15+
loader: path.resolve(__dirname, '../'),
16+
options: {
17+
objectAssign: 'Object.assign'
18+
},
19+
exclude: /node_modules/
20+
}
21+
]
22+
}
2923
};
3024

31-
webpack(baseConfig, function(err, stats) {
32-
if (stats.compilation.errors.length > 0) {
33-
console.error(stats.compilation.errors[0].message);
34-
process.exit(1);
35-
}
25+
for (let mode of ['development', 'production']) {
26+
baseConfig.mode = mode;
27+
baseConfig.entry = './test/fixtures/index.js';
28+
webpack(baseConfig, function (err, stats) {
29+
if (stats.compilation.errors.length > 0) {
30+
console.error(stats.compilation.errors[0].message);
31+
process.exit(1);
32+
}
3633

37-
baseConfig.entry = './test/fixtures/errors.js';
38-
webpack(baseConfig, function(err, stats) {
39-
var niceErrorMessage = [
40-
'ERROR in ./test/fixtures/errors.js',
41-
'Module build failed: ',
42-
'1 : // double arrow?',
43-
'2 : const add = (a, b) ==> 1',
44-
' ^',
45-
'Unexpected token (2:21)'
46-
].join('\n');
47-
assert.ok(stats.toString().includes(niceErrorMessage), 'output contains a nice error msg');
48-
});
49-
});
34+
baseConfig.entry = './test/fixtures/errors.js';
35+
webpack(baseConfig, function (err, stats) {
36+
assert.ok(stats.toString().includes([
37+
'ERROR in ./test/fixtures/errors.js',
38+
'Module build failed: BubleLoaderError: ',
39+
'1 : // double arrow?',
40+
'2 : const add = (a, b) ==> 1',
41+
' ^',
42+
'Unexpected token (2:21)'
43+
].join('\n')), 'output contains a nice error msg');
44+
});
45+
});
46+
}

yarn.lock

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)