Skip to content

Commit ad7396b

Browse files
authored
Merge pull request #701 from izaera/IFI-2730
refactor: move common platform code projects to JS Toolkit
2 parents ea46ed6 + dd30f4d commit ad7396b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1050
-656
lines changed

projects/js-toolkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"preversion": "echo Cannot version private package; false",
1313
"qa": "node scripts/qa/index.js",
1414
"test": "liferay-workspace-scripts test",
15-
"watch": "node scripts/watch.js"
15+
"watch": "node scripts/watch.js --all"
1616
},
1717
"version": "0.1.0"
1818
}

projects/js-toolkit/packages/dev-server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
"url": "https://github.com/liferay/liferay-frontend-projects.git"
2222
},
2323
"scripts": {
24-
"build": "tsc",
24+
"build": "node ../../scripts/build.js",
2525
"ci": "liferay-workspace-scripts ci",
26+
"clean": "node ../../scripts/clean.js",
2627
"format": "liferay-workspace-scripts format",
2728
"format:check": "liferay-workspace-scripts format:check",
2829
"lint": "liferay-workspace-scripts lint",

projects/js-toolkit/packages/dev-server/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4+
"lib": ["ES2015", "DOM"],
45
"outDir": "./lib",
56
"rootDir": "./src"
67
},
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"author": "Liferay Frontend Infrastructure Team <pt-frontend-infrastructure@liferay.com>",
3+
"dependencies": {
4+
"@liferay/portal-base": "^1.0.0",
5+
"cross-spawn": "^7.0.0",
6+
"escape-string-regexp": "^2.0.0",
7+
"globby": "^10.0.1"
8+
},
9+
"description": "Base library for adaptation autopresets",
10+
"license": "LGPL-3.0",
11+
"main": "lib/index.js",
12+
"name": "@liferay/portal-adapt-base",
13+
"repository": {
14+
"directory": "projects/js-toolkit/packages/portal-adapt-base",
15+
"type": "git",
16+
"url": "https://github.com/liferay/liferay-frontend-projects.git"
17+
},
18+
"scripts": {
19+
"build": "node ../../scripts/build.js",
20+
"ci": "liferay-workspace-scripts ci",
21+
"clean": "node ../../scripts/clean.js",
22+
"format": "liferay-workspace-scripts format",
23+
"format:check": "liferay-workspace-scripts format:check",
24+
"lint": "liferay-workspace-scripts lint",
25+
"lint:fix": "liferay-workspace-scripts lint:fix",
26+
"postversion": "liferay-workspace-scripts publish",
27+
"prepublishOnly": "yarn build",
28+
"preversion": "liferay-workspace-scripts ci",
29+
"test": "liferay-workspace-scripts test"
30+
},
31+
"version": "1.0.0"
32+
}

target-platforms/packages/portal-adapt-base/src/build.js renamed to projects/js-toolkit/packages/portal-adapt-base/src/build.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,37 @@
33
* SPDX-License-Identifier: LGPL-3.0-or-later
44
*/
55

6-
const {
7-
FilePath,
8-
TemplateRenderer,
9-
format,
10-
} = require('@liferay/js-toolkit-core');
11-
const project = require('liferay-npm-build-tools-common/lib/project');
6+
import {FilePath, TemplateRenderer, format} from '@liferay/js-toolkit-core';
7+
import * as project from 'liferay-npm-build-tools-common/lib/project';
128

13-
const runNodeModulesBin = require('./util/runNodeModulesBin');
14-
const runPkgJsonScript = require('./util/runPkgJsonScript');
9+
import runNodeModulesBin from './util/runNodeModulesBin';
10+
import runPkgJsonScript from './util/runPkgJsonScript';
1511

1612
const {fail, info, print} = format;
17-
const {ProjectType} = project;
13+
const {ANGULAR_CLI, CREATE_REACT_APP, VUE_CLI} = project.ProjectType;
1814

1915
const templatesDir = new FilePath(__dirname).join('templates');
2016

21-
module.exports = function build() {
17+
export default async function build(): Promise<void> {
2218
switch (project.default.probe.type) {
23-
case ProjectType.ANGULAR_CLI:
24-
buildWith('build');
19+
case ANGULAR_CLI:
20+
await buildWith('build');
2521
break;
2622

27-
case ProjectType.CREATE_REACT_APP:
28-
buildWith('build');
23+
case CREATE_REACT_APP:
24+
await buildWith('build');
2925
break;
3026

31-
case ProjectType.VUE_CLI:
32-
buildWith('build', ['--prod=true']);
27+
case VUE_CLI:
28+
await buildWith('build', ['--prod=true']);
3329
break;
3430

3531
default:
3632
failWithUnsupportedProjectType();
3733
}
38-
};
34+
}
3935

