-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michał Nykiel
committed
Oct 19, 2016
1 parent
31995e3
commit 3b6dd43
Showing
11 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
|
||
dist/ | ||
# Created by https://www.gitignore.io/api/node | ||
|
||
### Node ### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import test from 'ava'; | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import webpack from 'webpack'; | ||
import ModuleMappingPlugin from '../'; | ||
|
||
const outputPath = path.resolve('../dist'); | ||
|
||
async function build(config) { | ||
return new Promise((resolve, reject) => webpack(config, (err, stats) => { | ||
if (err || stats.hasErrors()) { | ||
reject(err || stats.toJson('errors-only') | ||
.errors); | ||
return; | ||
} | ||
resolve(`${config.output.path}/${config.output.filename}`); | ||
})); | ||
} | ||
|
||
function createConfig(entry, pluginConfig) { | ||
return { | ||
entry, | ||
output: { | ||
path: outputPath, | ||
filename: `bundle_${Date.now()}.js`, | ||
library: 'test', | ||
libraryTarget: 'commonjs2' | ||
}, | ||
resolve: { | ||
root: [path.resolve('./modules')] | ||
}, | ||
module: { | ||
loaders: [ | ||
{ test: /\.html$/, loader: 'raw' } | ||
] | ||
}, | ||
plugins: [ | ||
ModuleMappingPlugin(pluginConfig) | ||
] | ||
}; | ||
} | ||
|
||
function testPathType(pathType) { | ||
|
||
test(`should not map anything when using ${pathType} paths`, async t => { | ||
const bundlePath = await build(createConfig(`./modules/index-${pathType}.js`, {})); | ||
const testModule = require(bundlePath); | ||
t.is(testModule.fn(), 'foo'); | ||
}); | ||
|
||
|
||
test(`should map foo.js to bar.js when using ${pathType} paths`, async t => { | ||
const bundlePath = await build(createConfig(`./modules/index-${pathType}.js`, { | ||
'./modules/foo.js': './modules/bar.js' | ||
})); | ||
const testModule = require(bundlePath); | ||
t.is(testModule.fn(), 'bar'); | ||
t.is(testModule.html.trim(), '<div>foo</div>'); | ||
}); | ||
|
||
|
||
test(`should map foo.html to bar.html when using ${pathType} paths`, async t => { | ||
const bundlePath = await build(createConfig(`./modules/index-${pathType}.js`, { | ||
'./modules/foo.html': './modules/bar.html' | ||
})); | ||
const testModule = require(bundlePath); | ||
t.is(testModule.fn(), 'foo'); | ||
t.is(testModule.html.trim(), '<div>bar</div>'); | ||
}); | ||
|
||
|
||
test(`should map both foo.js and foo.html when using ${pathType} paths`, async t => { | ||
const bundlePath = await build(createConfig(`./modules/index-${pathType}.js`, { | ||
'./modules/foo.js': './modules/bar.js', | ||
'./modules/foo.html': './modules/bar.html' | ||
})); | ||
const testModule = require(bundlePath); | ||
t.is(testModule.fn(), 'bar'); | ||
t.is(testModule.html.trim(), '<div>bar</div>'); | ||
}); | ||
|
||
} | ||
|
||
testPathType('relative'); | ||
testPathType('absolute'); | ||
testPathType('module'); | ||
|
||
test.after.always('cleanup dist folder', t => fs.remove(outputPath)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div>bar</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = () => 'bar'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div>foo</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = () => 'foo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports.fn = require(__dirname + '/foo'); | ||
module.exports.html = require(__dirname + '/foo.html'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports.fn = require('foo'); | ||
module.exports.html = require('foo.html'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports.fn = require('./foo'); | ||
module.exports.html = require('./foo.html'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters