Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
pveyes committed Feb 17, 2018
0 parents commit 853cb2b
Show file tree
Hide file tree
Showing 14 changed files with 4,783 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"babel-preset-env"
]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
coverage/
dist/
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "all"
}
14 changes: 14 additions & 0 deletions .travis.yml
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
21 changes: 21 additions & 0 deletions LICENSE
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.
26 changes: 26 additions & 0 deletions README.md
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
38 changes: 38 additions & 0 deletions package.json
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"
]
}
}
27 changes: 27 additions & 0 deletions src/__tests__/__snapshots__/macro.test.js.snap
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'
↓ ↓ ↓ ↓ ↓ ↓
"
`;
6 changes: 6 additions & 0 deletions src/__tests__/fixtures/javascript.js
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);
});
3 changes: 3 additions & 0 deletions src/__tests__/fixtures/markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is the title

This is the description
24 changes: 24 additions & 0 deletions src/__tests__/macro.test.js
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');
`,
},
});
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* istanbul ignore next */
import macro from "./macro";

export default macro;
47 changes: 47 additions & 0 deletions src/macro.js
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)),
);
}
Loading

0 comments on commit 853cb2b

Please sign in to comment.