Skip to content

Commit cfe7c28

Browse files
committed
Build now uses prettier/standalone
1 parent e6a117e commit cfe7c28

11 files changed

Lines changed: 146 additions & 14 deletions

File tree

.github/workflows/CI.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jobs:
3636
run: npm install
3737
- name: Build
3838
run: npm run build
39+
- name: Build test app
40+
run: npm run build:test
3941
- name: Run tests
4042
run: npm run test:standalone
4143

@@ -54,8 +56,10 @@ jobs:
5456
run: npm run build
5557
- name: Downgrade Prettier to V2
5658
run: npm install [email protected]
59+
- name: Build test app
60+
run: npm run build:test
5761
- name: Run standalone tests
58-
run: npm run test:standalone tests/format tests/unit/prettier-version
62+
run: npm run test:standalone tests/format tests/integration tests/unit/prettier-version
5963

6064
test_linux:
6165
name: Test on Linux with Node ${{ matrix.node }}

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,41 @@ We follow Prettier's strategy for populating their plugins in the object `pretti
5959

6060
```html
6161
<script>
62+
async function format(code) {
63+
return await prettier.format(code, {
64+
parser: "solidity-parse",
65+
plugins: [solidityPlugin],
66+
});
67+
}
68+
6269
const originalCode = 'contract Foo {}';
63-
const formattedCode = prettier.format(originalCode, {
64-
parser: 'solidity-parse',
65-
plugins: prettierPlugins
66-
});
70+
const formattedCode = format(originalCode);
6771
</script>
6872
```
6973

7074
For more details and please have a look at [Prettier's documentation](https://prettier.io/docs/en/browser.html).
7175

76+
### Creating a package for the Browser
77+
78+
_Added in v1.1.4_
79+
80+
If you are creating your own package to be run in a browser, you might want to import the standalone files directly.
81+
82+
```Javascript
83+
import prettier from 'prettier/standalone';
84+
import solidityPlugin from 'prettier-plugin-solidity/standalone';
85+
86+
async function format(code) {
87+
return await prettier.format(code, {
88+
parser: "solidity-parse",
89+
plugins: [solidityPlugin],
90+
});
91+
}
92+
93+
const originalCode = 'contract Foo {}';
94+
const formattedCode = format(originalCode);
95+
```
96+
7297
## Configuration File
7398

7499
Prettier provides a flexible system to configure the formatting rules of a project. For more information please refer to the [documentation](https://prettier.io/docs/en/configuration.html).

jest.config.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
const TEST_STANDALONE = Boolean(process.env.TEST_STANDALONE);
2+
const testMatch = [
3+
'<rootDir>/tests/format/**/jsfmt.spec.js',
4+
5+
'<rootDir>/tests/unit/**/*.test.js'
6+
];
7+
8+
if (TEST_STANDALONE) {
9+
testMatch.push('<rootDir>/tests/integration/**/*.test.js');
10+
}
211

312
export default {
413
runner: 'jest-light-runner',
@@ -17,10 +26,7 @@ export default {
1726
'tests/format/RespectDefaultOptions'
1827
]
1928
: [],
20-
testMatch: [
21-
'<rootDir>/tests/format/**/jsfmt.spec.js',
22-
'<rootDir>/tests/unit/**/*.test.js'
23-
],
29+
testMatch,
2430
transform: {},
2531
watchPlugins: [
2632
'jest-watch-typeahead/filename',

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
"browser": "./dist/standalone.cjs",
88
"unpkg": "./dist/standalone.cjs",
99
"exports": {
10-
"import": "./src/index.js",
11-
"require": "./dist/standalone.cjs"
10+
".": {
11+
"import": "./src/index.js",
12+
"require": "./dist/standalone.cjs"
13+
},
14+
"./standalone": {
15+
"default": "./dist/standalone.cjs"
16+
}
1217
},
1318
"scripts": {
1419
"build": "webpack --env production",
1520
"build:dev": "webpack --env development",
21+
"build:test": "webpack --config test.config.js",
1622
"eslint": "eslint 'src/**' 'tests/**'",
1723
"lint": "npm run eslint && npm run prettier -- --list-different",
1824
"lint:fix": "npm run eslint -- --fix && npm run prettier -- --write",

test.config.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import path from 'node:path';
2+
// eslint-disable-next-line import/no-extraneous-dependencies
3+
import createEsmUtils from 'esm-utils';
4+
5+
const { __dirname } = createEsmUtils(import.meta);
6+
7+
// This is the production and development configuration.
8+
// It is focused on developer experience, fast rebuilds, and a minimal bundle.
9+
export default {
10+
entry: './tests/integration/test-app.js',
11+
mode: 'production',
12+
bail: true,
13+
14+
optimization: { minimize: false },
15+
target: ['browserslist'],
16+
17+
output: {
18+
filename: 'test.cjs',
19+
path: path.resolve(__dirname, 'dist'),
20+
globalObject: `
21+
typeof globalThis !== "undefined"
22+
? globalThis
23+
: typeof global !== "undefined"
24+
? global
25+
: typeof self !== "undefined"
26+
? self
27+
: this || {}
28+
`,
29+
library: {
30+
export: 'default',
31+
name: {
32+
commonjs: 'format',
33+
root: 'format'
34+
},
35+
type: 'umd2'
36+
}
37+
},
38+
39+
performance: {
40+
maxEntrypointSize: 1024 * 1024,
41+
maxAssetSize: 1024 * 1024
42+
}
43+
};

tests/integration/node.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import format from '../../dist/test.cjs';
2+
3+
test('Should run a a test app in a node environment', async () => {
4+
const data = 'contract CheckPackage {}';
5+
6+
expect(await format(data)).toEqual('contract CheckPackage {}\n');
7+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const path = require('path');
2+
const vm = require('vm');
3+
const createSandBox = require('../config/utils/create-sandbox.cjs');
4+
5+
const sandbox = createSandBox({
6+
files: [path.join(__dirname, '../../dist/test.cjs')]
7+
});
8+
9+
module.exports = {
10+
format(input) {
11+
return vm.runInNewContext('format($$$input);', {
12+
$$$input: input,
13+
...sandbox
14+
});
15+
}
16+
};

tests/integration/sandbox.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test('Should run a a test app in a sandbox', async () => {
2+
const data = 'contract CheckPackage {}';
3+
const entry = new URL('./sandbox-test-app.cjs', import.meta.url);
4+
5+
const { format } = await import(entry).then((module) => module.default);
6+
7+
expect(await format(data)).toEqual('contract CheckPackage {}\n');
8+
});

tests/integration/test-app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import prettier from 'prettier/standalone';
2+
import solidityPlugin from '../../dist/standalone.cjs';
3+
4+
export default async function format(code) {
5+
const formattedCode = await prettier.format(code, {
6+
parser: 'solidity-parse',
7+
plugins: [solidityPlugin]
8+
});
9+
return formattedCode;
10+
}

tests/unit/prettier-version/proxyquire-plugin.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const proxyquire = require('proxyquire');
2-
const prettier = require('prettier/standalone.js');
2+
const prettier = require('prettier/standalone');
33

44
const proxyquirePlugin = () => {
55
const prettierMock = { ...prettier, version: '2.2.1', '@global': true };
66

77
const plugin = proxyquire('../../../dist/standalone.cjs', {
8-
prettier: prettierMock
8+
'prettier/standalone': prettierMock
99
});
1010

1111
return { plugin, prettierMock };

0 commit comments

Comments
 (0)