Skip to content

Commit

Permalink
Merge branch 'master' into 7.0
Browse files Browse the repository at this point in the history
* master:
  added metadata passing from babel to webpack (#398)
  document globalOptions (#365)
  Docs: change babel-preset-es2015 to babel-preset-env (#404) [skip ci]

# Conflicts:
#	README.md
#	package.json
#	src/index.js
  • Loading branch information
danez committed Mar 6, 2017
2 parents 74601ce + 1fbbdf3 commit 70c8c4a
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 5 deletions.
15 changes: 10 additions & 5 deletions README.md
Expand Up @@ -18,10 +18,15 @@ This package allows transpiling JavaScript files using [Babel](https://github.co
__Notes:__ Issues with the output should be reported on the babel [issue tracker](https://github.com/babel/babel/issues);

<h2 align="center">Install</h2>
## Installation

```bash
npm install --save-dev babel-loader babel-core babel-preset-es2015
yarn add babel-loader babel-core babel-preset-env webpack --dev
```

We recommend using yarn, but you can also still use npm:

```bash
npm install --save-dev babel-loader babel-core babel-preset-env webpack
```

<h2 align="center">Usage</h2>
Expand All @@ -39,7 +44,7 @@ module: {
use: {
loader: 'babel-loader',
options: {
presets: ['es2015']
presets: ['env']
}
}
}
Expand All @@ -62,7 +67,7 @@ module: {
use: {
loader: 'babel-loader',
options: {
presets: ['es2015'],
presets: ['env'],
plugins: [require('babel-plugin-transform-object-rest-spread')]
}
}
Expand Down Expand Up @@ -118,7 +123,7 @@ rules: [
use: {
loader: 'babel-loader',
options: {
presets: ['es2015'],
presets: ['env'],
plugins: ['transform-runtime']
}
}
Expand Down
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -33,6 +33,10 @@
"eslint-plugin-flowtype": "^2.25.0",
"nyc": "^10.0.0",
"rimraf": "^2.4.3",
"react": "^15.1.0",
"react-intl":"^2.1.2",
"babel-plugin-react-intl": "^2.1.3",
"react-intl-webpack-plugin":"^0.0.3",
"webpack": "^2.2.0"
},
"scripts": {
Expand Down
19 changes: 19 additions & 0 deletions src/index.js
Expand Up @@ -66,6 +66,7 @@ const transpile = function(source, options) {
}
const code = result.code;
const map = result.map;
const metadata = result.metadata;

if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
map.sourcesContent = [source];
Expand All @@ -76,9 +77,16 @@ const transpile = function(source, options) {
return {
code: code,
map: map,
metadata: metadata,
};
};

function passMetadata(s, context, metadata) {
if (context[s]) {
context[s](metadata);
}
}

module.exports = function(source, inputSourceMap) {
// Handle filenames (#106)
const webpackRemainingChain = loaderUtils.getRemainingRequest(this).split("!");
Expand All @@ -87,6 +95,7 @@ module.exports = function(source, inputSourceMap) {
// Handle options
const loaderOptions = loaderUtils.getOptions(this);
const defaultOptions = {
metadataSubscribers: [],
inputSourceMap: inputSourceMap,
sourceRoot: process.cwd(),
filename: filename,
Expand Down Expand Up @@ -115,9 +124,13 @@ module.exports = function(source, inputSourceMap) {

const cacheDirectory = options.cacheDirectory;
const cacheIdentifier = options.cacheIdentifier;
const metadataSubscribers = options.metadataSubscribers;

delete options.cacheDirectory;
delete options.cacheIdentifier;
delete options.metadataSubscribers;

const context = this;

if (cacheDirectory) {
const callback = this.async();
Expand All @@ -129,10 +142,16 @@ module.exports = function(source, inputSourceMap) {
transform: transpile,
}, function(err, result) {
if (err) { return callback(err); }
metadataSubscribers.forEach(function (s) {
passMetadata(s, context, result.metadata);
});
return callback(null, result.code, result.map);
});
}

const result = transpile(source, options);
metadataSubscribers.forEach(function (s) {
passMetadata(s, context, result.metadata);
});
this.callback(null, result.code, result.map);
};
15 changes: 15 additions & 0 deletions test/fixtures/metadata.js
@@ -0,0 +1,15 @@
import {defineMessages} from 'react-intl';
class App {
constructor(arg='test') {
var m = defineMessages({
greeting: {
id: 'greetingId',
defaultMessage: 'Hello World!'
},
});

this.result = arg;
}
}

export default App;
16 changes: 16 additions & 0 deletions test/fixtures/metadataErr.js
@@ -0,0 +1,16 @@
import {defineMessages} from 'react-intl';
class App {
constructor(arg='test') {
var m = defineMessages({
greeting: {
id: 'greetingId',
defaultMessage: 'Hello World!'
},
});

bla bla
this.result = arg;
}
}

export default App;
154 changes: 154 additions & 0 deletions test/metadata.test.js
@@ -0,0 +1,154 @@
import test from "ava";
import fs from "fs";
import path from "path";
import assign from "object-assign";
import rimraf from "rimraf";
import webpack from "webpack";
import createTestDirectory from "./helpers/createTestDirectory";

const ReactIntlPlugin = require("react-intl-webpack-plugin");

const cacheDir = path.join(__dirname, "output/cache/cachefiles");
const outputDir = path.join(__dirname, "output/metadata");
const babelLoader = path.join(__dirname, "../lib");
const globalConfig = {
entry: "./test/fixtures/metadata.js",
output: {
path: outputDir,
filename: "[id].metadata.js",
},
plugins: [new ReactIntlPlugin(), ],
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
query: {
metadataSubscribers: [ReactIntlPlugin.metadataContextFunctionName],
plugins: [
["react-intl", { enforceDescriptions: false, }, ],
],
presets: [],
},
exclude: /node_modules/,
},
],
},
};

// Create a separate directory for each test so that the tests
// can run in parallel
test.cb.beforeEach((t) => {
createTestDirectory(outputDir, t.title, (err, directory) => {
if (err) return t.end(err);
t.context.directory = directory;
t.end();
});
});

test.cb.afterEach((t) => rimraf(t.context.directory, t.end));


test.cb("should pass metadata code snippet", (t) => {
const config = assign({}, globalConfig, {
output: {
path: t.context.directory,
filename: "[id].metadata.js",
},
});

webpack(config, (err) => {
t.is(err, null);

fs.readdir(t.context.directory, (err, files) => {
t.is(err, null);
t.true(files.length > 0);
fs.readFile(path.resolve(t.context.directory, "reactIntlMessages.json"),
function(err, data) {
t.is(err, null);
const text = data.toString();
const jsonText = JSON.parse(text);
t.true(jsonText.length == 1);
t.true(jsonText[0].id == "greetingId");
t.true(jsonText[0].defaultMessage == "Hello World!");
t.end();
});
});
});
});

test.cb("should not throw error", (t) => {
const config = assign({}, globalConfig, {
output: {
path: t.context.directory,
filename: "[id].metadata.js",
},
});

webpack(config, (err, stats) => {
t.is(err, null);
t.is(stats.compilation.errors.length, 0);
t.end();
});
});

test.cb("should throw error", (t) => {
const config = assign({}, globalConfig, {
output: {
path: t.context.directory,
filename: "[id].metadata.js",
},
entry: "./test/fixtures/metadataErr.js",
});

webpack(config, (err, stats) => {
t.is(err, null);
t.true(stats.compilation.errors.length > 0);
t.end();
});
});

test.cb("should pass metadata code snippet ( cache version )", (t) => {
const config = assign({}, globalConfig, {
output: {
path: t.context.directory,
filename: "[id].metadata.js",
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
query: {
metadataSubscribers: [ReactIntlPlugin.metadataContextFunctionName],
plugins: [
["react-intl", { enforceDescriptions: false, }, ],
],
cacheDirectory: cacheDir,
presets: [],
},
exclude: /node_modules/,
},
],
},
});

webpack(config, (err) => {
t.is(err, null);

fs.readdir(t.context.directory, (err, files) => {
t.is(err, null);
t.true(files.length > 0);
fs.readFile(path.resolve(t.context.directory, "reactIntlMessages.json"),
function(err, data) {
t.is(err, null);
const text = data.toString();
const jsonText = JSON.parse(text);
t.true(jsonText.length == 1);
t.true(jsonText[0].id == "greetingId");
t.true(jsonText[0].defaultMessage == "Hello World!");
t.end();
});
});
});
});

0 comments on commit 70c8c4a

Please sign in to comment.