Skip to content

Commit 633e01a

Browse files
authoredApr 26, 2020
fix: drop env.NODE_ENV from umd/iife (fix #274) (#337)
1 parent f15d134 commit 633e01a

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
 

‎src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ export class Bundler {
282282

283283
const env = Object.assign({}, config.env)
284284

285+
// drop precess.env.NODE_ENV from umd/iife
286+
if (
287+
['umd', 'umd-min', 'iife', 'iife-min'].includes(format) &&
288+
env.NODE_ENV === undefined
289+
) {
290+
env.NODE_ENV = minify ? 'production' : 'development'
291+
}
292+
285293
pluginsOptions.replace = {
286294
...Object.keys(env).reduce((res: Env, name) => {
287295
res[`process.env.${name}`] = JSON.stringify(env[name])

‎test/__snapshots__/index.test.ts.snap

+110
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,116 @@ exports[`uglify: uglify dist/index.min.js 1`] = `
292292
"
293293
`;
294294

295+
exports[`umd and iife should drop process.env.NODE_ENV: umd and iife should drop process.env.NODE_ENV dist/index.iife.js 1`] = `
296+
"var dropNodeEnv = (function (exports) {
297+
'use strict';
298+
299+
exports.a = '';
300+
exports.b = '';
301+
302+
{
303+
exports.a = 'live in dev';
304+
}
305+
306+
{
307+
exports.b = 'live in dev';
308+
}
309+
310+
return exports;
311+
312+
}({}));
313+
"
314+
`;
315+
316+
exports[`umd and iife should drop process.env.NODE_ENV: umd and iife should drop process.env.NODE_ENV dist/index.iife.min.js 1`] = `
317+
"var dropNodeEnv=function(r){\\"use strict\\";return r.a=\\"\\",r.b=\\"\\",r.a=\\"live in prod\\",r.b=\\"live in prod\\",r}({});
318+
"
319+
`;
320+
321+
exports[`umd and iife should drop process.env.NODE_ENV: umd and iife should drop process.env.NODE_ENV dist/index.umd.js 1`] = `
322+
"(function (global, factory) {
323+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
324+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
325+
(global = global || self, factory(global.dropNodeEnv = {}));
326+
}(this, function (exports) { 'use strict';
327+
328+
exports.a = '';
329+
exports.b = '';
330+
331+
{
332+
exports.a = 'live in dev';
333+
}
334+
335+
{
336+
exports.b = 'live in dev';
337+
}
338+
339+
Object.defineProperty(exports, '__esModule', { value: true });
340+
341+
}));
342+
"
343+
`;
344+
345+
exports[`umd and iife should drop process.env.NODE_ENV: umd and iife should drop process.env.NODE_ENV dist/index.umd.min.js 1`] = `
346+
"!function(e,o){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?o(exports):\\"function\\"==typeof define&&define.amd?define([\\"exports\\"],o):o((e=e||self).dropNodeEnv={})}(this,(function(e){\\"use strict\\";e.a=\\"\\",e.b=\\"\\",e.a=\\"live in prod\\",e.b=\\"live in prod\\",Object.defineProperty(e,\\"__esModule\\",{value:!0})}));
347+
"
348+
`;
349+
350+
exports[`umd and iife should preserve existing env.NODE_ENV: umd and iife should preserve existing env.NODE_ENV dist/index.iife.js 1`] = `
351+
"var dropNodeEnv = (function (exports) {
352+
'use strict';
353+
354+
exports.a = '';
355+
exports.b = '';
356+
357+
{
358+
exports.a = 'live in prod';
359+
}
360+
361+
{
362+
exports.b = 'live in prod';
363+
}
364+
365+
return exports;
366+
367+
}({}));
368+
"
369+
`;
370+
371+
exports[`umd and iife should preserve existing env.NODE_ENV: umd and iife should preserve existing env.NODE_ENV dist/index.iife.min.js 1`] = `
372+
"var dropNodeEnv=function(r){\\"use strict\\";return r.a=\\"\\",r.b=\\"\\",r.a=\\"live in prod\\",r.b=\\"live in prod\\",r}({});
373+
"
374+
`;
375+
376+
exports[`umd and iife should preserve existing env.NODE_ENV: umd and iife should preserve existing env.NODE_ENV dist/index.umd.js 1`] = `
377+
"(function (global, factory) {
378+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
379+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
380+
(global = global || self, factory(global.dropNodeEnv = {}));
381+
}(this, function (exports) { 'use strict';
382+
383+
exports.a = '';
384+
exports.b = '';
385+
386+
{
387+
exports.a = 'live in prod';
388+
}
389+
390+
{
391+
exports.b = 'live in prod';
392+
}
393+
394+
Object.defineProperty(exports, '__esModule', { value: true });
395+
396+
}));
397+
"
398+
`;
399+
400+
exports[`umd and iife should preserve existing env.NODE_ENV: umd and iife should preserve existing env.NODE_ENV dist/index.umd.min.js 1`] = `
401+
"!function(e,o){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?o(exports):\\"function\\"==typeof define&&define.amd?define([\\"exports\\"],o):o((e=e||self).dropNodeEnv={})}(this,(function(e){\\"use strict\\";e.a=\\"\\",e.b=\\"\\",e.a=\\"live in prod\\",e.b=\\"live in prod\\",Object.defineProperty(e,\\"__esModule\\",{value:!0})}));
402+
"
403+
`;
404+
295405
exports[`vue plugin: vue plugin dist/component.css 1`] = `
296406
"
297407
.main-content[data-v-5fccef39]{margin:0 auto;font-family:sans-serif;background-color:#ccf7e2;padding:10px;border-radius:5px;max-width:500px

‎test/fixtures/format/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
let a = ''
2+
let b = ''
3+
4+
if (process.env.NODE_ENV === 'development') {
5+
a = 'live in dev'
6+
} else {
7+
a = 'live in prod'
8+
}
9+
10+
if (process.env.NODE_ENV === 'production') {
11+
b = 'live in prod'
12+
} else {
13+
b = 'live in dev'
14+
}
15+
16+
export { a, b }

‎test/index.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,32 @@ snapshot(
255255
}
256256
}
257257
)
258+
259+
snapshot(
260+
{
261+
title: 'umd and iife should drop process.env.NODE_ENV',
262+
input: 'index.js',
263+
cwd: fixture('format')
264+
},
265+
{
266+
output: {
267+
format: ['umd', 'umd-min', 'iife', 'iife-min'],
268+
moduleName: 'dropNodeEnv'
269+
}
270+
}
271+
)
272+
273+
snapshot(
274+
{
275+
title: 'umd and iife should preserve existing env.NODE_ENV',
276+
input: 'index.js',
277+
cwd: fixture('format')
278+
},
279+
{
280+
output: {
281+
format: ['umd', 'umd-min', 'iife', 'iife-min'],
282+
moduleName: 'dropNodeEnv'
283+
},
284+
env: { NODE_ENV: 'production' }
285+
}
286+
)

1 commit comments

Comments
 (1)
Please sign in to comment.