Skip to content

Instantly share code, notes, and snippets.

View jackpopp's full-sized avatar

Jack Hannigan Popp jackpopp

View GitHub Profile
@jackpopp
jackpopp / 54.bash
Created October 12, 2018 14:05
Code block 54.bash for - test.md
NODE_ENV='production' node src/index.js
@jackpopp
jackpopp / 53.bash
Created October 12, 2018 14:05
Code block 53.bash for - test.md
NODE_ENV='development' node src/index.js
@jackpopp
jackpopp / 52.js
Created October 12, 2018 14:05
Code block 52.js for - test.md
const block = sourceToAST('{}').body[0];
/**
* If the value was true then set the blocks body to the consequent
* If the value was false and there is an alternate set the blocks body to the alternate
* If there is not alternate remove the entire node
*/
if (result) {
block.body = node.consequent.body;
return block;
@jackpopp
jackpopp / 51.js
Created October 12, 2018 14:05
Code block 51.js for - test.md
// from this
if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react.production.min.js');
} else {
module.exports = require('./cjs/react.development.js');
}
// to this
{
module.exports = require('./cjs/react.development.js');
@jackpopp
jackpopp / 50.js
Created October 12, 2018 14:05
Code block 50.js for - test.md
const { value } = literal;
const result = new Function(`return "${value}" ${operator} "${process.env.NODE_ENV}"`)();
@jackpopp
jackpopp / 49.js
Created October 12, 2018 14:05
Code block 49.js for - test.md
if (node.type === 'IfStatement') {
// This allows us to generate different bundles based on the NODE_ENV value, allowing development and production builds
// needs to be a binary expression
if (node.test.type !== 'BinaryExpression') return;
// check if either left or right are member expressions that look for the node env
// check if either the left or right string literal, if it is lets build and compare the two
const { left, right, operator } = node.test;
if ((left.type === 'MemberExpression' || right.type === 'MemberExpression') && (left.type === 'Literal' || right.type === 'Literal')) {
// the member expression and literal can be on either side of the test so figure that out and assign to variables
@jackpopp
jackpopp / 48.json
Created October 12, 2018 14:05
Code block 48.json for - test.md
"expression": {
"type": "MemberExpression",
"start": 0,
"end": 20,
"object": {
"type": "MemberExpression",
"start": 0,
"end": 11,
"object": {
"type": "Identifier",
@jackpopp
jackpopp / 47.js
Created October 12, 2018 14:05
Code block 47.js for - test.md
function iifeTemplate(source) {
return `(function() {const process = {env: { NODE_ENV: 'development' }};${source}}())`;
}
@jackpopp
jackpopp / 46.js
Created October 12, 2018 14:05
Code block 46.js for - test.md
if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react.production.min.js');
} else {
module.exports = require('./cjs/react.development.js');
}
@jackpopp
jackpopp / 45.js
Created October 12, 2018 14:05
Code block 45.js for - test.md
Uncaught ReferenceError: process is not defined
at require_3c705474d87ac8a9b47623a9c3833989 (generated.js:1528)
at require_785ce0fa348dcb9b9692a0ce512f61fe (generated.js:20858)
at generated.js:22699
at generated.js:22713