Skip to content

Commit 9348cd2

Browse files
committed
feat: Extend laravel-mix with i18n.
1 parent 9e6d6e1 commit 9348cd2

File tree

7 files changed

+12431
-3053
lines changed

7 files changed

+12431
-3053
lines changed

package-lock.json

Lines changed: 12369 additions & 3051 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
"jest": "^27.5.1",
3333
"prettier": "^2.5.1",
3434
"ts-jest": "^27.1.3",
35-
"typescript": "^4.6.2"
35+
"typescript": "^4.6.2",
36+
"laravel-mix": "^6.0.43",
37+
"php-parser": "^3.1.0-beta.4"
3638
},
3739
"dependencies": {
38-
"php-parser": "^3.1.0-beta.4",
3940
"vue": "^3.2.31"
4041
},
4142
"lint-staged": {

src/mix.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import mix from 'laravel-mix';
2+
import path from 'path';
3+
import fs from 'fs';
4+
import { Component } from 'laravel-mix/src/components/Component';
5+
import { parseAll } from './loader';
6+
7+
class BeforeBuildPlugin {
8+
callback: Function;
9+
10+
constructor(callback: Function) {
11+
this.callback = callback;
12+
}
13+
14+
apply(compiler): void {
15+
compiler.hooks.compile.tap('BeforeBuildPlugin', this.callback);
16+
}
17+
}
18+
19+
mix.extend('i18n', class extends Component {
20+
langPath: string;
21+
context: any;
22+
23+
register(langPath = 'lang'): void {
24+
this.langPath = this.context.paths.rootPath + path.sep + langPath;
25+
}
26+
27+
webpackConfig(config): void {
28+
let files = [];
29+
30+
config.watchOptions = {
31+
ignored: /php_\w+\.json/,
32+
};
33+
34+
config.plugins.push(new BeforeBuildPlugin(() => {
35+
files = parseAll(this.langPath);
36+
}))
37+
38+
this.context.listen('build', () => {
39+
files.forEach(file => {
40+
if (fs.existsSync(file.path)) {
41+
fs.unlinkSync(file.path);
42+
}
43+
});
44+
});
45+
}
46+
});

test/fixtures/lang/php_en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"auth.failed":"These credentials do not match our records.","auth.password":"The provided password is incorrect."}

test/fixtures/lang/php_pt.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"auth.failed":"As credenciais indicadas não coincidem com as registadas no sistema.","auth.password":"A palavra-passe indicada está incorreta."}

test/fixtures/mix-manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/fixtures/webpack.mix.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const mix = require('laravel-mix');
2+
const { parseAll } = require('../../dist/loader');
3+
4+
5+
mix.extend('foo', ({ context }) => {
6+
console.log(parseAll(context + '/lang'));
7+
});
8+
9+
// Trigger your new plugin.
10+
mix.foo('some-value');

0 commit comments

Comments
 (0)