Socket
Socket
Sign inDemoInstall

assets-webpack-plugin

Package Overview
Dependencies
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assets-webpack-plugin - npm Package Compare versions

Comparing version 2.3.0 to 3.0.0

.editorconfig

123

index.js

@@ -1,46 +0,43 @@

var mkdirp = require('mkdirp');
var path = require('path');
var fs = require('fs');
var merge = require('lodash.merge');
var getAssetKind = require('./lib/getAssetKind');
var isHMRUpdate = require('./lib/isHMRUpdate');
var isSourceMap = require('./lib/isSourceMap');
var createQueuedWriter = require('./lib/output/createQueuedWriter');
var createOutputWriter = require('./lib/output/createOutputWriter');
function AssetsWebpackPlugin (options) {
this.options = this.getOptions(options || {});
this.outputPath = path.join(this.options.path, this.options.filename);
this.options = merge({}, {
path: '.',
filename: 'webpack-assets.json',
prettyPrint: false,
update: false
}, options);
this.writer = createQueuedWriter(createOutputWriter(this.options));
}
AssetsWebpackPlugin.prototype = {
constructor: AssetsWebpackPlugin,
getOptions: function (options) {
var defaults = {
path: '.',
filename: 'webpack-assets.json',
prettyPrint: false
};
return Object.keys(defaults).reduce(function (map, option) {
map[option] = options.hasOwnProperty(option) ? options[option] : defaults[option];
return map;
}, {});
},
constructor: AssetsWebpackPlugin,
apply: function (compiler) {
var self = this;
apply: function (compiler) {
var self = this;
compiler.plugin("emit", function (compilation, callback) {
var options = compiler.options;
stats = compilation.getStats().toJson({
hash: true,
publicPath: true,
assets: true,
chunks: false,
modules: false,
source: false,
errorDetails: false,
timings: false
});
compiler.plugin('after-emit', function (compilation, callback) {
var options = compiler.options;
var stats = compilation.getStats().toJson({
hash: true,
publicPath: true,
assets: true,
chunks: false,
modules: false,
source: false,
errorDetails: false,
timings: false
});
// publicPath with resolved [hash] placeholder
var publicPath = stats.publicPath || '';
var publicPath = stats.publicPath || '';
// assetsByChunkName contains a hash with the bundle names and the produced files

@@ -52,52 +49,36 @@ // e.g. { one: 'one-bundle.js', two: 'two-bundle.js' }

// [ 'index-bundle-42b6e1ec4fa8c5f0303e.js',
// 'index-bundle-42b6e1ec4fa8c5f0303e.js.map' ]
// 'index-bundle-42b6e1ec4fa8c5f0303e.js.map' ]
// }
var assetsByChunkName = stats.assetsByChunkName;
var assetsByChunkName = stats.assetsByChunkName;
var output = Object.keys(assetsByChunkName).reduce(function (chunkMap, chunkName) {
var assets = assetsByChunkName[chunkName];
if (!Array.isArray(assets)) {
assets = [assets];
}
chunkMap[chunkName] = assets.reduce(function (typeMap, asset) {
if (isHMRUpdate(options, asset)) {
return typeMap;
}
var typeName = getAssetKind(options, asset);
typeMap[typeName] = publicPath + asset;
var output = Object.keys(assetsByChunkName).reduce(function (chunkMap, chunkName) {
var assets = assetsByChunkName[chunkName];
if (!Array.isArray(assets)) {
assets = [assets];
}
chunkMap[chunkName] = assets.reduce(function (typeMap, asset) {
if (isHMRUpdate(options, asset) || isSourceMap(options, asset)) {
return typeMap;
}, {});
}
return chunkMap;
}, {});
var typeName = getAssetKind(options, asset);
typeMap[typeName] = publicPath + asset;
self.writeOutput(output, compilation);
return typeMap;
}, {});
callback();
});
},
return chunkMap;
}, {});
writeOutput: function (assets, compiler) {
try {
mkdirp.sync(this.options.path);
} catch (e) {
compiler.errors.push(new Error(
'[AssetsWebpackPlugin]: Could not create output folder' + this.options.path
));
return;
}
var json = JSON.stringify(assets, null, this.options.prettyPrint ? 2 : null);
fs.writeFile(this.outputPath, json, function (err) {
self.writer(output, function (err) {
if (err) {
compiler.errors.push(new Error(
'[AssetsWebpackPlugin]: Unable to write to ' + this.outputPath
));
}
});
},
compilation.errors.push(err);
}
callback();
});
});
}
};
module.exports = AssetsWebpackPlugin;
var camelcase = require('camelcase');
var isSourceMap = require('./isSourceMap');
var getFileExtension = require('./getFileExtension');

@@ -8,11 +7,4 @@

