Comparing version 0.3.1 to 0.4.0
@@ -1,2 +0,2 @@ | ||
var e=require("path"),t=require("fs"),a=(0,require("babel-plugin-macros").createMacro)(function(a){var r=a.state,i=a.babel;a.references.default.forEach(function(a){if("CallExpression"!==a.parentPath.type)throw new Error("This is not supported: `"+a.findParent(i.types.isExpression).getSource()+"`. Please see the raw.macro documentation");!function(a){var r,i=a.referencePath,n=a.babel.types,s=i.parentPath,o=e.dirname(a.state.file.opts.filename);try{r=s.get("arguments")[0].evaluate().value}catch(e){}if(void 0===r)throw new Error("There was a problem evaluating the value of the argument for the code: "+s.getSource()+". If the value is dynamic, please make sure that its value is statically deterministic.");var l=require.resolve(r,{paths:[o]}),u=t.readFileSync(l,{encoding:"utf-8"});i.parentPath.replaceWith(n.expressionStatement(n.stringLiteral(u)))}({referencePath:a,state:r,babel:i})})});module.exports=a; | ||
var e=require("path"),r=require("fs"),t=require("babel-plugin-macros");function a(t,n,i,o){return void 0===o&&(o=""),t.objectExpression(n.map(function(n){if(Array.isArray(n))return t.objectProperty(t.stringLiteral(n[0]),a(t,n[1],e.join(i,n[0])));try{var l=require.resolve(n+o,{paths:[i]});return t.objectProperty(t.stringLiteral(n),t.stringLiteral(r.readFileSync(l,"utf-8")))}catch(e){return null}}).filter(Boolean))}module.exports=(0,t.createMacro)(function(t){var n=t.state,i=t.babel,o={value:0};t.references.default.forEach(function(t){if("CallExpression"!==t.parentPath.type)throw new Error("This is not supported: `"+t.findParent(i.types.isExpression).getSource()+"`. Please see the raw.macro documentation");!function(t){var n,i=t.referencePath,o=t.usageCounter,l=t.babel.types,s=i.parentPath,u=e.dirname(t.state.file.opts.filename),c=s.get("arguments")[0];switch(c.node.type){case"TemplateLiteral":var f=c.node,v=f.expressions,h=f.quasis;if(""===h[0].value.raw)throw new Error("Invalid value, variable interpolation can't be at the start of template literal");if(v.length>2)throw new Error("Invalid value. You can only have 2 dynamic values max. 1 for directory name, 1 for file name.");var p="__raw_dynamic__"+o.value;o.value++;var d=v.map(function(e,r){var t=r!==h.length-1&&c.node.quasis[r+1].value.raw.startsWith("/");return{name:e.name,isDirectory:t}}),m=e.join(u,h[0].value.raw),y=r.readdirSync(m).filter(function(t){try{return r.lstatSync(e.join(m,t)).isDirectory()===d[0].isDirectory}catch(e){return!1}}).flatMap(function(e){return d[0].isDirectory?e:e.endsWith(h[1].value.raw)?e.replace(h[1].value.raw,""):[]});return h.slice(1).forEach(function(t,a){d[a].isDirectory&&y.forEach(function(n,i){if(!t.tail){var o=e.join(m,n),l=r.readdirSync(o).filter(function(e){return e.endsWith(h[a+2].value.raw)});y[i]=[n,l]}})}),function e(r){return r.parentPath?e(r.parentPath):r}(i).unshiftContainer("body",l.variableDeclaration("var",[l.variableDeclarator(l.identifier(p),a(l,y,m,h[h.length-1].value.raw))])),void s.replaceWith(l.expressionStatement(d.reduce(function(e,r){return l.memberExpression(e,l.identifier(r.name),!0)},l.identifier(p))));case"StringLiteral":n=c.node.value}if(void 0===n)throw new Error("There was a problem evaluating the value of the argument for the code: "+s.getSource()+". If the value is dynamic, please make sure that its value is statically deterministic.");var w=require.resolve(n,{paths:[u]}),b=r.readFileSync(w,{encoding:"utf-8"});s.replaceWith(l.expressionStatement(l.stringLiteral(b)))}({referencePath:t,state:n,babel:i,usageCounter:o})})}); | ||
//# sourceMappingURL=raw.macro.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.rawMacro=t()}(this,function(){var e=require("path"),t=require("fs");return(0,require("babel-plugin-macros").createMacro)(function(r){var a=r.state,n=r.babel;r.references.default.forEach(function(r){if("CallExpression"!==r.parentPath.type)throw new Error("This is not supported: `"+r.findParent(n.types.isExpression).getSource()+"`. Please see the raw.macro documentation");!function(r){var a,n=r.referencePath,i=r.babel.types,o=n.parentPath,s=e.dirname(r.state.file.opts.filename);try{a=o.get("arguments")[0].evaluate().value}catch(e){}if(void 0===a)throw new Error("There was a problem evaluating the value of the argument for the code: "+o.getSource()+". If the value is dynamic, please make sure that its value is statically deterministic.");var u=require.resolve(a,{paths:[s]}),c=t.readFileSync(u,{encoding:"utf-8"});n.parentPath.replaceWith(i.expressionStatement(i.stringLiteral(c)))}({referencePath:r,state:a,babel:n})})})}); | ||
!function(e){"function"==typeof define&&define.amd?define(e):e()}(function(){var e=require("path"),r=require("fs"),t=require("babel-plugin-macros");function a(t,n,i,o){return void 0===o&&(o=""),t.objectExpression(n.map(function(n){if(Array.isArray(n))return t.objectProperty(t.stringLiteral(n[0]),a(t,n[1],e.join(i,n[0])));try{var l=require.resolve(n+o,{paths:[i]});return t.objectProperty(t.stringLiteral(n),t.stringLiteral(r.readFileSync(l,"utf-8")))}catch(e){return null}}).filter(Boolean))}module.exports=(0,t.createMacro)(function(t){var n=t.state,i=t.babel,o={value:0};t.references.default.forEach(function(t){if("CallExpression"!==t.parentPath.type)throw new Error("This is not supported: `"+t.findParent(i.types.isExpression).getSource()+"`. Please see the raw.macro documentation");!function(t){var n,i=t.referencePath,o=t.usageCounter,l=t.babel.types,u=i.parentPath,s=e.dirname(t.state.file.opts.filename),c=u.get("arguments")[0];switch(c.node.type){case"TemplateLiteral":var f=c.node,v=f.expressions,d=f.quasis;if(""===d[0].value.raw)throw new Error("Invalid value, variable interpolation can't be at the start of template literal");if(v.length>2)throw new Error("Invalid value. You can only have 2 dynamic values max. 1 for directory name, 1 for file name.");var h="__raw_dynamic__"+o.value;o.value++;var p=v.map(function(e,r){var t=r!==d.length-1&&c.node.quasis[r+1].value.raw.startsWith("/");return{name:e.name,isDirectory:t}}),m=e.join(s,d[0].value.raw),y=r.readdirSync(m).filter(function(t){try{return r.lstatSync(e.join(m,t)).isDirectory()===p[0].isDirectory}catch(e){return!1}}).flatMap(function(e){return p[0].isDirectory?e:e.endsWith(d[1].value.raw)?e.replace(d[1].value.raw,""):[]});return d.slice(1).forEach(function(t,a){p[a].isDirectory&&y.forEach(function(n,i){if(!t.tail){var o=e.join(m,n),l=r.readdirSync(o).filter(function(e){return e.endsWith(d[a+2].value.raw)});y[i]=[n,l]}})}),function e(r){return r.parentPath?e(r.parentPath):r}(i).unshiftContainer("body",l.variableDeclaration("var",[l.variableDeclarator(l.identifier(h),a(l,y,m,d[d.length-1].value.raw))])),void u.replaceWith(l.expressionStatement(p.reduce(function(e,r){return l.memberExpression(e,l.identifier(r.name),!0)},l.identifier(h))));case"StringLiteral":n=c.node.value}if(void 0===n)throw new Error("There was a problem evaluating the value of the argument for the code: "+u.getSource()+". If the value is dynamic, please make sure that its value is statically deterministic.");var w=require.resolve(n,{paths:[s]}),b=r.readFileSync(w,{encoding:"utf-8"});u.replaceWith(l.expressionStatement(l.stringLiteral(b)))}({referencePath:t,state:n,babel:i,usageCounter:o})})})}); | ||
//# sourceMappingURL=raw.macro.umd.js.map |
{ | ||
"name": "raw.macro", | ||
"description": "Read file contents at build time, similar to webpack raw-loader", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"engines": { | ||
"node": ">=8.9.0" | ||
}, | ||
"source": "src/macro.js", | ||
"main": "dist/raw.macro.js", | ||
@@ -30,20 +31,19 @@ "types": "raw.macro.d.ts", | ||
"test": "jest --coverage", | ||
"build": "microbundle -i src/index.js -o dist", | ||
"build": "microbundle", | ||
"format": "prettier --write", | ||
"ci": "npm run test -- --ci && npm run build", | ||
"prepare": "npm run build", | ||
"precommit": "lint-staged" | ||
"prepare": "npm run build" | ||
}, | ||
"dependencies": { | ||
"babel-plugin-macros": "^2.1.0" | ||
"babel-plugin-macros": "^2.8.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.3.3", | ||
"@babel/preset-env": "^7.3.1", | ||
"babel-plugin-tester": "^6.0.0", | ||
"husky": "^2.0.0", | ||
"jest": "^24.1.0", | ||
"lint-staged": "^7.0.0", | ||
"microbundle": "^0.7.0", | ||
"prettier": "^1.10.2" | ||
"@babel/core": "^7.10.5", | ||
"babel-plugin-tester": "^9.2.0", | ||
"husky": "^4.2.5", | ||
"jest": "^26.1.0", | ||
"microbundle": "^0.12.3", | ||
"prettier": "^2.0.5", | ||
"pretty-quick": "^2.0.1", | ||
"rollup": "^2.22.1" | ||
}, | ||
@@ -53,8 +53,7 @@ "jest": { | ||
}, | ||
"lint-staged": { | ||
"*.{js,md}": [ | ||
"prettier --write", | ||
"git add -A" | ||
] | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick" | ||
} | ||
} | ||
} |
@@ -7,2 +7,11 @@ # raw.macro | ||
## Installation | ||
In order to use raw.macro in your own project, you can use one of the following commands: | ||
``` | ||
$ yarn add --dev raw.macro | ||
$ npm install --save-dev raw.macro | ||
``` | ||
## Usage | ||
@@ -18,22 +27,26 @@ | ||
## Installation | ||
**Note: Because raw.macro uses babel internally to replace `raw()` calls, your transpiled code won't be changed if you only change the file that you import. This is because from babel perspective, your JS file is unchanged** | ||
In order to use raw.macro in your own project, you can use one of the following commands: | ||
One workaround that you can do that doesn't involve restarting your build system is making small changes where you put `raw()` calls, for example by adding `console.log()` with different content. | ||
``` | ||
$ yarn add --dev raw.macro | ||
$ npm install --save-dev raw.macro | ||
``` | ||
### Dynamic path import | ||
## Why | ||
You can also use import dynamic path using [template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals). You can even use them inside a function / React component! | ||
I came across a few problem when using `raw-loader` in `create-react-app`: | ||
```js | ||
import raw from "raw.macro"; | ||
* I need to use webpack loader syntax (which needs to be disabled via eslint). | ||
* Some newlines are removed unintentionally. | ||
function Article(props) { | ||
const content = raw(`../content/${props.locale}.md`); | ||
return <Markdown content={content} />; | ||
} | ||
``` | ||
This can also be useful in environment where webpack is not available / not extensible. Just use babel and you're good to go. | ||
**This method has 2 caveats:** | ||
1. You can only use up to two variables inside template literal. 1 for directory name, and 1 for file name. | ||
2. Using dynamic path import will includes all files that matches your dynamic path, which can make your JS bundle a lot bigger. This is also partly the reason of limitation described in #1 | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
53444
12
48
51
6
12
Updatedbabel-plugin-macros@^2.8.0