Skip to content

Commit

Permalink
Merge pull request #5 from jstrimpel/master
Browse files Browse the repository at this point in the history
make local file extension optional
  • Loading branch information
iamakulov authored May 21, 2018
2 parents 974854e + 8a48df4 commit 9b35782
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function MomentLocalesPlugin(options) {

if (localesToKeep.length > 0) {
var regExpPatterns = localesToKeep.map(function(localeName) {
return localeName + '\\.js';
return localeName + '(\\.js)?';
});

return new ContextReplacementPlugin(
Expand Down
54 changes: 54 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const MomentLocalesPlugin = require('./index.js');

test('returns a ContextReplacementPlugin instance when locales are passed', () => {
const result = new MomentLocalesPlugin({
localesToKeep: ['en-gb', 'ru'],
});

expect(result.constructor.name).toEqual('ContextReplacementPlugin');
expect(result.newContentRegExp.toString()).toEqual(
'/(en-gb(\\.js)?|ru(\\.js)?)$/'
);
});

test('returns an IgnorePlugin instance when locales are not passed', () => {
const result = new MomentLocalesPlugin({
localesToKeep: [],
});

expect(result.constructor.name).toEqual('IgnorePlugin');
});

test('works when no options are passed', () => {
const result = new MomentLocalesPlugin();

expect(result.constructor.name).toEqual('IgnorePlugin');
});

test('normalizes locales to match file names', () => {
const result = new MomentLocalesPlugin({
localesToKeep: ['en-gb-foo'],
});

expect(result.newContentRegExp.toString()).toMatch(/en-gb(\\.js)?/);
});

describe('validation', () => {
test('throws when an unknown option is passed', () => {
expect(() => new MomentLocalesPlugin({ foo: 'bar' })).toThrow(
/unknown option/
);
});

test('throws when not an array is passed into `localesToKeep`', () => {
expect(() => new MomentLocalesPlugin({ localesToKeep: {} })).toThrow(
/Expected .+ option to be an array, received/
);
});

test('throws when an invalid locale is passed into `localesToKeep`', () => {
expect(
() => new MomentLocalesPlugin({ localesToKeep: ['foo-bar'] })
).toThrow(/Moment.js doesn’t include/);
});
});

0 comments on commit 9b35782

Please sign in to comment.