Skip to content

Commit

Permalink
feat(config): add metadataSubscribes option
Browse files Browse the repository at this point in the history
- add metadataSubscribers option as an array of context functions to call
- pass metadata to metadataSubscribers functions
  • Loading branch information
Ognian authored and cogell committed Nov 11, 2016
1 parent 20e3ea6 commit 79b7cbe
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -22,6 +22,7 @@
"babel-core": "^6.0.0",
"babel-eslint": "^7.1.0",
"babel-plugin-istanbul": "^2.0.3",
"babel-plugin-react-intl": "2.1.3",
"babel-preset-es2015": "^6.0.0",
"babel-preset-latest": "^6.16.0",
"codecov": "^1.0.1",
Expand All @@ -31,6 +32,9 @@
"eslint-plugin-babel": "^3.3.0",
"eslint-plugin-flowtype": "^2.25.0",
"nyc": "^8.3.2",
"react": "^15.1.0",
"react-intl":"^2.1.2",
"react-intl-webpack-plugin":"0.0.3",
"rimraf": "^2.4.3",
"webpack": "2.1.0-beta.22"
},
Expand Down
19 changes: 19 additions & 0 deletions src/index.js
Expand Up @@ -56,6 +56,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 @@ -64,9 +65,17 @@ 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) {
let result = {};

Expand All @@ -79,6 +88,7 @@ module.exports = function(source, inputSourceMap) {
const loaderOptions = loaderUtils.parseQuery(this.query);
const userOptions = assign({}, globalOptions, loaderOptions);
const defaultOptions = {
metadataSubscribers: [],
inputSourceMap: inputSourceMap,
sourceRoot: process.cwd(),
filename: filename,
Expand Down Expand Up @@ -107,11 +117,14 @@ 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;

this.cacheable();
const context = this;

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

result = transpile(source, options);
metadataSubscribers.map(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;
139 changes: 139 additions & 0 deletions test/metadata.test.js
@@ -0,0 +1,139 @@
"use strict";

import fs from "fs";
import path from "path";
import assign from "object-assign";
import test from "ava";
import mkdirp from "mkdirp";
import rimraf from "rimraf";
import webpack from "webpack";
import ReactIntlPlugin from "react-intl-webpack-plugin";

const cacheDir = path.resolve(__dirname, "./output/cache/cachefiles");
const outputDir = path.resolve(__dirname, "./output/metadata");
const babelLoader = path.resolve(__dirname, "../");
const globalConfig = {
entry: path.join(__dirname, "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) => {
const directory = path.join(outputDir, t.title.replace(/ /g, "_"));
t.context.directory = directory;
rimraf(directory, (err) => {
if (err) return t.end(err);
mkdirp(directory, t.end);
});
});

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

test.cb("should pass metadata code snippet", function(t) {
const config = assign({}, globalConfig, {
});

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

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

t.end();
});
});
});
});

test.cb("should not throw error ", function(t) {
const config = assign({}, globalConfig, {});

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

test.cb("should throw error ", function(t) {
const config = assign({}, globalConfig, {
entry: "./test/fixtures/metadataErr.js",
});

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

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

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

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

t.end();
});
});
});
});


0 comments on commit 79b7cbe

Please sign in to comment.