Skip to content

Commit fd109c5

Browse files
authored
Merge pull request #95 from Lombiq/issue/OSOE-338
OSOE-338: Javasript compilation improvements
2 parents 6ee339a + 7090d2c commit fd109c5

File tree

6 files changed

+77
-94
lines changed

6 files changed

+77
-94
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"browsers": [
8+
">0.2%",
9+
"not dead",
10+
"not op_mini all"
11+
]
12+
},
13+
"exclude": ["transform-async-to-generator", "transform-regenerator"],
14+
"modules": false,
15+
"loose": true
16+
}
17+
]
18+
],
19+
"sourceMaps": "inline",
20+
"targets": "defaults"
21+
}

Lombiq.NodeJs.Extensions/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
"copyfiles": "2.4.1",
1414
"eslint": "8.47.0",
1515
"if-env": "1.0.4",
16-
"klaw": "4.1.0",
16+
"glob": "10.3.10",
1717
"markdownlint": "0.30.0",
1818
"nodemon": "3.0.1",
1919
"npm-run-all": "4.1.5",
2020
"postcss": "8.4.31",
2121
"postcss-cli": "10.1.0",
2222
"prettier": "3.0.3",
23-
"rimraf": "5.0.1",
23+
"rimraf": "5.0.5",
2424
"sass": "1.69.5",
2525
"stylelint": "15.11.0",
2626
"stylelint-config-standard-scss": "11.1.0",
@@ -71,9 +71,7 @@
7171
"---": "------------------------------------------------- SCRIPTS ------------------------------------------------",
7272
"build:scripts": "SRC=$(node scripts/get-path js source) && if-env SRC=! && echo \"Skipping scripts\" && exit 0 || run-p --print-name --continue-on-error lint:scripts compile:scripts",
7373
"lint:scripts": "SRC=$(node scripts/get-path js source) && node scripts/lint-scripts \"$SRC\" || AREA=scripts STEP=linting pnpm error",
74-
"compile:scripts": "SRC=$(node scripts/get-path js source) && if-env SRC=! && echo \"Skipping scripts\" && exit 0 || DEST=$(node scripts/get-path js target) run-s --print-name _compile:scripts _minify:scripts",
75-
"_compile:scripts": "babel \"$SRC\" --out-dir \"$DEST\" --config-file \"${INIT_CWD}/config/babel.config.json\" || AREA=scripts STEP=compilation pnpm error",
76-
"_minify:scripts": "node scripts/minify \"$DEST\" || AREA=scripts STEP=minifying pnpm error",
74+
"compile:scripts": "SRC=$(node scripts/get-path js source) DEST=$(node scripts/get-path js target) && if-env SRC=! && echo \"Skipping scripts\" && exit 0 || node scripts/compile-scripts \"$SRC\" \"$DEST\" \"${INIT_CWD}/config/\" || AREA=scripts STEP=compilation pnpm error",
7775
"clean:scripts": "DEST=$(node scripts/get-path js target) && rimraf \"$DEST/**/*.*\"",
7876
"watch:scripts": "SRC=$(node scripts/get-path js source) && nodemon --watch \"$SRC\" --exec \"pnpm build:scripts\"",
7977
"----": "------------------------------------------------ MARKDOWN -----------------------------------------------",

Lombiq.NodeJs.Extensions/pnpm-lock.yaml

Lines changed: 13 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const babel = require('@babel/core');
2+
const path = require('path');
3+
const process = require('process');
4+
5+
const { glob } = require('glob');
6+
const { minify } = require("terser");
7+
const { readFile, writeFile, mkdir } = require('fs').promises;
8+
9+
const { handleErrorObjectAndExit } = require('./handle-error');
10+
11+
const [ sourcePath, destinationPath, configPath ] = process.argv.slice(2);
12+
13+
function readJsonConfig(fileName) {
14+
return readFile(path.join(configPath, fileName), 'utf8')
15+
.then(text => JSON.parse(text));
16+
}
17+
18+
async function compileScripts() {
19+
const browserConfig = readJsonConfig('babel.config.json');
20+
const moduleConfig = readJsonConfig('babel.module.config.json');
21+
const scriptFiles = await glob('/**/*.{js,mjs}', { root: sourcePath, ignore: 'node_modules/**' });
22+
23+
await Promise.all(scriptFiles.map(async filePath => {
24+
const config = filePath.toLowerCase().endsWith('.mjs') ? moduleConfig : browserConfig;
25+
const result = await babel.transformFileAsync(filePath, config);
26+
27+
const destinationFilePath = path.join(destinationPath, path.relative(sourcePath, filePath))
28+
await mkdir(path.dirname(destinationFilePath), { recursive: true });
29+
await writeFile(destinationFilePath, result.code);
30+
31+
const minifiedPath = destinationFilePath.replace(/\.(m?js)$/, '.min.$1')
32+
const sourceMapOptions = { content: 'inline', url : path.basename(minifiedPath) + '.map' };
33+
const minifiedCode = await minify(result.code, { sourceMap: sourceMapOptions });
34+
await writeFile(minifiedPath, minifiedCode.code);
35+
await writeFile(minifiedPath + ".map", minifiedCode.map);
36+
}));
37+
}
38+
39+
compileScripts().catch(handleErrorObjectAndExit);

Lombiq.NodeJs.Extensions/scripts/minify.js

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

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Do you want to quickly try out this project and see it in action? Check it out,
2323

2424
## Prerequisites
2525

26-
1. To use this project, you will most of all need [Node.js](https://nodejs.org/) 16.9 or newer. We suggest installing the current LTS version which is higher than this minimum.
26+
1. To use this project, you will most of all need [Node.js](https://nodejs.org/) 18 or newer. We suggest installing the current LTS version which is higher than this minimum.
2727
2. Please follow our recommended setup guides for [Windows](Lombiq.NodeJs.Extensions/Docs/SetupWindows.md) or [Linux](Lombiq.NodeJs.Extensions/Docs/SetupLinux.md), as applicable.
2828

2929
[PNPM](https://pnpm.io) 8 (for package management and script execution) is automatically enabled via `corepack` in the `EnablePnpm` MSBuild target, so you don't have to install it separately. Since PNPM 8 dropped support for Node.js 14 or older, those `node` versions won't work.

0 commit comments

Comments
 (0)