Skip to content

Commit 748798b

Browse files
test: side effects (#109)
1 parent 04c1fa7 commit 748798b

File tree

7 files changed

+154
-3
lines changed

7 files changed

+154
-3
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"memfs": "^3.2.0",
7070
"npm-run-all": "^4.1.5",
7171
"prettier": "^2.0.5",
72+
"rx": "^4.1.0",
7273
"standard-version": "^8.0.0",
7374
"webpack": "^4.43.0"
7475
},

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default function loader() {
2727
/*
2828
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
2929
*
30-
* fixes:
30+
* Fixes:
3131
* - https://github.com/webpack-contrib/expose-loader/issues/55
3232
* - https://github.com/webpack-contrib/expose-loader/issues/49
3333
*/
@@ -47,7 +47,7 @@ export default function loader() {
4747

4848
// Change the request from an /abolute/path.js to a relative ./path.js.
4949
// This prevents [chunkhash] values from changing when running webpack builds in different directories.
50-
const newRequest = contextify(this.rootContext, getRemainingRequest(this));
50+
const newRequest = contextify(this.context, getRemainingRequest(this));
5151
const stringifiedNewRequest = stringifyRequest(this, `-!${newRequest}`);
5252

5353
let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifiedNewRequest});\n`;

test/__snapshots__/loader.test.js.snap

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,116 @@ Object {
749749

750750
exports[`loader should work with multiple exposes: warnings 1`] = `Array []`;
751751

752+
exports[`loader should work with side-effects free modules: errors 1`] = `Array []`;
753+
754+
exports[`loader should work with side-effects free modules: module 1`] = `
755+
"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./rx.all.js\\");
756+
var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../../src/runtime/getGlobalThis.js\\");
757+
var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___();
758+
___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___;
759+
module.exports = ___EXPOSE_LOADER_IMPORT___;
760+
"
761+
`;
762+
763+
exports[`loader should work with side-effects free modules: result 1`] = `
764+
Object {
765+
"ExposeLoader": Object {
766+
"default": [Function],
767+
},
768+
"global-commonjs2-single-export": Object {
769+
"foo": "bar",
770+
},
771+
"myGlobal": Object {
772+
"AnonymousObservable": [Function],
773+
"AnonymousObserver": [Function],
774+
"AnonymousSubject": [Function],
775+
"ArgumentOutOfRangeError": [Function],
776+
"AsyncSubject": [Function],
777+
"BehaviorSubject": [Function],
778+
"BinaryDisposable": [Function],
779+
"CompositeDisposable": [Function],
780+
"CompositeError": [Function],
781+
"ConnectableObservable": [Function],
782+
"Disposable": [Function],
783+
"EmptyError": [Function],
784+
"FlatMapObservable": [Function],
785+
"HistoricalScheduler": [Function],
786+
"MockDisposable": [Function],
787+
"NAryDisposable": [Function],
788+
"NotImplementedError": [Function],
789+
"NotSupportedError": [Function],
790+
"Notification": [Function],
791+
"ObjectDisposedError": [Function],
792+
"Observable": [Function],
793+
"ObservableBase": [Function],
794+
"Observer": [Function],
795+
"Pauser": [Function],
796+
"ReactiveTest": Object {
797+
"created": 100,
798+
"disposed": 1000,
799+
"onCompleted": [Function],
800+
"onError": [Function],
801+
"onNext": [Function],
802+
"subscribe": [Function],
803+
"subscribed": 200,
804+
},
805+
"Recorded": [Function],
806+
"RefCountDisposable": [Function],
807+
"ReplaySubject": [Function],
808+
"Scheduler": [Function],
809+
"SerialDisposable": [Function],
810+
"SingleAssignmentDisposable": [Function],
811+
"Subject": [Function],
812+
"Subscription": [Function],
813+
"TestScheduler": [Function],
814+
"TimeoutError": [Function],
815+
"VirtualTimeScheduler": [Function],
816+
"config": Object {
817+
"Promise": [Function],
818+
"longStackSupport": false,
819+
"useNativeEvents": false,
820+
},
821+
"doneEnumerator": Object {
822+
"done": true,
823+
"value": undefined,
824+
},
825+
"helpers": Object {
826+
"defaultComparer": [Function],
827+
"defaultError": [Function],
828+
"defaultKeySerializer": [Function],
829+
"defaultNow": [Function],
830+
"defaultSubComparer": [Function],
831+
"identity": [Function],
832+
"isArrayLike": [Function],
833+
"isFunction": [Function],
834+
"isIterable": [Function],
835+
"isPromise": [Function],
836+
"iterator": Symbol(Symbol.iterator),
837+
"noop": [Function],
838+
"notImplemented": [Function],
839+
"notSupported": [Function],
840+
},
841+
"internals": Object {
842+
"AbstractObserver": [Function],
843+
"Enumerable": [Function],
844+
"PriorityQueue": [Function],
845+
"SchedulePeriodicRecursive": [Function],
846+
"ScheduledItem": [Function],
847+
"ScheduledObserver": [Function],
848+
"addProperties": [Function],
849+
"addRef": [Function],
850+
"bindCallback": [Function],
851+
"inherits": [Function],
852+
"isEqual": [Function],
853+
"isObject": [Function],
854+
"tryCatch": [Function],
855+
},
856+
},
857+
}
858+
`;
859+
860+
exports[`loader should work with side-effects free modules: warnings 1`] = `Array []`;
861+
752862
exports[`loader should work: errors 1`] = `Array []`;
753863

754864
exports[`loader should work: module 1`] = `

test/fixtures/side-effects.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'rx';
2+
3+
function test() {
4+
return 'test';
5+
}
6+
7+
export default test;

test/helpers/getCompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
1919
module: {
2020
rules: [
2121
{
22-
test: /global-.+\.js/i,
22+
test: /(global-.+|rx\.all)\.js/i,
2323
rules: [
2424
{
2525
loader: path.resolve(__dirname, '../../src'),

test/loader.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* @jest-environment node
3+
*/
14
import path from 'path';
25

36
import webpack from 'webpack';
@@ -13,6 +16,8 @@ import {
1316
readAsset,
1417
} from './helpers';
1518

19+
jest.setTimeout(30000);
20+
1621
describe('loader', () => {
1722
it('should work', async () => {
1823
const compiler = getCompiler('simple-commonjs2-single-export.js', {
@@ -465,4 +470,26 @@ describe('loader', () => {
465470
expect(getErrors(stats)).toMatchSnapshot('errors');
466471
expect(getWarnings(stats)).toMatchSnapshot('warnings');
467472
});
473+
474+
it('should work with side-effects free modules', async () => {
475+
const compiler = getCompiler(
476+
'side-effects.js',
477+
{
478+
exposes: 'myGlobal',
479+
},
480+
{
481+
mode: 'production',
482+
}
483+
);
484+
const stats = await compile(compiler);
485+
486+
expect(getModuleSource('rx.all-exposed.js', stats)).toMatchSnapshot(
487+
'module'
488+
);
489+
expect(
490+
execute(readAsset('main.bundle.js', compiler, stats))
491+
).toMatchSnapshot('result');
492+
expect(getErrors(stats)).toMatchSnapshot('errors');
493+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
494+
});
468495
});

0 commit comments

Comments
 (0)