Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c4fa07d

Browse files
100% test coverage and improved docs.
1 parent b072f8c commit c4fa07d

File tree

9 files changed

+1294
-695
lines changed

9 files changed

+1294
-695
lines changed

‎.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,3 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38-
39-
tests/output/*

‎README.md

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,53 @@
1-
<h1 align="center">Laravel localization Loader</h1>
2-
<p align="center">Laravel localization Loader for Webpack. Convert Laravel Translation files (php or json) to JavaScript Objects.</p>
1+
<div align=center>
2+
3+
# Laravel localization Loader
4+
5+
Laravel localization Loader for Webpack.
6+
7+
Convert Laravel Translation files (php or json) to JavaScript Objects.
8+
9+
## Features
10+
11+
- Support both Laravel PHP and JSON translation files.
12+
- 100% test coverage.
13+
- Only has two dependencies: [json-loader](https://github.com/webpack-contrib/json-loader) and [php-array-loader](https://github.com/rmariuzzo/php-array-loader).
314

415
## Installation
516

617
```shell
7-
npm install laravel-localization-loader
18+
npm install laravel-localization-loader --save-dev
19+
```
20+
21+
or
22+
23+
```shell
24+
yarn add laravel-localization-loader --dev
825
```
926

1027
## Configuration
1128

12-
### Webpack 2
29+
### Webpack 2+
1330

1431
```js
1532
// webpack.config.js
1633
module.exports = {
17-
// ...
1834
module: {
1935
rules: [
2036
{
21-
// Matches all PHP files in `resources/lang` directory.
37+
// Matches all PHP or JSON files in `resources/lang` directory.
2238
test: /resources\/lang.+\.(php|json)$/,
2339
loader: 'laravel-localization-loader',
2440
}
2541
]
26-
},
27-
// ...
42+
}
2843
}
2944
```
3045

31-
## Usage with Lang.js
46+
## Usage
47+
48+
### Lang.js
49+
50+
First, you will need to install [Lang.js](https://github.com/rmariuzzo/Lang.js) then you may want to create a `messages.js` files that look as follow:
3251

3352
```js
3453
// messages.js
@@ -41,6 +60,8 @@ export default {
4160
}
4261
```
4362

63+
Then somewhere else in your awesome app:
64+
4465
```js
4566
// page.js
4667
import Lang from 'lang.js'
@@ -50,8 +71,35 @@ const lang = new Lang({ messages })
5071
lang.get('messages.hello')
5172
```
5273

53-
## Test
74+
Profit!
5475

55-
```shell
56-
yarn test
57-
```
76+
## Development
77+
78+
1. Clone and fork this repo.
79+
2. Install dependencies: yarn or npm install.
80+
3. [Run tests](#test).
81+
4. Prepare a pull request.
82+
83+
### Test
84+
85+
- `yarn test` – to run all tests.
86+
- `yarn test -- --watch` – to run all tests in watch mode.
87+
88+
#### Coverage
89+
90+
- `yarn test -- --coverage` – to run all tests with coverage.
91+
- `yarn test -- --coverage --watch` – to run all tests with coverage in watch mode.
92+
93+
### Publish
94+
95+
1. Bump package version: `yarn version --new-version x.x.x -m 'Version %s.'`.
96+
2. Publish to NPM registry: `npm publish`.
97+
3. Push new tag: `git push origin --tags`.
98+
99+
<div align=center>
100+
101+
Made with :heart: by [Rubens Mariuzzo](https://github.com/rmariuzzo).
102+
103+
[MIT license](LICENSE)
104+
105+
</div>

‎index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
* @private
6+
*/
7+
18
var phpArrayLoader = require('php-array-loader')
29
var jsonLoader = require('json-loader')
310