module.exports = function getAssetKind (options, asset) {
var isMap = isSourceMap(options, asset);
if (isMap) {
asset = asset.replace(/\.(?:source[ _.-]?)?map/i, '');
}
var ext = getFileExtension(asset);
if (isMap) {
ext += 'SourceMap';
}
return camelcase(ext);
var ext = getFileExtension(asset);
return camelcase(ext);
};

@@ -5,5 +5,5 @@ var URL = require('url');

module.exports = function getFileExtension (asset) {
var url = URL.parse(asset);
var ext = path.extname(url.pathname);
return ext ? ext.slice(1) : '';
}
var url = URL.parse(asset);
var ext = path.extname(url.pathname);
return ext ? ext.slice(1) : '';
};
var pathTemplate = require('./pathTemplate');
module.exports = function isHMRUpdate (options, asset) {
var hotUpdateChunkFilename = options.output.hotUpdateChunkFilename;
var hotUpdateTemplate = pathTemplate(hotUpdateChunkFilename);
return hotUpdateTemplate.matches(asset);
var hotUpdateChunkFilename = options.output.hotUpdateChunkFilename;
var hotUpdateTemplate = pathTemplate(hotUpdateChunkFilename);
return hotUpdateTemplate.matches(asset);
};
var pathTemplate = require('./pathTemplate');
module.exports = function isSourceMap (options, asset) {
var sourceMapFilename = options.output.sourceMapFilename;
var sourcemapTemplate = pathTemplate(sourceMapFilename);
return sourcemapTemplate.matches(asset);
var sourceMapFilename = options.output.sourceMapFilename;
var sourcemapTemplate = pathTemplate(sourceMapFilename);
return sourcemapTemplate.matches(asset);
};

@@ -11,14 +11,14 @@ var escapeRegExp = require('escape-string-regexp');

if (template_cache[str]) {
return template_cache[str];
}
if (template_cache[str]) {
return template_cache[str];
}
return template_cache[str] = new PathTemplate(str);
}
return template_cache[str] = new PathTemplate(str);
};
function PathTemplate (template) {
this.template = template;
this.fields = parseTemplate(template);
this.matcher = createTemplateMatcher(this.fields);
this.template = template;
this.fields = parseTemplate(template);
this.matcher = createTemplateMatcher(this.fields);
}

@@ -28,3 +28,3 @@

constructor: PathTemplate,
constructor: PathTemplate,

@@ -36,5 +36,5 @@ /**

*/
matches: function (path) {
return this.matcher.test(path);
},
matches: function (path) {
return this.matcher.test(path);
},

@@ -46,21 +46,22 @@ /**

*/
resolve: function (data) {
return this.fields.reduce(function (output, field) {
var replacement = '',
placeholder = field.placeholder
;
if (field.prefix) {
output += field.prefix;
}
if (placeholder) {
replacement = data[placeholder] || '';
if (field.width && (placeholder === 'hash' || placeholder === 'chunkhash')) {
replacement = replacement.slice(0, width);
}
output += replacement;
}
resolve: function (data) {
return this.fields.reduce(function (output, field) {
var replacement = '',
placeholder = field.placeholder,
width = field.width;
return output;
}, '');
}
if (field.prefix) {
output += field.prefix;
}
if (placeholder) {
replacement = data[placeholder] || '';
if (width && (placeholder === 'hash' || placeholder === 'chunkhash')) {
replacement = replacement.slice(0, width);
}
output += replacement;
}
return output;
}, '');
}
};

@@ -75,3 +76,3 @@

* }
*
*
* The values in the object conceptually represent a span of literal text followed by a single replacement field.

@@ -85,48 +86,48 @@ * If there is no literal text (which can happen if two replacement fields occur consecutively),

function parseTemplate (str) {
var fields = [];
var char = '', pos = 0;
var prefix = '', placeholder;
var match = null, input;
var fields = [];
var char = '', pos = 0;
var prefix = '';
var match = null, input;
while (true) {
char = str[pos];
while (true) { // eslint-disable-line no-constant-condition
char = str[pos];
if (!char) {
if (!char) {
fields.push({
prefix: prefix,
placeholder: null
});
break;
} else if (char === '[') {
input = str.slice(pos);
match = SIMPLE_PLACEHOLDER_RX.exec(input);
if (match) {
fields.push({
prefix: prefix,
placeholder: null
});
break;
} else if (char === '[') {
placeholder: match[1].toLowerCase()
});
pos += match[0].length;
prefix = '';
continue;
}
input = str.slice(pos)
match = SIMPLE_PLACEHOLDER_RX.exec(input);
if (match) {
fields.push({
prefix: prefix,
placeholder: match[1].toLowerCase()
});
pos += match[0].length;
prefix = '';
continue;
}
match = HASH_PLACEHOLDER_RX.exec(input);
if (match) {
fields.push({
prefix: prefix,
placeholder: match[1].toLowerCase(),
width: parseInt(match[2] || 0, 10)
});
pos += match[0].length;
prefix = '';
continue;
}
match = HASH_PLACEHOLDER_RX.exec(input);
if (match) {
fields.push({
prefix: prefix,
placeholder: match[1].toLowerCase(),
width: parseInt(match[2] || 0, 10)
});
pos += match[0].length;
prefix = '';
continue;
}
}
prefix += char;
pos++;
}
prefix += char;
pos++;
}
return fields;
return fields;
}

