Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added parserOptions option #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```
19 changes: 14 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -24,6 +24,7 @@
"magic-string": "^0.25.7"
},
"devDependencies": {
"@types/acorn": "^4.0.6",
"rollup": "^2.23.0",
"typescript": "^3.9.7"
}
Expand Down
32 changes: 28 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Node } from 'estree';
import { walk } from 'estree-walker';
import MagicString from 'magic-string';


enum NodeType {
Literal = 'Literal',
CallExpression = 'CallExpression',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
},
"include": ["./src"],
"exclude": ["node_modules", "dist"],
"types": [
"acorn",
],
}