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/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 8b19592..7ddf6a9 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", @@ -24,6 +24,7 @@ "magic-string": "^0.25.7" }, "devDependencies": { + "@types/acorn": "^4.0.6", "rollup": "^2.23.0", "typescript": "^3.9.7" } diff --git a/src/index.ts b/src/index.ts index 970fdbd..78d2d31 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,38 @@ 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; } 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 +133,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", + ], }