40-
/**
41-
* @param {string} script
42-
* @param {Array<*>} args
43-
*/
44-
async function buildWith(script, args = []) {
36+
async function buildWith(script: string, args: string[] = []): Promise<void> {
4537
runPkgJsonScript(project.default, script, args);
4638

4739
const renderer = new TemplateRenderer(
@@ -59,7 +51,7 @@ async function buildWith(script, args = []) {
5951
runNodeModulesBin(project.default, 'liferay-npm-bundler');
6052
}
6153

62-
function failWithUnsupportedProjectType() {
54+
function failWithUnsupportedProjectType(): void {
6355
print(
6456
fail`
6557
Oops! Your project type is not supported by {Liferay JS Toolkit} or cannot be
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com>
3+
* SPDX-License-Identifier: LGPL-3.0-or-later
4+
*/
5+
6+
import portalBase from '@liferay/portal-base';
7+
8+
import build from './build';
9+
10+
import type {Tasks} from '@liferay/portal-base';
11+
12+
export default function (platformPath: string, taskOverrides: Tasks): void {
13+
portalBase(platformPath, {
14+
build,
15+
...(taskOverrides || {}),
16+
});
17+
}

target-platforms/packages/portal-adapt-base/src/loader/adaptStaticURLs.js renamed to projects/js-toolkit/packages/portal-adapt-base/src/loader/adaptStaticURLs.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,34 @@
33
* SPDX-License-Identifier: LGPL-3.0-or-later
44
*/
55

6-
const {FilePath} = require('@liferay/js-toolkit-core');
7-
const escapeStringRegexp = require('escape-string-regexp');
8-
const globby = require('globby');
9-
const project = require('liferay-npm-build-tools-common/lib/project');
6+
import {FilePath} from '@liferay/js-toolkit-core';
7+
import escapeStringRegexp from 'escape-string-regexp';
8+
import globby from 'globby';
9+
import {
10+
BundlerLoaderContext,
11+
BundlerLoaderReturn,
12+
} from 'liferay-npm-build-tools-common/lib/api/loaders';
13+
import * as project from 'liferay-npm-build-tools-common/lib/project';
1014

11-
const replaceTokens = require('../util/replaceTokens');
15+
import replaceTokens from '../util/replaceTokens';
16+
17+
/**
18+
* Configuration options for `adapt-static-urls` loader
19+
*/
20+
export interface Options {
21+
22+
/** Project relative path of directory containing assets */
23+
docroot: string;
24+
25+
/** List of regexps to match assets that need their URL to be processed */
26+
include: string[] | string;
27+
28+
/** Prefix to add to file path (after 'o/${project.jar.webContextPath}/') */
29+
prefix?: string;
30+
31+
/** Prepend a / to `o/${project.jar.webContextPath}/` */
32+
prependSlash?: boolean;
33+
}
1234

1335
/**
1436
* A loader to rewrite static asset URLs inside a file (usually a webpack
@@ -21,17 +43,12 @@ const replaceTokens = require('../util/replaceTokens');
2143
* [create-jar.features.web-context](https://github.com/liferay/liferay-frontend-projects/tree/master/maintenance/projects/js-toolkit/docs/.npmbundlerrc-file-reference.md#create-jarfeaturesweb-context).
2244
* inside `.npmbundlerrc`.
2345
*
24-
* @remarks
25-
* Valid options are:
26-
*
27-
* - docroot: project relative path of directory containing assets
28-
* - include: list of regexps to match assets that need their URL to be processed
29-
* - prefix: prefix to add to file path (after 'o/${project.jar.webContextPath}/')
30-
* - prependSlash: prepend a / to `o/${project.jar.webContextPath}/`
31-
*
3246
* @deprecated use `adapt-static-urls-at-runtime.ts` instead
3347
*/
34-
module.exports = function adaptStaticURLs(context, options) {
48+
export default function adaptStaticURLs(
49+
context: BundlerLoaderContext,
50+
options: Options
51+
): BundlerLoaderReturn {
3552
const {content, log} = context;
3653
const {docroot, include, prefix = '', prependSlash = false} = replaceTokens(
3754
project.default,
@@ -82,4 +99,4 @@ module.exports = function adaptStaticURLs(context, options) {
8299
});
83100

84101
return modifiedContent;
85-
};
102+
}

target-platforms/packages/portal-adapt-base/src/loader/adaptStaticURLsAtRuntime.js renamed to projects/js-toolkit/packages/portal-adapt-base/src/loader/adaptStaticURLsAtRuntime.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@
33
* SPDX-License-Identifier: LGPL-3.0-or-later
44
*/
55

6-
const {FilePath} = require('@liferay/js-toolkit-core');
7-
const escapeStringRegexp = require('escape-string-regexp');
8-
const globby = require('globby');
9-
const project = require('liferay-npm-build-tools-common/lib/project');
6+
import {FilePath} from '@liferay/js-toolkit-core';
7+
import escapeStringRegexp from 'escape-string-regexp';
8+
import globby from 'globby';
9+
import {
10+
BundlerLoaderContext,
11+
BundlerLoaderReturn,
12+
} from 'liferay-npm-build-tools-common/lib/api/loaders';
13+
import * as project from 'liferay-npm-build-tools-common/lib/project';
1014

11-
const replaceTokens = require('../util/replaceTokens');
15+
import replaceTokens from '../util/replaceTokens';
16+
17+
/** Configuration options for `adapt-static-urls` loader */
18+
export interface Options {
19+
20+
/** Project relative path of directory containing assets */
21+
docroot: string;
22+
23+
/** List of regexps to match assets that need their URL to be processed */
24+
include: string[] | string;
25+
26+
/** Whether to look for the whole URL inside the string or just a part */
27+
matchSubstring: boolean;
28+
}
1229

1330
/**
1431
* A loader to rewrite static asset URLs inside a file (usually a webpack
@@ -20,14 +37,11 @@ const replaceTokens = require('../util/replaceTokens');
2037
* The web context path is taken from property
2138
* [create-jar.features.web-context](https://github.com/liferay/liferay-frontend-projects/tree/master/maintenance/projects/js-toolkit/docs/.npmbundlerrc-file-reference.md#create-jarfeaturesweb-context).
2239
* inside `.npmbundlerrc`.
23-
*
24-
* Valid options are:
25-
*
26-
* - docroot: project relative path of directory containing assets
27-
* - include: list of regexps to match assets that need their URL to be processed
28-
* - matchSubstring: whether to look for the whole URL inside the string or just a part
2940
*/
30-
module.exports = function adaptStaticURLsAtRuntime(context, options) {
41+
export default function adaptStaticURLsAtRuntime(
42+
context: BundlerLoaderContext,
43+
options: Options
44+
): BundlerLoaderReturn {
3145
const {content, log} = context;
3246
const {docroot, include, matchSubstring = false} = replaceTokens(
3347
project.default,
@@ -82,4 +96,4 @@ module.exports = function adaptStaticURLsAtRuntime(context, options) {
8296
});
8397

8498
return modifiedContent;
85-
};
99+
}

target-platforms/packages/portal-adapt-base/src/loader/addCSSPortletHeader.js renamed to projects/js-toolkit/packages/portal-adapt-base/src/loader/addCSSPortletHeader.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@
33
* SPDX-License-Identifier: LGPL-3.0-or-later
44
*/
55

6-
const project = require('liferay-npm-build-tools-common/lib/project');
6+
import {
7+
BundlerLoaderContext,
8+
BundlerLoaderReturn,
9+
} from 'liferay-npm-build-tools-common/lib/api/loaders';
10+
import * as project from 'liferay-npm-build-tools-common/lib/project';
711

8-
const replaceTokens = require('../util/replaceTokens');
12+
import replaceTokens from '../util/replaceTokens';
13+
14+
/** Configuration options for `add-css-portlet-header` loader */
15+
export interface Options {
16+
17+
/**
18+
* Path to the CSS file to use as `header-portlet-css` property of the
19+
* portlet.
20+
*/
21+
css: string;
22+
}
923

1024
/**
1125
* A loader that adds the `com.liferay.portlet.header-portlet-css` to the
@@ -16,14 +30,11 @@ const replaceTokens = require('../util/replaceTokens');
1630
*
1731
* It is normally used from adapted projects to inject the framework's generated
1832
* CSS file into the portlet.
19-
*
20-
* @remarks
21-
*
22-
* Valid options are:
23-
*
24-
* - css: path to the CSS file to use as `header-portlet-css` property of the portlet
2533
*/
26-
module.exports = function addCSSPortletHeader(context, options) {
34+
export default function addCSSPortletHeader(
35+
context: BundlerLoaderContext,
36+
options: Options
37+
): BundlerLoaderReturn {
2738
const {content, log} = context;
2839
const {css} = replaceTokens(project.default, options);
2940

@@ -44,4 +55,4 @@ module.exports = function addCSSPortletHeader(context, options) {
4455
log.info('add-css-portlet-header', `Added ${css} as portlet CSS file`);
4556

4657
return JSON.stringify(pkgJson, null, '\t');
47-
};
58+
}

0 commit comments

Comments
 (0)