Skip to content

Commit 2c77729

Browse files
authored
Merge pull request #1144 from izaera/LPS-188020
feat(npm-script): generate __liferay__/lang.json file
2 parents 45257ea + e694826 commit 2c77729

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

projects/npm-tools/packages/npm-scripts/src/utils/getMainWebpackConfig.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ const path = require('path');
77
const TerserPlugin = require('terser-webpack-plugin');
88

99
const convertImportsToExternals = require('./convertImportsToExternals');
10+
const getBndWebContextPath = require('./getBndWebContextPath');
1011

1112
module.exports = function getMainWebpackConfig(
1213
projectDir,
1314
buildConfig,
14-
babelConfig
15+
babelConfig,
16+
langKeys = {}
1517
) {
1618
if (!buildConfig.main) {
1719
return;
@@ -22,6 +24,14 @@ module.exports = function getMainWebpackConfig(
2224
const {imports} = buildConfig;
2325
const externals = convertImportsToExternals(imports, 2);
2426

27+
// Add language resources as externals
28+
29+
const languageResourcesModule = `@liferay/lang${getBndWebContextPath(
30+
projectDir
31+
)}.js`;
32+
33+
externals[languageResourcesModule] = languageResourcesModule;
34+
2535
const webpackConfig = {
2636
entry: {
2737
[`__liferay__/index`]: {
@@ -38,10 +48,19 @@ module.exports = function getMainWebpackConfig(
3848
{
3949
exclude: /node_modules/,
4050
test: /\.jsx?$/,
41-
use: {
42-
loader: require.resolve('babel-loader'),
43-
options: babelConfig,
44-
},
51+
use: [
52+
{
53+
loader: require.resolve('babel-loader'),
54+
options: babelConfig,
55+
},
56+
{
57+
loader: require.resolve('./webpackLanguageLoader'),
58+
options: {
59+
langKeys,
60+
projectDir,
61+
},
62+
},
63+
],
4564
},
4665
{
4766
exclude: /node_modules/,
@@ -59,10 +78,19 @@ module.exports = function getMainWebpackConfig(
5978
{
6079
exclude: /node_modules/,
6180
test: /\.tsx?/,
62-
use: {
63-
loader: require.resolve('babel-loader'),
64-
options: babelConfig,
65-
},
81+
use: [
82+
{
83+
loader: require.resolve('babel-loader'),
84+
options: babelConfig,
85+
},
86+
{
87+
loader: require.resolve('./webpackLanguageLoader'),
88+
options: {
89+
langKeys,
90+
projectDir,
91+
},
92+
},
93+
],
6694
},
6795
],
6896
},

projects/npm-tools/packages/npm-scripts/src/utils/runWebpackAsBundler.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
/* eslint-disable @liferay/no-dynamic-require */
77

8+
const fs = require('fs');
9+
const path = require('path');
10+
811
const createTempFile = require('./createTempFile');
912
const getExternalExportsWebpackConfigs = require('./getExternalExportsWebpackConfigs');
1013
const getInternalExportsWebpackConfigs = require('./getInternalExportsWebpackConfigs');
@@ -21,10 +24,13 @@ module.exports = async function runWebpackAsBundler(
2124
) {
2225
const start = Date.now();
2326

27+
const langKeys = {};
28+
2429
const mainWebpackConfig = getMainWebpackConfig(
2530
projectDir,
2631
buildConfig,
27-
babelConfig
32+
babelConfig,
33+
langKeys
2834
);
2935

3036
if (mainWebpackConfig) {
@@ -70,6 +76,16 @@ module.exports = async function runWebpackAsBundler(
7076
await runWebpack(webpackConfig, buildConfig.report);
7177
}
7278

79+
// Write lang.json
80+
81+
fs.writeFileSync(
82+
path.join(buildConfig.output, '__liferay__', 'lang.json'),
83+
JSON.stringify({
84+
keys: Object.keys(langKeys),
85+
}),
86+
'utf-8'
87+
);
88+
7389
const lapse = Math.floor((Date.now() - start) / 1000);
7490

7591
/* eslint-disable-next-line no-console */
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* SPDX-FileCopyrightText: © 2019 Liferay, Inc. <https://liferay.com>
3+
* SPDX-License-Identifier: BSD-3-Clause
4+
*/
5+
6+
/* eslint-disable @liferay/no-dynamic-require */
7+
8+
const getBndWebContextPath = require('./getBndWebContextPath');
9+
10+
const regexp = /Liferay\s*\.\s*Language\s*\.\s*get\s*\(\s*["']([^)]+)["']\s*\)/g;
11+
12+
module.exports = function (content) {
13+
const {langKeys, projectDir} = this.getOptions();
14+
15+
let matched = false;
16+
let matches;
17+
18+
while ((matches = regexp.exec(content))) {
19+
matched = true;
20+
21+
for (let i = 1; i < matches.length; i++) {
22+
langKeys[matches[i]] = true;
23+
}
24+
}
25+
26+
return matched
27+
? `
28+
import '@liferay/lang${getBndWebContextPath(projectDir)}.js';
29+
${content}`
30+
: content;
31+
};

0 commit comments

Comments
 (0)