Skip to content

Commit

Permalink
update to babel 7
Browse files Browse the repository at this point in the history
reference: plasticine#74
  • Loading branch information
orrisroot authored and marknadig committed Jan 26, 2024
1 parent 804e729 commit c102c6c
Show file tree
Hide file tree
Showing 51 changed files with 1,822 additions and 693 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -3,7 +3,6 @@ node_js:
- 12
- 10
- 8
- 6
cache: yarn
script:
- yarn test
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');
});
});
2 changes: 1 addition & 1 deletion config/shared.js
Expand Up @@ -4,7 +4,7 @@ const ROOT_PATH = path.resolve(__dirname, '..');
const SOURCE_PATH = path.resolve(ROOT_PATH, 'src');
const TESTS_PATH = path.resolve(ROOT_PATH, '__tests__');
const TEMP_PATH = path.resolve(ROOT_PATH, 'tmp');
const NODE_EXTERNAL_DEPS = ['babel-core'];
const NODE_EXTERNAL_DEPS = ['@babel/core'];

module.exports = {
SOURCE_PATH,
Expand Down
13 changes: 11 additions & 2 deletions config/webpack.config.js
Expand Up @@ -22,8 +22,17 @@ module.exports = {
include: [constants.SOURCE_PATH, constants.TESTS_PATH],
query: {
cacheDirectory: true,
presets: ['es2015'],
plugins: ['add-module-exports', 'transform-flow-strip-types'],
presets: [
[
'@babel/preset-env',
{
targets: {
node: '6',
},
},
],
],
plugins: ['@babel/plugin-transform-flow-strip-types'],
},
},
],
Expand Down
11 changes: 9 additions & 2 deletions config/webpack.test.config.js
Expand Up @@ -22,8 +22,15 @@ module.exports = {
include: [constants.SOURCE_PATH, constants.TESTS_PATH],
query: {
cacheDirectory: true,
presets: ['es2015'],
plugins: ['add-module-exports', 'transform-flow-strip-types'],
presets: [
[
'@babel/preset-env',
{
modules: 'cjs',
},
],
],
plugins: ['@babel/plugin-transform-flow-strip-types'],
},
},
],
Expand Down
38 changes: 38 additions & 0 deletions config/webpack.test.esm.config.js
@@ -0,0 +1,38 @@
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: [
[
'@babel/preset-env',
{
modules: false,
},
],
],
plugins: ['@babel/plugin-transform-flow-strip-types'],
},
},
],
},
};
3 changes: 0 additions & 3 deletions example/webpack1-babel/.babelrc

This file was deleted.

18 changes: 0 additions & 18 deletions example/webpack1-babel/package.json

This file was deleted.

3 changes: 0 additions & 3 deletions example/webpack1-babel/src/bar.js

This file was deleted.

38 changes: 0 additions & 38 deletions example/webpack1-babel/test/main_test.js

This file was deleted.

2 changes: 1 addition & 1 deletion example/webpack2-babel/.babelrc
@@ -1,3 +1,3 @@
{
"presets": "es2015"
"presets": ["@babel/preset-env"]
}
6 changes: 3 additions & 3 deletions example/webpack2-babel/package.json
Expand Up @@ -5,9 +5,9 @@
"build": "webpack --config webpack.conf.js"
},
"devDependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.18.0",
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"babel-loader": "^8.1.0",
"jasmine-core": "^3.1.0",
"jsdom": "^11.7.0",
"karma-jasmine": "^1.1.1",
Expand Down
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": [["@babel/preset-env", {"modules": false}]]
}
File renamed without changes.
Expand Up @@ -18,7 +18,7 @@ module.exports = function karmaConfig(config) {
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
browsers: ['jsdom'],
singleRun: true,
concurrency: Infinity,
webpack: webpackConfig,
Expand Down
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": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"babel-loader": "^8.1.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;
@@ -1,5 +1,3 @@
/* global __VALUEA__ */

export default function getFoo() {
return __VALUEA__;
}
@@ -1,7 +1,5 @@
/* eslint-disable import/prefer-default-export */

import getFoo from 'getFoo';
import { BAR } from 'bar';
import {BAR} from 'bar';

export function getValue() {
return getFoo() * BAR;
Expand Down
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);
});
});
});

0 comments on commit c102c6c

Please sign in to comment.