Skip to content

Commit

Permalink
feat: 🎸 ESM support
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Jul 2, 2023
1 parent 6dc0836 commit c8f6953
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 112 deletions.
144 changes: 56 additions & 88 deletions __tests__/cli.test.ts

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions __tests__/formatter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import path from 'path';
import fs from 'fs';
import { BladeFormatter, Formatter } from '../src/main';
import { BladeFormatter, Formatter } from '../src/main.js';
import * as cmd from './support/cmd';
import * as util from './support/util';

Expand Down Expand Up @@ -1057,7 +1057,7 @@ describe('formatter', () => {
});

test('should consider directive in html tag', async () => {
const cmdResult = await cmd.execute(path.resolve('bin', 'blade-formatter'), [
const cmdResult = await cmd.execute(path.resolve('bin', 'blade-formatter.js'), [
path.resolve('__tests__', 'fixtures', 'inline_php_tag.blade.php'),
]);

Expand Down Expand Up @@ -2445,11 +2445,11 @@ describe('formatter', () => {

test('sort tailwindcss classs option can work', function () {
const content = [
`<div class="justify-center z-50 z-10 z-20 container text-left foo md:text-center">`,
`<div class="justify-center z-10 z-20 z-50 container text-left foo md:text-center">`,
`</div>`,
].join('\n');
const expected = [
`<div class="foo container z-50 z-10 z-20 justify-center text-left md:text-center">`,
`<div class="foo container z-10 z-20 z-50 justify-center text-left md:text-center">`,
`</div>`,
``,
].join('\n');
Expand All @@ -2475,7 +2475,7 @@ describe('formatter', () => {
].join('\n');

const expected = [
`<div class="foo container z-50 z-10 z-20 justify-center text-left md:text-center">`,
`<div class="foo container z-10 z-20 z-50 justify-center text-left md:text-center">`,
`</div>`,
``,
].join('\n');
Expand All @@ -2494,7 +2494,7 @@ describe('formatter', () => {
].join('\n');

const expected = [
`<div class="foo container z-50 z-10 z-20 justify-center text-left md:text-center">`,
`<div class="foo container z-10 z-20 z-50 justify-center text-left md:text-center">`,
`</div>`,
``,
].join('\n');
Expand Down
4 changes: 2 additions & 2 deletions __tests__/support/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import * as cmd from './cmd';

export function assertFormatted(file: any) {
const response = cmd.executeSync(path.resolve('bin', 'blade-formatter'), [file, '-c']);
const response = cmd.executeSync(path.resolve('bin', 'blade-formatter.js'), [file, '-c']);

const output = response.output.join('\n');
const exitCode = response.status;
Expand All @@ -21,7 +21,7 @@ export function assertNotFormatted(file: any) {
}
});

const response = cmd.executeSync(path.resolve('bin', 'blade-formatter'), [file, '-c']);
const response = cmd.executeSync(path.resolve('bin', 'blade-formatter.js'), [file, '-c']);

const output = response.output.join('\n');
const exitCode = response.status;
Expand Down
4 changes: 2 additions & 2 deletions __tests__/support/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function populateFixtures(targetDir: any) {
}

export async function checkIfTemplateIsFormattedTwice(input: any, target: any, options: string[] = []) {
const cmdResult = await cmd.execute(path.resolve('bin', 'blade-formatter'), [
const cmdResult = await cmd.execute(path.resolve('bin', 'blade-formatter.js'), [
...options,
path.resolve('__tests__', 'fixtures', input),
]);
Expand All @@ -17,7 +17,7 @@ export async function checkIfTemplateIsFormattedTwice(input: any, target: any, o

expect(cmdResult).toEqual(formatted.toString('utf-8'));

const cmdResult2 = await cmd.execute(path.resolve('bin', 'blade-formatter'), [
const cmdResult2 = await cmd.execute(path.resolve('bin', 'blade-formatter.js'), [
...options,
path.resolve('__tests__', 'fixtures', target),
]);
Expand Down
4 changes: 0 additions & 4 deletions bin/blade-formatter

This file was deleted.

11 changes: 11 additions & 0 deletions bin/blade-formatter.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node

"use strict";

function runCli(cli) {
return cli.default.default();
}

var dynamicImport = new Function("module", "return import(module)");

module.exports.promise = dynamicImport("../dist/cli-bundle.cjs").then(runCli);
3 changes: 3 additions & 0 deletions bin/blade-formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

import './blade-formatter.cjs';
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module 'concat-stream';
declare module 'glob';
declare module 'fs-extra';
declare module '@prettier/plugin-php/standalone';
declare module '@prettier/plugin-php/standalone.js';
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,31 @@
],
"version": "1.32.13",
"description": "An opinionated blade template formatter for Laravel",
"main": "./dist/bundle.js",
"main": "./dist/bundle.cjs",
"types": "./dist/types/main.d.ts",
"type": "module",
"exports": {
".": {
"import": "./dist/bundle.js",
"require": "./dist/bundle.cjs",
"default": "./dist/bundle.js"
}
},
"./*": "./*"
},
"scripts": {
"build": "cross-env NODE_ENV=production node esbuild.ts",
"build": "cross-env NODE_ENV=production node esbuild.js && cross-env NODE_ENV=production ESM_BUILD=true node esbuild.js",
"prepublish": "tsc src/main.ts --declaration --emitDeclarationOnly --outDir ./dist/types || true",
"watch": "node esbuild.ts",
"watch": "node esbuild.js",
"test": "yarn run build && node --experimental-vm-modules node_modules/.bin/jest",
"lint": "eslint src -c .eslintrc.js --ext ts",
"fix": "prettier {src,__tests__}/**/*.ts --write",
"check_formatted": "prettier **/*.ts -c",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"prepare": "husky install",
"bin": "cross-env ./bin/blade-formatter"
"bin": "cross-env ./bin/blade-formatter.cjs"
},
"bin": {
"blade-formatter": "bin/blade-formatter"
"blade-formatter": "bin/blade-formatter.cjs"
},
"author": "Shuhei Hayashibara",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import _ from 'lodash';
import fs from 'fs';
import os from 'os';
import chalk from 'chalk';
import prettier from 'prettier/standalone';
import prettier from 'prettier/standalone.js';
// @ts-ignore
import phpPlugin from '@prettier/plugin-php/standalone';
import phpPlugin from '@prettier/plugin-php/standalone.js';
import detectIndent from 'detect-indent';
import { indentStartTokens, phpKeywordStartTokens, phpKeywordEndTokens } from './indent';
import { nestedParenthesisRegex } from './regex';
Expand Down
2 changes: 1 addition & 1 deletion src/vsctm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class VscodeTextmate {
return (async () => {
this.vsctm = vsctm.default ?? vsctm;
// @ts-ignore
this.oniguruma = oniguruma || vscodeOniguruma.default || vscodeOniguruma;
this.oniguruma = oniguruma || vscodeOniguruma?.default || vscodeOniguruma;
await this.loadWasm();
return this;
})();
Expand Down

0 comments on commit c8f6953

Please sign in to comment.