Skip to content

Commit

Permalink
fix: work around swc problem
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Nov 23, 2020
1 parent e0fb520 commit 2524457
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
18 changes: 0 additions & 18 deletions file-content-injector.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
const fs = require("fs");

[
// TODO: Remove this when https://github.com/swc-project/swc/issues/1188 has been resolved
{
file: "node_modules/core-js/internals/whitespaces.js",
match: String.raw`module.exports = '\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF';`,
replacement: String.raw`module.exports = String.fromCharCode("0x0009") + String.fromCharCode("0x000A") + String.fromCharCode("0x000B") + String.fromCharCode("0x000C") + String.fromCharCode("0x000D") + String.fromCharCode("0x0020") + String.fromCharCode("0x00A0") + String.fromCharCode("0x1680") + String.fromCharCode("0x2000") + String.fromCharCode("0x2001") + String.fromCharCode("0x2002") + String.fromCharCode("0x2003") + String.fromCharCode("0x2004") + String.fromCharCode("0x2005") + String.fromCharCode("0x2006") + String.fromCharCode("0x2007") + String.fromCharCode("0x2008") + String.fromCharCode("0x2009") + String.fromCharCode("0x200A") + String.fromCharCode("0x202F") + String.fromCharCode("0x205F") + String.fromCharCode("0x3000") + String.fromCharCode("0x2028") + String.fromCharCode("0x2029") + String.fromCharCode("0xFEFF");`
},
// TODO: Remove this when https://github.com/swc-project/swc/issues/1179 has been resolved
{
file: "node_modules/@wessberg/connection-observer/dist/index.js",
Expand All @@ -18,18 +12,6 @@ const fs = require("fs");
file: "node_modules/@polyfiller/object-fit/polyfill/index.js",
match: String.raw`if (new.target === undefined) {`,
replacement: String.raw`if ((this instanceof ComputedStyleObserver ? this.constructor : void 0) === undefined) {`
},
// TODO: Remove this when https://github.com/swc-project/swc/issues/1180 has been resolved
{
file: "node_modules/core-js/internals/collection-strong.js",
match: String.raw`else that.size++;`,
replacement: String.raw`else {that.size++;}`
},
// TODO: Remove this when https://github.com/swc-project/swc/issues/1180 has been resolved
{
file: "node_modules/core-js/internals/collection-strong.js",
match: String.raw`else that.size--;`,
replacement: String.raw`else {that.size--;}`
}
].forEach(replace);

Expand Down
2 changes: 1 addition & 1 deletion polyfill-lib/shady-css/src/document-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function handler(mxns) {
if (n.nodeType !== Node.ELEMENT_NODE) {
continue;
}
n = /** @type {HTMLElement} */ (n); // eslint-disable-line no-self-assign

let root = n.getRootNode();
let currentScope = getCurrentScope(n);
// node was scoped, but now is in document
Expand Down
33 changes: 33 additions & 0 deletions src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,34 @@ import {generateRandomHash} from "../util/hash-util/hash-util";
import {unlinkSync, writeFileSync} from "fs";
import {transform} from "@swc/core";

/**
* TODO: Remove this when https://github.com/swc-project/swc/issues/1227 has been resolved
*/
function workAroundSwcBug(str: string): string {
const unicodeEscape = /(\\+)u\{([0-9a-fA-F]+)\}/g;

function escape(code: any) {
let str = code.toString(16);
// Sigh, node 6 doesn't have padStart
// TODO: Remove in Babel 8, when we drop node 6.
while (str.length < 4) str = "0" + str;
return "\\u" + str;
}

function replacer(match: any, backslashes: any, code: any) {
if (backslashes.length % 2 === 0) {
return match;
}

const char = String.fromCodePoint(parseInt(code, 16));
const escaped = backslashes.slice(0, -1) + escape(char.charCodeAt(0));

return char.length === 1 ? escaped : escaped + escape(char.charCodeAt(1));
}

return str.replace(unicodeEscape, replacer);
}

function stringifyPolyfillFeature(feature: IPolyfillFeature): string {
const metaEntries = Object.entries(feature.meta);
const metaEntriesText = metaEntries
Expand Down Expand Up @@ -83,6 +111,11 @@ export async function build({paths, features, featuresRequested, userAgent, cont
ecmaVersion = "es2019";
}

// TODO: Remove this when https://github.com/swc-project/swc/issues/1227 has been resolved
if (code.includes("\\u{")) {
code = workAroundSwcBug(code);
}

({code, map} = await transform(code, {
sourceMaps: sourcemap ? "inline" : false,
inputSourceMap: map,
Expand Down

0 comments on commit 2524457

Please sign in to comment.