@@ -139,38 +140,38 @@

function createTemplateMatcher (fields) {
var length = fields.length;
var pattern = fields.reduce(function (pattern, field, i) {
if (i === 0) {
pattern = '^';
}
if (field.prefix) {
pattern += '(' + escapeRegExp(field.prefix) + ')';
}
if (field.placeholder) {
switch (field.placeholder) {
case 'id':
pattern += '\\d+';
break;
case 'hash':
case 'chunkhash':
pattern += '[0-9a-fA-F]';
pattern += field.width ? '{1,' + field.width + '}' : '+';
break;
case 'name':
case 'file':
case 'filebase':
pattern += '.+?';
break;
case 'query':
pattern += '(?:\\?.+?)?';
break;
}
}
if (i === length - 1) {
pattern += '$';
}
var length = fields.length;
var pattern = fields.reduce(function (pattern, field, i) {
if (i === 0) {
pattern = '^';
}
if (field.prefix) {
pattern += '(' + escapeRegExp(field.prefix) + ')';
}
if (field.placeholder) {
switch (field.placeholder) {
case 'id':
pattern += '\\d+';
break;
case 'hash':
case 'chunkhash':
pattern += '[0-9a-fA-F]';
pattern += field.width ? '{1,' + field.width + '}' : '+';
break;
case 'name':
case 'file':
case 'filebase':
pattern += '.+?';
break;
case 'query':
pattern += '(?:\\?.+?)?';
break;
}
}
if (i === length - 1) {
pattern += '$';
}
return pattern;
}, '');
return pattern;
}, '');
return new RegExp(pattern);
return new RegExp(pattern);
}
{
"name": "assets-webpack-plugin",
"version": "2.3.0",
"version": "3.0.0",
"description": "Emits a json file with assets paths",
"main": "index.js",
"scripts": {
"test": "jshint *.js test && ./node_modules/.bin/mocha test"
"lint": "eslint --fix .",
"test": "npm run lint && ./node_modules/.bin/mocha test"
},

@@ -29,4 +30,4 @@ "repository": {

"css-loader": "^0.9.1",
"eslint": "^1.6.0",
"extract-text-webpack-plugin": "^0.3.8",
"jshint": "^2.5.2",
"lodash": "^3.9.3",

@@ -41,4 +42,6 @@ "mocha": "^2.2.5",

"escape-string-regexp": "^1.0.3",
"lodash.assign": "^3.2.0",
"lodash.merge": "^3.3.2",
"mkdirp": "^0.5.1"
}
}

@@ -29,3 +29,3 @@ assets-webpack-plugin

* `"bundle_name"` is the name of the bundle (the key of the entry object in your webpack config, or "main" if your entry is an array).
* `"asset_kind"` is the camel-cased file extension of the asset, except for source maps where `asset_kind = camelcase(file_xtension) + 'SourceMap'`.
* `"asset_kind"` is the camel-cased file extension of the asset

@@ -43,4 +43,3 @@ For example, given the following webpack config:

publicPath: "/js/",
filename: '[name]_[hash].bundle.js',
sourceMapFilename: '[file].map'
filename: '[name]_[hash].bundle.js'
}

@@ -55,8 +54,6 @@ }

"one": {
"js": "/js/one_2bb80372ebe8047a68d4.bundle.js",
"jsSourceMap": "/js/one_2bb80372ebe8047a68d4.bundle.js.map"
"js": "/js/one_2bb80372ebe8047a68d4.bundle.js"
},
"two": {
"js": "/js/two_2bb80372ebe8047a68d4.bundle.js",
"jsSourceMap": "/js/two_2bb80372ebe8047a68d4.bundle.js.map"
"js": "/js/two_2bb80372ebe8047a68d4.bundle.js"
}

@@ -109,2 +106,8 @@ }

__update__: When set to true, the output json file will be updated instead of overwritten. Defaults to false.
```js
new AssetsPlugin({update: true})
```
__prettyPrint__: Whether to format the json output for readability. Defaults to false.

@@ -116,3 +119,29 @@

