├── .babelrc ├── .eslintignore ├── .eslintrc ├── .gitattributes ├── .github ├── dependabot.yml └── workflows │ ├── build.yml │ ├── publish.yml │ └── release.yml ├── .gitignore ├── .npmignore ├── .npmrc ├── .nvmrc ├── CHANGELOG.md ├── LICENSE ├── README.md ├── gulpfile.js ├── package.json ├── scripts ├── build │ ├── index.js │ └── rollup.config.js ├── changelog │ └── index.js ├── clean │ └── index.js ├── config.js ├── lint │ └── index.js ├── log │ └── index.js ├── release │ └── index.js └── test │ └── index.js ├── src ├── .eslintrc ├── index.d.ts ├── index.js └── rollup-plugin-prettier.js └── test ├── .eslintrc ├── fixtures ├── .eslintrc ├── bundle.js └── prettier.config.js ├── index.spec.js ├── it └── it.spec.js ├── rollup-plugin-prettier.spec.js └── utils ├── install-warn-spy.js ├── join-lines.js ├── verify-warn-logs-because-of-source-map.js └── verify-warn-logs-not-triggered.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ 4 | "@babel/preset-env", { 5 | "modules": "cjs", 6 | "targets": { 7 | "node": "14" 8 | } 9 | } 10 | ] 11 | ], 12 | 13 | "plugins": [ 14 | "add-module-exports" 15 | ], 16 | 17 | "env": { 18 | "rollup": { 19 | "presets": [ 20 | [ 21 | "@babel/preset-env", { 22 | "modules": false, 23 | "targets": { 24 | "node": "14" 25 | } 26 | } 27 | ] 28 | ] 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | ## 2 | # The MIT License (MIT) 3 | # 4 | # Copyright (c) 2017-2023 Mickael Jeanroy 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | ## 24 | 25 | # NPM 26 | /dist 27 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended", 4 | "airbnb-base" 5 | ], 6 | "parserOptions": { 7 | "ecmaVersion": "latest" 8 | }, 9 | "env": { 10 | "es6": true, 11 | "node": true 12 | }, 13 | "rules": { 14 | "max-len": [2, 140, 2], 15 | 16 | "import/prefer-default-export": "off", 17 | 18 | "import/no-extraneous-dependencies": ["error", { 19 | "devDependencies": [ 20 | "scripts/**/*.js", 21 | "test/**/*.js", 22 | "gulpfile.js" 23 | ] 24 | }], 25 | 26 | "no-console": [2, { 27 | "allow": ["warn", "error"] 28 | }], 29 | 30 | "quote-props": ["error", "consistent-as-needed"], 31 | "no-plusplus": "off", 32 | "no-underscore-dangle": "off", 33 | "no-use-before-define": ["error", { 34 | "functions": false, 35 | "classes": true, 36 | "variables": true, 37 | "allowNamedExports": true 38 | }], 39 | 40 | "valid-jsdoc": [2, { 41 | "requireReturn": true, 42 | "requireParamDescription": true, 43 | "requireReturnDescription": true, 44 | "prefer": { 45 | "return": "return", 46 | "arg": "param", 47 | "argument": "param" 48 | }, 49 | "preferType": { 50 | "object": "object" 51 | } 52 | }] 53 | }, 54 | 55 | "overrides": [ 56 | { 57 | "files": [ 58 | "**/*.ts" 59 | ], 60 | "env": { 61 | "browser": false, 62 | "es6": true, 63 | "node": true 64 | }, 65 | "extends": [ 66 | "eslint:recommended", 67 | "plugin:@typescript-eslint/eslint-recommended", 68 | "plugin:@typescript-eslint/recommended" 69 | ], 70 | "parser": "@typescript-eslint/parser", 71 | "plugins": [ 72 | "@typescript-eslint" 73 | ], 74 | "rules": { 75 | "indent": ["error", 2], 76 | "linebreak-style": ["error", "unix"], 77 | "quotes": ["error", "single"], 78 | "comma-dangle": ["error", "always-multiline"], 79 | "@typescript-eslint/no-explicit-any": 0 80 | } 81 | } 82 | ] 83 | } 84 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ## 2 | # The MIT License (MIT) 3 | # 4 | # Copyright (c) 2017-2023 Mickael Jeanroy 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | ## 24 | 25 | *.js text eol=lf 26 | *.ts text eol=lf 27 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | ## 2 | # The MIT License (MIT) 3 | # 4 | # Copyright (c) 2017-2023 Mickael Jeanroy 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | ## 24 | 25 | # See: https://docs.github.com/en/code-security/supply-chain-security/configuration-options-for-dependency-updates 26 | 27 | version: 2 28 | updates: 29 | - package-ecosystem: npm 30 | directory: "/" 31 | schedule: 32 | interval: daily 33 | open-pull-requests-limit: 10 34 | groups: 35 | typescript-eslint: 36 | patterns: 37 | - "@typescript-eslint/*" 38 | babel: 39 | patterns: 40 | - "@babel/*" 41 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | ## 2 | # The MIT License (MIT) 3 | # 4 | # Copyright (c) 2017-2023 Mickael Jeanroy 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | ## 24 | 25 | name: CI 26 | on: [push] 27 | jobs: 28 | build: 29 | runs-on: ${{ matrix.os }} 30 | strategy: 31 | matrix: 32 | os: [ windows-latest, ubuntu-latest, macos-latest ] 33 | node: [ 18, 19, 20, 21, 22 ] 34 | steps: 35 | - uses: actions/checkout@v4.2.2 36 | - uses: actions/setup-node@v4.2.0 37 | name: Set up NodeJS 38 | with: 39 | node-version: ${{ matrix.node }} 40 | - name: Install 41 | run: npm install 42 | - name: Test 43 | run: npm test -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | ## 2 | # The MIT License (MIT) 3 | # 4 | # Copyright (c) 2016-2023 Mickael Jeanroy 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | ## 24 | 25 | name: Publish 26 | 27 | on: 28 | workflow_dispatch: 29 | inputs: 30 | version: 31 | description: "Version to publish" 32 | required: true 33 | type: string 34 | dry_run: 35 | description: "Dry run" 36 | required: true 37 | type: boolean 38 | default: true 39 | 40 | jobs: 41 | build: 42 | runs-on: ubuntu-latest 43 | steps: 44 | - uses: actions/checkout@v4.2.2 45 | with: 46 | fetch-tags: true 47 | - uses: actions/setup-node@v4.2.0 48 | name: Set up NodeJS 49 | with: 50 | node-version: 22 51 | registry-url: 'https://registry.npmjs.org' 52 | - name: Fetch all tags 53 | run: | 54 | git fetch --prune --unshallow --tags 55 | git tag --list --sort=committerdate 56 | - name: Checkout tag 57 | run: git checkout ${{ inputs.version }} 58 | - name: npm install 59 | run: npm install --no-audit --no-fund 60 | - name: Publish (test) 61 | run: npm publish --dry-run 62 | - name: Publish 63 | if: inputs.dry_run == false 64 | run: npm publish 65 | env: 66 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | ## 2 | # The MIT License (MIT) 3 | # 4 | # Copyright (c) 2016-2023 Mickael Jeanroy 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | ## 24 | 25 | name: Release 26 | 27 | on: 28 | workflow_dispatch: 29 | inputs: 30 | version: 31 | description: "Release version" 32 | required: true 33 | type: choice 34 | options: 35 | - patch 36 | - minor 37 | - major 38 | 39 | jobs: 40 | build: 41 | runs-on: ubuntu-latest 42 | steps: 43 | - uses: actions/checkout@v4.2.2 44 | - uses: actions/setup-node@v4.2.0 45 | name: Set up NodeJS 46 | with: 47 | node-version: 22 48 | - name: Configure Git User 49 | run: | 50 | git config user.email "mickael.jeanroy@gmail.com" 51 | git config user.name "Mickael Jeanroy" 52 | - name: Install 53 | run: npm install 54 | - name: Test 55 | run: npm test 56 | - name: Release 57 | run: npm run release:${{ inputs.version }} 58 | - name: Git log 59 | run: | 60 | git log -n5 --oneline 61 | git tag --sort=-creatordate 62 | - name: Git push 63 | run: | 64 | git push origin ${{ github.ref_name }} 65 | git push --tags -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## 2 | # The MIT License (MIT) 3 | # 4 | # Copyright (c) 2017-2023 Mickael Jeanroy 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | ## 24 | 25 | # IDE 26 | /.idea 27 | /*.iml 28 | 29 | # NPM 30 | /node_modules/ 31 | /npm-debug.log 32 | /package-lock.json 33 | 34 | # Build 35 | /dist/ 36 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | ## 2 | # The MIT License (MIT) 3 | # 4 | # Copyright (c) 2017-2023 Mickael Jeanroy 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | ## 24 | 25 | # NPM modules 26 | node_modules/ 27 | 28 | # Do not send dev files 29 | /.idea 30 | /package 31 | /test 32 | /src 33 | /scripts 34 | /.babelrc 35 | /.eslintrc 36 | /.eslintignore 37 | /.gitattributes 38 | /.github 39 | /.npmrc 40 | /.nvmrc 41 | /.travis.yml 42 | /gulpfile.js 43 | /*.tar.gz 44 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | ## 2 | # The MIT License (MIT) 3 | # 4 | # Copyright (c) 2017-2023 Mickael Jeanroy 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | ## 24 | 25 | package-lock=false 26 | save-exact=true 27 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 22.14.0 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017-2023 Mickael Jeanroy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-prettier 2 | 3 | [![Greenkeeper badge](https://badges.greenkeeper.io/mjeanroy/rollup-plugin-prettier.svg)](https://greenkeeper.io/) 4 | [![Build Status](https://travis-ci.org/mjeanroy/rollup-plugin-prettier.svg?branch=master)](https://travis-ci.org/mjeanroy/rollup-plugin-prettier) 5 | [![Npm version](https://badge.fury.io/js/rollup-plugin-prettier.svg)](https://badge.fury.io/js/rollup-plugin-prettier) 6 | 7 | Rollup plugin that can be used to run [prettier](http://npmjs.com/package/prettier) on the final bundle. 8 | 9 | ## How to use 10 | 11 | Install the plugin with NPM: 12 | 13 | `npm install --save-dev prettier rollup-plugin-prettier` 14 | 15 | Then add it to your rollup configuration: 16 | 17 | ```javascript 18 | const path = require('path'); 19 | const prettier = require('rollup-plugin-prettier'); 20 | 21 | module.exports = { 22 | input: path.join(__dirname, 'src', 'index.js'), 23 | 24 | output: { 25 | file: path.join(__dirname, 'dist', 'bundle.js'), 26 | }, 27 | 28 | plugins: [ 29 | // Run plugin with prettier options. 30 | prettier({ 31 | tabWidth: 2, 32 | singleQuote: false, 33 | }), 34 | ], 35 | }; 36 | ``` 37 | 38 | ## Source Maps 39 | 40 | If source map is enabled in the global rollup options, then a source map will be generated on the formatted bundle (except if sourcemap are explicitely disabled in the prettier options). 41 | 42 | Note that this may take some time since `prettier` package is not able to generate a sourcemap and this plugin must compute the diff between the original bundle and the formatted result and generate the corresponding sourcemap: for this reason, sourcemap are disabled by default. 43 | 44 | Here is an example: 45 | 46 | ```javascript 47 | const path = require('path'); 48 | const prettier = require('rollup-plugin-prettier'); 49 | 50 | module.exports = { 51 | input: path.join(__dirname, 'src', 'index.js'), 52 | 53 | output: { 54 | file: path.join(__dirname, 'dist', 'bundle.js'), 55 | sourcemap: true, 56 | }, 57 | 58 | plugins: [ 59 | prettier({ 60 | sourcemap: true, // Can also be disabled/enabled here. 61 | }), 62 | ], 63 | }; 64 | ``` 65 | 66 | ## ChangeLogs 67 | 68 | - 4.1.2 69 | - Fix a bug where prettier configuration file were not correctly resolved since `prettier@^3.1.1` (same as [this bug in prettier-vscode](https://github.com/prettier/prettier-vscode/issues/3104), broken since [this commit in prettier](https://github.com/prettier/prettier/pull/15363)). 70 | - Dependency upgrades 71 | - 4.1.1 72 | - Fix support rollup for ^4.0.0, that was intended to be introduced in `4.1.0` 73 | - Dependency upgrades 74 | - 4.1.0 75 | - ~Support rollup ^4.0.0~ 76 | - Dependency upgrades 77 | - 4.0.0 78 | - Support prettier ^3.0.0 79 | - Dependency upgrades 80 | - 3.1.0 81 | - Reformat asynchrnously to prepare support for prettier ^3.0.0 82 | - 3.0.0 83 | - Support rollup ^3.0.0 84 | - 2.2.2 85 | - Remove IDE files from published package 86 | - 2.2.1 87 | - Fix typings 88 | - Dependency updates 89 | - 2.2.0 90 | - Add typings ([#696](https://github.com/mjeanroy/rollup-plugin-prettier/pull/696), thanks [@pastelmind](https://github.com/pastelmind)!) 91 | - Dependency updates 92 | - 2.1.0 93 | - Add option to not log warning due to heavy diff computation ([#435](https://github.com/mjeanroy/rollup-plugin-prettier/pull/435)) 94 | - Dependency updates 95 | - 2.0.0 96 | - Support node >= 10 (still support node >= 6, but it not tested anymore). 97 | - Update dev dependencies. 98 | - 1.0.0 99 | - **Breaking Change**: `prettier` dependency is now a peer dependency instead of a "direct" dependency: user of the plugin can choose to use prettier 1.x.x or prettier 2.x.x (note that this plugin should be compatible with all versions of prettier). 100 | - Support node >= 6. 101 | - Support rollup >= 1.0.0 102 | - Remove support of deprecated option (`sourceMap` was deprecated in favor of `sourcemap`). 103 | - 0.7.0 104 | - Dependency updates. 105 | - Update rollup peer dependency version. 106 | - 0.6.0 107 | - Add support for rollup >= 1 (thanks to [@Andarist](https://github.com/Andarist), see [#211](https://github.com/mjeanroy/rollup-plugin-prettier/pull/211)) 108 | - Various dependency updates. 109 | - 0.5.0 110 | - Support resolution of prettier config file (see [#195](https://github.com/mjeanroy/rollup-plugin-prettier/issues/195)). 111 | - Various dependency updates. 112 | - 0.4.0 113 | - Add compatibility with rollup >= 0.53 with output `sourcemap` option (see [rollup #1583](https://github.com/rollup/rollup/issues/1583)). 114 | - Avoid side-effect and do not change the plugin options (see [032be5](https://github.com/mjeanroy/rollup-plugin-prettier/commit/032be56317ab83cd87c2460f1dadc05a617c0d12)). 115 | - Various dependency updates. 116 | - 0.3.0 117 | - Support new `sourcemap` (lowercase) option of rollup. 118 | - Sourcemap can now be activated/disabled in the plugin options. 119 | - 0.2.0 120 | - Dependency update (`magic-string`) 121 | - 0.1.0 First release 122 | 123 | ## License 124 | 125 | MIT License (MIT) 126 | 127 | ## Contributing 128 | 129 | If you find a bug or think about enhancement, feel free to contribute and submit an issue or a pull request. 130 | 131 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | require('@babel/register')({ 26 | ignore: [ 27 | /node_modules/, 28 | ], 29 | }); 30 | 31 | const gulp = require('gulp'); 32 | const clean = require('./scripts/clean'); 33 | const lint = require('./scripts/lint'); 34 | const build = require('./scripts/build'); 35 | const test = require('./scripts/test'); 36 | const release = require('./scripts/release'); 37 | const changelog = require('./scripts/changelog'); 38 | 39 | const prebuild = gulp.series(clean, lint); 40 | const pretest = gulp.series(prebuild, build); 41 | const prerelease = gulp.series(pretest, test.test); 42 | 43 | module.exports = { 44 | 'clean': clean, 45 | 'lint': lint, 46 | 'build': gulp.series(prebuild, build), 47 | 'tdd': gulp.series(pretest, test.tdd), 48 | 'test': gulp.series(pretest, test.test), 49 | 'release:patch': gulp.series(prerelease, release.patch), 50 | 'release:minor': gulp.series(prerelease, release.minor), 51 | 'release:major': gulp.series(prerelease, release.major), 52 | 'changelog': changelog, 53 | }; 54 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-prettier", 3 | "version": "4.1.2", 4 | "description": "Run prettier formatter with rollup", 5 | "main": "dist/index.js", 6 | "author": "Mickael Jeanroy ", 7 | "license": "MIT", 8 | "repository": { 9 | "type": "git", 10 | "url": "git@github.com:mjeanroy/rollup-plugin-prettier.git" 11 | }, 12 | "types": "dist/index.d.ts", 13 | "scripts": { 14 | "clean": "gulp clean", 15 | "lint": "gulp lint", 16 | "build": "gulp build", 17 | "tdd": "gulp tdd", 18 | "test": "gulp test", 19 | "release": "gulp release:minor", 20 | "release:patch": "gulp release:patch", 21 | "release:minor": "gulp release:minor", 22 | "release:major": "gulp release:major", 23 | "changelog": "gulp changelog" 24 | }, 25 | "keywords": [ 26 | "rollup", 27 | "rollup-plugin", 28 | "prettier" 29 | ], 30 | "peerDependencies": { 31 | "prettier": "^1.0.0 || ^2.0.0 || ^3.0.0", 32 | "rollup": "^1.0.0 || ^2.0.0 || ^3.0.0 || ^4.0.0" 33 | }, 34 | "dependencies": { 35 | "@types/prettier": "^1.0.0 || ^2.0.0 || ^3.0.0", 36 | "diff": "8.0.2", 37 | "lodash.hasin": "4.5.2", 38 | "lodash.isempty": "4.4.0", 39 | "lodash.isnil": "4.0.0", 40 | "lodash.omitby": "4.6.0", 41 | "magic-string": "0.30.17" 42 | }, 43 | "devDependencies": { 44 | "@babel/core": "7.27.3", 45 | "@babel/parser": "7.27.3", 46 | "@babel/preset-env": "7.27.2", 47 | "@babel/register": "7.27.1", 48 | "@rollup/plugin-babel": "6.0.4", 49 | "@typescript-eslint/eslint-plugin": "8.33.0", 50 | "@typescript-eslint/parser": "8.33.0", 51 | "ansi-colors": "4.1.3", 52 | "babel-plugin-add-module-exports": "1.0.4", 53 | "eslint": "8.57.0", 54 | "eslint-config-airbnb-base": "15.0.0", 55 | "eslint-plugin-import": "2.31.0", 56 | "fancy-log": "2.0.0", 57 | "globalthis": "1.0.4", 58 | "gulp": "5.0.0", 59 | "gulp-conventional-changelog": "5.0.0", 60 | "gulp-jasmine": "4.0.0", 61 | "jasmine": "3.10.0", 62 | "jasmine-core": "3.10.1", 63 | "lodash.startswith": "4.2.1", 64 | "prettier": "3.5.3", 65 | "q": "1.5.1", 66 | "rimraf": "6.0.1", 67 | "rollup": "4.41.1", 68 | "rollup-plugin-strip-banner": "3.1.0", 69 | "tmp": "0.2.3", 70 | "typescript": "5.8.3" 71 | }, 72 | "engines": { 73 | "node": ">=6.0.0" 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /scripts/build/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | const path = require('node:path'); 26 | const gulp = require('gulp'); 27 | const rollup = require('rollup'); 28 | const rollupConfig = require('./rollup.config'); 29 | const config = require('../config'); 30 | 31 | module.exports = gulp.series( 32 | buildOutput, 33 | copyTypings, 34 | ); 35 | 36 | // eslint-disable-next-line require-jsdoc 37 | function buildOutput() { 38 | return rollup.rollup(rollupConfig).then((bundle) => ( 39 | Promise.all(rollupConfig.output.map((output) => ( 40 | bundle.write(output) 41 | ))) 42 | )); 43 | } 44 | 45 | // eslint-disable-next-line require-jsdoc 46 | function copyTypings() { 47 | return gulp.src(path.join(config.src, 'index.d.ts')).pipe(gulp.dest(config.dist)); 48 | } 49 | -------------------------------------------------------------------------------- /scripts/build/rollup.config.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | // Required with prettier >= 2.3.0 with node 11 26 | require('globalthis').shim(); 27 | 28 | const path = require('node:path'); 29 | const fs = require('node:fs'); 30 | const prettier = require('prettier'); 31 | const stripBanner = require('rollup-plugin-strip-banner'); 32 | const babel = require('@rollup/plugin-babel').default; 33 | const config = require('../config'); 34 | const pkg = require('../../package.json'); 35 | 36 | module.exports = { 37 | input: path.join(config.src, 'index.js'), 38 | 39 | output: [ 40 | { 41 | format: 'cjs', 42 | file: path.join(config.dist, 'index.js'), 43 | exports: 'default', 44 | banner() { 45 | const rawBanner = fs.readFileSync( 46 | path.join(config.root, 'LICENSE'), 47 | ); 48 | 49 | const commentedBanner = rawBanner.toString() 50 | .trim() 51 | .split('\n') 52 | .map((line) => ` * ${line}`) 53 | .map((line) => line.trim()); 54 | 55 | const blockComment = [ 56 | '/**', 57 | ...commentedBanner, 58 | ' */', 59 | '', 60 | ]; 61 | 62 | return blockComment.join('\n'); 63 | }, 64 | }, 65 | ], 66 | 67 | plugins: [ 68 | stripBanner(), 69 | 70 | babel({ 71 | envName: 'rollup', 72 | babelHelpers: 'bundled', 73 | }), 74 | 75 | { 76 | renderChunk(code) { 77 | return prettier.format(code, { 78 | parser: 'babel', 79 | }); 80 | }, 81 | }, 82 | ], 83 | 84 | external: [ 85 | // Node dependencies. 86 | ...['path', 'fs'], 87 | 88 | // Does not include any dependencies. 89 | ...Object.keys(pkg.dependencies), 90 | ...Object.keys(pkg.peerDependencies), 91 | ], 92 | }; 93 | -------------------------------------------------------------------------------- /scripts/changelog/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | const path = require('node:path'); 26 | const gulp = require('gulp'); 27 | const conventionalChangelog = require('gulp-conventional-changelog'); 28 | const config = require('../config'); 29 | 30 | module.exports = function changelog() { 31 | const changelogFile = path.join(config.root, 'CHANGELOG.md'); 32 | return gulp.src(changelogFile, { buffer: false }) 33 | .pipe(conventionalChangelog({ releaseCount: 0 })) 34 | .pipe(gulp.dest(config.root)); 35 | }; 36 | -------------------------------------------------------------------------------- /scripts/clean/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | const { rimraf } = require('rimraf'); 26 | const config = require('../config'); 27 | 28 | module.exports = function clean() { 29 | return rimraf(config.dist); 30 | }; 31 | -------------------------------------------------------------------------------- /scripts/config.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | const path = require('node:path'); 26 | 27 | const ROOT = path.join(__dirname, '..'); 28 | 29 | module.exports = { 30 | root: ROOT, 31 | src: path.join(ROOT, 'src'), 32 | test: path.join(ROOT, 'test'), 33 | scripts: path.join(ROOT, 'scripts'), 34 | dist: path.join(ROOT, 'dist'), 35 | pkg: path.join(ROOT, 'package.json'), 36 | }; 37 | -------------------------------------------------------------------------------- /scripts/lint/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | const path = require('node:path'); 26 | const config = require('../config'); 27 | const log = require('../log'); 28 | 29 | module.exports = function lint() { 30 | const nodeVersion = process.versions.node; 31 | const major = Number(nodeVersion.split('.')[0]); 32 | if (major <= 14) { 33 | log.debug(`Skipping ESLint because of node version compatibility (currenly in used: ${nodeVersion})`); 34 | return Promise.resolve(); 35 | } 36 | 37 | const sourcesOf = (ext) => ([ 38 | path.join(config.root, `*.${ext}`), 39 | path.join(config.src, '**', `*.${ext}`), 40 | path.join(config.test, '**', `*.${ext}`), 41 | path.join(config.scripts, '**', `*.${ext}`), 42 | ]); 43 | 44 | const inputs = [ 45 | ...sourcesOf('js'), 46 | ...sourcesOf('ts'), 47 | ]; 48 | 49 | log.debug('Linting files: '); 50 | 51 | inputs.forEach((f) => { 52 | log.debug(` ${f}`); 53 | }); 54 | 55 | // eslint-disable-next-line global-require 56 | const { ESLint } = require('eslint'); 57 | 58 | // eslint-disable-next-line global-require 59 | const fancyLog = require('fancy-log'); 60 | 61 | const eslint = new ESLint({ 62 | errorOnUnmatchedPattern: false, 63 | }); 64 | 65 | const lintFiles = eslint.lintFiles(inputs); 66 | const loadFormatter = eslint.loadFormatter('stylish'); 67 | 68 | return Promise.all([lintFiles, loadFormatter]).then(([results, formatter]) => { 69 | for (let i = 0; i < results.length; ++i) { 70 | const lintResult = results[i]; 71 | if (lintResult.warningCount > 0 || lintResult.errorCount > 0 || lintResult.fatalErrorCount > 0) { 72 | fancyLog(formatter.format(results)); 73 | throw new Error('ESLintError'); 74 | } 75 | } 76 | }); 77 | }; 78 | -------------------------------------------------------------------------------- /scripts/log/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | const log = require('fancy-log'); 26 | const colors = require('ansi-colors'); 27 | 28 | /** 29 | * Log message to output using `DEBUG` level. 30 | * 31 | * @param {string} msg Message to log. 32 | * @return {void} 33 | */ 34 | function debug(msg) { 35 | log(colors.grey(msg)); 36 | } 37 | 38 | /** 39 | * Log message to output using `ERROR` level. 40 | * 41 | * @param {string} msg Message to log. 42 | * @return {void} 43 | */ 44 | function error(msg) { 45 | log(colors.red(msg)); 46 | } 47 | 48 | module.exports = { 49 | debug, 50 | error, 51 | }; 52 | -------------------------------------------------------------------------------- /scripts/release/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | const util = require('node:util'); 26 | const exec = util.promisify(require('node:child_process').exec); 27 | const gulp = require('gulp'); 28 | const log = require('../log'); 29 | const config = require('../config'); 30 | 31 | /** 32 | * Run command with a pre-configured `cwd` (current working directory) 33 | * and an encoding set to `utf-8'. 34 | * 35 | * @param {string} cmd Command to run. 36 | * @return {Promise} A promise resolved when the command has been executed. 37 | */ 38 | function run(cmd) { 39 | log.debug(` ${cmd}...`); 40 | return exec(cmd, { 41 | cwd: config.root, 42 | encoding: 'utf-8', 43 | }); 44 | } 45 | 46 | /** 47 | * Update version in number in `package.json` file. 48 | * 49 | * @param {'major'|'minor'|'patch'} type The semver level identifier (`major`, `minor` or `patch`). 50 | * @return {Promise} A promise resolved when the version bumped has been executed. 51 | */ 52 | function bumpVersion(type) { 53 | return run(`git add -f "${config.dist}"`).then(() => ( 54 | run(`npm version "${type}" -f -m 'release: release version'`) 55 | )); 56 | } 57 | 58 | /** 59 | * Prepare the next release cycle: 60 | * - Remove the `dist` directory containing bundle tagged on given version. 61 | * - Create a new commit preparing the next release. 62 | * 63 | * @return {Promise} A promise resolved when the task has been executed. 64 | */ 65 | function prepareNextRelease() { 66 | return run(`git rm -r "${config.dist}"`).then(() => ( 67 | run("git commit -m 'release: prepare next release'") 68 | )); 69 | } 70 | 71 | /** 72 | * Create the release task. 73 | * 74 | * @param {'major'|'minor'|'patch'} level The version level upgrade. 75 | * @return {function} The release task function. 76 | */ 77 | function createReleaseTask(level) { 78 | /** 79 | * Prepare the release: upgrade version number according to 80 | * the specified level. 81 | * 82 | * @return {Promise} A promise resolved when the release is done. 83 | */ 84 | function doRelease() { 85 | return bumpVersion(level); 86 | } 87 | 88 | return gulp.series( 89 | doRelease, 90 | prepareNextRelease, 91 | ); 92 | } 93 | 94 | module.exports = { 95 | patch: createReleaseTask('patch'), 96 | minor: createReleaseTask('minor'), 97 | major: createReleaseTask('major'), 98 | }; 99 | -------------------------------------------------------------------------------- /scripts/test/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | const path = require('node:path'); 26 | const gulp = require('gulp'); 27 | const jasmine = require('gulp-jasmine'); 28 | const build = require('../build'); 29 | const config = require('../config'); 30 | 31 | /** 32 | * Run unit test suite and exit. 33 | * 34 | * @return {WritableStream} The pipe stream. 35 | */ 36 | function test() { 37 | const spec = path.join(config.test, '**', '*.spec.js'); 38 | return gulp.src(spec).pipe(jasmine()); 39 | } 40 | 41 | /** 42 | * Watch for changes, and run unit test suite on every change. 43 | * 44 | * @param {function} done The `done` callback. 45 | * @return {void} 46 | */ 47 | function tdd(done) { 48 | gulp.watch(path.join(config.dist, '**', '*.js'), test); 49 | gulp.watch(path.join(config.src, '**', '*.js'), build); 50 | gulp.watch(path.join(config.test, '**', '*.js'), test); 51 | done(); 52 | } 53 | 54 | module.exports = { 55 | tdd, 56 | test, 57 | }; 58 | -------------------------------------------------------------------------------- /src/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parserOptions": { 3 | "sourceType": "module" 4 | } 5 | } -------------------------------------------------------------------------------- /src/index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 @pastelmind 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * @file Type definition for rollup-plugin-prettier 27 | */ 28 | 29 | import type { Options as PrettierOptions } from 'prettier'; 30 | import type { Plugin } from 'rollup'; 31 | 32 | declare namespace prettier { 33 | interface Options extends PrettierOptions { 34 | /** 35 | * Directory to look for a Prettier config file. 36 | * 37 | * If omitted, defaults to `process.cwd()`. 38 | */ 39 | cwd?: string; 40 | 41 | /** 42 | * Whether to generate a sourcemap. 43 | * 44 | * Note: This may take some time because rollup-plugin-prettier diffs the 45 | * output to manually generate a sourcemap. 46 | */ 47 | sourcemap?: boolean | 'silent'; 48 | } 49 | } 50 | 51 | declare function prettier(options?: prettier.Options): Plugin; 52 | 53 | export = prettier; 54 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | import { RollupPluginPrettier } from './rollup-plugin-prettier'; 26 | 27 | /** 28 | * Create rollup plugin compatible with rollup >= 1.0.0 29 | * 30 | * @param {Object} options Plugin options. 31 | * @return {Object} Plugin instance. 32 | */ 33 | export default function rollupPluginPrettier(options) { 34 | const plugin = new RollupPluginPrettier(options); 35 | 36 | return { 37 | /** 38 | * Plugin name (used by rollup for error messages and warnings). 39 | * @type {string} 40 | */ 41 | name: plugin.name, 42 | 43 | /** 44 | * Function called by `rollup` before generating final bundle. 45 | * 46 | * @param {string} source Souce code of the final bundle. 47 | * @param {Object} chunkInfo Chunk info. 48 | * @param {Object} outputOptions Output option. 49 | * @return {Promise} The result containing a `code` property and, if a enabled, a `map` property. 50 | */ 51 | renderChunk(source, chunkInfo, outputOptions) { 52 | return plugin.reformat(source, { 53 | sourcemap: outputOptions.sourcemap, 54 | }); 55 | }, 56 | }; 57 | } 58 | -------------------------------------------------------------------------------- /src/rollup-plugin-prettier.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | import path from 'path'; 26 | import hasIn from 'lodash.hasin'; 27 | import isEmpty from 'lodash.isempty'; 28 | import isNil from 'lodash.isnil'; 29 | import omitBy from 'lodash.omitby'; 30 | import MagicString from 'magic-string'; 31 | import * as diff from 'diff'; 32 | import prettier from 'prettier'; 33 | 34 | /** 35 | * The plugin options that are currently supported. 36 | * @type {Set} 37 | */ 38 | const OPTIONS = new Set([ 39 | 'sourcemap', 40 | 'cwd', 41 | ]); 42 | 43 | /** 44 | * Resolve prettier config, using `resolveConfig` by default, switch to `resolveConfigSync` otherwise. 45 | * If none of these methods are available, returns `null` as a fallback. 46 | * 47 | * @param {string} cwd The current working directory. 48 | * @returns {Promise} A promise resolved with prettier configuration, or null. 49 | */ 50 | function resolvePrettierConfig(cwd) { 51 | // Since prettier 3.1.1, the resolver searches from a file path, not a directory. 52 | // Let's fake it by concatenating with a file name. 53 | const fromFile = path.join(cwd, '__noop__.js'); 54 | 55 | // Be careful, `resolveConfig` function does not exist on old version of prettier. 56 | if (prettier.resolveConfig) { 57 | return prettier.resolveConfig(fromFile); 58 | } 59 | 60 | if (prettier.resolveConfigSync) { 61 | return Promise.resolve(prettier.resolveConfigSync(cwd)); 62 | } 63 | 64 | return Promise.resolve(null); 65 | } 66 | 67 | /** 68 | * The plugin. 69 | * 70 | * @class 71 | */ 72 | export class RollupPluginPrettier { 73 | /** 74 | * Initialize plugin & prettier. 75 | * 76 | * @param {Object} options Initalization option. 77 | */ 78 | constructor(options = {}) { 79 | // Initialize plugin name. 80 | this.name = 'rollup-plugin-prettier'; 81 | 82 | // Initialize main options. 83 | this._options = Promise.resolve( 84 | omitBy((options), (value, key) => OPTIONS.has(key)), 85 | ); 86 | 87 | // Try to resolve config file if it exists 88 | const cwd = hasIn(options, 'cwd') ? options.cwd : process.cwd(); 89 | this._options = Promise.all([resolvePrettierConfig(cwd), this._options]).then((results) => ( 90 | Object.assign({}, ...results.map((result) => result || {})) 91 | )); 92 | 93 | // Reset empty options. 94 | this._options = this._options.then((opts) => ( 95 | isEmpty(opts) ? undefined : opts 96 | )); 97 | 98 | // Check if sourcemap is enabled by default. 99 | this._sourcemap = hasIn(options, 'sourcemap') ? options.sourcemap : null; 100 | } 101 | 102 | /** 103 | * Get the `sourcemap` value. 104 | * 105 | * @return {boolean} The `sourcemap` flag value. 106 | */ 107 | getSourcemap() { 108 | return this._sourcemap; 109 | } 110 | 111 | /** 112 | * Disable sourcemap. 113 | * 114 | * @return {void} 115 | */ 116 | enableSourcemap() { 117 | this._sourcemap = true; 118 | } 119 | 120 | /** 121 | * Reformat source code using prettier. 122 | * 123 | * @param {string} source The source code to reformat. 124 | * @param {{sourcemap: boolean} | null | undefined} outputOptions Output options. 125 | * @return {Promise<{code: string, map: object}|{code: string}>} The transformation result. 126 | */ 127 | reformat(source, outputOptions) { 128 | return this._options.then((options) => ( 129 | this._reformat(source, outputOptions, options) 130 | )); 131 | } 132 | 133 | /** 134 | * Reformat source code using prettier. 135 | * 136 | * @param {string} source The source code to reformat. 137 | * @param {{sourcemap: boolean} | null | undefined} outputOptions Output options. 138 | * @param {object} options Prettier options. 139 | * @returns {Promise<{code: string, map: object}|{code: string}>} The reformat response. 140 | * @private 141 | */ 142 | _reformat(source, outputOptions, options) { 143 | const opts = outputOptions || {}; 144 | const { sourcemap } = opts; 145 | return Promise.resolve(prettier.format(source, options)).then((output) => ( 146 | this._processOutput(source, sourcemap, output) 147 | )); 148 | } 149 | 150 | /** 151 | * Process output generated by prettier: 152 | * - Generate sourcemap if need be. 153 | * - Otherwise returns prettier output as chunk output. 154 | * 155 | * @param {string} source The source code to reformat. 156 | * @param {boolean|null|undefined} sourcemap If sourcemap should be generated or not. 157 | * @param {string} output Prettier output. 158 | * @returns {{code: string, map: object}|{code: string}} The reformat response. 159 | * @private 160 | */ 161 | _processOutput(source, sourcemap, output) { 162 | // Should we generate sourcemap? 163 | // The sourcemap option may be a boolean or any truthy value (such as a `string`). 164 | // Note that this option should be false by default as it may take a (very) long time. 165 | const defaultSourcemap = isNil(this._sourcemap) ? false : this._sourcemap; 166 | const outputSourcemap = isNil(sourcemap) ? defaultSourcemap : sourcemap; 167 | if (!outputSourcemap) { 168 | return { code: output }; 169 | } 170 | 171 | if (defaultSourcemap !== 'silent') { 172 | console.warn(`[${this.name}] Sourcemap is enabled, computing diff is required`); 173 | console.warn(`[${this.name}] This may take a moment (depends on the size of your bundle)`); 174 | } 175 | 176 | const magicString = new MagicString(source); 177 | const changes = diff.diffChars(source, output); 178 | 179 | if (changes && changes.length > 0) { 180 | let idx = 0; 181 | 182 | changes.forEach((part) => { 183 | if (part.added) { 184 | magicString.prependLeft(idx, part.value); 185 | idx -= part.count; 186 | } else if (part.removed) { 187 | magicString.remove(idx, idx + part.count); 188 | } 189 | 190 | idx += part.count; 191 | }); 192 | } 193 | 194 | return { 195 | code: magicString.toString(), 196 | map: magicString.generateMap({ 197 | hires: true, 198 | }), 199 | }; 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parserOptions": { 3 | "sourceType": "module" 4 | }, 5 | "env": { 6 | "jasmine": true 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parserOptions": { 3 | "sourceType": "module" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/bundle.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* eslint-disable */ 26 | 27 | export function sum(array) { return array.reduce((acc, x) => acc+x, 0) } 28 | -------------------------------------------------------------------------------- /test/fixtures/prettier.config.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | module.exports = { 26 | parser: 'babel', 27 | singleQuote: true, 28 | tabWidth: 1, 29 | }; 30 | -------------------------------------------------------------------------------- /test/index.spec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | import path from 'path'; 26 | import rollupPluginPrettier from '../src/index'; 27 | import { verifyWarnLogsBecauseOfSourcemap } from './utils/verify-warn-logs-because-of-source-map'; 28 | import { verifyWarnLogsNotTriggered } from './utils/verify-warn-logs-not-triggered'; 29 | import { installWarnSpy } from './utils/install-warn-spy'; 30 | import { joinLines } from './utils/join-lines'; 31 | 32 | describe('rollup-plugin-prettier', () => { 33 | beforeEach(() => { 34 | installWarnSpy(); 35 | }); 36 | 37 | it('should have a name', () => { 38 | const instance = rollupPluginPrettier(); 39 | expect(instance.name).toBe('rollup-plugin-prettier'); 40 | }); 41 | 42 | it('should run prettier', () => { 43 | const instance = rollupPluginPrettier({ 44 | parser: 'babel', 45 | }); 46 | 47 | const code = 'var foo=0;var test="hello world";'; 48 | const chunk = { isEntry: false, imports: [] }; 49 | const outputOptions = {}; 50 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 51 | expect(console.warn).not.toHaveBeenCalled(); 52 | expect(result.map).not.toBeDefined(); 53 | expect(result.code).toBe( 54 | joinLines([ 55 | 'var foo = 0;', 56 | 'var test = "hello world";', 57 | '', 58 | ]), 59 | ); 60 | }); 61 | }); 62 | 63 | it('should run prettier with sourcemap in output options', () => { 64 | const instance = rollupPluginPrettier({ 65 | parser: 'babel', 66 | }); 67 | 68 | const code = 'var foo=0;var test="hello world";'; 69 | const chunk = { isEntry: false, imports: [] }; 70 | const outputOptions = { sourcemap: true }; 71 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 72 | verifyWarnLogsBecauseOfSourcemap(); 73 | expect(result.map).toBeDefined(); 74 | expect(result.code).toBe( 75 | joinLines([ 76 | 'var foo = 0;', 77 | 'var test = "hello world";', 78 | '', 79 | ]), 80 | ); 81 | }); 82 | }); 83 | 84 | it('should run prettier with sourcemap in output options skipping warn message', () => { 85 | const instance = rollupPluginPrettier({ 86 | sourcemap: 'silent', 87 | parser: 'babel', 88 | }); 89 | 90 | const code = 'var foo=0;var test="hello world";'; 91 | const chunk = { isEntry: false, imports: [] }; 92 | const outputOptions = { sourcemap: true }; 93 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 94 | verifyWarnLogsNotTriggered(); 95 | expect(result.map).toBeDefined(); 96 | expect(result.code).toBe( 97 | joinLines([ 98 | 'var foo = 0;', 99 | 'var test = "hello world";', 100 | '', 101 | ]), 102 | ); 103 | }); 104 | }); 105 | 106 | it('should run prettier with sourcemap (lowercase) in plugin options', () => { 107 | const instance = rollupPluginPrettier({ 108 | sourcemap: true, 109 | parser: 'babel', 110 | }); 111 | 112 | const code = 'var foo=0;var test="hello world";'; 113 | const chunk = { isEntry: false, imports: [] }; 114 | const outputOptions = {}; 115 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 116 | verifyWarnLogsBecauseOfSourcemap(); 117 | expect(result.map).toBeDefined(); 118 | expect(result.code).toBe( 119 | joinLines([ 120 | 'var foo = 0;', 121 | 'var test = "hello world";', 122 | '', 123 | ]), 124 | ); 125 | }); 126 | }); 127 | 128 | it('should run prettier with sourcemap disabled in output options', () => { 129 | const instance = rollupPluginPrettier({ 130 | parser: 'babel', 131 | }); 132 | 133 | const code = 'var foo=0;var test="hello world";'; 134 | const chunk = { isEntry: false, imports: [] }; 135 | const outputOptions = { sourcemap: false }; 136 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 137 | verifyWarnLogsNotTriggered(); 138 | expect(result.map).not.toBeDefined(); 139 | expect(result.code).toBe( 140 | joinLines([ 141 | 'var foo = 0;', 142 | 'var test = "hello world";', 143 | '', 144 | ]), 145 | ); 146 | }); 147 | }); 148 | 149 | it('should run prettier with options', () => { 150 | const options = { 151 | parser: 'babel', 152 | singleQuote: true, 153 | }; 154 | 155 | const instance = rollupPluginPrettier(options); 156 | const code = 'var foo=0;var test="hello world";'; 157 | const chunk = { isEntry: false, imports: [] }; 158 | const outputOptions = { sourcemap: false }; 159 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 160 | expect(result.code).toBe( 161 | joinLines([ 162 | 'var foo = 0;', 163 | "var test = 'hello world';", 164 | '', 165 | ]), 166 | ); 167 | }); 168 | }); 169 | 170 | it('should remove unnecessary spaces', () => { 171 | const instance = rollupPluginPrettier({ 172 | parser: 'babel', 173 | }); 174 | 175 | const code = 'var foo = 0;\nvar test = "hello world";'; 176 | const chunk = { isEntry: false, imports: [] }; 177 | const outputOptions = { sourcemap: false }; 178 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 179 | expect(result.code).toBe( 180 | joinLines([ 181 | 'var foo = 0;', 182 | 'var test = "hello world";', 183 | '', 184 | ]), 185 | ); 186 | }); 187 | }); 188 | 189 | it('should add and remove characters', () => { 190 | const instance = rollupPluginPrettier({ 191 | parser: 'babel', 192 | }); 193 | 194 | const code = 'var foo = 0;var test = "hello world";'; 195 | const chunk = { isEntry: false, imports: [] }; 196 | const outputOptions = { sourcemap: false }; 197 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 198 | expect(result.code).toBe( 199 | joinLines([ 200 | 'var foo = 0;', 201 | 'var test = "hello world";', 202 | '', 203 | ]), 204 | ); 205 | }); 206 | }); 207 | 208 | it('should avoid side effect and do not modify plugin options', () => { 209 | const options = { 210 | parser: 'babel', 211 | sourcemap: false, 212 | }; 213 | 214 | const instance = rollupPluginPrettier(options); 215 | const code = 'var foo = 0;'; 216 | const chunk = { isEntry: false, imports: [] }; 217 | const outputOptions = {}; 218 | return instance.renderChunk(code, chunk, outputOptions).then(() => { 219 | // It should not have been touched. 220 | expect(options).toEqual({ 221 | parser: 'babel', 222 | sourcemap: false, 223 | }); 224 | }); 225 | }); 226 | 227 | it('should run prettier without sourcemap options', () => { 228 | const options = { 229 | parser: 'babel', 230 | sourcemap: false, 231 | }; 232 | 233 | const code = 'var foo = 0;'; 234 | const instance = rollupPluginPrettier(options); 235 | const chunk = { isEntry: false, imports: [] }; 236 | const outputOptions = {}; 237 | 238 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 239 | expect(result.map).toBeUndefined(); 240 | expect(options).toEqual({ 241 | parser: 'babel', 242 | sourcemap: false, 243 | }); 244 | }); 245 | }); 246 | 247 | it('should run prettier without sourcemap options and custom other options', () => { 248 | const options = { 249 | parser: 'babel', 250 | sourcemap: false, 251 | singleQuote: true, 252 | }; 253 | 254 | const code = joinLines([ 255 | 'var name = "John Doe";', 256 | ]); 257 | 258 | const instance = rollupPluginPrettier(options); 259 | const chunk = { isEntry: false, imports: [] }; 260 | const outputOptions = {}; 261 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 262 | expect(result.code).toEqual(joinLines([ 263 | 'var name = \'John Doe\';', 264 | '', 265 | ])); 266 | 267 | expect(options).toEqual({ 268 | parser: 'babel', 269 | sourcemap: false, 270 | singleQuote: true, 271 | }); 272 | }); 273 | }); 274 | 275 | it('should run prettier using config file from given current working directory', () => { 276 | const cwd = path.join(__dirname, 'fixtures'); 277 | const parser = 'babel'; 278 | const options = { 279 | parser, 280 | cwd, 281 | }; 282 | 283 | const instance = rollupPluginPrettier(options); 284 | const code = joinLines([ 285 | 'var name = "John Doe";', 286 | 'if (true) {', 287 | ' console.log(name);', 288 | '}', 289 | ]); 290 | 291 | const chunk = { isEntry: false, imports: [] }; 292 | const outputOptions = {}; 293 | return instance.renderChunk(code, chunk, outputOptions).then((result) => { 294 | expect(result.code).toEqual(joinLines([ 295 | 'var name = \'John Doe\';', 296 | 'if (true) {', 297 | ' console.log(name);', 298 | '}', 299 | '', 300 | ])); 301 | 302 | expect(options).toEqual({ 303 | parser, 304 | cwd, 305 | }); 306 | }); 307 | }); 308 | }); 309 | -------------------------------------------------------------------------------- /test/it/it.spec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | import fs from 'fs'; 26 | import path from 'path'; 27 | import tmp from 'tmp'; 28 | import Q from 'q'; 29 | import * as rollup from 'rollup'; 30 | import prettier from '../../src/index'; 31 | import { verifyWarnLogsBecauseOfSourcemap } from '../utils/verify-warn-logs-because-of-source-map'; 32 | import { verifyWarnLogsNotTriggered } from '../utils/verify-warn-logs-not-triggered'; 33 | import { joinLines } from '../utils/join-lines'; 34 | 35 | describe('rollup-plugin-prettier', () => { 36 | let tmpDir; 37 | 38 | beforeEach(() => { 39 | spyOn(console, 'warn'); 40 | }); 41 | 42 | beforeEach(() => { 43 | tmpDir = tmp.dirSync({ 44 | unsafeCleanup: true, 45 | }); 46 | }); 47 | 48 | afterEach(() => { 49 | tmpDir.removeCallback(); 50 | }); 51 | 52 | it('should run prettier on final bundle', (done) => { 53 | const output = path.join(tmpDir.name, 'bundle.js'); 54 | const config = { 55 | input: getBundlePath(), 56 | output: { 57 | file: output, 58 | format: 'es', 59 | }, 60 | 61 | plugins: [ 62 | prettier({ 63 | parser: 'babel', 64 | }), 65 | ], 66 | }; 67 | 68 | rollup.rollup(config) 69 | .then((bundle) => bundle.write(config.output)) 70 | .then(() => { 71 | fs.readFile(output, 'utf8', (err, data) => { 72 | if (err) { 73 | done.fail(err); 74 | } 75 | 76 | const content = data.toString(); 77 | 78 | expect(content).toBeDefined(); 79 | expect(content).toContain( 80 | joinLines([ 81 | 'function sum(array) {', 82 | ' return array.reduce((acc, x) => acc + x, 0);', 83 | '}', 84 | ]), 85 | ); 86 | 87 | done(); 88 | }); 89 | }); 90 | }); 91 | 92 | it('should run prettier on final bundle with sourcemap set in output option', (done) => { 93 | const output = path.join(tmpDir.name, 'bundle.js'); 94 | const config = { 95 | input: getBundlePath(), 96 | 97 | output: { 98 | file: output, 99 | format: 'es', 100 | sourcemap: 'inline', 101 | }, 102 | 103 | plugins: [ 104 | prettier({ 105 | parser: 'babel', 106 | }), 107 | ], 108 | }; 109 | 110 | rollup.rollup(config) 111 | .then((bundle) => bundle.write(config.output)) 112 | .then(() => { 113 | fs.readFile(output, 'utf8', (err, data) => { 114 | if (err) { 115 | done.fail(err); 116 | return; 117 | } 118 | 119 | const content = data.toString(); 120 | expect(content).toContain('//# sourceMappingURL'); 121 | verifyWarnLogsBecauseOfSourcemap(); 122 | done(); 123 | }); 124 | }) 125 | .catch((err) => { 126 | done.fail(err); 127 | }); 128 | }); 129 | 130 | it('should run prettier on final bundle with sourcemap set to "silent" in output option', (done) => { 131 | const output = path.join(tmpDir.name, 'bundle.js'); 132 | const config = { 133 | input: getBundlePath(), 134 | 135 | output: { 136 | file: output, 137 | format: 'es', 138 | sourcemap: 'inline', 139 | }, 140 | 141 | plugins: [ 142 | prettier({ 143 | sourcemap: 'silent', 144 | parser: 'babel', 145 | }), 146 | ], 147 | }; 148 | 149 | rollup.rollup(config) 150 | .then((bundle) => bundle.write(config.output)) 151 | .then(() => { 152 | fs.readFile(output, 'utf8', (err, data) => { 153 | if (err) { 154 | done.fail(err); 155 | return; 156 | } 157 | 158 | const content = data.toString(); 159 | expect(content).toContain('//# sourceMappingURL'); 160 | verifyWarnLogsNotTriggered(); 161 | done(); 162 | }); 163 | }) 164 | .catch((err) => { 165 | done.fail(err); 166 | }); 167 | }); 168 | 169 | it('should run prettier on final bundle with sourcemap set in output array option', (done) => { 170 | const output = path.join(tmpDir.name, 'bundle.js'); 171 | const config = { 172 | input: getBundlePath(), 173 | 174 | output: [ 175 | { file: output, format: 'es', sourcemap: 'inline' }, 176 | ], 177 | 178 | plugins: [ 179 | prettier({ 180 | parser: 'babel', 181 | }), 182 | ], 183 | }; 184 | 185 | rollup.rollup(config) 186 | .then((bundle) => ( 187 | Q.all(config.output.map((out) => bundle.write(out))) 188 | )) 189 | .then(() => { 190 | fs.readFile(output, 'utf8', (err, data) => { 191 | if (err) { 192 | done.fail(err); 193 | return; 194 | } 195 | 196 | const content = data.toString(); 197 | expect(content).toContain('//# sourceMappingURL'); 198 | verifyWarnLogsBecauseOfSourcemap(); 199 | done(); 200 | }); 201 | }) 202 | .catch((err) => { 203 | done.fail(err); 204 | }); 205 | }); 206 | 207 | it('should enable sourcemap (lowercase) on plugin', (done) => { 208 | const output = path.join(tmpDir.name, 'bundle.js'); 209 | const config = { 210 | input: getBundlePath(), 211 | 212 | output: { 213 | file: output, 214 | format: 'es', 215 | sourcemap: true, 216 | }, 217 | 218 | plugins: [ 219 | prettier({ 220 | parser: 'babel', 221 | sourcemap: true, 222 | }), 223 | ], 224 | }; 225 | 226 | rollup.rollup(config) 227 | .then((bundle) => bundle.write(config.output)) 228 | .then(() => { 229 | fs.readFile(output, 'utf8', (err) => { 230 | if (err) { 231 | done.fail(err); 232 | return; 233 | } 234 | 235 | verifyWarnLogsBecauseOfSourcemap(); 236 | done(); 237 | }); 238 | }) 239 | .catch((err) => { 240 | done.fail(err); 241 | }); 242 | }); 243 | 244 | /** 245 | * Get the output bundle absolute path. 246 | * 247 | * @return {string} Bundle absolute path. 248 | */ 249 | function getBundlePath() { 250 | return path.join(__dirname, '..', 'fixtures', 'bundle.js'); 251 | } 252 | }); 253 | -------------------------------------------------------------------------------- /test/rollup-plugin-prettier.spec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | import path from 'path'; 26 | import { RollupPluginPrettier } from '../src/rollup-plugin-prettier'; 27 | import { verifyWarnLogsBecauseOfSourcemap } from './utils/verify-warn-logs-because-of-source-map'; 28 | import { verifyWarnLogsNotTriggered } from './utils/verify-warn-logs-not-triggered'; 29 | import { installWarnSpy } from './utils/install-warn-spy'; 30 | import { joinLines } from './utils/join-lines'; 31 | 32 | describe('RollupPluginPrettier', () => { 33 | beforeEach(() => { 34 | installWarnSpy(); 35 | }); 36 | 37 | it('should create the plugin with a name', () => { 38 | const plugin = new RollupPluginPrettier({ 39 | parser: 'babel', 40 | }); 41 | 42 | expect(plugin.name).toBe('rollup-plugin-prettier'); 43 | expect(plugin.getSourcemap()).toBeNull(); 44 | }); 45 | 46 | it('should run plugin without sourcemap by default', () => { 47 | const plugin = new RollupPluginPrettier({ 48 | parser: 'babel', 49 | }); 50 | 51 | const code = 'var foo=0;var test="hello world";'; 52 | return plugin.reformat(code).then((result) => { 53 | verifyWarnLogsNotTriggered(); 54 | expect(result.map).not.toBeDefined(); 55 | expect(result.code).toBe( 56 | joinLines([ 57 | 'var foo = 0;', 58 | 'var test = "hello world";', 59 | '', 60 | ]), 61 | ); 62 | }); 63 | }); 64 | 65 | it('should run prettier with sourcemap', () => { 66 | const plugin = new RollupPluginPrettier({ 67 | parser: 'babel', 68 | sourcemap: true, 69 | }); 70 | 71 | const code = 'var foo=0;var test="hello world";'; 72 | return plugin.reformat(code).then((result) => { 73 | expect(plugin.getSourcemap()).toBe(true); 74 | 75 | verifyWarnLogsBecauseOfSourcemap(); 76 | expect(result.map).toBeDefined(); 77 | expect(result.code).toBe( 78 | joinLines([ 79 | 'var foo = 0;', 80 | 'var test = "hello world";', 81 | '', 82 | ]), 83 | ); 84 | }); 85 | }); 86 | 87 | it('should run prettier with sourcemap skipping warn message', () => { 88 | const plugin = new RollupPluginPrettier({ 89 | parser: 'babel', 90 | sourcemap: 'silent', 91 | }); 92 | 93 | const code = 'var foo=0;var test="hello world";'; 94 | return plugin.reformat(code).then((result) => { 95 | expect(plugin.getSourcemap()).toBe('silent'); 96 | 97 | verifyWarnLogsNotTriggered(); 98 | expect(result.map).toBeDefined(); 99 | expect(result.code).toBe( 100 | joinLines([ 101 | 'var foo = 0;', 102 | 'var test = "hello world";', 103 | '', 104 | ]), 105 | ); 106 | }); 107 | }); 108 | 109 | it('should run prettier with sourcemap if it has been enabled', () => { 110 | const plugin = new RollupPluginPrettier({ 111 | parser: 'babel', 112 | }); 113 | 114 | expect(plugin.getSourcemap()).toBeNull(); 115 | 116 | // Enable sourcemap explicitely. 117 | plugin.enableSourcemap(); 118 | 119 | const code = 'var foo=0;var test="hello world";'; 120 | return plugin.reformat(code).then((result) => { 121 | expect(plugin.getSourcemap()).toBe(true); 122 | 123 | verifyWarnLogsBecauseOfSourcemap(); 124 | expect(result.map).toBeDefined(); 125 | expect(result.code).toBe( 126 | joinLines([ 127 | 'var foo = 0;', 128 | 'var test = "hello world";', 129 | '', 130 | ]), 131 | ); 132 | }); 133 | }); 134 | 135 | it('should run prettier with sourcemap enable in reformat', () => { 136 | const plugin = new RollupPluginPrettier({ 137 | parser: 'babel', 138 | sourcemap: false, 139 | }); 140 | 141 | const code = 'var foo=0;var test="hello world";'; 142 | const outputOptions = { sourcemap: true }; 143 | 144 | return plugin.reformat(code, outputOptions).then((result) => { 145 | expect(plugin.getSourcemap()).toBe(false); 146 | 147 | verifyWarnLogsBecauseOfSourcemap(); 148 | expect(result.map).toBeDefined(); 149 | expect(result.code).toBe( 150 | joinLines([ 151 | 'var foo = 0;', 152 | 'var test = "hello world";', 153 | '', 154 | ]), 155 | ); 156 | }); 157 | }); 158 | 159 | it('should run prettier without sourcemap enable in reformat', () => { 160 | const plugin = new RollupPluginPrettier({ 161 | parser: 'babel', 162 | sourcemap: true, 163 | }); 164 | 165 | const code = 'var foo=0;var test="hello world";'; 166 | const outputOptions = { sourcemap: false }; 167 | return plugin.reformat(code, outputOptions).then((result) => { 168 | expect(plugin.getSourcemap()).toBe(true); 169 | 170 | verifyWarnLogsNotTriggered(); 171 | expect(result.map).not.toBeDefined(); 172 | expect(result.code).toBe( 173 | joinLines([ 174 | 'var foo = 0;', 175 | 'var test = "hello world";', 176 | '', 177 | ]), 178 | ); 179 | }); 180 | }); 181 | 182 | it('should run prettier using config file from given current working directory', () => { 183 | const cwd = path.join(__dirname, 'fixtures'); 184 | const parser = 'babel'; 185 | const options = { 186 | parser, 187 | cwd, 188 | }; 189 | 190 | const plugin = new RollupPluginPrettier(options); 191 | const code = joinLines([ 192 | 'var name = "John Doe";', 193 | 'if (true) {', 194 | ' console.log(name);', 195 | '}', 196 | ]); 197 | 198 | return plugin.reformat(code).then((result) => { 199 | expect(result.code).toEqual(joinLines([ 200 | 'var name = \'John Doe\';', 201 | 'if (true) {', 202 | ' console.log(name);', 203 | '}', 204 | '', 205 | ])); 206 | 207 | expect(options).toEqual({ 208 | parser, 209 | cwd, 210 | }); 211 | }); 212 | }); 213 | }); 214 | -------------------------------------------------------------------------------- /test/utils/install-warn-spy.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | import startsWith from 'lodash.startswith'; 26 | 27 | /** 28 | * Install smart `console.warn` spy. 29 | * 30 | * @return {void} 31 | */ 32 | export function installWarnSpy() { 33 | const { warn } = console; 34 | 35 | spyOn(console, 'warn').and.callFake((msg, ...args) => { 36 | if (!startsWith(msg, '[rollup-plugin-prettier]')) { 37 | warn(msg, ...args); 38 | } 39 | }); 40 | } 41 | -------------------------------------------------------------------------------- /test/utils/join-lines.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * Join given lines into a single line. 27 | * 28 | * @param {Array} lines The lines to join. 29 | * @param {string} separator The separator, defaults to End Of Line character. 30 | * @return {string} The full text. 31 | */ 32 | export function joinLines(lines, separator = '\n') { 33 | return lines.join(separator); 34 | } 35 | -------------------------------------------------------------------------------- /test/utils/verify-warn-logs-because-of-source-map.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * Verify that warning have been logged to the console because of sourcemap. 27 | * 28 | * @return {void} 29 | */ 30 | export function verifyWarnLogsBecauseOfSourcemap() { 31 | expect(console.warn).toHaveBeenCalledWith( 32 | '[rollup-plugin-prettier] Sourcemap is enabled, computing diff is required', 33 | ); 34 | 35 | expect(console.warn).toHaveBeenCalledWith( 36 | '[rollup-plugin-prettier] This may take a moment (depends on the size of your bundle)', 37 | ); 38 | } 39 | -------------------------------------------------------------------------------- /test/utils/verify-warn-logs-not-triggered.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2017-2023 Mickael Jeanroy 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * Verify that warning due to sourcemaps have not been logged. 27 | * 28 | * @return {void} 29 | */ 30 | export function verifyWarnLogsNotTriggered() { 31 | expect(console.warn).not.toHaveBeenCalledWith( 32 | '[rollup-plugin-prettier] Sourcemap is enabled, computing diff is required', 33 | ); 34 | 35 | expect(console.warn).not.toHaveBeenCalledWith( 36 | '[rollup-plugin-prettier] This may take a moment (depends on the size of your bundle)', 37 | ); 38 | } 39 | --------------------------------------------------------------------------------