-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 853cb2b
Showing
14 changed files
with
4,783 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"presets": [ | ||
"babel-preset-env" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
coverage/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"trailingComma": "all" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
sudo: false | ||
language: node_js | ||
|
||
notifications: | ||
email: false | ||
|
||
node_js: | ||
- 6 | ||
- 8 | ||
|
||
script: npm run ci | ||
|
||
branches: | ||
only: master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Fatih Kalifa | ||
|
||
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: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# raw.macro | ||
|
||
[![Build Status](https://travis-ci.org/pveyes/raw.macro.svg?branch=master)](https://travis-ci.org/pveyes/raw.macro) | ||
|
||
> Webpack [`raw-loader`](https://github.com/webpack-contrib/raw-loader) implemented as [`babel-plugin-macro`](https://github.com/kentcdodds/babel-plugin-macros) | ||
## Why | ||
|
||
I came across a few problem when using `raw-loader` in `create-react-app`. | ||
|
||
* I need to use webpack loader syntax (which needs to be disabled via eslint) | ||
* Some newlines are removed unintentionally | ||
|
||
## Usage | ||
|
||
Similar to nodejs `require` call: | ||
|
||
```js | ||
import raw from "raw.macro"; | ||
|
||
const markdown = raw("./README.md"); | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "raw.macro", | ||
"version": "0.0.0", | ||
"main": "dist/raw.macro.js", | ||
"license": "MIT", | ||
"files": [ | ||
"dist/" | ||
], | ||
"scripts": { | ||
"test": "jest --coverage", | ||
"build": "microbundle -i src/index.js -o dist", | ||
"format": "prettier --write", | ||
"ci": "npm run test -- --ci && npm run build", | ||
"precommit": "lint-staged" | ||
}, | ||
"dependencies": { | ||
"babel-plugin-macros": "^2.1.0", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^6.1.1" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.26.0", | ||
"babel-plugin-tester": "^5.0.0", | ||
"babel-preset-env": "^1.6.1", | ||
"jest": "^22.3.0", | ||
"microbundle": "^0.4.3", | ||
"prettier": "^1.10.2" | ||
}, | ||
"jest": { | ||
"testRegex": "__tests__/.*.test.js$" | ||
}, | ||
"lint-staged": { | ||
"*.{js,md}": [ | ||
"prettier --write", | ||
"git add -A" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`macros correct usage: correct usage 1`] = ` | ||
" | ||
import raw from '../macro'; | ||
const md = raw('./fixtures/markdown.md'); | ||
const js = raw('./fixtures/javascript.js'); | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const md = \\"# This is the title\\\\n\\\\nThis is the description\\\\n\\"; | ||
const js = | ||
'const sum = require(\\"../sum\\");\\\\n\\\\ntest(\\"add two numbers\\", () => {\\\\n const result = sum(1, 2);\\\\n expect(result).toEqual(3);\\\\n});\\\\n'; | ||
" | ||
`; | ||
|
||
exports[`macros no usage: no usage 1`] = ` | ||
" | ||
import raw from '../macro' | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const sum = require("../sum"); | ||
|
||
test("add two numbers", () => { | ||
const result = sum(1, 2); | ||
expect(result).toEqual(3); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# This is the title | ||
|
||
This is the description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const path = require("path"); | ||
const pluginTester = require("babel-plugin-tester"); | ||
const plugin = require("babel-plugin-macros"); | ||
const prettier = require("prettier"); | ||
|
||
pluginTester({ | ||
plugin, | ||
snapshot: true, | ||
babelOptions: { | ||
filename: __filename, | ||
}, | ||
formatResult(result) { | ||
return prettier.format(result, { trailingComma: "es5" }); | ||
}, | ||
tests: { | ||
"no usage": `import raw from '../macro'`, | ||
"correct usage": ` | ||
import raw from '../macro'; | ||
const md = raw('./fixtures/markdown.md'); | ||
const js = raw('./fixtures/javascript.js'); | ||
`, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* istanbul ignore next */ | ||
import macro from "./macro"; | ||
|
||
export default macro; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const { createMacro } = require("babel-plugin-macros"); | ||
|
||
export default createMacro(rawMacros); | ||
|
||
function rawMacros({ references, state, babel }) { | ||
references.default.forEach(referencePath => { | ||
if (referencePath.parentPath.type === "CallExpression") { | ||
requireRaw({ referencePath, state, babel }); | ||
} else { | ||
throw new Error( | ||
`This is not supported: \`${referencePath | ||
.findParent(babel.types.isExpression) | ||
.getSource()}\`. Please see the raw.macro documentation`, | ||
); | ||
} | ||
}); | ||
} | ||
|
||
function requireRaw({ referencePath, state, babel }) { | ||
const filename = state.file.opts.filename; | ||
const t = babel.types; | ||
const callExpressionPath = referencePath.parentPath; | ||
const dirname = path.dirname(filename); | ||
let rawPath; | ||
|
||
try { | ||
rawPath = callExpressionPath.get("arguments")[0].evaluate().value; | ||
} catch (err) { | ||
// swallow error, print better error below | ||
} | ||
|
||
if (rawPath === undefined) { | ||
throw new Error( | ||
`There was a problem evaluating the value of the argument for the code: ${callExpressionPath.getSource()}. ` + | ||
`If the value is dynamic, please make sure that its value is statically deterministic.`, | ||
); | ||
} | ||
|
||
const fullPath = path.resolve(dirname, rawPath); | ||
const fileContent = fs.readFileSync(fullPath, { encoding: "utf-8" }); | ||
|
||
referencePath.parentPath.replaceWith( | ||
t.expressionStatement(t.stringLiteral(fileContent)), | ||
); | ||
} |
Oops, something went wrong.