### Using in multi-compiler mode
If you use webpack multi-compiler mode and want your assets written to a single file,
you __must__ use the same instance of the plugin in the different configurations.
For example:
```js
var webpack = require('webpack');
var AssetsPlugin = require('assets-webpack-plugin');
var assetsPluginInstance = new AssetsPlugin();
webpack([
{
entry: {one: 'src/one.js'},
output: {path: 'build', filename: 'one-bundle.js'},
plugins: [assetsPluginInstance]
},
{
entry: {two:'src/two.js'},
output: {path: 'build', filename: 'two-bundle.js'},
plugins: [assetsPluginInstance]
}
]);
```
### Using this with Rails

@@ -119,0 +148,0 @@

@@ -1,3 +0,2 @@

var webpack = require('webpack');
var getAssetKind = require('../lib/getAssetKind.js');
var getAssetKind = require('../lib/getAssetKind.js');
var chai = require('chai');

@@ -7,61 +6,32 @@ var expect = chai.expect;

describe('getAssetKind', function() {
var webpackConfig;
describe('getAssetKind', function () {
var webpackConfig;
beforeEach(function () {
webpackConfig = {
output: {
filename: '[name].js?[hash]',
sourceMapFilename: '[file].map[query]'
},
devtool: 'sourcemap'
};
});
beforeEach(function () {
webpackConfig = {
output: {
filename: '[name].js?[hash]',
sourceMapFilename: '[file].map[query]'
},
devtool: 'sourcemap'
};
});
describe('js', function() {
describe('js', function () {
it('returns js', function () {
var input = 'desktop.js';
var res = getAssetKind(webpackConfig, input);
expect(res).to.eq('js');
});
it('returns js', function () {
var input = 'desktop.js';
var res = getAssetKind(webpackConfig, input);
expect(res).to.eq('js');
});
it('returns js with hash', function() {
var input = 'desktop.js?9b913c8594ce98e06b21';
var res = getAssetKind(webpackConfig, input);
expect(res).to.eq('js');
});
it('returns js with hash', function () {
var input = 'desktop.js?9b913c8594ce98e06b21';
var res = getAssetKind(webpackConfig, input);
expect(res).to.eq('js');
});
});
});
describe('map', function() {
it('returns map', function() {
var input = 'desktop.js.map';
var res = getAssetKind(webpackConfig, input);
expect(res).to.eq('jsSourceMap');
});
it('returns map', function() {
var input = 'desktop.js.map?9b913c8594ce98e06b21';
var res = getAssetKind(webpackConfig, input);
expect(res).to.eq('jsSourceMap');
});
it('detects sourcemap even without extension', function() {
webpackConfig = {
output: {
filename: '[name].js?[hash]',
sourceMapFilename: 'srcmap_[hash]_[file][query]'
},
devtool: 'sourcemap'
};
var input = 'srcmap_9b913c8594ce98e06b21_desktop.js?9b913c8594ce98e06b21';
var res = getAssetKind(webpackConfig, input);
expect(res).to.eq('jsSourceMap');
});
});
});
var getFileExtension = require('../lib/getFileExtension');
var chai = require('chai');
var expect = chai.expect;
var expect = require('chai').expect;
function expectExtension(fileName, expected) {
var actual = getFileExtension(fileName) ;
expect(actual).to.eq(expected);
function expectExtension (fileName, expected) {
var actual = getFileExtension(fileName) ;
expect(actual).to.eq(expected);
}
describe('getFileExt', function() {
var webpackConfig;
describe('getFileExt', function () {
it('returns the right extension with simple file names', function() {
expectExtension('main.js', 'js');
expectExtension('main-9b913c8594ce98e06b21.js', 'js');
});
it('returns the right extension with simple file names', function () {
expectExtension('main.js', 'js');
expectExtension('main-9b913c8594ce98e06b21.js', 'js');
});
it('returns the right extension with query strings', function() {
expectExtension('main.js?9b913c8594ce98e06b21', 'js');
expectExtension('desktop.js.map?9b913c8594ce98e06b21', 'map');
});
it('returns the right extension with query strings', function () {
expectExtension('main.js?9b913c8594ce98e06b21', 'js');
expectExtension('desktop.js.map?9b913c8594ce98e06b21', 'map');
});
});
/*jshint expr: true*/
var path = require('path');
var fs = require('fs');
// var mocha = require('mocha');
var chai = require('chai');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var rm_rf = require('rimraf');
var mkdirp = require('mkdirp');
var _ = require('lodash');
var Plugin = require('../index.js');
var expect = chai.expect;
var OUTPUT_DIR = path.join(__dirname, '../tmp');
var expectOutput = require('./utils/expectOutput')(OUTPUT_DIR);
function expectOutput(args, done) {
if (!args.config) {
throw new Error('Expected args.config');
}
if (!args.expected) {
throw new Error('Expected args.expected');
}
if (!done) {
throw new Error('Expected done');
}
describe('Plugin', function () {
var webpackConfig = args.config;
var expectedResult = args.expected;
var outputFile = args.outputFile;
beforeEach(function (done) {
rm_rf(OUTPUT_DIR, done);
});
// Create output folder
mkdirp(OUTPUT_DIR, function(err) {
expect(err).to.be.null;
it('generates a default file for a single entry point', function (done) {
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: 'index-bundle.js'
},
plugins: [new Plugin({
path: 'tmp'
})]
};
outputFile = outputFile || 'webpack-assets.json';
var expected = {
main: {
js: 'index-bundle.js'
}
};
expected = JSON.stringify(expected);
webpack(webpackConfig, function(err, stats) {
expect(err).to.be.null;
expect(stats.hasErrors()).to.be.false;
var args = {
config: webpackConfig,
expected: expected
};
var content = fs.readFileSync(path.join(OUTPUT_DIR, outputFile)).toString();
expectOutput(args, done);
});
if (_.isRegExp(expectedResult)) {
expect(content).to.match(expectedResult);
} else if(_.isString(expectedResult)) {
expect(content).to.contain(expectedResult);
} else {
// JSON object provided
var actual = JSON.parse(content);
expect(actual).to.eql(expectedResult);
}
it('generates a default file with multiple entry points', function (done) {
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/one.js'),
two: path.join(__dirname, 'fixtures/two.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]-bundle.js'
},
plugins: [new Plugin({path: 'tmp'})]
};
done();
});
var expected = {
one: {
js: 'one-bundle.js'
},
two: {
js: 'two-bundle.js'
}
};
});
}
var args = {
config: webpackConfig,
expected: expected
};
describe('Plugin', function() {
expectOutput(args, done);
});
beforeEach(function(done) {
rm_rf(OUTPUT_DIR, done);
});
it('allows you to specify your own filename', function (done) {
it('generates a default file for a single entry point', function(done) {
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: 'index-bundle.js'
},
plugins: [new Plugin({
path: 'tmp'
})]
};
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: 'index-bundle.js'
},
plugins: [new Plugin({
filename: 'foo.json',
path: 'tmp'
})]
};
var expected = {
main: {
js: 'index-bundle.js'
}
};
expected = JSON.stringify(expected);
var expected = {
main: {
js: 'index-bundle.js'
}
};
var args = {
config: webpackConfig,
expected: expected
};
var args = {
config: webpackConfig,
expected: expected,
outputFile: 'foo.json'
};
expectOutput(args, done);
});
expectOutput(args, done);
});
it('generates a default file with multiple entry points', function(done) {
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/one.js'),
two: path.join(__dirname, 'fixtures/two.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]-bundle.js'
},
plugins: [new Plugin({path: 'tmp'})]
};
it('skips source maps', function (done) {
var expected = {
one: {
js: 'one-bundle.js'
},
two: {
js: 'two-bundle.js'
}
};
var webpackConfig = {
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: 'index-bundle.js'
},
plugins: [new Plugin({path: 'tmp'})]
};
var args = {
config: webpackConfig,
expected: expected
};
var expected = {
main: {
js: 'index-bundle.js'
}
};
expectOutput(args, done);
});
var args = {
config: webpackConfig,
expected: expected
};
it('allows you to specify your own filename', function(done) {
expectOutput(args, done);
});
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: 'index-bundle.js'
},
plugins: [new Plugin({
filename: 'foo.json',
path: 'tmp'
})]
};
it('handles hashes in bundle filenames', function (done) {
var expected = {
main: {
js: 'index-bundle.js'
}
};
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: 'index-bundle-[hash].js'
},
plugins: [new Plugin({path: 'tmp'})]
};
var args = {
config: webpackConfig,
expected: expected,
outputFile: 'foo.json'
};
var expected = /{"main":{"js":"index-bundle-[0-9a-f]+\.js"}}/;
expectOutput(args, done);
});
var args = {
config: webpackConfig,
expected: expected
};
it('works with source maps', function(done) {
expectOutput(args, done);
});
var webpackConfig = {
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: 'index-bundle.js'
},
plugins: [new Plugin({path: 'tmp'})]
};
it('handles hashes in a different position', function (done) {
var expected = {
main: {
js: 'index-bundle.js',
jsSourceMap: 'index-bundle.js.map'
}
};
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: '[name].js?[hash]'
},
plugins: [new Plugin({path: 'tmp'})]
};
var args = {
config: webpackConfig,
expected: expected
};
var expected = /{"main":{"js":"main\.js\?[0-9a-f]+"}}/;
expectOutput(args, done);
});
var args = {
config: webpackConfig,
expected: expected
};
it('works with source maps and hash', function(done) {
var webpackConfig = {
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: 'index-bundle-[hash].js'
},
plugins: [new Plugin({path: 'tmp'})]
};
expectOutput(args, done);
});
var expected = /{"main":{"js":"index-bundle-[0-9a-f]+\.js","jsSourceMap":"index-bundle-[0-9a-f]+\.js\.map"}}/;
it('works with ExtractTextPlugin for stylesheets', function (done) {
var args = {
config: webpackConfig,
expected: expected
};
expectOutput(args, done);
});
it('handles hashes in bundle filenames', function(done) {
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: 'index-bundle-[hash].js'
},
plugins: [new Plugin({path: 'tmp'})]
};
var expected = /{"main":{"js":"index-bundle-[0-9a-f]+\.js"}}/;
var args = {
config: webpackConfig,
expected: expected
};
expectOutput(args, done);
});
it('handles hashes in a different position', function(done) {
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
filename: '[name].js?[hash]'
},
plugins: [new Plugin({path: 'tmp'})]
};
var expected = /{"main":{"js":"main\.js\?[0-9a-f]+"}}/;
var args = {
config: webpackConfig,
expected: expected
};
expectOutput(args, done);
});
it('works with ExtractTextPlugin for stylesheets', function(done) {
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/one.js'),
two: path.join(__dirname, 'fixtures/two.js'),
styles: path.join(__dirname, 'fixtures/styles.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]-bundle.js'
},
module: {
loaders: [
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/one.js'),
two: path.join(__dirname, 'fixtures/two.js'),
styles: path.join(__dirname, 'fixtures/styles.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]-bundle.js'
},
module: {
loaders: [
{test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader')}
]
},
plugins: [
new ExtractTextPlugin('[name]-bundle.css', {allChunks: true}),
new Plugin({
path: 'tmp'
})
]
};
]
},
plugins: [
new ExtractTextPlugin('[name]-bundle.css', {allChunks: true}),
new Plugin({
path: 'tmp'
})
]
};
var expected = {
one: {
js: "one-bundle.js"
},
two: {
js: "two-bundle.js"
},
styles: {
js: "styles-bundle.js",
css: "styles-bundle.css"
}
};
var expected = {
one: {
js: 'one-bundle.js'
},
two: {
js: 'two-bundle.js'
},
styles: {
js: 'styles-bundle.js',
css: 'styles-bundle.css'
}
};
var args = {
config: webpackConfig,
expected: expected
};
var args = {
config: webpackConfig,
expected: expected
};
expectOutput(args, done);
});
expectOutput(args, done);
});
it.skip('generates a default file with multiple compilers', function(done) {
var webpackConfig = [
{
entry: {
one: path.join(__dirname, 'fixtures/one.js')
},
output: {
path: OUTPUT_DIR,
filename: 'one-bundle.js'
},
plugins: [new Plugin({
multiCompiler: true,
path: 'tmp'
})]
},
{
entry: {
two: path.join(__dirname, 'fixtures/two.js')
},
output: {
path: OUTPUT_DIR,
filename: 'two-bundle.js'
},
plugins: [new Plugin({
multiCompiler: true,
path: 'tmp'
})]
}
];
it('includes full publicPath', function (done) {
var expected = {
one: {
js: "one-bundle.js"
},
two: {
js: "two-bundle.js"
}
};
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
publicPath: '/public/path/[hash]/',
filename: 'index-bundle.js'
},
plugins: [new Plugin({path: 'tmp'})]
};
var args = {
config: webpackConfig,
expected: expected
};
var expected = new RegExp('/public/path/[0-9a-f]+/index-bundle.js', 'i');
expectOutput(args, done);
});
var args = {
config: webpackConfig,
expected: expected
};
it('includes full publicPath', function(done) {
expectOutput(args, done);
});
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
output: {
path: OUTPUT_DIR,
publicPath: '/public/path/[hash]/',
filename: 'index-bundle.js'
},
plugins: [new Plugin({path: 'tmp'})]
};
it('works with CommonChunksPlugin', function (done) {
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/common-chunks/one.js'),
two: path.join(__dirname, 'fixtures/common-chunks/two.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({name: 'common'}),
new Plugin({path: 'tmp'})
]
};
var expected = new RegExp('/public/path/[0-9a-f]+/index-bundle.js', 'i');
var expected = {
one: {js: 'one.js'},
two: {js: 'two.js'},
common: {js: 'common.js'}
};
var args = {
config: webpackConfig,
expected: expected
};
var args = {
config: webpackConfig,
expected: expected
};
expectOutput(args, done);
});
expectOutput(args, done);
it('works with CommonChunksPlugin', function (done) {
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/common-chunks/one.js'),
two: path.join(__dirname, 'fixtures/common-chunks/two.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({name: "common"}),
new Plugin({path: 'tmp'})
]
};
});
var expected = {
one: {js: 'one.js'},
two: {js: 'two.js'},
common: {js: 'common.js'}
};
var args = {
config: webpackConfig,
expected: expected
};
expectOutput(args, done);
});
});

@@ -9,33 +9,33 @@ var chai = require('chai');

it('detects HMR updates', function () {
var config = {
output: {
hotUpdateChunkFilename: 'hmr-yo[id].[hash].js[query]'
}
};
var input = 'hmr-yo42.b4d455.js?f00b43';
var res = isHMRUpdate(config, input);
expect(res).to.eq(true);
});
it('detects HMR updates', function () {
var config = {
output: {
hotUpdateChunkFilename: 'hmr-yo[id].[hash].js[query]'
}
};
var input = 'hmr-yo42.b4d455.js?f00b43';
var res = isHMRUpdate(config, input);
expect(res).to.eq(true);
});
it('detects HMR updates with tricky templates', function () {
var config = {
output: {
hotUpdateChunkFilename: '[id][hash][name]hmr.js[query]'
}
};
var input = '42940455foo-hmr.js?f00b43';
var res = isHMRUpdate(config, input);
expect(res).to.eq(true);
});
it('detects HMR updates with tricky templates', function () {
var config = {
output: {
hotUpdateChunkFilename: '[id][hash][name]hmr.js[query]'
}
};
var input = '42940455foo-hmr.js?f00b43';
var res = isHMRUpdate(config, input);
expect(res).to.eq(true);
});
it("doesn't yield false positives", function () {
var config = {
output: {
hotUpdateChunkFilename: '[id][hash][name]hmr.js[query]'
}
};
expect(isHMRUpdate(config, '42940455foo-hmr?f00b43')).to.eq(false);
});
it('doesn\'t yield false positives', function () {
var config = {
output: {
hotUpdateChunkFilename: '[id][hash][name]hmr.js[query]'
}
};
expect(isHMRUpdate(config, '42940455foo-hmr?f00b43')).to.eq(false);
});
});

