Files
Bubberstation/tgui/packages/common/string.babel-plugin.cjs
T
Aleksej Komarov 602270d178 tgui: MIT License (#50969)
* MIT license headers

* various tweaks

- Tweak jsdoc headers a bit
- Use an old-school format with SPDX-License-Identifier for SCSS
- Add headers to tgui dmcode

* Simplify the license section

* Rebuild tgui, small tweaks

Co-authored-by: ZeWaka <zewakagamer@gmail.com>

About The Pull Request

All relevant source code now contains copyright headers, that explicitly assert copyright and license for every file.

This has been done to prepare TGUI for wider adoption. Goon station devs are interested in using TGUI, and hopefully this will result in a nice collaboration and improvements to both codebases.

The following files were relicensed under MIT:

    code/controllers/subsystem/tgui.dm
    code/modules/tgui/*.dm
    tgui/**/*.js
    tgui/**/*.scss

The following files were kept untouched:

    tgui/packages/tgui/interfaces/**/*.js
    tgui/packages/tgui/styles/interfaces/**/*.scss

Project is still basically AGPL-3.0 under /tg/station's parent license (with added MIT texts), but allows importing MIT code into MIT-compatible codebases.
2020-05-11 10:32:52 +12:00

74 lines
1.9 KiB
JavaScript

/**
* This plugin saves overall about 10KB on the final bundle size, so it's
* sort of worth it.
*
* We are using a .cjs extension because:
*
* 1. Webpack CLI only supports CommonJS modules;
* 2. tgui-dev-server supports both, but we still need to signal NodeJS
* to import it as a CommonJS module, hence .cjs extension.
*
* We need to copy-paste the whole "multiline" function because we can't
* synchronously import an ES module from a CommonJS module.
*
* @file
* @copyright 2020 Aleksej Komarov
* @license MIT
*/
/**
* Removes excess whitespace and indentation from the string.
*/
const multiline = str => {
const lines = str.split('\n');
// Determine base indentation
let minIndent;
for (let line of lines) {
for (let indent = 0; indent < line.length; indent++) {
const char = line[indent];
if (char !== ' ') {
if (minIndent === undefined || indent < minIndent) {
minIndent = indent;
}
break;
}
}
}
if (!minIndent) {
minIndent = 0;
}
// Remove this base indentation and trim the resulting string
// from both ends.
return lines
.map(line => line.substr(minIndent).trimRight())
.join('\n')
.trim();
};
const StringPlugin = ref => {
return {
visitor: {
TaggedTemplateExpression: path => {
if (path.node.tag.name === 'multiline') {
const { quasi } = path.node;
if (quasi.expressions.length > 0) {
throw new Error('Multiline tag does not support expressions!');
}
if (quasi.quasis.length > 1) {
throw new Error('Quasis is longer than 1');
}
const { value } = quasi.quasis[0];
value.raw = multiline(value.raw);
value.cooked = multiline(value.cooked);
path.replaceWith(quasi);
}
},
},
};
};
module.exports = {
__esModule: true,
default: StringPlugin,
};