Skip to content

Instantly share code, notes, and snippets.

@Quixomatic
Created January 13, 2023 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Quixomatic/990d64edf450485ed7a04c50fd366ddd to your computer and use it in GitHub Desktop.
Save Quixomatic/990d64edf450485ed7a04c50fd366ddd to your computer and use it in GitHub Desktop.
Replacement file to make `@quixomatic/ui-renderer-react` work properly
/**
* @license
* Copyright (c) 2020 ServiceNow, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
const e = require('fs');
const r = require('path');
const { getModuleBase: i } = require('@servicenow/cli-utils');
module.exports = function jsxPragmatic(n) {
const s = n.types;
const isLegacy = (i, n) =>
(i.dependencies && (i.dependencies['@seismic/compatibility'] || i.dependencies['sn-seismic-archetype'])) || (e.existsSync(r.join(n, '.builderrc')) && ~e.readFileSync(r.join(n, '.builderrc')).indexOf('sn-seismic-archetype'));
const isReact = (e, r) => e.dependencies && (e.dependencies['@seismic/react-renderer'] || e.dependencies['@servicenow/ui-renderer-react'] || e.dependencies['@quixomatic/ui-renderer-react']) && r.some((e) => 'react' === e.source.value);
const getPragmaDefDefault = (n, s, t) => {
const c = i(n);
const o = JSON.parse(e.readFileSync(r.join(c, 'package.json')));
return isLegacy(o, c) ? s['sn-seismic-renderers'] : isReact(o, t) ? s['@quixomatic/ui-renderer-react'] || s['@servicenow/ui-renderer-react'] || s['@seismic/react-renderer'] : s['@servicenow/ui-renderer-snabbdom'] || s['@seismic/snabbdom-renderer'];
};
const getPragmaDef = (e, r) => {
const {
opts: { imports: i },
} = r;
const {
node: { body: n },
} = e;
const t = n.filter((e) => s.isImportDeclaration(e));
const c = t.filter((e) => i[e.source.value]).map((e) => i[e.source.value])[0] || getPragmaDefDefault(r.file.opts.filename, i, t);
return c;
};
const getPragmaImport = (e) => s.importDeclaration([s.importSpecifier(s.identifier(e.import), s.identifier(e.export || 'default'))], s.stringLiteral(e.module));
const t = {
Program: {
exit: function (e, r) {
if (!r.get('jsxDetected')) return;
const i = getPragmaDef(e, r);
i && e.unshiftContainer('body', getPragmaImport(i));
},
},
JSXElement: function (e, r) {
r.set('jsxDetected', !0);
},
};
return {
pre: function () {
if (!this.opts.imports) throw new Error('babel-plugin-jsx-now-pragmatic: You must specify `imports`');
},
inherits: require('babel-plugin-syntax-jsx'),
visitor: t,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment