├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── fixture-expected.glsl ├── fixture.glsl ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | bundle.js 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | bundle.js 5 | test 6 | test.js 7 | demo 8 | example 9 | .npmignore -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright (c) 2014 [stackgl](http://github.com/stackgl/) contributors 5 | 6 | *stackgl contributors listed at * 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # glslify-fancy-imports 2 | 3 | [![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges) 4 | 5 | glslify transform that provides you with a cleaner module import/export syntax ✨ 6 | 7 | May eventually be available in glslify directly, but using this for people to try out and express opinions. Regardless, the old syntax will still be available for backwards compatibility. 8 | 9 | ## Setup 10 | 11 | [![NPM](https://nodei.co/npm/glslify-fancy-imports.png)](https://www.npmjs.com/package/glslify-fancy-imports) 12 | 13 | After installing, you can include this as a local transform from the CLI like to enable: 14 | 15 | ``` bash 16 | glslify index.glsl -t glslify-fancy-imports 17 | ``` 18 | 19 | Alternatively you can add the transform by adding `glslify.transform` to your `package.json` file: 20 | 21 | ``` json 22 | { 23 | "name": "my-package", 24 | "dependencies": { 25 | "glslify": "^4.0.0", 26 | "glslify-fancy-imports": "^1.0.0" 27 | }, 28 | "glslify": { 29 | "transform": [ 30 | "glslify-fancy-imports" 31 | ] 32 | } 33 | } 34 | ``` 35 | 36 | ## Usage 37 | 38 | Right now the fancy import syntax is just sugar on top of the existing syntax, e.g. the following: 39 | 40 | ``` glsl 41 | import z from './test' 42 | import y from './test' where { map1 = source2, map2 = source1 } 43 | import x from './test' where { 44 | map1 = source1, 45 | map2 = source2 46 | } 47 | 48 | export w 49 | ``` 50 | 51 | Gets converted into: 52 | 53 | ``` glsl 54 | #pragma glslify: z = require('./test') 55 | #pragma glslify: y = require('./test', map1 = source2, map2 = source1) 56 | #pragma glslify: x = require('./test', map1 = source1, map2 = source2) 57 | 58 | #pragma glslify: export(w) 59 | ``` 60 | 61 | The key difference is that there's no more `#pragma glslify:`. The imports/exports have their own glslify-specific language syntax (for better, or for worse). 62 | 63 | You'll also notice that you can now declare your module name mappings over multiple lines, which is handy for packages that require a lot of configuration this way. 64 | 65 | ## Contributing 66 | 67 | See [stackgl/contributing](https://github.com/stackgl/contributing) for details. 68 | 69 | ## License 70 | 71 | MIT. See [LICENSE.md](http://github.com/stackgl/glslify-fancy-imports/blob/master/LICENSE.md) for details. 72 | -------------------------------------------------------------------------------- /fixture-expected.glsl: -------------------------------------------------------------------------------- 1 | precision mediump float; 2 | 3 | #pragma glslify: z = require('./test') 4 | #pragma glslify: y = require('./test', map1 = source2, map2 = source1) 5 | #pragma glslify: x = require('./test', map1 = source1, map2 = source2) 6 | 7 | void main() { 8 | 9 | } 10 | 11 | #pragma glslify: export(w) 12 | -------------------------------------------------------------------------------- /fixture.glsl: -------------------------------------------------------------------------------- 1 | precision mediump float; 2 | 3 | import z from './test' 4 | import y from './test' where { map1 = source2, map2 = source1 } 5 | import x from './test' where { 6 | map1 = source1, 7 | map2 = source2 8 | } 9 | 10 | void main() { 11 | 12 | } 13 | 14 | export w 15 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const string = require('glsl-token-string') 2 | const tokenize = require('glsl-tokenizer') 3 | 4 | module.exports = fancyImportsTransform 5 | module.exports.tokens = fancyImports 6 | 7 | function fancyImportsTransform (file, src, opts, next) { 8 | try { 9 | src = string(fancyImports(tokenize(src))) 10 | } catch (e) { 11 | return next(e) 12 | } 13 | 14 | next(null, src) 15 | } 16 | 17 | function fancyImports (tokens) { 18 | // 19 | // Rewrite import statements to glslify-friendly syntax, e.g. 20 | // 21 | // import b from 'lorems' 22 | // import a from 'module' where { map = b } 23 | // 24 | // Becomes: 25 | // 26 | // #pragma glslify: b = require('lorems') 27 | // #pragma glslify: a = require('module', map = b) 28 | // 29 | for (var i = 0; i < tokens.length; i++) { 30 | var token = tokens[i] 31 | if (token.type !== 'ident') continue 32 | if (token.data !== 'import') continue 33 | 34 | var nameIndex = skipWhitespace(i) 35 | if (!tokens[nameIndex]) continue 36 | if (tokens[nameIndex].type !== 'ident') { 37 | throw new Error('Expected variable identifier after "import" keyword.') 38 | } 39 | 40 | var fromIndex = skipWhitespace(nameIndex) 41 | if (!tokens[fromIndex]) continue 42 | if (tokens[fromIndex].data !== 'from') { 43 | throw new Error('Expected "from" keyword in "import" statement.') 44 | } 45 | 46 | var pathIndex = skipWhitespace(fromIndex) 47 | if (!tokens[pathIndex]) continue 48 | var pathStart = tokens[pathIndex].data 49 | if (pathStart !== '"' && pathStart !== "'") { 50 | throw new Error('"import" statements must use a quoted string for the import path.') 51 | } 52 | 53 | var pathFinish = skipTillMatching(pathIndex) + 1 54 | var nextIndex = skipWhitespace(pathFinish) 55 | var whereToken = tokens[nextIndex] 56 | 57 | var varName = tokens[nameIndex].data 58 | var srcFile = string(tokens.slice(pathIndex, pathFinish)) 59 | 60 | if (whereToken.data !== 'where') { 61 | i = replaceImportWithPragma(i, pathFinish) 62 | continue 63 | } 64 | 65 | var curlyIndex = skipWhitespace(nextIndex) 66 | if (!tokens[curlyIndex]) continue 67 | var closeCurly = findOppositeCurly(curlyIndex) 68 | var mappings = string(tokens.slice(curlyIndex + 1, closeCurly)) 69 | 70 | i = replaceImportWithPragma(i, closeCurly + 1, mappings) 71 | } 72 | 73 | // 74 | // Does the same for export statements, e.g. 75 | // 76 | // export hello 77 | // 78 | // Becomes: 79 | // 80 | // #pragma glslify: export(hello) 81 | // 82 | for (var i = 0; i < tokens.length; i++) { 83 | if (tokens[i].data !== 'export') continue 84 | 85 | var token = tokens[i] 86 | var symbolIndex = skipWhitespace(i) 87 | var symbolToken = tokens[symbolIndex] 88 | var nextIndex = skipWhitespace(symbolIndex) 89 | var remainingWhitespace = string(tokens.slice(symbolIndex + 1, nextIndex)) 90 | var exportName = symbolToken.data 91 | 92 | if (!/\n|\r/.test(remainingWhitespace)) { 93 | throw new Error('"export" statements must finish with a newline, and can only export a single value') 94 | } 95 | 96 | i = replaceExportWithPragma(i, symbolIndex + 1) 97 | } 98 | 99 | return tokens 100 | 101 | function replaceImportWithPragma (start, end, mappings) { 102 | var data = '#pragma glslify: ' + varName + ' = require(' + srcFile 103 | 104 | if (mappings) { 105 | data = data + ', ' + mappings.replace(/\s+/g, ' ').trim() 106 | } 107 | 108 | tokens.splice(start, end - start, { 109 | type: 'preprocessor', 110 | data: data + ')' 111 | }) 112 | 113 | return start + 1 114 | } 115 | 116 | function replaceExportWithPragma (start, end) { 117 | tokens.splice(start, end - start, { 118 | type: 'preprocessor', 119 | data: '#pragma glslify: export(' + exportName + ')' 120 | }) 121 | 122 | return start + 1 123 | } 124 | 125 | function skipWhitespace (i) { 126 | for (i++; i < tokens.length; i++) { 127 | if (tokens[i].type !== 'whitespace') { 128 | return i 129 | } 130 | } 131 | } 132 | 133 | function skipTillMatching (i) { 134 | var char = tokens[i].data 135 | for (i++; i < tokens.length; i++) { 136 | if (tokens[i].data === char) { 137 | return i 138 | } 139 | } 140 | } 141 | 142 | function findOppositeCurly (i) { 143 | var depth = 1 144 | for (i++; i < tokens.length; i++) { 145 | var char = tokens[i].data 146 | if (char === '{') depth++; else 147 | if (char === '}') depth-- 148 | if (!depth) return i 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glslify-fancy-imports", 3 | "version": "1.0.1", 4 | "description": "glslify transform that provides you with a cleaner module import/export syntax ✨", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "scripts": { 8 | "test": "node test | tap-spec" 9 | }, 10 | "author": { 11 | "name": "Hugh Kennedy", 12 | "email": "hughskennedy@gmail.com", 13 | "url": "http://github.com/hughsk" 14 | }, 15 | "dependencies": { 16 | "glsl-token-string": "^1.0.1", 17 | "glsl-tokenizer": "^2.0.2" 18 | }, 19 | "devDependencies": { 20 | "tap-spec": "^4.1.1", 21 | "tape": "^4.2.2" 22 | }, 23 | "repository": { 24 | "type": "git", 25 | "url": "git://github.com/stackgl/glslify-fancy-imports.git" 26 | }, 27 | "keywords": [ 28 | "ecosystem:stackgl" 29 | ], 30 | "homepage": "https://github.com/stackgl/glslify-fancy-imports", 31 | "bugs": { 32 | "url": "https://github.com/stackgl/glslify-fancy-imports/issues" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const unfancify = require('./') 2 | const test = require('tape') 3 | const path = require('path') 4 | const fs = require('fs') 5 | 6 | const expected = path.join(__dirname, 'fixture-expected.glsl') 7 | const fixture = path.join(__dirname, 'fixture.glsl') 8 | 9 | test('fancy imports', function (t) { 10 | const src = fs.readFileSync(fixture, 'utf8') 11 | 12 | t.plan(1) 13 | 14 | unfancify(fixture, src, {}, function (err, unfancy) { 15 | if (err) return t.ifError(err) 16 | t.equal(unfancy, fs.readFileSync(expected, 'utf8')) 17 | }) 18 | }) 19 | --------------------------------------------------------------------------------