Skip to content

Commit 613d79b

Browse files
Jantherfvictorio
andauthored
Standalone (#727)
* 2 standalone tests were breaking. * bundling the library for browser usage * Test passing and actually using our the standalone file * add tests for the bundle on CI * fix jest config * fix github action * removing prettier from the bundle * only require the necessary functions from semver * running standalone tests in isolation * Umd configuration closer to the one used by prettier. * commonjs exported name changed to `prettierPluginSolidity` from `solidity` * Supress warnings about assets size * correct resolution of 'emoji-regex' * package.json changes for browser support * including source maps in the package * cleaning up the usage of private utils that we borrowed from Prettier. 30k leaner build * moving utils away from prettier-comments folder since now is slim and understandable enough to be maintained. * Adding documentation for the standalone build * using ## instead - to avoid a bug in the markdown standalone plugin * moduleMapper doesn't work with jest-light-runner but we don't need it since we build everything before running tests * cleaning up the standalone tests that have problems * Use source-map instead of cheap-module-source-map Co-authored-by: Franco Victorio <victorio.franco@gmail.com>
1 parent 8963cd1 commit 613d79b

File tree

25 files changed

+2295
-957
lines changed

25 files changed

+2295
-957
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
coverage/**/*.js
2+
dist/**/*.js
23
tests/format/**/*.sol
34
tests/format/Markdown/Markdown.md
45
tests/format/RespectDefaultOptions/respect-default-options.js

.github/workflows/CI.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ jobs:
2828
- name: lint
2929
run: npm run lint
3030

31+
test_bundle:
32+
name: Test production
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/setup-node@v1
36+
with:
37+
node-version: 14
38+
- uses: actions/checkout@v2
39+
- uses: actions/cache@v2
40+
id: cache
41+
with:
42+
path: |
43+
node_modules
44+
key: ${{ runner.os }}-${{ hashFiles('package-lock.json') }}
45+
- name: Install
46+
run: npm install
47+
- name: Build
48+
run: npm run build
49+
- name: Run tests
50+
run: npm run test:standalone
51+
3152
test_linux:
3253
name: Test on Linux with Node ${{ matrix.node }}
3354
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ bower_components
3131

3232
# Compiled binary addons (https://nodejs.org/api/addons.html)
3333
build/Release
34+
dist
3435

3536
# Dependency directories
3637
node_modules/

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
coverage/**/*.js
2+
dist/**/*.js
23
tests/format/**/*.sol
34
tests/format/Markdown/Markdown.md
45
tests/format/RespectDefaultOptions/respect-default-options.js

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ If you like this project, please consider contributing to our [Gitcoin grant](ht
1313

1414
## Installation and usage
1515

16+
### Using in NodeJS
17+
1618
Install both `prettier` and `prettier-plugin-solidity`:
1719

1820
```Bash
@@ -39,6 +41,35 @@ Or you can use it as part of your linting to check that all your code is prettif
3941

4042
> Prettier Solidity only works with valid code. If there is a syntax error, nothing will be done and a parser error will be thrown.
4143
44+
### Using in the Browser
45+
46+
_Added in v1.0.1_
47+
48+
To use this package in the browser, you need to load Prettier's standalone bundle before loading the build provided in this package.
49+
50+
```html
51+
<script src="https://unpkg.com/prettier@latest"></script>
52+
<script src="https://unpkg.com/prettier-plugin-solidity@latest"></script>
53+
```
54+
55+
Prettier's unpkg field points to `https://unpkg.com/prettier/standalone.js`, in a similar way this plugin points to `https://unpkg.com/prettier-plugin-solidity/dist/standalone.js`.
56+
57+
Once the scripts are loaded you will have access the globals `prettier` and `prettierPlugins`.
58+
59+
We follow Prettier's strategy for populating their plugins in the object `prettierPlugins`, you can load other plugins like `https://unpkg.com/prettier@2.8.0/parser-markdown.js` and Prettier will have access to multiple parsers.
60+
61+
```html
62+
<script>
63+
const originalCode = 'contract Foo {}';
64+
const formattedCode = prettier.format(originalCode, {
65+
parser: 'solidity-parse',
66+
plugins: prettierPlugins
67+
});
68+
</script>
69+
```
70+
71+
For more details and please have a look at [Prettier's documentation](https://prettier.io/docs/en/browser.html).
72+
4273
## Configuration File
4374

4475
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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
const TEST_STANDALONE = Boolean(process.env.TEST_STANDALONE);
22

33
module.exports = {
4-
moduleNameMapper: {
5-
'^prettier$': TEST_STANDALONE
6-
? '<rootDir>/node_modules/prettier/standalone'
7-
: '<rootDir>/node_modules/prettier'
8-
},
94
runner: 'jest-light-runner',
105
setupFiles: ['<rootDir>/tests/config/setup.js'],
116
snapshotSerializers: [
127
'jest-snapshot-serializer-raw',
138
'jest-snapshot-serializer-ansi'
149
],
1510
testEnvironment: 'node',
11+
// ignore console warnings in TEST_STANDALONE
12+
silent: TEST_STANDALONE,
13+
testPathIgnorePatterns: TEST_STANDALONE
14+
? [
15+
// Standalone mode doesn't have default options.
16+
// This has been reported https://github.com/prettier/prettier/issues/11107
17+
'tests/format/RespectDefaultOptions'
18+
]
19+
: [],
1620
testMatch: [
1721
'<rootDir>/tests/format/**/jsfmt.spec.js',
1822
'<rootDir>/tests/unit/**/*.js'

0 commit comments

Comments
 (0)