11+
/**
12+
* Module exports.
13+
*/
14+
15+
module.exports = laravelLocalizationLoader
16+
417
/**
518
* The Laravel Localization loader.
619
* @param {string} source The source contents.
720
* @return {string} The parsed contents.
821
*/
9-
var laravelLocalizationLoader = function(source) {
22+
23+
function laravelLocalizationLoader(source) {
1024
var isPHP = ~source.indexOf('<?php')
1125

1226
if (isPHP) {
@@ -15,5 +29,3 @@ var laravelLocalizationLoader = function(source) {
1529
return jsonLoader(source)
1630
}
1731
}
18-
19-
module.exports = laravelLocalizationLoader

‎jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
"verbose": true,
3+
"notify": false,
4+
"modulePathIgnorePatterns": ["<rootDir>/tests/output/"],
5+
"coveragePathIgnorePatterns": ["<rootDir>/tests/output/"]
6+
}

‎package.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
22
"name": "laravel-localization-loader",
33
"version": "1.0.3",
44
"description": "Laravel Localization loader for webpack. Convert Laravel Translation strings to JavaScript Objects.",
5-
"main": "loader/index.js",
5+
"keywords": [
6+
"laravel",
7+
"localization",
8+
"loader",
9+
"webpack",
10+
"webpack-loader",
11+
"laravel-localization"
12+
],
13+
"main": "index.js",
14+
"homepage": "https://github.com/rmariuzzo/Laravel-localization-loader",
615
"repository": "git@github.com:rmariuzzo/laravel-localization-loader.git",
716
"author": "Rubens Mariuzzo <rubens@mariuzzo.com>",
817
"license": "MIT",
918
"scripts": {
10-
"test": "jest --config tests/jest.config.json"
19+
"test": "jest"
1120
},
1221
"devDependencies": {
13-
"jest": "^19.0.2",
14-
"webpack": "^2.2.1",
22+
"jest": "^21.2.1",
23+
"tmp": "^0.0.33",
24+
"webpack": "^3.8.1",
1525
"webpack-merge": "^4.0.0"
1626
},
1727
"dependencies": {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"string": "Rubens",
3+
"number": 123,
4+
"parent": {
5+
"child": "Mariuzzo"
6+
}
7+
}

‎tests/jest.config.json

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

‎tests/loader.test.js

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,71 @@
1-
var path = require('path')
2-
var merge = require('webpack-merge')
3-
var webpack = require('webpack')
1+
'use strict'
42

5-
test('should load PHP Laravel translation file', () => {
6-
return runWebpack({
7-
entry: path.join(__dirname, './fixtures/resources/lang/en/messages.php'),
3+
/**
4+
* Test dependencies.
5+
*/
6+
7+
const tmp = require('tmp')
8+
const path = require('path')
9+
const merge = require('webpack-merge')
10+
const webpack = require('webpack')
11+
12+
/**
13+
* Test cases.
14+
*/
15+
16+
describe('laravel-localization-loader', () => {
17+
18+
let testDir
19+
20+
beforeEach(() => {
21+
testDir = tmp.dirSync({ unsafeCleanup: true })
822
})
9-
.then((result) => {
10-
expect(result).toBeDefined()
11-
expect(result).toHaveProperty('string', 'Rubens')
12-
expect(result).toHaveProperty('number', 123)
13-
expect(result).toHaveProperty('parent.child', 'Mariuzzo')
23+
24+
afterEach(() => {
25+
testDir.removeCallback()
26+
})
27+
28+
it('should load PHP Laravel translation file', () => {
29+
return runWebpack(testDir.name, {
30+
entry: path.join(__dirname, './fixtures/resources/lang/en/messages.php'),
31+
})
32+
.then((result) => {
33+
expect(result).toBeDefined()
34+
expect(result).toHaveProperty('string', 'Rubens')
35+
expect(result).toHaveProperty('number', 123)
36+
expect(result).toHaveProperty('parent.child', 'Mariuzzo')
37+
})
38+
})
39+
it('should load JSON Laravel translation file', () => {
40+
return runWebpack(testDir.name, {
41+
entry: path.join(__dirname, './fixtures/resources/lang/en/messages.json'),
42+
})
43+
.then((result) => {
44+
expect(result).toBeDefined()
45+
expect(result).toHaveProperty('string', 'Rubens')
46+
expect(result).toHaveProperty('number', 123)
47+
expect(result).toHaveProperty('parent.child', 'Mariuzzo')
48+
})
1449
})
1550
})
1651

17-
function runWebpack(config) {
52+
/**
53+
* Test utilities.
54+
*/
55+
56+
/**
57+
* Run webpack with a default configuration.
58+
* @param {Object} config Optional configuration to be merged.
59+
* @return {Promise}
60+
*/
61+
62+
function runWebpack(outputDir, config) {
1863
return new Promise((resolve, reject) => {
19-
var webpackConfig = merge({
64+
65+
// Merge default webpack configuration with any provided.
66+
const webpackConfig = merge({
2067
output: {
21-
path: path.join(__dirname,'output'),
68+
path: outputDir,
2269
filename: 'translation.js',
2370
libraryTarget: 'umd',
2471
},
@@ -37,16 +84,23 @@ function runWebpack(config) {
3784
}
3885
}, config)
3986

87+
// Run webpack.
4088
webpack(webpackConfig, (webpackError, stats) => {
41-
var error = webpackError ||
89+
90+
// Check for webpack errors.
91+
const error = webpackError ||
4292
(stats.hasErrors() && stats.compilation.errors[0]) ||
4393
(stats.hasWarnings() && stats.compilation.warnings[0])
94+
4495
if (error) {
4596
return reject(error)
4697
}
4798

48-
delete require.cache[path.resolve(__dirname, './output/translation.js')]
49-
return resolve(require('./output/translation.js'))
99+
// Remove webpack output from Node require's cache.
100+
delete require.cache[`${outputDir}/translation.js`]
101+
102+
// Resolve with the recente webpack's output.
103+
return resolve(require(`${outputDir}/translation.js`))
50104
})
51105
})
52106
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /