Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Fix regression in parsing loader string chains
Browse files Browse the repository at this point in the history
Fixes GH-68
  • Loading branch information
amireh committed Aug 8, 2016
1 parent 6cb84d4 commit 156ec29
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ _TODO: test against other projects_

## Changes

**master**

- Fixed a regression in parsing loader "string chains" (multiple loaders specified in the same string separated by `!`), refs GH-68

**2.1.3**

- Fixed an issue where certain loader configurations with queries weren't being
Expand Down
22 changes: 16 additions & 6 deletions lib/WebpackUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports.normalizeLoader = function(input) {
}
// webpack/happypack string loader(s):
else if (typeof input === 'string') {
normals = input.split('!').map(extractPathAndQueryFromString);
normals = [].concat(extractPathAndQueryFromString(input));
}

return normals.map(function(loader) {
Expand Down Expand Up @@ -96,19 +96,22 @@ exports.normalizeLoader = function(input) {
*/
exports.extractLoaders = function extractLoaders(entry) {
if (typeof entry === 'object' && entry.loaders) {
return entry.loaders.map(extractLoaders);
return entry.loaders.reduce(function(list, loader) {
return list.concat(extractLoaders(loader));
}, []);
}
else if (typeof entry === 'string') {
return extractPathAndQueryFromString(entry);
}
else if (typeof entry.loader === 'string' && entry.query) {
return { path: entry.loader, query: entry.query };
}
else if (typeof entry.loader === 'string' && entry.loader.indexOf('?') > -1) {
else if (typeof entry.loader === 'string') {
return extractPathAndQueryFromString(entry.loader);
}
else {
return { path: entry.loader };
console.error(entry);
assert(false, "HappyPack: Unrecognized loader configuration variant!");
}
};

Expand Down Expand Up @@ -147,10 +150,17 @@ exports.resolveLoaders = function(compiler, loaders, done) {
exports.applyLoaders = require('./applyLoaders');

function extractPathAndQueryFromString(string) {
var fragments = string.split('?');
var loaders = string.split('!');

if (loaders.length > 1) {
return loaders.map(extractPathAndQueryFromString);
}

var loaderString = loaders[0];
var fragments = loaderString.split('?');

if (fragments.length === 1) {
return { path: string };
return { path: loaderString };
}

return { path: fragments[0], query: '?' + fragments[1] };
Expand Down
60 changes: 55 additions & 5 deletions lib/__tests__/HappyPlugin.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,61 @@ describe('[Integration] HappyPlugin', function() {
module: {
loaders: [{
test: /.js$/,
loader: path.resolve(__dirname, '../HappyLoader')
}]
},

resolveLoader: {
root: path.dirname(loader.path)
},

plugins: [
new HappyPlugin({
loaders: [
{ path: loader._name, query: { string: 'HAPPY' } }
]
})
]
});

compiler.run(function(err, rawStats) {
if (err) { return done(err); }

var stats = rawStats.toJson();

if (stats.errors.length > 0) {
return done(stats.errors);
}

assert.match(fs.readFileSync(path.join(outputDir, 'main.js'), 'utf-8'), '// ?{"string":"HAPPY"}');

done();
});
});

it("infers loaders from webpack config", function(done) {
var outputDir = testSuite.createDirectory('dist').path;
var loader = testSuite.createLoader(function(s) {
return s + '\n// ' + this.query;
});

var compiler = webpack({
entry: {
main: [
TestUtils.fixturePath('integration/index.js')
]
},

output: {
filename: '[name].js',
path: outputDir
},

module: {
loaders: [{
happy: { id: 'js' },
loader: loader._name,
query: {
string: 'HAPPY'
}
test: /.js$/,
loader: loader._name + '?string=HAPPY',
}]
},

Expand All @@ -223,7 +273,7 @@ describe('[Integration] HappyPlugin', function() {
return done(stats.errors);
}

assert.match(fs.readFileSync(path.join(outputDir, 'main.js'), 'utf-8'), '// ?{"string":"HAPPY"}');
assert.match(fs.readFileSync(path.join(outputDir, 'main.js'), 'utf-8'), '// ?string=HAPPY');

done();
});
Expand Down
126 changes: 126 additions & 0 deletions lib/__tests__/WebpackUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,132 @@ describe('WebpackUtils', function() {
}
]
},

{
description: 'multiple loaders in a string',
input: {
loader: 'react-hot!babel'
},

output: [
{
path: 'react-hot',
},
{
path: 'babel',
}
],

outputNormal: [
{
path: 'react-hot',
},
{
path: 'babel'
}
]
},

{
description: 'multiple loaders in a string with an embedded query',
input: {
loader: 'react-hot!babel?presets[]=es2015'
},

output: [
{
path: 'react-hot',
},
{
path: 'babel',
query: '?presets[]=es2015',
}
],

outputNormal: [
{
path: 'react-hot',
},
{
path: 'babel',
query: '?presets[]=es2015',
}
]
},
{
description: 'multiple loaders in a string with multiple embedded queries',
input: {
loader: 'style!css?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]!autoprefixer?browsers=last 2 version!sass?outputStyle=expanded&sourceMap',
},

output: [
{
path: 'style',
},
{
path: 'css',
query: '?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]',
},
{
path: 'autoprefixer',
query: '?browsers=last 2 version',
},
{
path: 'sass',
query: '?outputStyle=expanded&sourceMap',
},
],

outputNormal: [
{
path: 'style',
},
{
path: 'css',
query: '?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]',
},
{
path: 'autoprefixer',
query: '?browsers=last 2 version',
},
{
path: 'sass',
query: '?outputStyle=expanded&sourceMap',
},
]
},

{
description: 'multiple loaders in a string array',
input: {
loaders: [ 'style!css', 'less' ]
},

output: [
{
path: 'style',
},
{
path: 'css',
},
{
path: 'less',
},
],

outputNormal: [
{
path: 'style',
},
{
path: 'css',
},
{
path: 'less',
},
]
},

];

describe('.extractLoaders', function() {
Expand Down

0 comments on commit 156ec29

Please sign in to comment.