-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
OSOE-338: Javasript compilation improvements
- Loading branch information
Showing
6 changed files
with
77 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"browsers": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
] | ||
}, | ||
"exclude": ["transform-async-to-generator", "transform-regenerator"], | ||
"modules": false, | ||
"loose": true | ||
} | ||
] | ||
], | ||
"sourceMaps": "inline", | ||
"targets": "defaults" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const babel = require('@babel/core'); | ||
const path = require('path'); | ||
const process = require('process'); | ||
|
||
const { glob } = require('glob'); | ||
const { minify } = require("terser"); | ||
const { readFile, writeFile, mkdir } = require('fs').promises; | ||
|
||
const { handleErrorObjectAndExit } = require('./handle-error'); | ||
|
||
const [ sourcePath, destinationPath, configPath ] = process.argv.slice(2); | ||
|
||
function readJsonConfig(fileName) { | ||
return readFile(path.join(configPath, fileName), 'utf8') | ||
.then(text => JSON.parse(text)); | ||
} | ||
|
||
async function compileScripts() { | ||
const browserConfig = readJsonConfig('babel.config.json'); | ||
const moduleConfig = readJsonConfig('babel.module.config.json'); | ||
const scriptFiles = await glob('/**/*.{js,mjs}', { root: sourcePath, ignore: 'node_modules/**' }); | ||
|
||
await Promise.all(scriptFiles.map(async filePath => { | ||
const config = filePath.toLowerCase().endsWith('.mjs') ? moduleConfig : browserConfig; | ||
const result = await babel.transformFileAsync(filePath, config); | ||
|
||
const destinationFilePath = path.join(destinationPath, path.relative(sourcePath, filePath)) | ||
await mkdir(path.dirname(destinationFilePath), { recursive: true }); | ||
await writeFile(destinationFilePath, result.code); | ||
|
||
const minifiedPath = destinationFilePath.replace(/\.(m?js)$/, '.min.$1') | ||
const sourceMapOptions = { content: 'inline', url : path.basename(minifiedPath) + '.map' }; | ||
const minifiedCode = await minify(result.code, { sourceMap: sourceMapOptions }); | ||
await writeFile(minifiedPath, minifiedCode.code); | ||
await writeFile(minifiedPath + ".map", minifiedCode.map); | ||
})); | ||
} | ||
|
||
compileScripts().catch(handleErrorObjectAndExit); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters