Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ESM support #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions __tests__/tests.esm.js
@@ -0,0 +1,80 @@
import assert from 'assert';
import moduleInjector from 'self!./modules/es6.js';

const MODULE_A_STUB = {
a() {
return 'a - stubbed';
},
};

const MODULE_B_STUB = () => 'b - stubbed';

describe('inject-loader (ESM)', () => {
it('works when no injections were provided', () => {
const module = moduleInjector();

assert.equal(module.getA(), 'a - original');
assert.equal(module.getB(), 'b - original');
assert.equal(module.getC(), 'c - original');
});

it('works when one injection was provided', () => {
const module = moduleInjector({
'./a.js': MODULE_A_STUB,
});

assert.equal(module.getA(), 'a - stubbed');
assert.equal(module.getB(), 'b - original');
assert.equal(module.getC(), 'c - original');
});

it('works when a falsey injection was provided', () => {
const module = moduleInjector({
'./c.js': undefined,
});

assert.equal(module.getA(), 'a - original');
assert.equal(module.getB(), 'b - original');
assert.equal(module.getC(), undefined);
});

it('works when multiple injections were provided', () => {
const module = moduleInjector({
'./a.js': MODULE_A_STUB,
'./b.js': MODULE_B_STUB,
});

assert.equal(module.getA(), 'a - stubbed');
assert.equal(module.getB(), 'b - stubbed');
assert.equal(module.getC(), 'c - original');
});

it('throws an error when invalid dependencies are provided', () => {
const injectInvalidDependencies = () => {
moduleInjector({
'./b.js': null,
'./d.js': null,
});
};

assert.throws(injectInvalidDependencies, /Injection Error in/);
assert.throws(
injectInvalidDependencies,
/The following injections are invalid:\n - \.\/d\.js\n/
);
assert.throws(
injectInvalidDependencies,
/The following injections were passed in:\n - \.\/b\.js\n - \.\/d\.js\n/
);
assert.throws(
injectInvalidDependencies,
/Valid injection targets for this module are:\n - \.\/a\.js\n - \.\/b\.js\n - \.\/c\.js/
);
});

it('does not break someObject.require calls', () => {
const module = moduleInjector();

assert.equal(module.callRequireMethod(), 'require method in a.js');
});
});
31 changes: 31 additions & 0 deletions config/webpack.test.esm.config.js
@@ -0,0 +1,31 @@
const path = require('path');
const constants = require('./shared');

module.exports = {
entry: path.resolve(constants.TESTS_PATH, 'tests.esm.js'),
target: 'node',
mode: 'production',
output: {
path: constants.TEMP_PATH,
filename: 'testBundleESM.js',
},
resolveLoader: {
alias: {
self: constants.TEMP_PATH,
},
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: [constants.SOURCE_PATH, constants.TESTS_PATH],
query: {
cacheDirectory: true,
presets: [['es2015', {modules: false}]],
plugins: ['transform-flow-strip-types'],
},
},
],
},
};
4 changes: 3 additions & 1 deletion example/webpack1-babel/webpack.conf.js
Expand Up @@ -32,6 +32,8 @@ module.exports = {
},

resolveLoader: {
modulesDirectories: ['node_modules', '../../'],
alias: {
'inject-loader': path.resolve(__dirname, '../../tmp'),
},
},
};
10 changes: 8 additions & 2 deletions example/webpack2-babel/webpack.conf.js
Expand Up @@ -18,7 +18,11 @@ module.exports = {
main: './test/main_test',
},

plugins: [new webpack.DefinePlugin({__VALUEA__: 10})],
plugins: [
new webpack.DefinePlugin({
__VALUEA__: 10,
}),
],

output: {
path: path.resolve(__dirname, './dest'),
Expand All @@ -36,6 +40,8 @@ module.exports = {
},

resolveLoader: {
modules: ['node_modules', '../../'],
alias: {
'inject-loader': path.resolve(__dirname, '../../tmp'),
},
},
};
3 changes: 3 additions & 0 deletions example/webpack2-esm/.babelrc
@@ -0,0 +1,3 @@
{
"presets": [["es2015", {"modules": false}]]
}
2 changes: 2 additions & 0 deletions example/webpack2-esm/.gitignore
@@ -0,0 +1,2 @@
dest
yarn.lock
26 changes: 26 additions & 0 deletions example/webpack2-esm/karma.conf.js
@@ -0,0 +1,26 @@
const webpackConfig = require('./webpack.conf.js');

module.exports = function karmaConfig(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],

files: [{pattern: 'test/*_test.js', watched: false}],

preprocessors: {
'test/*_test.js': ['webpack'],
},
client: {
clearContext: false,
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['jsdom'],
singleRun: true,
concurrency: Infinity,
webpack: webpackConfig,
});
};
19 changes: 19 additions & 0 deletions example/webpack2-esm/package.json
@@ -0,0 +1,19 @@
{
"private": true,
"scripts": {
"test": "karma start",
"build": "webpack --config webpack.conf.js"
},
"devDependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.18.0",
"jasmine-core": "^3.1.0",
"jsdom": "^11.7.0",
"karma": "^2.0.0",
"karma-jasmine": "^1.1.1",
"karma-jsdom-launcher": "^6.1.3",
"karma-webpack": "^3.0.0",
"webpack": "~2"
}
}
1 change: 1 addition & 0 deletions example/webpack2-esm/src/bar.js
@@ -0,0 +1 @@
export const BAR = 2;
3 changes: 3 additions & 0 deletions example/webpack2-esm/src/getFoo.js
@@ -0,0 +1,3 @@
export default function getFoo() {
return __VALUEA__;
}
6 changes: 6 additions & 0 deletions example/webpack2-esm/src/main.js
@@ -0,0 +1,6 @@
import getFoo from 'getFoo';
import {BAR} from 'bar';

export function getValue() {
return getFoo() * BAR;
}
30 changes: 30 additions & 0 deletions example/webpack2-esm/test/main_test.js
@@ -0,0 +1,30 @@
import {getValue as mainGetValue} from 'main';
import mainModuleInjector from 'inject-loader!main';

describe('Main', () => {
it('works without injecting', () => {
expect(mainGetValue()).toEqual(20);
});

describe('injecting code into module dependencies', () => {
it('allows for injecting code into a subset of dependencies', () => {
let mainModuleInjected = mainModuleInjector({
bar: {BAR: 5},
});
expect(mainModuleInjected.getValue()).toEqual(50);

mainModuleInjected = mainModuleInjector({
getFoo: () => 10,
});
expect(mainModuleInjected.getValue()).toEqual(20);
});

it('allows for injecting code mulitple dependencies', () => {
let mainModuleInjected = mainModuleInjector({
getFoo: () => 5,
bar: {BAR: 5},
});
expect(mainModuleInjected.getValue()).toEqual(25);
});
});
});
47 changes: 47 additions & 0 deletions example/webpack2-esm/webpack.conf.js
@@ -0,0 +1,47 @@
const path = require('path');
const webpack = require('webpack');

module.exports = {
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /(\/node_modules$)/,
},
],
},

target: 'node',

entry: {
main: './test/main_test',
},

plugins: [
new webpack.DefinePlugin({
__VALUEA__: 10,
}),
],

output: {
path: path.resolve(__dirname, './dest'),
filename: '[name].js',
},

resolve: {
extensions: ['.js'],
modules: [
__dirname,
path.resolve(__dirname, './node_modules'),
path.resolve(__dirname, './src'),
path.resolve(__dirname, './test'),
],
},

resolveLoader: {
alias: {
'inject-loader': path.resolve(__dirname, '../../tmp'),
},
},
};
10 changes: 8 additions & 2 deletions example/webpack3-babel/webpack.conf.js
Expand Up @@ -18,7 +18,11 @@ module.exports = {
main: './test/main_test',
},

plugins: [new webpack.DefinePlugin({__VALUEA__: 10})],
plugins: [
new webpack.DefinePlugin({
__VALUEA__: 10,
}),
],

output: {
path: path.resolve(__dirname, './dest'),
Expand All @@ -36,6 +40,8 @@ module.exports = {
},

resolveLoader: {
modules: ['node_modules', '../../'],
alias: {
'inject-loader': path.resolve(__dirname, '../../tmp'),
},
},
};
3 changes: 3 additions & 0 deletions example/webpack3-esm/.babelrc
@@ -0,0 +1,3 @@
{
"presets": [["es2015", {"modules": false}]]
}
2 changes: 2 additions & 0 deletions example/webpack3-esm/.gitignore
@@ -0,0 +1,2 @@
dest
yarn.lock
26 changes: 26 additions & 0 deletions example/webpack3-esm/karma.conf.js
@@ -0,0 +1,26 @@
const webpackConfig = require('./webpack.conf.js');

module.exports = function karmaConfig(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],

files: [{pattern: 'test/*_test.js', watched: false}],

preprocessors: {
'test/*_test.js': ['webpack'],
},
client: {
clearContext: false,
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['jsdom'],
singleRun: true,
concurrency: Infinity,
webpack: webpackConfig,
});
};
19 changes: 19 additions & 0 deletions example/webpack3-esm/package.json
@@ -0,0 +1,19 @@
{
"private": true,
"scripts": {
"test": "karma start",
"build": "webpack --config webpack.conf.js"
},
"devDependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.18.0",
"jasmine-core": "^3.1.0",
"jsdom": "^11.7.0",
"karma-jasmine": "^1.1.1",
"karma-jsdom-launcher": "^6.1.3",
"karma-webpack": "^3.0.0",
"karma": "^2.0.0",
"webpack": "~3"
}
}
1 change: 1 addition & 0 deletions example/webpack3-esm/src/bar.js
@@ -0,0 +1 @@
export const BAR = 2;
3 changes: 3 additions & 0 deletions example/webpack3-esm/src/getFoo.js
@@ -0,0 +1,3 @@
export default function getFoo() {
return __VALUEA__;
}
6 changes: 6 additions & 0 deletions example/webpack3-esm/src/main.js
@@ -0,0 +1,6 @@
import getFoo from 'getFoo';
import {BAR} from 'bar';

export function getValue() {
return getFoo() * BAR;
}