Skip to content

Commit

Permalink
wip #1817 webpack specific require.context()
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock authored and alansemenov committed Oct 16, 2023
1 parent 7faad91 commit 27bdc9d
Showing 176 changed files with 699 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .swcrc
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
},
"exclude": [
".*\\.d\\.ts$",
"/lib/"
// "/lib/" // TODO: Why was this line here?
],
"module": {
"type": "commonjs",
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -38,10 +38,13 @@ dependencies {
include "com.enonic.xp:lib-node:${xpVersion}"
include "com.enonic.xp:lib-auth:${xpVersion}"
include "com.enonic.xp:lib-i18n:${xpVersion}"
include "com.enonic.xp:lib-io:${xpVersion}"
include "com.enonic.lib:lib-admin-ui:${libAdminUiVersion}"
devResources "com.enonic.lib:lib-admin-ui:${libAdminUiVersion}"
include "com.enonic.lib:lib-graphql:2.0.1"
include "com.enonic.lib:lib-mustache:2.1.0"
include 'com.enonic.lib:lib-router:3.1.0'
include 'com.enonic.lib:lib-static:1.0.3'

testImplementation "com.enonic.xp:testing:${xpVersion}"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1'
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -10,8 +10,10 @@
"scripts": {
"build": "concurrently -c auto -g --timings npm:build:*",
"build:css": "postcss -o build/resources/main/assets/styles/main.css build/less/main.css",
"build:js": "tsup",
"build:server": "tsup -d build/resources/main",
"build:static": "tsup -d build/resources/main/static",
"check:types": "tsc --pretty --skipLibCheck --noEmit",
"clean": "del build",
"fix": "eslint --fix src/**/*.ts --cache",
"less": "lessc --source-map src/main/resources/assets/styles/main.less build/less/main.css",
"lint": "eslint src/**/*.ts --quiet --cache",
@@ -22,11 +24,19 @@
"hasher": "^1.2.0",
"jquery": "^3.7.1",
"nanoid": "^5.0.2",
"jquery-ui": "^1.13.2",
"owasp-password-strength-test": "^1.3.0",
"q": "^1.5.1"
},
"devDependencies": {
"@enonic-types/global": "^7.13.2",
"@enonic-types/lib-admin": "^7.13.2",
"@enonic-types/lib-i18n": "^7.13.2",
"@enonic-types/lib-io": "^7.13.2",
"@enonic-types/lib-portal": "^7.13.2",
"@enonic/esbuild-plugin-copy-with-hash": "^0.0.1",
"@enonic/eslint-config": "^1.2.0",
"@enonic/tsup-plugin-manifest": "^0.0.1",
"@fal-works/esbuild-plugin-global-externals": "^2.1.2",
"@swc/core": "^1.3.93",
"@types/hasher": "^0.0.33",
@@ -41,10 +51,13 @@
"concurrently": "^8.2.1",
"css-loader": "^6.8.1",
"cssnano": "^6.0.1",
"del-cli": "^5.1.0",
"enonic-admin-artifacts": "^1.8.5",
"eslint": "^8.51.0",
"esbuild-plugin-external-global": "^1.0.1",
"esbuild-plugin-globals": "^0.2.0",
"eslint": "^8.50.0",
"glob": "^10.3.4",
"less": "^4.2.0",
"less-loader": "^11.1.3",
"mini-css-extract-plugin": "^2.7.6",
4 changes: 3 additions & 1 deletion src/main/resources/admin/tools/main/main.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="{{jqueryUrl}}"></script>
<script type="text/javascript" src="{{jqueryUiUrl}}"></script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<meta name="viewport" content="width=device-width, user-scalable=no">
@@ -42,7 +44,7 @@
<script type="text/javascript" src="{{launcherPath}}" data-config-theme="dark" async></script>

<!-- App javascript -->
<script defer type="text/javascript" src="{{assetsUri}}/js/app-users-bundle.js" data-config-service-url="{{configServiceUrl}}"></script>
<script defer type="text/javascript" src="{{appUsersBundleUrl}}" data-config-service-url="{{configServiceUrl}}"></script>

</body>
</html>
29 changes: 0 additions & 29 deletions src/main/resources/admin/tools/main/main.js

This file was deleted.

71 changes: 71 additions & 0 deletions src/main/resources/admin/tools/main/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type {
Request,
Response,
} from '/types';


import {
getLauncherPath,
getLocales
} from '/lib/xp/admin';
// @ts-expect-error Cannot find module '/lib/mustache' or its corresponding type declarations.ts(2307)
import mustache from '/lib/mustache';
// @ts-expect-error Cannot find module '/lib/router' or its corresponding type declarations.ts(2307)
import Router from '/lib/router';
import {
assetUrl,
serviceUrl
} from '/lib/xp/portal';
import { localize } from '/lib/xp/i18n';
import { immutableGetter, getAdminUrl } from '/lib/urlHelper';
import {
FILEPATH_MANIFEST_CJS,
FILEPATH_MANIFEST_NODE_MODULES,
GETTER_ROOT,
} from '/constants';

const TOOL_NAME = 'main';
const VIEW = resolve('./main.html');

const router = Router();

router.all(`/${GETTER_ROOT}/{path:.+}`, (r: Request) => {
return immutableGetter(r);
});

function get(_request: Request): Response {
const params = {
appUsersBundleUrl: getAdminUrl({
path: 'main.js'
}, TOOL_NAME),
assetsUri: assetUrl({path: ''}),
appName: localize({
key: 'admin.tool.displayName',
bundles: ['i18n/phrases'],
locale: getLocales()
}),
configServiceUrl: serviceUrl({service: 'config'}),
jqueryUrl: getAdminUrl({
manifestPath: FILEPATH_MANIFEST_NODE_MODULES,
path: 'jquery/dist/jquery.min.js',
}, TOOL_NAME),
jqueryUiUrl: getAdminUrl({
manifestPath: FILEPATH_MANIFEST_NODE_MODULES,
path: 'jquery-ui/dist/jquery-ui.min.js',
}, TOOL_NAME),
launcherPath: getLauncherPath(),
};

return {
contentType: 'text/html',
body: mustache.render(VIEW, params),
headers: {
'content-security-policy': 'default-src \'self\'; script-src \'self\' \'unsafe-eval\'; style-src \'self\' \'unsafe-inline\'; object-src \'none\'; img-src \'self\' data:'
}
};
}

router.get('', (r: Request) => get(r)); // Default admin tool path
router.get('/', (r: Request) => get(r)); // Just in case someone adds a slash on the end

export const all = (r: Request) => router.dispatch(r);
4 changes: 4 additions & 0 deletions src/main/resources/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const GETTER_ROOT = 'static';
export const FILEPATH_MANIFEST_CJS = `/${GETTER_ROOT}/manifest.cjs.json`;
// export const FILEPATH_MANIFEST_ESM = `/${GETTER_ROOT}/manifest.esm.json`;
export const FILEPATH_MANIFEST_NODE_MODULES = `/${GETTER_ROOT}/node_modules-manifest.json`;
38 changes: 38 additions & 0 deletions src/main/resources/lib/ioResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
getResource,
readText
} from '/lib/xp/io';


export function readResource(filename: string) {
const resource = getResource(filename);
if (!resource || !resource.exists()) {
throw new Error(`Empty or not found: ${filename}`);
}
let content: string;
try {
content = readText(resource.getStream());
// log.debug('readResource: filename:%s content:%s', filename, content);
} catch (e) {
log.error(e.message);
throw new Error(`Couldn't read resource: ${filename}`);
}
return content;
}

function jsonParseResource(filename: string) {
const content = readResource(filename);
let obj: object;
try {
obj = JSON.parse(content);
log.debug('jsonParseResource obj:%s', JSON.stringify(obj, null, 4));
} catch (e) {
log.error(e.message);
log.info("Content dump from '" + filename + "':\n" + content);
throw new Error(`couldn't parse as JSON content of resource: ${filename}`);
}
return obj;
}


export default jsonParseResource;
9 changes: 9 additions & 0 deletions src/main/resources/lib/runMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare const Java: {
type: (_classPath: string) => {
get: () => string
}
};

export const XP_RUN_MODE = `${Java.type('com.enonic.xp.server.RunMode').get()}`; // PROD || DEV
export const IS_DEV_MODE = XP_RUN_MODE === 'DEV';
export const IS_PROD_MODE = XP_RUN_MODE === 'PROD';
70 changes: 70 additions & 0 deletions src/main/resources/lib/urlHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type {Request, Response} from '/types';

// @ts-expect-error TS2307: Cannot find module '/lib/enonic/static' or its corresponding type declarations.
import { buildGetter } from '/lib/enonic/static';
import { getToolUrl } from '/lib/xp/admin';
import {
FILEPATH_MANIFEST_CJS,
FILEPATH_MANIFEST_NODE_MODULES,
GETTER_ROOT
} from '../constants';
import ioResource from './ioResource';
import { IS_DEV_MODE } from './runMode';


interface UrlPostfixParams {
manifestPath?: string
path: string,
};

type UrlParams = UrlPostfixParams & {urlPrefix: string};


const manifests = {
[FILEPATH_MANIFEST_CJS]: ioResource(FILEPATH_MANIFEST_CJS),
// [FILEPATH_MANIFEST_ESM]: ioResource(FILEPATH_MANIFEST_ESM),
[FILEPATH_MANIFEST_NODE_MODULES]: ioResource(FILEPATH_MANIFEST_NODE_MODULES),
}

const getImmutableUrl = ({
manifestPath = FILEPATH_MANIFEST_CJS,
path,
urlPrefix
}: UrlParams) => {
if (IS_DEV_MODE) {
manifests[manifestPath] = ioResource(manifestPath);
}

return `${urlPrefix}/${GETTER_ROOT}/${manifests[manifestPath][path]}`;
}

export const getAdminUrl = ({
manifestPath = FILEPATH_MANIFEST_CJS,
path,
}: UrlPostfixParams, tool: string) => {
const urlPrefix = getToolUrl(app.name, tool);

return getImmutableUrl({
manifestPath,
path,
urlPrefix
});
}

export const immutableGetter = buildGetter({
etag: false, // default is true in production and false in development
getCleanPath: (request: Request) => {
log.debug('request:%s', JSON.stringify(request, null, 4));
log.debug('contextPath:%s', request.contextPath);
log.debug('rawPath:%s', request.rawPath);

const prefix = request.contextPath;
let cleanPath = prefix ? request.rawPath.substring(prefix.length) : request.rawPath;
cleanPath = cleanPath.replace(`${GETTER_ROOT}/`, '');

log.debug('cleanPath:%s', cleanPath);

return cleanPath;
},
root: GETTER_ROOT
}) as (_request: Request) => Response;
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions src/main/resources/static/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"baseUrl": ".",
"esModuleInterop": true, // Needed for the 'q' npm module
"module": "commonjs",
"lib": [
"DOM",
"ES2020",
"ES2015.Promise"
],
"moduleResolution": "node",
"paths": {},
"rootDir": ".",
"skipLibCheck": true,
"target": "es5", // Modern browsers
"types": [
"hasher",
"q",
"nanoid",
"owasp-password-strength-test"
],
},
"include": [
"./**/*.ts"
],
}
File renamed without changes.
31 changes: 31 additions & 0 deletions src/main/resources/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
// This file is used by your code editor and the build system,
// for TypeScript files except for those under ./assets/.
// https://www.typescriptlang.org/tsconfig
"compilerOptions": {
"baseUrl": ".",
"lib": [
"ES5"
],
"paths": {
"/lib/xp/*": ["../../../node_modules/@enonic-types/lib-*"],
"/*": ["./*"]
},
"rootDir": ".",
"skipLibCheck": true,
// "typeRoots": [
// "node_modules/@types",
// "node_modules/@enonic-types"
// ],
"types": [
"@enonic-types/global"
// "global" // When typeRoots is set the prefix @enonic-types/ must be removed.
]
},
"exclude": [
"./assets/**/*",
],
"include": [
"./**/*.ts"
]
}
6 changes: 6 additions & 0 deletions src/main/resources/types/PageContributions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface PageContributions {
headBegin?: string[]
headEnd?: string[]
bodyBegin?: string[]
bodyEnd?: string[]
}
Loading

0 comments on commit 27bdc9d

Please sign in to comment.