From 7c2fa1d4476e80425895f8c77c1e71b11c29f735 Mon Sep 17 00:00:00 2001 From: Petr Shibelbein Date: Wed, 2 Feb 2022 15:09:20 +0300 Subject: [PATCH 1/4] feat: added options.parserOptions --- src/index.ts | 24 ++++++++++++++++++++---- tsconfig.json | 3 +++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 970fdbd..e22d91a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { Node } from 'estree'; import { walk } from 'estree-walker'; import MagicString from 'magic-string'; + enum NodeType { Literal = 'Literal', CallExpression = 'CallExpression', @@ -35,12 +36,30 @@ export interface IRenameExtensionsOptions { * Extensions should include the dot for both input and output. */ map: (name: string) => string; + + parserOptions?: acorn.Options; } export function isEmpty(array: any[] | undefined) { return !array || array.length === 0; } +const defaultParserOptions: acorn.Options = { + ecmaVersion: 6, + sourceType: 'module', +}; + +export function getParserOptions(options?: acorn.Options): acorn.Options { + if (!options) { + return defaultParserOptions; + } + + return { + ...defaultParserOptions, + ...options + }; +} + export function getRequireSource(node: any): Node | false { if (node.type !== NodeType.CallExpression) { return false; @@ -106,10 +125,7 @@ export default function rename(options: IRenameExtensionsOptions): Plugin { if (file.code) { const magicString = new MagicString(file.code); - const ast = this.parse(file.code, { - ecmaVersion: 6, - sourceType: 'module', - }); + const ast = this.parse(file.code, getParserOptions(options.parserOptions)); walk(ast, { enter(node) { diff --git a/tsconfig.json b/tsconfig.json index 9d72a3c..a509893 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,4 +12,7 @@ }, "include": ["./src"], "exclude": ["node_modules", "dist"], + "types": [ + "acorn", + ], } From 197b5a898bb9ad399f7e707edc445c94dc92dbb7 Mon Sep 17 00:00:00 2001 From: Petr Shibelbein Date: Wed, 2 Feb 2022 15:12:11 +0300 Subject: [PATCH 2/4] flow: bump package version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8b19592..4af814b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-rename", - "version": "1.0.1", + "version": "1.0.2", "description": "Plugin to rename file name before they're emitted in rollup", "main": "dist/index.js", "homepage": "https://github.com/yesmeck/rollup-plugin-rename", From 68d150d664ca40bdb4ea4ba8b64551e6c0a1a123 Mon Sep 17 00:00:00 2001 From: Petr Shibelbein Date: Wed, 2 Feb 2022 16:05:27 +0300 Subject: [PATCH 3/4] fix: resolved some npm issues (and added missing @types/acorn) --- package-lock.json | 19 ++++++++++++++----- package.json | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 957aaa2..a558e83 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-rename", - "version": "1.0.1", + "version": "1.0.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -21,15 +21,24 @@ } } }, + "@types/acorn": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/@types/acorn/-/acorn-4.0.6.tgz", + "integrity": "sha512-veQTnWP+1D/xbxVrPC3zHnCZRjSrKfhbMUlEA43iMZLu7EsnTtkJklIuwrCPbOi8YkvDQAiW05VQQFvvz9oieQ==", + "dev": true, + "requires": { + "@types/estree": "*" + } + }, "@types/estree": { "version": "0.0.39", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz", "integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==" }, - "acorn-walk": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-7.2.0.tgz", - "integrity": "sha512-OPdCF6GsMIP+Az+aWfAAOEt2/+iVDKE7oy6lJ098aoe59oAmK76qV6Gw60SbZ8jHuG2wH058GF4pLFbYamYrVA==" + "estree-walker": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz", + "integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==" }, "fsevents": { "version": "2.1.3", diff --git a/package.json b/package.json index 4af814b..7ddf6a9 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "magic-string": "^0.25.7" }, "devDependencies": { + "@types/acorn": "^4.0.6", "rollup": "^2.23.0", "typescript": "^3.9.7" } From 8ec86165d43f9c8eb9c6d9a325482e0b88ca60d1 Mon Sep 17 00:00:00 2001 From: Petr Shibelbein Date: Wed, 2 Feb 2022 18:32:24 +0300 Subject: [PATCH 4/4] doc: updated README --- README.MD | 11 +++++++++++ src/index.ts | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/README.MD b/README.MD index 0d88dda..3cf06de 100644 --- a/README.MD +++ b/README.MD @@ -56,5 +56,16 @@ export interface IRenameExtensionsOptions { * Extensions should include the dot for both input and output. */ map: (name: string) => string; + + + /** + * An acorn.Options object. + * This option will extend the default: + * `{ ecmaVersion: 6, sourceType: 'module' }` + * Provide it if you do not transpile any es7+ features + * @see https://github.com/acornjs/acorn/blob/master/acorn/src/options.js + * @see https://github.com/acornjs/acorn/blob/9899904395d67776a78702fe5640ea4bc12b9ec6/acorn/dist/acorn.d.ts#L14 + */ + parserOptions?: acorn.Options; } ``` diff --git a/src/index.ts b/src/index.ts index e22d91a..78d2d31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,6 +37,14 @@ export interface IRenameExtensionsOptions { */ map: (name: string) => string; + /** + * An acorn.Options object. + * This option will extend the default: + * `{ ecmaVersion: 6, sourceType: 'module' }` + * Provide it if you do not transpile any es7+ features + * @see https://github.com/acornjs/acorn/blob/master/acorn/src/options.js + * @see https://github.com/acornjs/acorn/blob/9899904395d67776a78702fe5640ea4bc12b9ec6/acorn/dist/acorn.d.ts#L14 + */ parserOptions?: acorn.Options; }