Skip to content

Commit

Permalink
Use path.join instead of path.resolve to support bundle names sta…
Browse files Browse the repository at this point in the history
…rting with `/`
  • Loading branch information
th0r committed Nov 6, 2016
1 parent 10a873e commit f23781a
Show file tree
Hide file tree
Showing 13 changed files with 836 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"root": true,
"parserOptions": {
"ecmaVersion": 2015,
"ecmaVersion": 2017,
"sourceType": "module",
"ecmaFeatures": {
"impliedStrict": true,
Expand Down
34 changes: 19 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@
"opener": "^1.4.2"
},
"devDependencies": {
"babel-core": "^6.18.0",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-2": "^6.18.0",
"chai": "^3.5.0",
"del": "^2.2.2",
"eslint": "^3.8.1",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-plumber": "^1.1.0",
"gulp-util": "^3.0.7",
"gulp-watch": "^4.3.10",
"mocha": "^3.1.2",
"stream-combiner2": "^1.1.1"
"babel-core": "6.18.0",
"babel-plugin-transform-runtime": "6.15.0",
"babel-polyfill": "6.16.0",
"babel-preset-es2015": "6.18.0",
"babel-preset-stage-2": "6.18.0",
"chai": "3.5.0",
"chai-subset": "1.3.0",
"del": "2.2.2",
"eslint": "3.8.1",
"gulp": "3.9.1",
"gulp-babel": "6.1.2",
"gulp-plumber": "1.1.0",
"gulp-util": "3.0.7",
"gulp-watch": "4.3.10",
"mocha": "3.1.2",
"nightmare": "2.8.1",
"sinon": "1.17.6",
"stream-combiner2": "1.1.1",
"webpack": "1.13.3"
},
"keywords": [
"webpack",
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getChartData(bundleStats, bundleDir) {
if (bundleDir) {
// Checking if all assets are exist
const bundleScriptsFound = _.every(bundleStats.assets, statAsset => {
const assetFile = path.resolve(bundleDir, statAsset.name);
const assetFile = path.join(bundleDir, statAsset.name);
const assetExists = fs.existsSync(assetFile);

if (!assetExists) {
Expand All @@ -40,7 +40,7 @@ function getChartData(bundleStats, bundleDir) {
// Parsing assets and getting real module sizes
parsedModuleSizes = _.transform(bundleStats.assets, (result, statAsset) => {
_.assign(result,
getModuleSizesFromBundle(path.resolve(bundleDir, statAsset.name))
getModuleSizesFromBundle(path.join(bundleDir, statAsset.name))
);
}, {});
}
Expand Down
9 changes: 8 additions & 1 deletion test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"extends": "../.eslintrc.json",
"env": {
"mocha": true
"mocha": true,
"browser": true
},
"globals": {
"sinon": true,
"expect": true,
"makeWebpackConfig": true,
"webpackCompile": true
}
}
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output
42 changes: 42 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const chai = require('chai');
const webpack = require('webpack');

chai.use(require('chai-subset'));

global.expect = chai.expect;
global.sinon = require('sinon');
global.webpackCompile = webpackCompile;
global.makeWebpackConfig = makeWebpackConfig;

const BundleAnalyzerPlugin = require('../lib/BundleAnalyzerPlugin');

function webpackCompile(config) {
return new Promise((resolve, reject) => {
webpack(config, err => {
if (err) return reject(err);
resolve();
});
});
}

function makeWebpackConfig(opts) {
opts = {
analyzerOpts: {
analyzerMode: 'static',
openAnalyzer: false
},
...opts
};

return {
context: __dirname,
entry: './src',
output: {
path: `${__dirname}/output`,
filename: 'bundle.js'
},
plugins: [
new BundleAnalyzerPlugin(opts.analyzerOpts)
]
};
}
1 change: 0 additions & 1 deletion test/parseUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs');

const _ = require('lodash');
const { expect } = require('chai');
const { getModuleSizesFromBundle } = require('../lib/parseUtils');

const BUNDLES_DIR = `${__dirname}/bundles`;
Expand Down
1 change: 1 addition & 0 deletions test/src/a-clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'module a';
1 change: 1 addition & 0 deletions test/src/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'module a';
1 change: 1 addition & 0 deletions test/src/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'module b';
3 changes: 3 additions & 0 deletions test/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('./a');
require('./b');
require('./a-clone');
38 changes: 38 additions & 0 deletions test/webpack-configs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require('fs');
const del = require('del');
const Nightmare = require('nightmare');
const nightmare = Nightmare();

describe('Webpack config', function () {
before(function () {
del.sync(`${__dirname}/output`);
this.clock = sinon.useFakeTimers();
});

afterEach(function () {
del.sync(`${__dirname}/output`);
});

after(function () {
this.clock.restore();
});

it('with head slash in bundle filename should be supported', async function () {
const config = makeWebpackConfig();

config.output.filename = '/bundle.js';

await webpackCompile(config);
this.clock.tick(1000);

expect(fs.existsSync(`${__dirname}/output/bundle.js`)).to.be.true;
expect(fs.existsSync(`${__dirname}/output/report.html`)).to.be.true;
const chartData = await nightmare
.goto(`file://${__dirname}/output/report.html`)
.evaluate(() => window.chartData);
expect(chartData[0]).to.containSubset({
parsedSize: 213,
statSize: 141
});
});
});
Loading

0 comments on commit f23781a

Please sign in to comment.