Skip to content

Commit

Permalink
feat(core): Add replacement of NODE_ENV and BROWSER env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Oct 16, 2018
1 parent 46946bd commit 236acd2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/pectin-core/lib/getOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ module.exports = function getOutput(pkg, cwd) {
output.push({
file: path.resolve(cwd, pkg.browser),
format: 'cjs',
browser: true,
});
} else if (pkg.browser) {
// specific files (advanced)
output.push(
pkg.browser[pkg.main] && {
file: path.resolve(cwd, pkg.browser[pkg.main]),
format: 'cjs',
browser: true,
}
);
output.push(
pkg.browser[pkg.module] && {
file: path.resolve(cwd, pkg.browser[pkg.module]),
format: 'esm',
browser: true,
}
);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/pectin-core/lib/getPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const commonjs = require('rollup-plugin-commonjs');
const json = require('rollup-plugin-json');
const nodeResolve = require('rollup-plugin-node-resolve');
const mainEntry = require('rollup-plugin-main-entry');
const replace = require('rollup-plugin-replace');
const subpathExternals = require('rollup-plugin-subpath-externals');
const svg = require('rollup-plugin-svg');
const { terser } = require('rollup-plugin-terser');
Expand All @@ -28,6 +29,12 @@ module.exports = async function getPlugins(pkg, cwd, output) {
// just in case dependencies have missed the memo
jsnext: true,
}),
// https://github.com/rollup/rollup-plugin-replace#usage
replace(
Object.assign(env ? { 'process.env.NODE_ENV': JSON.stringify(env) } : {}, {
'process.env.BROWSER': JSON.stringify(output.browser || false),
})
),
// https://github.com/rollup/rollup-plugin-json#usage
json(),
// https://github.com/antony/rollup-plugin-svg
Expand Down
1 change: 1 addition & 0 deletions packages/pectin-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"rollup-plugin-json": "^3.0.0",
"rollup-plugin-main-entry": "file:../rollup-plugin-main-entry",
"rollup-plugin-node-resolve": "^3.3.0",
"rollup-plugin-replace": "^2.1.0",
"rollup-plugin-subpath-externals": "file:../rollup-plugin-subpath-externals",
"rollup-plugin-svg": "^1.0.1",
"rollup-plugin-terser": "^3.0.0"
Expand Down
44 changes: 40 additions & 4 deletions packages/pectin-core/test/pectin-core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('pectin-core', () => {
expect.objectContaining({ name: 'main-entry' }),
expect.objectContaining({ name: 'subpath-externals' }),
expect.objectContaining({ name: 'node-resolve' }),
expect.objectContaining({ name: 'replace' }),
expect.objectContaining({ name: 'json' }),
expect.objectContaining({ name: 'babel' }),
expect.objectContaining({ name: 'commonjs' }),
Expand All @@ -101,6 +102,7 @@ describe('pectin-core', () => {
expect.objectContaining({ name: 'main-entry' }),
expect.objectContaining({ name: 'subpath-externals' }),
expect.objectContaining({ name: 'node-resolve' }),
expect.objectContaining({ name: 'replace' }),
expect.objectContaining({ name: 'json' }),
// order is important, must come before babel()
expect.objectContaining({ name: 'svg' }),
Expand Down Expand Up @@ -234,7 +236,13 @@ describe('pectin-core', () => {
const cwd = createFixture({
'package.json': File(pkg),
src: Dir({
'index.js': File('export default class Basic {};'),
'index.js': File(`
export default class Basic {
constructor() {
this.isBrowser = process.env.BROWSER;
}
};
`),
}),
});

Expand Down Expand Up @@ -262,6 +270,8 @@ function _classCallCheck(instance, Constructor) {
var Basic = function Basic() {
_classCallCheck(this, Basic);
this.isBrowser = false;
};
module.exports = Basic;
Expand All @@ -277,6 +287,8 @@ function _classCallCheck(instance, Constructor) {
var Basic = function Basic() {
_classCallCheck(this, Basic);
this.isBrowser = false;
};
export default Basic;
Expand All @@ -294,6 +306,8 @@ function _classCallCheck(instance, Constructor) {
var Basic = function Basic() {
_classCallCheck(this, Basic);
this.isBrowser = true;
};
module.exports = Basic;
Expand All @@ -317,7 +331,13 @@ module.exports = Basic;
const cwd = createFixture({
'package.json': File(pkg),
src: Dir({
'index.js': File('export default class Advanced {};'),
'index.js': File(`
export default class Advanced {
constructor() {
this.isBrowser = process.env.BROWSER;
}
};
`),
}),
});
const configs = await pectinCore.createMultiConfig(pkg, { cwd });
Expand All @@ -344,6 +364,8 @@ var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallC
var Advanced = function Advanced() {
_classCallCheck(this, Advanced);
this.isBrowser = false;
};
module.exports = Advanced;
Expand All @@ -355,6 +377,8 @@ import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
var Advanced = function Advanced() {
_classCallCheck(this, Advanced);
this.isBrowser = false;
};
export default Advanced;
Expand All @@ -370,6 +394,8 @@ var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallC
var Advanced = function Advanced() {
_classCallCheck(this, Advanced);
this.isBrowser = true;
};
module.exports = Advanced;
Expand All @@ -381,6 +407,8 @@ import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
var Advanced = function Advanced() {
_classCallCheck(this, Advanced);
this.isBrowser = true;
};
export default Advanced;
Expand Down Expand Up @@ -450,7 +478,15 @@ return index;
const cwd = createFixture({
'package.json': File(pkg),
src: Dir({
'index.js': File('export default function main() { console.log("yay"); }'),
'index.js': File(`
export default function main() {
console.log("yay");
if (process.env.NODE_ENV === 'production') {
console.log('hooray');
}
}
`),
}),
});
const configs = await pectinCore.createMultiConfig(pkg, { cwd });
Expand All @@ -475,7 +511,7 @@ return main;
"
`);
expect(minOutput).toMatchInlineSnapshot(`
"!function(e,o){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=o():\\"function\\"==typeof define&&define.amd?define(o):e.ScopedUmd=o()}(this,function(){\\"use strict\\";return function(){console.log(\\"yay\\")}});
"!function(e,o){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=o():\\"function\\"==typeof define&&define.amd?define(o):e.ScopedUmd=o()}(this,function(){\\"use strict\\";return function(){console.log(\\"yay\\"),console.log(\\"hooray\\")}});
"
`);
});
Expand Down

0 comments on commit 236acd2

Please sign in to comment.