Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit a3f069c

Browse files
authored
Merge pull request #27 from carlobeltrame/master
Enable auto-reloading in watch mode when translations change
2 parents da0dff5 + 6a2a83f commit a3f069c

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed

all.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ module.exports = function (indexContent) {
2121

2222
this.addDependency(baseDirectory);
2323

24-
const jsonContents = jsonLoader.execute(baseDirectory, options);
25-
const phpContents = phpLoader.execute(baseDirectory, options);
24+
const jsonContents = jsonLoader.execute(baseDirectory, options, this);
25+
const phpContents = phpLoader.execute(baseDirectory, options, this);
2626
const bundle = _.merge(phpContents, jsonContents);
2727

2828
return "module.exports = " + JSON.stringify(bundle);

json-loader.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ let fs = require('fs');
22
let path = require('path');
33

44
const jsonLoader = {
5-
execute(baseDirectory, options) {
5+
execute(baseDirectory, options, loader) {
66
let bundle = {};
77

88
files = fs.readdirSync(baseDirectory).filter((file) => {
@@ -11,7 +11,9 @@ const jsonLoader = {
1111

1212
files.forEach((file) => {
1313
var lang = file.replace('.json', '');
14-
var content = fs.readFileSync(path.join(baseDirectory, file));
14+
var filePath = path.join(baseDirectory, file);
15+
loader.addDependency(filePath);
16+
var content = fs.readFileSync(filePath);
1517

1618
if (typeof options.namespace !== 'undefined') {
1719
bundle[lang] = {};

json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ module.exports = function (indexContent) {
2121
this.addDependency(baseDirectory);
2222

2323
return "module.exports = " + JSON.stringify(
24-
jsonLoader.execute(baseDirectory, options)
24+
jsonLoader.execute(baseDirectory, options, this)
2525
);
2626
}

php-loader.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const phpArrayParser = require('php-array-parser');
66
const klaw = require('klaw-sync');
77

88
const phpLoader = {
9-
execute (baseDirectory, options) {
9+
execute (baseDirectory, options, loader) {
1010
var bundle = {};
1111
var directories;
1212

@@ -28,7 +28,9 @@ const phpLoader = {
2828
}).forEach((file) => {
2929
var filename = file.path.split(langDirectory + path.sep)[1];
3030
var filename = filename.replace('\\', '/');
31-
var content = fs.readFileSync(path.join(langDirectory, filename), 'utf8');
31+
var filePath = path.join(langDirectory, filename);
32+
loader.addDependency(filePath);
33+
var content = fs.readFileSync(filePath, 'utf8');
3234

3335
// Remove left part of return expression and any ending `?>`.
3436
const ret = content.indexOf('return') + 'return'.length

php.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ module.exports = function (indexContent) {
2222
this.addDependency(baseDirectory);
2323

2424
return "module.exports = " + JSON.stringify(
25-
phpLoader.execute(baseDirectory, options)
25+
phpLoader.execute(baseDirectory, options, this)
2626
);
2727
}

test/php.test.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ let path = require('path')
33
let phpLoader = require('./../php-loader')
44

55
describe('it should load php language files', function () {
6+
const loaderMock = { addDependency: () => {} };
7+
68
it('should load regular php translation', function () {
7-
let content = phpLoader.execute('./test/fixtures/php', {});
9+
let content = phpLoader.execute('./test/fixtures/php', {}, loaderMock);
810

911
assert.deepEqual(content.en, {
1012
translation: {
@@ -17,7 +19,7 @@ describe('it should load php language files', function () {
1719
});
1820

1921
it('should load both languages', function () {
20-
let content = phpLoader.execute('./test/fixtures/php', {});
22+
let content = phpLoader.execute('./test/fixtures/php', {}, loaderMock);
2123

2224
assert.deepEqual(content.en, {
2325
translation: {
@@ -41,7 +43,7 @@ describe('it should load php language files', function () {
4143
it('should be able to replace parameters', function () {
4244
let content = phpLoader.execute('./test/fixtures/php-with-parameters', {
4345
parameters: '{{ $1 }}'
44-
});
46+
}, loaderMock);
4547

4648
assert.deepEqual(content.en, {
4749
validation: {
@@ -51,7 +53,7 @@ describe('it should load php language files', function () {
5153
});
5254

5355
it('should be able to load nested folders', function () {
54-
let content = phpLoader.execute('./test/fixtures/php-with-nested-folders', {});
56+
let content = phpLoader.execute('./test/fixtures/php-with-nested-folders', {}, loaderMock);
5557

5658
assert.deepEqual(content.en, {
5759
validation: {
@@ -68,7 +70,7 @@ describe('it should load php language files', function () {
6870
let namespace = 'test';
6971
let content = phpLoader.execute('./test/fixtures/php-with-namespace', {
7072
namespace: namespace,
71-
});
73+
}, loaderMock);
7274

7375
assert.deepEqual(content.en[namespace], {
7476
validation: {
@@ -82,7 +84,7 @@ describe('it should load php language files', function () {
8284
let content = phpLoader.execute('./test/fixtures/php-with-namespace-parameters', {
8385
namespace: namespace,
8486
parameters: '{{ $1 }}'
85-
});
87+
}, loaderMock);
8688

8789
assert.deepEqual(content.en[namespace], {
8890
validation: {
@@ -92,7 +94,7 @@ describe('it should load php language files', function () {
9294
});
9395

9496
it('should not fail execution with invalid file', function () {
95-
let content = phpLoader.execute('./test/fixtures/php-with-one-invalid-file', {});
97+
let content = phpLoader.execute('./test/fixtures/php-with-one-invalid-file', {}, loaderMock);
9698

9799
assert.deepEqual(content.en, {
98100
translation: {
@@ -103,4 +105,17 @@ describe('it should load php language files', function () {
103105
}
104106
});
105107
});
108+
109+
it('should register translation files as dependencies for live reloading', function () {
110+
let dependency = '';
111+
const loaderMock = { addDependency: (dep) => {
112+
dependency = dep;
113+
} };
114+
115+
phpLoader.execute('./test/fixtures/php-with-namespace', {}, loaderMock);
116+
117+
const expected = '/test/fixtures/php-with-namespace/en/validation.php';
118+
const actual = dependency.substring(dependency.length - expected.length);
119+
assert.deepStrictEqual(actual.split(path.sep), expected.split('/'));
120+
});
106121
});

0 commit comments

Comments
 (0)