@@ -9,33 +9,33 @@ var chai = require('chai');

it('detects sourcemaps', function () {
var config = {
output: {
sourceMapFilename: 'sourcemap-yo[id].[hash].js[query]'
}
};
var input = 'sourcemap-yo42.b4d455.js?f00b43';
var res = isSourceMap(config, input);
expect(res).to.eq(true);
});
it('detects sourcemaps', function () {
var config = {
output: {
sourceMapFilename: 'sourcemap-yo[id].[hash].js[query]'
}
};
var input = 'sourcemap-yo42.b4d455.js?f00b43';
var res = isSourceMap(config, input);
expect(res).to.eq(true);
});
it('detects sourcemaps with tricky templates', function () {
var config = {
output: {
sourceMapFilename: '[id][hash][name]_map.js[query]'
}
};
var input = '42940455foo_map.js?f00b43';
var res = isSourceMap(config, input);
expect(res).to.eq(true);
});
it('detects sourcemaps with tricky templates', function () {
var config = {
output: {
sourceMapFilename: '[id][hash][name]_map.js[query]'
}
};
var input = '42940455foo_map.js?f00b43';
var res = isSourceMap(config, input);
expect(res).to.eq(true);
});
it("doesn't yield false positives", function () {
var config = {
output: {
sourceMapFilename: '[id][hash][name]_map.js[query]'
}
};
expect(isSourceMap(config, '42940455foo.js?f00b43')).to.eq(false);
});
it('doesn\'t yield false positives', function () {
var config = {
output: {
sourceMapFilename: '[id][hash][name]_map.js[query]'
}
};
expect(isSourceMap(config, '42940455foo.js?f00b43')).to.eq(false);
});
});

