From 1c89f43a043a95bd5ebdec6dfaec63ec99a443f2 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 3 Jun 2023 16:16:42 -0300 Subject: [PATCH] fix: linaria PR #1256 --- .../linaria-esbuild-plugin-fixed.mjs | 108 ++++++++++++++++++ .../taler-wallet-webextension/package.json | 2 +- .../patch-linaria.sh | 9 ++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 packages/taler-wallet-webextension/linaria-esbuild-plugin-fixed.mjs create mode 100755 packages/taler-wallet-webextension/patch-linaria.sh diff --git a/packages/taler-wallet-webextension/linaria-esbuild-plugin-fixed.mjs b/packages/taler-wallet-webextension/linaria-esbuild-plugin-fixed.mjs new file mode 100644 index 000000000..9f7576fc1 --- /dev/null +++ b/packages/taler-wallet-webextension/linaria-esbuild-plugin-fixed.mjs @@ -0,0 +1,108 @@ +// src/index.ts +import fs from "fs"; +import path from "path"; +import { transformSync } from "esbuild"; +import { slugify, transform } from "@linaria/babel-preset"; +var nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/; +function linaria({ sourceMap, preprocessor, esbuildOptions, ...rest } = {}) { + let options = esbuildOptions; + return { + name: "linaria", + setup(build) { + const cssLookup = /* @__PURE__ */ new Map(); + const asyncResolve = async (token, importer) => { + const context = path.isAbsolute(importer) + ? path.dirname(importer) + : path.join(process.cwd(), path.dirname(importer)); + const result = await build.resolve(token, { + resolveDir: context, + kind: "import-statement", + }); + if (result.errors.length > 0) { + throw new Error(`Cannot resolve ${token}`); + } + return result.path; + }; + build.onResolve({ filter: /\.linaria\.css$/ }, (args) => { + return { + namespace: "linaria", + path: args.path, + }; + }); + build.onLoad({ filter: /.*/, namespace: "linaria" }, (args) => { + return { + contents: cssLookup.get(args.path), + loader: "css", + resolveDir: path.basename(args.path), + }; + }); + build.onLoad({ filter: /\.(js|jsx|ts|tsx)$/ }, async (args) => { + const rawCode = fs.readFileSync(args.path, "utf8"); + const { ext, name: filename } = path.parse(args.path); + const loader = ext.replace(/^\./, ""); + if (nodeModulesRegex.test(args.path)) { + return { + loader, + contents: rawCode, + }; + } + if (!options) { + options = {}; + if ("jsxFactory" in build.initialOptions) { + options.jsxFactory = build.initialOptions.jsxFactory; + } + if ("jsxFragment" in build.initialOptions) { + options.jsxFragment = build.initialOptions.jsxFragment; + } + } + const transformed = transformSync(rawCode, { + ...options, + sourcefile: args.path, + sourcemap: sourceMap, + loader, + }); + let { code } = transformed; + if (sourceMap) { + const esbuildMap = Buffer.from(transformed.map).toString("base64"); + code += `/*# sourceMappingURL=data:application/json;base64,${esbuildMap}*/`; + } + const result = await transform( + code, + { + filename: args.path, + preprocessor, + pluginOptions: rest, + }, + asyncResolve + ); + if (!result.cssText) { + return { + contents: code, + loader, + resolveDir: path.dirname(args.path), + }; + } + let { cssText } = result; + const slug = slugify(cssText); + const cssFilename = `${filename}_${slug}.linaria.css`; + let contents = `import ${JSON.stringify(cssFilename)}; ${result.code}`; + if (sourceMap && result.cssSourceMapText) { + const map = Buffer.from(result.cssSourceMapText).toString("base64"); + cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`; + const linariaMap = Buffer.from( + JSON.stringify(result.sourceMap) + ).toString("base64"); + contents += `/*# sourceMappingURL=data:application/json;base64,${linariaMap}*/`; + } + cssLookup.set(cssFilename, cssText); + return { + contents, + loader, + resolveDir: path.dirname(args.path), + }; + }); + }, + }; +} +export { linaria as default }; +//# sourceMappingURL=index.mjs.map diff --git a/packages/taler-wallet-webextension/package.json b/packages/taler-wallet-webextension/package.json index bb2bbb4b5..801a11f71 100644 --- a/packages/taler-wallet-webextension/package.json +++ b/packages/taler-wallet-webextension/package.json @@ -12,7 +12,7 @@ "clean": "rimraf dist lib tsconfig.tsbuildinfo", "test": "./test.mjs && mocha --require source-map-support/register 'dist/test/**/*.test.js' 'dist/test/**/test.js'", "test:coverage": "nyc pnpm test", - "compile": "tsc && ./build.mjs", + "compile": "./patch-linaria.sh && tsc && ./build.mjs", "prepare": "tsc", "dev": "./dev.mjs", "pretty": "prettier --write src", diff --git a/packages/taler-wallet-webextension/patch-linaria.sh b/packages/taler-wallet-webextension/patch-linaria.sh new file mode 100755 index 000000000..46dc57927 --- /dev/null +++ b/packages/taler-wallet-webextension/patch-linaria.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# This file is in the public domain. + +# Fix: ERROR Cannot find module 'xxx' with esbuild 0.17 and linaria 4.2 +# remove when this PR is accepted +# https://github.com/callstack/linaria/pull/1256 + +cp linaria-esbuild-plugin-fixed.mjs ./node_modules/@linaria/esbuild/dist/index.mjs +