├── .codeclimate.yml ├── .eslintignore ├── .eslintrc.js ├── .gitignore ├── .markdownlint.json ├── .npmignore ├── .prettierrc.json ├── .travis.yml ├── .vscode ├── launch.json └── settings.json ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── README.md ├── appveyor.yml ├── index.d.ts ├── index.js ├── index.js.map ├── package.json ├── rollup.config.js ├── src ├── index.js ├── make-filter.js ├── parse-options.js └── proc-file.js └── test ├── fixtures ├── cc-error.js ├── cc-unclosed.js ├── cc.js ├── def-file-var.js ├── defaults.js ├── no-cc.js └── var-macros.js └── index.js /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | 3 | plugins: 4 | eslint: 5 | enabled: true 6 | channel: "eslint-5" 7 | 8 | checks: 9 | method-complexity: 10 | enabled: true 11 | config: 12 | threshold: 6 13 | 14 | exclude_patterns: 15 | - "**/*.d.ts" 16 | - "**/test/" 17 | - "**/node_modules/" 18 | - "cov-int/" 19 | - "coverage/" 20 | - "index.js" 21 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | **/coverage 2 | **/test/expect 3 | **/test/fixtures 4 | **/*.md 5 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | const OFF = 0 2 | const WARN = 1 3 | const ON = 2 4 | const YES = true 5 | 6 | const noUselessRenameOpts = { 7 | ignoreDestructuring: false, 8 | ignoreImport: false, 9 | ignoreExport: false, 10 | } 11 | 12 | module.exports = { 13 | root: YES, 14 | reportUnusedDisableDirectives: YES, 15 | 16 | parserOptions: { 17 | impliedStrict: YES, 18 | sourceType: 'module', 19 | }, 20 | 21 | // https://github.com/sindresorhus/globals/blob/master/globals.json 22 | env: { 23 | node: YES, 24 | es2017: YES, 25 | }, 26 | 27 | // https://github.com/eslint/eslint/blob/master/conf/eslint-recommended.js 28 | extends: ['eslint:recommended'], 29 | 30 | rules: { 31 | 'accessor-pairs': ON, 32 | 'array-callback-return': ON, 33 | 'block-scoped-var': ON, 34 | 'complexity': [ON, 8], // default is 20 35 | 'consistent-return': ON, 36 | 'consistent-this': [ON, '_self'], 37 | 'dot-notation': WARN, 38 | 'eol-last': ON, 39 | 'eqeqeq': [ON, 'always', { null: 'ignore' }], 40 | 'for-direction': ON, 41 | 'func-call-spacing': ON, 42 | 'getter-return': ON, 43 | 'guard-for-in': WARN, 44 | 'handle-callback-err': [ON, '^err(or)?$'], 45 | 'keyword-spacing': ON, 46 | 'linebreak-style': [ON, 'unix'], 47 | 'max-depth': [ON, 3], 48 | 'max-len': [1, 120, 4, { ignoreUrls: YES, ignoreRegExpLiterals: YES }], 49 | 'max-lines-per-function': [ON, { max: 25, skipBlankLines: YES, skipComments: YES }], 50 | 'max-lines': OFF, 51 | 'max-nested-callbacks': [ON, 3], // default is 10 52 | 'max-params': [ON, 3], 53 | 'max-statements': [ON, 12], 54 | 'new-cap': [ON, { newIsCap: true, capIsNew: false }], 55 | 'new-parens': ON, 56 | 'no-alert': ON, 57 | 'no-array-constructor': ON, 58 | 'no-caller': ON, 59 | 'no-catch-shadow': ON, 60 | 'no-console': ON, 61 | 'no-div-regex': ON, 62 | 'no-dupe-args': ON, 63 | 'no-dupe-class-members': ON, 64 | 'no-dupe-else-if': ON, 65 | 'no-dupe-keys': ON, 66 | 'no-duplicate-imports': [ON, { includeExports: YES }], 67 | 'no-else-return': ON, 68 | 'no-empty-pattern': ON, 69 | 'no-eval': ON, 70 | 'no-ex-assign': ON, 71 | 'no-extend-native': ON, 72 | 'no-extra-bind': ON, 73 | 'no-extra-boolean-cast': ON, 74 | 'no-extra-label': ON, 75 | 'no-fallthrough': ON, 76 | 'no-func-assign': ON, 77 | 'no-global-assign': ON, 78 | 'no-implicit-globals': ON, 79 | 'no-implied-eval': ON, 80 | 'no-import-assign': ON, 81 | 'no-inner-declarations': [ON, 'functions'], 82 | 'no-invalid-regexp': ON, 83 | 'no-irregular-whitespace': ON, 84 | 'no-iterator': ON, 85 | 'no-label-var': ON, 86 | 'no-labels': [ON, { allowLoop: false, allowSwitch: false }], 87 | 'no-lone-blocks': ON, 88 | 'no-lonely-if': ON, 89 | 'no-loop-func': ON, 90 | 'no-loss-of-precision': WARN, 91 | 'no-misleading-character-class': ON, 92 | 'no-multi-str': ON, 93 | 'no-multiple-empty-lines': [ON, { max: 2 }], 94 | 'no-native-reassign': ON, 95 | 'no-new-func': ON, 96 | 'no-new-object': ON, 97 | 'no-new-symbol': ON, 98 | 'no-new-wrappers': ON, 99 | 'no-new': ON, 100 | 'no-obj-calls': WARN, 101 | 'no-octal-escape': ON, 102 | 'no-octal': ON, 103 | 'no-proto': ON, 104 | 'no-prototype-builtins': WARN, 105 | 'no-redeclare': ON, 106 | 'no-regex-spaces': ON, 107 | 'no-return-assign': [ON, 'except-parens'], 108 | 'no-return-await': ON, 109 | 'no-script-url': ON, 110 | 'no-self-assign': ON, 111 | 'no-self-compare': ON, 112 | 'no-sequences': ON, 113 | 'no-shadow-restricted-names': ON, 114 | 'no-sparse-arrays': ON, 115 | 'no-template-curly-in-string': ON, 116 | 'no-this-before-super': ON, 117 | 'no-throw-literal': ON, 118 | 'no-undef-init': ON, 119 | 'no-undef': ON, 120 | 'no-unmodified-loop-condition': WARN, 121 | 'no-unneeded-ternary': [ON, { defaultAssignment: false }], 122 | 'no-unreachable': ON, 123 | 'no-unsafe-finally': ON, 124 | 'no-unsafe-negation': ON, 125 | 'no-unused-labels': ON, 126 | 'no-useless-backreference': ON, 127 | 'no-unused-expressions': WARN, 128 | 'no-unused-vars': ON, 129 | 'no-use-before-define': [ON, { functions: false }], 130 | 'no-useless-call': ON, 131 | 'no-useless-computed-key': ON, 132 | 'no-useless-concat': ON, 133 | 'no-useless-escape': ON, 134 | 'no-useless-rename': [ON, noUselessRenameOpts], 135 | 'no-useless-return': ON, 136 | 'no-var': ON, 137 | 'no-void': ON, 138 | 'no-whitespace-before-property': ON, 139 | 'no-with': ON, 140 | 'object-curly-spacing': [ON, 'always'], 141 | 'one-var-declaration-per-line': ON, 142 | 'one-var': [ON, { initialized: 'never' }], 143 | 'prefer-const': [ON, { destructuring: 'all' }], 144 | 'prefer-numeric-literals': ON, 145 | 'prefer-promise-reject-errors': ON, 146 | 'radix': ON, 147 | 'require-atomic-updates': ON, 148 | 'require-await': ON, 149 | 'require-yield': ON, 150 | 'strict': [ON, 'never'], 151 | 'symbol-description': ON, 152 | 'unicode-bom': [ON, 'never'], 153 | 'use-isnan': ON, 154 | 'valid-typeof': [ON, { requireStringLiterals: true }], 155 | 'yoda': [ON, 'never'], 156 | }, 157 | overrides: [ 158 | { 159 | files: ['**/test/**'], 160 | env: { 161 | mocha: YES, 162 | }, 163 | rules: { 164 | 'no-console': OFF, 165 | 'max-len': OFF, 166 | 'max-lines-per-function': OFF, 167 | }, 168 | }, 169 | ], 170 | } 171 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Share VSCode config, launcher, and tasks setup, but no more 2 | .vscode/**/* 3 | !.vscode/launch.json 4 | !.vscode/settings.json 5 | !.vscode/tasks.json 6 | 7 | # Common git and npm ignored folders and files 8 | .nyc_output 9 | .rpt2_cache 10 | cov-int 11 | coverage 12 | coverage.* 13 | lib-cov 14 | node_modules 15 | package-lock.json 16 | logs 17 | pids 18 | *.pid 19 | *.seed 20 | *.tgz 21 | **/*.bak 22 | **/*.lcov 23 | **/*.lock 24 | **/*.log 25 | -------------------------------------------------------------------------------- /.markdownlint.json: -------------------------------------------------------------------------------- 1 | { 2 | "default": true, 3 | "line-length": false, 4 | "no-duplicate-header": { "siblings_only": true }, 5 | "no-inline-html": false 6 | } 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Common git and npm ignored folders and files 2 | .nyc_output 3 | .rpt2_cache 4 | cov-int 5 | coverage 6 | coverage.* 7 | lib-cov 8 | node_modules 9 | package-lock.json 10 | logs 11 | pids 12 | *.pid 13 | *.seed 14 | *.tgz 15 | **/*.bak 16 | **/*.lcov 17 | **/*.lock 18 | **/*.log 19 | 20 | # Additional files to ignore 21 | .vscode 22 | test 23 | Makefile 24 | **/*.yml 25 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "alignObjectProperties": false, 3 | "alignTernaryLines": false, 4 | "arrowParens": "avoid", 5 | "bracketSpacing": true, 6 | "breakBeforeElse": false, 7 | "endOfLine": "lf", 8 | "generatorStarSpacing": true, 9 | "htmlWhitespaceSensitivity": "css", 10 | "indentChains": true, 11 | "insertPragma": false, 12 | "jsxBracketSameLine": false, 13 | "jsxSingleQuote": false, 14 | "printWidth": 92, 15 | "proseWrap": "never", 16 | "quoteProps": "consistent", 17 | "semi": false, 18 | "singleQuote": true, 19 | "spaceBeforeFunctionParen": false, 20 | "tabWidth": 2, 21 | "trailingComma": "es5", 22 | "useTabs": false, 23 | "yieldStarSpacing": true 24 | } 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: node_js 4 | 5 | node_js: 6 | - "10.12" 7 | - "14" 8 | 9 | branches: 10 | only: 11 | - master 12 | - dev 13 | 14 | before_script: 15 | - make setup_cover 16 | 17 | script: 18 | - yarn lint && yarn test 19 | 20 | after_script: 21 | - make send_cover 22 | 23 | notifications: 24 | email: false 25 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Mocha Tests", 11 | "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", 12 | "args": [ 13 | "-u", "tdd", 14 | "--timeout", "999999", 15 | "--colors", 16 | "${workspaceFolder}/test" 17 | ], 18 | "preLaunchTask": "npm: build", 19 | "internalConsoleOptions": "openOnSessionStart" 20 | } 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "eslint.enable": true, 3 | "eslint.format.enable": true, 4 | "eslint.workingDirectories": ["src", "test"], 5 | "javascript.format.enable": false, 6 | "typescript.format.enable": false, 7 | "javascript.preferences.quoteStyle": "single", 8 | "typescript.preferences.quoteStyle": "single", 9 | "typescript.disableAutomaticTypeAcquisition": true, 10 | "javascript.suggest.completeFunctionCalls": true, 11 | "typescript.suggest.completeFunctionCalls": true, 12 | "typescript.format.semicolons": "remove", 13 | "typescript.tsdk": "node_modules/typescript/lib", 14 | "files.eol": "\n", 15 | "files.insertFinalNewline": true, 16 | "files.trimTrailingWhitespace": true, 17 | "editor.formatOnSave": false, 18 | "editor.codeActionsOnSave": { 19 | "source.fixAll.eslint": true 20 | }, 21 | "[javascript]": { 22 | "editor.defaultFormatter": "esbenp.prettier-vscode", 23 | "editor.formatOnSave": true 24 | }, 25 | "[typescript]": { 26 | "editor.defaultFormatter": "esbenp.prettier-vscode", 27 | "editor.formatOnSave": true 28 | }, 29 | "[markdown]": { 30 | "editor.defaultFormatter": "esbenp.prettier-vscode", 31 | "editor.formatOnSave": true 32 | }, 33 | "[json]": { 34 | "editor.defaultFormatter": "esbenp.prettier-vscode", 35 | "editor.formatOnSave": true 36 | }, 37 | "[jsonc]": { 38 | "editor.defaultFormatter": "esbenp.prettier-vscode", 39 | "editor.formatOnSave": true 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-jscc changes 2 | 3 | ## \[2.0.0] - 2020-08-28 4 | 5 | ### Added 6 | 7 | - PrettierX + TypeScript for formating 8 | - [markdownlint](https://www.npmjs.com/package/markdownlint) and [prettierx](https://github.com/brodybits/prettierx) config files 9 | - VS Code settings for this project 10 | 11 | ### Changed 12 | 13 | - Update dependencies (using rollup ^2.26) and adjust rollup config 14 | - Update ESLint config, now PrettierX is used for code formating 15 | - Update code format to comply with prettierx rules 16 | - Require Rollup v2 & NodeJS v10 or above 17 | - Update test (remove NodeJS v6, add v14) 18 | - Update license 19 | 20 | ### Fixed 21 | 22 | - PR #5: fix source map support - Thanks to @billowz 23 | - Upgrade rollup to 1.0.0 24 | - Added `mapContent` option 25 | - Added `sourcemap` test case 26 | - Fix source path in the sourcemap 27 | - Fix #8: if entry point is named '.mjs' it does not works by @muayyad-alsadi 28 | 29 | ## \[1.0.0] - 2018-11-23 30 | 31 | ### Added 32 | 33 | - Added TypeScript v3 definitions. 34 | - Test and badges for [Code Climate](https://codeclimate.com) and [Codecov](https://codecov.io). 35 | 36 | ### Changed 37 | 38 | - Only CommonJS version with dependency on jscc v1.1.0 39 | - The minimum supported version of node.js is 6 40 | - The predefined extensions were extended to include those of React and TypeScript. 41 | - `RegEx` and `Date` values now outputs its literal content in replacements. 42 | - Objects containing `NaN` now outputs `NaN` in replacements. 43 | - `Infinite` and `-Infinity` in JSON objects are replaced with `Number.MAX_VALUE` and `Number.MIN_VALUE`, respectively. 44 | 45 | ## \[0.3.3] - fix in jscc 46 | 47 | - Using jscc v0.3.3 that fixes bug in sourceMap generation. 48 | 49 | ## \[0.3.2] - sync with jscc 50 | 51 | - From this release, the version number will be in sync with jscc. 52 | - Updated `devDependencies`. 53 | 54 | ## \[0.2.2] - jscc own repo 55 | 56 | - The staring sequence of HTML comments (`' 4 | [![npm Version][npm-badge]][npm-url] 5 | [![Build Status][build-badge]][build-url] 6 | [![AppVeyor Status][wbuild-badge]][wbuild-url] 7 | [![Maintainability][climate-badge]][climate-url] 8 | [![Test coverage][codecov-badge]][codecov-url] 9 | [![License][license-badge]][license-url] 10 | 11 | 12 | Conditional compilation and compile-time variable replacement for [Rollup](http://rollupjs.org/). 13 | 14 | rollup-plugin-jscc is **not** a transpiler, it is a wrapper of [jscc](https://github.com/aMarCruz/jscc), a tiny and powerful, language agnostic file preprocessor that uses JavaScript to transform text based on expressions at compile time. 15 | 16 | With jscc, you have: 17 | 18 | - Conditional inclusion/exclusion of blocks, based on compile-time variables\* 19 | - Compile-time variables with all the power of JavaScript expressions 20 | - Replacement of variables in the sources, by its value _at compile-time_ 21 | - Sourcemap support, useful for JavaScript sources. 22 | - TypeScript v3 definitions 23 | 24 | \* This feature allows you the conditional declaration of ES6 imports (See the [example](#example)). 25 | 26 | Since jscc is a preprocessor, rollup-plugin-jscc is implemented as a _file loader_, so it runs before any transpiler and is invisible to them. This behavior allows you to use it in a wide range of file types but, if necessary, it can be used as a Rollup _transformer_ instead of a loader. 27 | 28 | _**NOTE**_ 29 | 30 | The removal of non-jscc comments is not included, but you can use [rollup-plugin-cleanup](https://github.com/aMarCruz/rollup-plugin-cleanup), which brings compaction and normalization of lines in addition to the conditional removal of JS comments. 31 | 32 | ## Support my Work 33 | 34 | I'm a full-stack developer with more than 20 year of experience and I try to share most of my work for free and help others, but this takes a significant amount of time, effort and coffee so, if you like my work, please consider... 35 | 36 | [][kofi-url] 37 | 38 | Of course, feedback, PRs, and stars are also welcome 🙃 39 | 40 | Thanks for your support! 41 | 42 | ## Install 43 | 44 | ```bash 45 | npm i rollup-plugin-jscc -D 46 | # or 47 | yarn add rollup-plugin-jscc -D 48 | ``` 49 | 50 | rollup-plugin-jscc requires rollup v2.0 and node.js v10.12 or later. 51 | 52 | ### Usage 53 | 54 | rollup.config.js 55 | 56 | ```js 57 | import { rollup } from 'rollup' 58 | import jscc from 'rollup-plugin-jscc' 59 | 60 | export default { 61 | input: 'src/main.js', 62 | plugins: [ 63 | jscc({ 64 | values: { _APPNAME: 'My App', _DEBUG: 1 }, 65 | }), 66 | ], 67 | //...other options 68 | } 69 | ``` 70 | 71 | in your source: 72 | 73 | ```js 74 | /*#if _DEBUG 75 | import mylib from 'mylib-debug'; 76 | //#else */ 77 | import mylib from 'mylib' 78 | //#endif 79 | 80 | mylib.log('Starting $_APPNAME v$_VERSION...') 81 | ``` 82 | 83 | output: 84 | 85 | ```js 86 | import mylib from 'mylib-debug' 87 | 88 | mylib.log('Starting My App v1.0.0...') 89 | ``` 90 | 91 | That's it. 92 | 93 | \* jscc has two predefined memvars: `_FILE` and `_VERSION`, in addition to giving access to the environment variables through the nodejs [`proccess.env`](https://nodejs.org/api/process.html#process_process_env) object. 94 | 95 | ## Options 96 | 97 | Plain JavaScript object, with all properties optional. 98 | 99 | | Name | Type | Description | 100 | | --- | --- | --- | 101 | | asloader | boolean | If `false`, run the plugin as a `transformer`, otherwise run as `loader` (the default). | 102 | | escapeQuotes | string | String with the type of quotes to escape in the output of strings: 'single', 'double' or 'both'.
**Default** nothing. | 103 | | keepLines | boolean | Preserves the empty lines of directives and blocks that were removed.
Use this option with `sourceMap:false` if you are interested only in keeping the line numbering.
**Default** `false` | 104 | | mapHires | boolean | Make a hi-res source-map, if `sourceMap:true` (the default).
**Default** `true` | 105 | | prefixes | string | RegExp |
Array<string|RegExp> | The start of a directive. That is the characters before the '#', usually the start of comments.
**Default** `['//', '/*', '