Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
Merged
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
node_modules
dist
*.js
*.d.ts
*.js.map

!rollup.config.js
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ import { dataToEsm } from 'rollup-pluginutils';
const esModuleSource = dataToEsm({
custom: 'data',
to: ['treeshake']
}, options = {
}, {
compact: false,
indent: '\t',
preferConst: false,
Expand Down
1,728 changes: 1,026 additions & 702 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/pluginutils.cjs.js",
"module": "dist/pluginutils.es.js",
"jsnext:main": "dist/pluginutils.es.js",
"typings": "src/index.d.ts",
"typings": "dist/pluginutils.d.ts",
"files": [
"src",
"dist",
Expand All @@ -15,21 +15,22 @@
"@types/estree": "0.0.39",
"@types/jest": "^24.0.11",
"@types/micromatch": "^3.1.0",
"@types/node": "^11.11.3",
"@types/node": "^11.13.0",
"husky": "^1.3.1",
"jest": "^24.5.0",
"jest": "^24.7.0",
"lint-staged": "^8.1.5",
"prettier": "^1.16.4",
"rollup": "^1.6.0",
"rollup-plugin-typescript": "^1.0.0",
"ts-jest": "^24.0.0",
"tslint": "^5.14.0",
"typescript": "^3.3.3333",
"rollup": "^1.8.0",
"rollup-plugin-typescript": "^1.0.1",
"shx": "^0.3.2",
"ts-jest": "^24.0.1",
"tslint": "^5.15.0",
"typescript": "^3.4.1",
"typescript-eslint-parser": "^22.0.0"
},
"scripts": {
"test": "jest",
"build": "rollup -c",
"build": "rollup -c && shx cp src/pluginutils.d.ts dist/pluginutils.d.ts",
"lint": "npm run lint:nofix -- --fix",
"lint:nofix": "tslint --project .",
"pretest": "npm run build",
Expand Down
7 changes: 5 additions & 2 deletions src/addExtension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { extname } from 'path';
import { AddExtension } from './pluginutils';

export default function addExtension(filename: string, ext: string = '.js'): string {
const addExtension: AddExtension = function addExtension(filename, ext = '.js') {
if (!extname(filename)) filename += ext;
return filename;
}
};

export { addExtension as default };
15 changes: 9 additions & 6 deletions src/attachScopes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Node, walk } from 'estree-walker';
import { AttachedScope, AttachScopes } from './pluginutils';

const blockDeclarations = {
const: true,
Expand Down Expand Up @@ -51,13 +52,13 @@ function extractNames(param: Node): Array<string> {
}

interface ScopeOptions {
parent?: Scope;
parent?: AttachedScope;
block?: boolean;
params?: Array<Node>;
}

class Scope {
parent?: Scope;
class Scope implements AttachedScope {
parent?: AttachedScope;
isBlockScope: boolean;
declarations: { [key: string]: boolean };

Expand Down Expand Up @@ -93,7 +94,7 @@ class Scope {
}
}

export default function attachScopes(ast: Node, propertyName: string = 'scope'): Scope {
const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'scope') {
let scope = new Scope();

walk(ast, {
Expand All @@ -114,7 +115,7 @@ export default function attachScopes(ast: Node, propertyName: string = 'scope'):
});
}

let newScope: Scope | undefined;
let newScope: AttachedScope | undefined;

// create new function scope
if (/Function/.test(node.type)) {
Expand Down Expand Up @@ -163,4 +164,6 @@ export default function attachScopes(ast: Node, propertyName: string = 'scope'):
});

return scope;
}
};

export { attachScopes as default };
14 changes: 5 additions & 9 deletions src/createFilter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as mm from 'micromatch';
import { resolve, sep } from 'path';
import { CreateFilter } from './pluginutils';
import ensureArray from './utils/ensureArray';

export default function createFilter(
include?: Array<string | RegExp> | string | RegExp | null,
exclude?: Array<string | RegExp> | string | RegExp | null
): (id: string | any) => boolean {
const createFilter: CreateFilter = function createFilter(include?, exclude?) {
const getMatcher = (id: string | RegExp) =>
isRegexp(id)
id instanceof RegExp
? id
: {
test: mm.matcher(
Expand Down Expand Up @@ -38,8 +36,6 @@ export default function createFilter(

return !includeMatchers.length;
};
}
};

function isRegexp(val: any): val is RegExp {
return val instanceof RegExp;
}
export { createFilter as default };
16 changes: 5 additions & 11 deletions src/dataToEsm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import makeLegalIdentifier from './makeLegalIdentifier';
import { DataToEsm } from './pluginutils';

export type Indent = string | null | undefined;

Expand Down Expand Up @@ -39,16 +40,7 @@ function serialize(obj: any, indent: Indent, baseIndent: string): string {
return JSON.stringify(obj);
}

export interface Options {
compact?: boolean;
indent?: string;
namedExports?: boolean;
objectShorthand?: boolean;
preferConst?: boolean;
}

// convert data object into separate named exports (and default)
export default function dataToNamedExports(data: any, options: Options = {}): string {
const dataToEsm: DataToEsm = function dataToEsm(data, options = {}) {
const t = options.compact ? '' : 'indent' in options ? options.indent : '\t';
const _ = options.compact ? '' : ' ';
const n = options.compact ? '' : '\n';
Expand Down Expand Up @@ -84,4 +76,6 @@ export default function dataToNamedExports(data: any, options: Options = {}): st
return (
namedExportCode + `export default${_}{${n}${t}${defaultExportRows.join(`,${n}${t}`)}${n}};${n}`
);
}
};

export { dataToEsm as default };
8 changes: 6 additions & 2 deletions src/makeLegalIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MakeLegalIdentifier } from './pluginutils';

const reservedWords =
'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public';
const builtins =
Expand All @@ -6,12 +8,14 @@ const builtins =
const forbiddenIdentifiers = new Set<string>(`${reservedWords} ${builtins}`.split(' '));
forbiddenIdentifiers.add('');

export default function makeLegalIdentifier(str: string): string {
export const makeLegalIdentifier: MakeLegalIdentifier = function makeLegalIdentifier(str) {
str = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, '_');

if (/\d/.test(str[0]) || forbiddenIdentifiers.has(str)) {
str = `_${str}`;
}

return str;
}
};

export { makeLegalIdentifier as default };
35 changes: 35 additions & 0 deletions src/pluginutils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Node } from 'estree-walker';

export interface AttachedScope {
parent?: AttachedScope;
isBlockScope: boolean;
declarations: { [key: string]: boolean };
addDeclaration(node: Node, isBlockDeclaration: boolean, isVar: boolean): void;
contains(name: string): boolean;
}

export interface DataToEsmOptions {
compact?: boolean;
indent?: string;
namedExports?: boolean;
objectShorthand?: boolean;
preferConst?: boolean;
}

export type AddExtension = (filename: string, ext?: string) => string;
export const addExtension: AddExtension;

export type AttachScopes = (ast: Node, propertyName?: string) => AttachedScope;
export const attachScopes: AttachScopes;

export type CreateFilter = (
include?: Array<string | RegExp> | string | RegExp | null,
exclude?: Array<string | RegExp> | string | RegExp | null
) => (id: string | any) => boolean;
export const createFilter: CreateFilter;

export type MakeLegalIdentifier = (str: string) => string;
export const makeLegalIdentifier: MakeLegalIdentifier;

export type DataToEsm = (data: any, options?: DataToEsmOptions) => string;
export const dataToEsm: DataToEsm;