├── .gitignore ├── test ├── test-no-above-parsed.ts ├── test-one-above-parsed.ts ├── test-three-above-parsed.ts ├── test-lf-parsed.ts ├── test-crlf-parsed.ts ├── test-no-script.vue ├── test-no-above.vue ├── test-one-above.vue ├── test-three-above.vue ├── test-lf.vue ├── test-two-above-parsed.ts ├── test-crlf.vue ├── test-two-above.vue └── index.spec.js ├── .npmignore ├── tslint.json ├── .vscode ├── extensions.json └── settings.json ├── tsconfig.json ├── .travis.yml ├── package.json ├── README.md └── src └── index.ts /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /lib/ 3 | node_modules/ 4 | 5 | -------------------------------------------------------------------------------- /test/test-no-above-parsed.ts: -------------------------------------------------------------------------------- 1 | ////////////////// 2 | const foo = 'bar'; 3 | console.log(foo) 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .vscode 3 | tsconfig.json 4 | tslint.json 5 | src/ 6 | test/ 7 | .travis.yml -------------------------------------------------------------------------------- /test/test-one-above-parsed.ts: -------------------------------------------------------------------------------- 1 | // 2 | //////////////// 3 | const foo = 'bar'; 4 | console.log(foo) 5 | -------------------------------------------------------------------------------- /test/test-three-above-parsed.ts: -------------------------------------------------------------------------------- 1 | // 2 | // 3 | // 4 | //////////// 5 | const foo = 'bar'; 6 | console.log(foo) 7 | -------------------------------------------------------------------------------- /test/test-lf-parsed.ts: -------------------------------------------------------------------------------- 1 | // tslint:disable 2 | // 3 | // 4 | // 5 | // tslint:enable 6 | const foo = 'bar'; 7 | console.log(foo) 8 | -------------------------------------------------------------------------------- /test/test-crlf-parsed.ts: -------------------------------------------------------------------------------- 1 | // tslint:disable 2 | // 3 | // 4 | // 5 | // tslint:enable 6 | const foo = 'bar'; 7 | console.log(foo) 8 | -------------------------------------------------------------------------------- /test/test-no-script.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 13 | -------------------------------------------------------------------------------- /test/test-no-above.vue: -------------------------------------------------------------------------------- 1 | 5 | 6 | 12 | -------------------------------------------------------------------------------- /test/test-one-above.vue: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 13 | -------------------------------------------------------------------------------- /test/test-three-above.vue: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 15 | -------------------------------------------------------------------------------- /test/test-lf.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 9 | 10 | 16 | -------------------------------------------------------------------------------- /test/test-two-above-parsed.ts: -------------------------------------------------------------------------------- 1 | // tslint:disable 2 | // 3 | // tslint:enable 4 | const foo = 'bar'; 5 | console.log(foo) 6 | -------------------------------------------------------------------------------- /test/test-crlf.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 9 | 10 | 16 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": [ 4 | "tslint-config-standard" 5 | ], 6 | "jsRules": {}, 7 | "rules": { 8 | "no-unused-variable": true, 9 | "quotemark": [true, "single"] 10 | }, 11 | "rulesDirectory": [] 12 | } -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp 6 | "dbaeumer.vscode-eslint", 7 | "sysoev.language-stylus", 8 | "octref.vetur" 9 | ] 10 | } -------------------------------------------------------------------------------- /test/test-two-above.vue: -------------------------------------------------------------------------------- 1 | **************************************************************************************************************************************************************** 2 | 3 | 7 | 8 | 14 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | // this aligns with Vue's browser support 4 | "target": "es5", 5 | // this enables stricter inference for data properties on `this` 6 | "strict": true, 7 | // if using webpack 2+ or rollup, to leverage tree shaking: 8 | "module": "umd", 9 | "moduleResolution": "node", 10 | "lib": [ 11 | "es6", "dom" 12 | ], 13 | "baseUrl": ".", 14 | "paths": { 15 | "@/*": [ 16 | "src/*" 17 | ] 18 | }, 19 | "declaration": true, 20 | "outDir": "lib" 21 | }, 22 | "include": [ 23 | "src/**/*.ts", 24 | "src/**/*.vue" 25 | //"test/**/*.ts", 26 | //"test/**/*.vue" 27 | ], 28 | "exclude": [ 29 | "node_modules" 30 | ] 31 | } 32 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: "8" 3 | deploy: 4 | provider: npm 5 | email: prograhammer@gmail.com 6 | skip_cleanup: true 7 | 'on': 8 | tags: true 9 | branch: master 10 | api_key: 11 | secure: CgB3e1pxeP7kLYHLt6ich7+LiFzVeOBtDuCveLD9rBycjLzPbNAlfqzDC2ce1MEJC2YOhsaytafeKoR9XynT50t09Ct/KtASeqAWTN+TjLbeAHilqHCj8iaYthOe7tMxxyVBEfvkxXTWcSyPH2qgCzHRC0k9F9RQM6wO7lLWP5Sz9TF524OQxCiljqrK1VrR4vKrsRMmiLolc2VWI0jQ+B+YFAjXYPsjooneztLViH95LGnv4BdaPOx3QZMBF6Hl4jK0/9zy6dNZUgAqRZVNXX530P7wX6adOGEkMieekKpn8ffsfPpaZKWFMrSf0toqF2UQyoqEkrcqBL82/Es2WNKYYKj+mhQkPTDWEYaBbO9jqNkwoBP8Gn+1mMjwmpI49n+0bDctdc6/tWDrRves1yRvqWWsJlf6ERibOlEx4qTgPyZSxUKsCC5J7z1iDa57MIY76C7kJi8RkulZU0NFLZsNL/s8t5yhq8dCTn0B1xLkqzQ5i9CEY0t3F2WNMJKT5xVGmPYqUANn1p4IR+SzSk9sVrsv6Qclbh3/kSQk+G8SO7hO/d9WLqqAMkguccJuFyTkA1anb5LKoGWleEq2/yKIdzZu7NQnm1Nywf9TOnkp6H687sUV3Z2OR2DWG8LA8QTHKtkXSAKtT36SnSz+cSq5yTwsrO2BvH1xRcB6b8M= 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-parser", 3 | "version": "1.1.6", 4 | "description": "Get contents from tags in .vue files (using AST tree).", 5 | "author": "David Graham ", 6 | "license": "MIT", 7 | "homepage": "https://github.com/prograhammer/vue-parser", 8 | "private": false, 9 | "main": "./lib/index.js", 10 | "typings": "./lib/index.d.ts", 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/prograhammer/vue-parser.git" 14 | }, 15 | "keywords": [ 16 | "vue", 17 | "parser", 18 | "ast", 19 | "sfc" 20 | ], 21 | "bugs": { 22 | "url": "https://github.com/prograhammer/vue-parser/issues" 23 | }, 24 | "scripts": { 25 | "clean": "rimraf lib", 26 | "compile": "tsc ", 27 | "test": "npm run build && mocha -R spec ./test", 28 | "build": "npm run clean && npm run compile" 29 | }, 30 | "devDependencies": { 31 | "chai": "^4.1.2", 32 | "mocha": "^4.0.1", 33 | "rimraf": "^2.4.4", 34 | "tslint": "^5.0.0", 35 | "tslint-config-standard": "^7.0.0", 36 | "typescript": "^2.6.2" 37 | }, 38 | "engines": { 39 | "node": ">= 4.0.0", 40 | "npm": ">= 3.0.0" 41 | }, 42 | "dependencies": { 43 | "parse5": "^3.0.3" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vue Parser 2 | 3 | ![Important](https://travis-ci.org/prograhammer/vue-parser.svg?branch=master "Travis CI badge") 4 | 5 | A simple way to parse a `.vue` file's contents (single file component) to get specific content from a 6 | tag like a template, script, or style tag. 7 | The returned content can be padded repeatedly with a specific string so that line numbers are retained 8 | (ie. good for reporting linting errors). You can also get the full [parse5](https://github.com/inikulin/parse5/blob/master/lib/index.d.ts#L193) 9 | node/element which includes the location info of where the tag content is located. 10 | 11 | # Basic Usage 12 | 13 | ```javascript 14 | import vueParser from 'vue-parser' 15 | 16 | const vueFileContents = ` 17 | 21 | 22 | 34 | 35 | 40 | ` 41 | 42 | const myScriptContents = vueParser.parse(vueFileContents, 'script', { lang: ['js', 'jsx'] }) 43 | 44 | console.log(myScriptContents) 45 | 46 | ``` 47 | 48 | The console output would like like this 49 | (notice default padding string is `// ` since comments are typically ignored by linters/compilers/etc.): 50 | 51 | ```text 52 | // 53 | // 54 | // 55 | // 56 | // 57 | // 58 | export default { 59 | name: 'Hello', 60 | 61 | data () { 62 | return { 63 | msg: 'Hello World!' 64 | } 65 | } 66 | 67 | } 68 | ``` 69 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | // Place your settings in this file to override the default and user settings 2 | { 3 | // Settings by file type. 4 | "[javascript]": { 5 | "editor.tabSize": 2, 6 | "editor.insertSpaces": true 7 | }, 8 | "[typescript]": { 9 | "editor.tabSize": 2, 10 | "editor.insertSpaces": true 11 | }, 12 | "[vue]": { 13 | "editor.tabSize": 2, 14 | "editor.insertSpaces": true 15 | }, 16 | "[stylus]": { 17 | "editor.tabSize": 2, 18 | "editor.insertSpaces": true 19 | }, 20 | 21 | // This is required for ESLint to work in Vue in VS Code. 22 | "eslint.options": { 23 | "extensions": [".html", ".js", ".vue", ".jsx"] 24 | }, 25 | "eslint.validate": [ 26 | { 27 | "language": "html", 28 | "autoFix": true 29 | }, 30 | { 31 | "language": "vue", 32 | "autoFix": true 33 | }, 34 | { 35 | "language": "javascript", 36 | "autoFix": true 37 | }, 38 | { 39 | "language": "javascriptreact", 40 | "autoFix": true 41 | } 42 | ], 43 | 44 | // When you hit ctrl+e to search, you don't want node_modules to be included. 45 | "search.exclude": { 46 | "**/.git": true, 47 | "**/node_modules": true, 48 | "**/tmp": true 49 | }, 50 | "emeraldwalk.runonsave": { 51 | "autoClearConsole": true, 52 | "commands": [ 53 | { 54 | "match": "\\.vue$", 55 | "cmd": "sed '1,/