@@ -10,12 +10,12 @@

describe('parsing', function () {
describe('parsing', function () {
it('parses the empty string', function () {
expect(parseTemplate('').fields).to.eql([
it('parses the empty string', function () {
expect(parseTemplate('').fields).to.eql([
{prefix: '', placeholder: null}
]);
});
]);
});
it('parses consecutive placeholders', function () {
expect(parseTemplate('[id][name][query]').fields).to.eql([
it('parses consecutive placeholders', function () {
expect(parseTemplate('[id][name][query]').fields).to.eql([
{prefix: '', placeholder: 'id'},

@@ -25,7 +25,7 @@ {prefix: '', placeholder: 'name'},

{prefix: '', placeholder: null}
]);
});
]);
});
it('parses placeholders and prefixes', function () {
expect(parseTemplate('some[id]and[name]then[query]').fields).to.eql([
it('parses placeholders and prefixes', function () {
expect(parseTemplate('some[id]and[name]then[query]').fields).to.eql([
{prefix: 'some', placeholder: 'id'},

@@ -35,73 +35,73 @@ {prefix: 'and', placeholder: 'name'},

{prefix: '', placeholder: null}
]);
});
]);
});
it('handles unknown placeholders', function () {
expect(parseTemplate('some[unknown][id]then[hash:pwnd][query]').fields).to.eql([
it('handles unknown placeholders', function () {
expect(parseTemplate('some[unknown][id]then[hash:pwnd][query]').fields).to.eql([
{prefix: 'some[unknown]', placeholder: 'id'},
{prefix: 'then[hash:pwnd]', placeholder: 'query'},
{prefix: '', placeholder: null}
]);
});
]);
});
it('handles wierdly formatted placeholders', function () {
expect(parseTemplate('some[chunk[id]then[whoops[query]]').fields).to.eql([
it('handles wierdly formatted placeholders', function () {
expect(parseTemplate('some[chunk[id]then[whoops[query]]').fields).to.eql([
{prefix: 'some[chunk', placeholder: 'id'},
{prefix: 'then[whoops', placeholder: 'query'},
{prefix: ']', placeholder: null}
]);
});
]);
});
it('parses hash width syntax', function () {
expect(parseTemplate('[hash:10]and[chunkhash:42]').fields).to.eql([
it('parses hash width syntax', function () {
expect(parseTemplate('[hash:10]and[chunkhash:42]').fields).to.eql([
{prefix: '', placeholder: 'hash', width: 10},
{prefix: 'and', placeholder: 'chunkhash', width: 42},
{prefix: '', placeholder: null}
]);
});
]);
});
});
});
describe('matching', function () {
describe('matching', function () {
it('matches strings without placeholders', function () {
var tpl = parseTemplate('foo-bar.jsx');
expect(tpl.matches('foo-bar.jsx')).to.eq(true);
expect(tpl.matches('foo-bar.css')).to.eq(false);
});
it('matches strings without placeholders', function () {
var tpl = parseTemplate('foo-bar.jsx');
expect(tpl.matches('foo-bar.jsx')).to.eq(true);
expect(tpl.matches('foo-bar.css')).to.eq(false);
});
it('matches strings with [id] placeholder', function () {
var tpl = parseTemplate('foo-bar.[id].js');
expect(tpl.matches('foo-bar.666.js')).to.eq(true);
expect(tpl.matches('foo-bar.nope.js')).to.eq(false);
});
it('matches strings with [id] placeholder', function () {
var tpl = parseTemplate('foo-bar.[id].js');
expect(tpl.matches('foo-bar.666.js')).to.eq(true);
expect(tpl.matches('foo-bar.nope.js')).to.eq(false);
});
it('matches strings with [name] placeholder', function () {
var tpl = parseTemplate('[name].js');
expect(tpl.matches('foo-bar.chunk.js')).to.eq(true);
expect(tpl.matches('foo-bar.chunk.css')).to.eq(false);
});
it('matches strings with [name] placeholder', function () {
var tpl = parseTemplate('[name].js');
expect(tpl.matches('foo-bar.chunk.js')).to.eq(true);
expect(tpl.matches('foo-bar.chunk.css')).to.eq(false);
});
it('matches strings with [query] placeholder', function () {
var tpl = parseTemplate('[name].js[query]');
expect(tpl.matches('foo-bar.js?anything')).to.eq(true);
it('matches strings with [query] placeholder', function () {
var tpl = parseTemplate('[name].js[query]');
expect(tpl.matches('foo-bar.js?anything')).to.eq(true);
// query parameter is optional, so this should match too
expect(tpl.matches('foo-bar.js')).to.eq(true);
});
expect(tpl.matches('foo-bar.js')).to.eq(true);
});
it('matches strings with [hash] placeholder', function () {
var tpl = parseTemplate('[name]_[hash].js');
expect(tpl.matches('foo-bar_f00b43.js')).to.eq(true);
expect(tpl.matches('foo-bar_w00t.js')).to.eq(false);
});
it('matches strings with [hash] placeholder', function () {
var tpl = parseTemplate('[name]_[hash].js');
expect(tpl.matches('foo-bar_f00b43.js')).to.eq(true);
expect(tpl.matches('foo-bar_w00t.js')).to.eq(false);
});
it('matches strings with constrained-width [hash] placeholder', function () {
var tpl = parseTemplate('[name]_[hash:6].js');
expect(tpl.matches('foo-bar_f00.js')).to.eq(true);
expect(tpl.matches('foo-bar_b4d455.js')).to.eq(true);
expect(tpl.matches('foo-bar_f00b43b47.js')).to.eq(false);
});
it('matches strings with constrained-width [hash] placeholder', function () {
var tpl = parseTemplate('[name]_[hash:6].js');
expect(tpl.matches('foo-bar_f00.js')).to.eq(true);
expect(tpl.matches('foo-bar_b4d455.js')).to.eq(true);
expect(tpl.matches('foo-bar_f00b43b47.js')).to.eq(false);
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc