Skip to content

Commit

Permalink
try out synckit (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott authored Apr 5, 2022
1 parent b17a1c6 commit 3afbf2f
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 74 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-spiders-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Update the internal sync engine to synckit
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"description": "A Prettier Plugin for formatting Astro files",
"main": "dist/index.js",
"files": [
"dist/**"
"dist/**",
"workers/*"
],
"engines": {
"node": "^14.13.1 || >=16.0.0",
Expand Down Expand Up @@ -38,9 +39,9 @@
},
"dependencies": {
"@astrojs/compiler": "^0.13.1",
"make-synchronous": "0.1.1",
"prettier": "^2.4.1",
"sass-formatter": "^0.7.2"
"sass-formatter": "^0.7.2",
"synckit": "^0.6.2"
},
"devDependencies": {
"@changesets/cli": "^2.16.0",
Expand Down
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import parse from './parse';
import printer from './printer';
import { options } from './options';
import { Parser, Printer, SupportLanguage } from 'prettier';
import { createSyncFn } from 'synckit';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

// the worker path must be absolute
const parse = createSyncFn(require.resolve('../workers/parse-worker.js'));

// do whatever you want, you will get the result synchronously!

export const languages: Partial<SupportLanguage>[] = [
{
Expand All @@ -14,7 +21,7 @@ export const languages: Partial<SupportLanguage>[] = [

export const parsers: Record<string, Parser> = {
astro: {
parse,
parse: (source) => parse(source),
astFormat: 'astro',
locStart: (node) => node.start,
locEnd: (node) => node.end,
Expand Down
16 changes: 0 additions & 16 deletions src/parse.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/syncUtils.ts

This file was deleted.

9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ import {
// TextNode,
} from './nodes';

import { serialize } from './syncUtils';
import { createSyncFn } from 'synckit';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

// the worker path must be absolute
const serialize = createSyncFn(
require.resolve('../workers/serialize-worker.js')
);

// import makeSynchronous from 'make-synchronous';

Expand Down
8 changes: 8 additions & 0 deletions workers/parse-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { runAsWorker } = require('synckit');

runAsWorker(async (source) => {
const dynamicImport = new Function('file', 'return import(file)');
const { parse } = await dynamicImport('@astrojs/compiler');
const { ast } = await parse(source);
return ast;
});
7 changes: 7 additions & 0 deletions workers/serialize-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { runAsWorker } = require('synckit');

runAsWorker(async (node) => {
const dynamicImport = new Function('file', 'return import(file)');
const { serialize } = await dynamicImport('@astrojs/compiler/utils');
return await serialize(node);
});
45 changes: 7 additions & 38 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,6 @@ cross-spawn@^7.0.2:
shebang-command "^2.0.0"
which "^2.0.1"

crypto-random-string@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-2.0.0.tgz#ef2a7a966ec11083388369baa02ebead229b30d5"
integrity sha512-v1plID3y9r/lPhviJ1wrXpLeyUIGAZ2SHNYTEapm7/8A9nLPoyvVp3RK/EPFqn5kEznyWgYZNsRtYYIWbuG8KA==

csv-generate@^3.4.3:
version "3.4.3"
resolved "https://registry.yarnpkg.com/csv-generate/-/csv-generate-3.4.3.tgz#bc42d943b45aea52afa896874291da4b9108ffff"
Expand Down Expand Up @@ -1105,11 +1100,6 @@ escape-string-regexp@^1.0.5:
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=

escape-string-regexp@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz#a30304e99daa32e23b2fd20f51babd07cffca344"
integrity sha512-UpzcLCXolUWcNu5HtVMHYdXJjArjsF9C0aNnquZYY4uW/Vu0miy5YoWvbV345HauVvcAUnpRuhMMcqTcGOY2+w==

escape-string-regexp@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz#14ba83a5d373e3d311e5afca29cf5bfad965bf34"
Expand Down Expand Up @@ -1817,14 +1807,6 @@ make-error@^1.1.1:
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

make-synchronous@0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/make-synchronous/-/make-synchronous-0.1.1.tgz#0169f6ec769c3cf8948d66790da262740c1209e7"
integrity sha512-Y4SxxqhaoyMDokJQ0AZz0E+bLhRkOSR7Z/IQoTKPdS6HYi3aobal2kMHoHHoqBadPWjf07P4K1FQLXOx3wf9Yw==
dependencies:
subsume "^3.0.0"
type-fest "^0.16.0"

map-obj@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-1.0.1.tgz#d933ceb9205d82bdcf4886f6742bdc2b4dea146d"
Expand Down Expand Up @@ -2454,14 +2436,6 @@ strip-json-comments@^3.1.0, strip-json-comments@^3.1.1:
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==

subsume@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/subsume/-/subsume-3.0.0.tgz#22c92730f441ad72ee9af4bdad42dc4ff830cfaf"
integrity sha512-6n/UfV8UWKwJNO8OAOiKntwEMihuBeeoJfzpL542C+OuvT4iWG9SwjrXkOmsxjb4SteHUsos9SvrdqZ9+ICwTQ==
dependencies:
escape-string-regexp "^2.0.0"
unique-string "^2.0.0"

suf-log@^2.5.3:
version "2.5.3"
resolved "https://registry.yarnpkg.com/suf-log/-/suf-log-2.5.3.tgz#0919a7fceea532a99b578c97814c4e335b2d64d1"
Expand Down Expand Up @@ -2495,6 +2469,13 @@ supports-preserve-symlinks-flag@^1.0.0:
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==

synckit@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/synckit/-/synckit-0.6.2.tgz#e1540b97825f2855f7170b98276e8463167f33eb"
integrity sha512-Vhf+bUa//YSTYKseDiiEuQmhGCoIF3CVBhunm3r/DQnYiGT4JssmnKQc44BIyOZRK2pKjXXAgbhfmbeoC9CJpA==
dependencies:
tslib "^2.3.1"

term-size@^2.1.0:
version "2.2.1"
resolved "https://registry.yarnpkg.com/term-size/-/term-size-2.2.1.tgz#2a6a54840432c2fb6320fea0f415531e90189f54"
Expand Down Expand Up @@ -2611,11 +2592,6 @@ type-fest@^0.13.1:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.13.1.tgz#0172cb5bce80b0bd542ea348db50c7e21834d934"
integrity sha512-34R7HTnG0XIJcBSn5XhDd7nNFPRcXYRZrBB2O2jdKqYODldSzBAqzsWoZYYvduky73toYS/ESqxPvkDf/F0XMg==

type-fest@^0.16.0:
version "0.16.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.16.0.tgz#3240b891a78b0deae910dbeb86553e552a148860"
integrity sha512-eaBzG6MxNzEn9kiwvtre90cXaNLkmadMWa1zQMs3XORCXNbsH/OewwbxC5ia9dCxIxnTAsSxXJaa/p5y8DlvJg==

type-fest@^0.20.2:
version "0.20.2"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4"
Expand All @@ -2636,13 +2612,6 @@ typescript@^4.4.3:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==

unique-string@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-2.0.0.tgz#39c6451f81afb2749de2b233e3f7c5e8843bd89d"
integrity sha512-uNaeirEPvpZWSgzwsPGtU2zVSTrn/8L5q/IexZmH0eH6SA73CmAA5U4GwORTxQAZs95TAXLNqeLoPPNO5gZfWg==
dependencies:
crypto-random-string "^2.0.0"

universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
Expand Down

0 comments on commit 3afbf2f

Please sign in to comment.