Skip to content

Commit

Permalink
test: side effects (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 23, 2020
1 parent 04c1fa7 commit 748798b
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 3 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"rx": "^4.1.0",
"standard-version": "^8.0.0",
"webpack": "^4.43.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function loader() {
/*
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
*
* fixes:
* Fixes:
* - https://github.com/webpack-contrib/expose-loader/issues/55
* - https://github.com/webpack-contrib/expose-loader/issues/49
*/
Expand All @@ -47,7 +47,7 @@ export default function loader() {

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

let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifiedNewRequest});\n`;
Expand Down
110 changes: 110 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,116 @@ Object {

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

exports[`loader should work with side-effects free modules: errors 1`] = `Array []`;

exports[`loader should work with side-effects free modules: module 1`] = `
"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!./rx.all.js\\");
var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../../src/runtime/getGlobalThis.js\\");
var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___();
___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___;
module.exports = ___EXPOSE_LOADER_IMPORT___;
"
`;

exports[`loader should work with side-effects free modules: result 1`] = `
Object {
"ExposeLoader": Object {
"default": [Function],
},
"global-commonjs2-single-export": Object {
"foo": "bar",
},
"myGlobal": Object {
"AnonymousObservable": [Function],
"AnonymousObserver": [Function],
"AnonymousSubject": [Function],
"ArgumentOutOfRangeError": [Function],
"AsyncSubject": [Function],
"BehaviorSubject": [Function],
"BinaryDisposable": [Function],
"CompositeDisposable": [Function],
"CompositeError": [Function],
"ConnectableObservable": [Function],
"Disposable": [Function],
"EmptyError": [Function],
"FlatMapObservable": [Function],
"HistoricalScheduler": [Function],
"MockDisposable": [Function],
"NAryDisposable": [Function],
"NotImplementedError": [Function],
"NotSupportedError": [Function],
"Notification": [Function],
"ObjectDisposedError": [Function],
"Observable": [Function],
"ObservableBase": [Function],
"Observer": [Function],
"Pauser": [Function],
"ReactiveTest": Object {
"created": 100,
"disposed": 1000,
"onCompleted": [Function],
"onError": [Function],
"onNext": [Function],
"subscribe": [Function],
"subscribed": 200,
},
"Recorded": [Function],
"RefCountDisposable": [Function],
"ReplaySubject": [Function],
"Scheduler": [Function],
"SerialDisposable": [Function],
"SingleAssignmentDisposable": [Function],
"Subject": [Function],
"Subscription": [Function],
"TestScheduler": [Function],
"TimeoutError": [Function],
"VirtualTimeScheduler": [Function],
"config": Object {
"Promise": [Function],
"longStackSupport": false,
"useNativeEvents": false,
},
"doneEnumerator": Object {
"done": true,
"value": undefined,
},
"helpers": Object {
"defaultComparer": [Function],
"defaultError": [Function],
"defaultKeySerializer": [Function],
"defaultNow": [Function],
"defaultSubComparer": [Function],
"identity": [Function],
"isArrayLike": [Function],
"isFunction": [Function],
"isIterable": [Function],
"isPromise": [Function],
"iterator": Symbol(Symbol.iterator),
"noop": [Function],
"notImplemented": [Function],
"notSupported": [Function],
},
"internals": Object {
"AbstractObserver": [Function],
"Enumerable": [Function],
"PriorityQueue": [Function],
"SchedulePeriodicRecursive": [Function],
"ScheduledItem": [Function],
"ScheduledObserver": [Function],
"addProperties": [Function],
"addRef": [Function],
"bindCallback": [Function],
"inherits": [Function],
"isEqual": [Function],
"isObject": [Function],
"tryCatch": [Function],
},
},
}
`;

exports[`loader should work with side-effects free modules: warnings 1`] = `Array []`;

exports[`loader should work: errors 1`] = `Array []`;

exports[`loader should work: module 1`] = `
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/side-effects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'rx';

function test() {
return 'test';
}

export default test;
2 changes: 1 addition & 1 deletion test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
module: {
rules: [
{
test: /global-.+\.js/i,
test: /(global-.+|rx\.all)\.js/i,
rules: [
{
loader: path.resolve(__dirname, '../../src'),
Expand Down
27 changes: 27 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @jest-environment node
*/
import path from 'path';

import webpack from 'webpack';
Expand All @@ -13,6 +16,8 @@ import {
readAsset,
} from './helpers';

jest.setTimeout(30000);

describe('loader', () => {
it('should work', async () => {
const compiler = getCompiler('simple-commonjs2-single-export.js', {
Expand Down Expand Up @@ -465,4 +470,26 @@ describe('loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work with side-effects free modules', async () => {
const compiler = getCompiler(
'side-effects.js',
{
exposes: 'myGlobal',
},
{
mode: 'production',
}
);
const stats = await compile(compiler);

expect(getModuleSource('rx.all-exposed.js', stats)).toMatchSnapshot(
'module'
);
expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});
});

0 comments on commit 748798b

Please sign in to comment.