Skip to content

Commit 3d8b4e4

Browse files
committed
Prettier formatting
1 parent b31b604 commit 3d8b4e4

File tree

17 files changed

+274
-286
lines changed

17 files changed

+274
-286
lines changed

src/index.js

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,79 @@ const chalk = require('chalk');
1111
*/
1212
function OmitJSforCSSPlugin(options) {
1313
const defaults = {
14-
preview : false, // OPTIONAL - {Boolean} - A preview of the files that are to be omitted (Will not actually omit)
15-
cacheOnWatch : false, // OPTIONAL - {Boolean} - Whether it should cache the JS filenames that should be omitted on watch
16-
verbose : false // OPTIONAL - {Boolean} - Whether it should display which files will be omitted
14+
preview: false, // OPTIONAL - {Boolean} - A preview of the files that are to be omitted (Will not actually omit)
15+
cacheOnWatch: false, // OPTIONAL - {Boolean} - Whether it should cache the JS filenames that should be omitted on watch
16+
verbose: false // OPTIONAL - {Boolean} - Whether it should display which files will be omitted
1717
};
18-
19-
if(typeof options !== "undefined" && (options === null || typeof options !== "object") || Array.isArray(options)){
18+
19+
if ((typeof options !== 'undefined' && (options === null || typeof options !== 'object')) || Array.isArray(options)) {
2020
throw new Error('OmitJSforCSSPlugin only takes an options "object" as an argument');
2121
}
2222

2323
this.options = Object.assign({}, defaults, options || {});
2424
this.cacheOmittedFilename = [];
25-
};
25+
}
2626

2727
/**
2828
* @function omitFiles
2929
* @param {Object} omitted - The omitted file's details
3030
* @param {Object} compilation
3131
*/
32-
OmitJSforCSSPlugin.prototype.omitFiles = function(omitted, compilation){
33-
if(this.options.preview){
34-
console.log(chalk.bold(chalk.red('PREVIEW')) + chalk.grey(' File to be omitted for ')+ chalk.bold(chalk.green(omitted.chunkName)) +' : ' + chalk.bold(chalk.green(omitted.filename)));
35-
}
36-
else {
37-
this.options.verbose && console.log(chalk.grey('File Omitted for ')+ chalk.bold(chalk.green(omitted.chunkName)) + chalk.grey(' : ') + chalk.bold(chalk.green(omitted.filename)));
38-
delete compilation.assets[omitted.filename];
39-
}
32+
OmitJSforCSSPlugin.prototype.omitFiles = function(omitted, compilation) {
33+
if (this.options.preview) {
34+
console.log(
35+
chalk.bold(chalk.red('PREVIEW')) + chalk.grey(' File to be omitted for ') + chalk.bold(chalk.green(omitted.chunkName)) + ' : ' + chalk.bold(chalk.green(omitted.filename))
36+
);
37+
} else {
38+
this.options.verbose &&
39+
console.log(chalk.grey('File Omitted for ') + chalk.bold(chalk.green(omitted.chunkName)) + chalk.grey(' : ') + chalk.bold(chalk.green(omitted.filename)));
40+
delete compilation.assets[omitted.filename];
41+
}
4042
};
4143

4244
/**
4345
* @function findOmissibleFiles
4446
* @param {Object} compilation
4547
*/
46-
OmitJSforCSSPlugin.prototype.findOmissibleFiles = function(compilation){
48+
OmitJSforCSSPlugin.prototype.findOmissibleFiles = function(compilation) {
4749
// Every chunk / entry point
48-
compilation.chunks.forEach((chunk) => {
50+
compilation.chunks.forEach(chunk => {
4951
// Chunks origin files. ex. origin entry point, ![] entry
5052
let resourceOrigin = {};
51-
let assetTypeCount = { internal : 0, css : 0 };
53+
let assetTypeCount = { internal: 0, css: 0 };
5254

53-
chunk.origins.forEach((origin) => {
54-
if(typeof origin.module.resource === "string") {
55+
chunk.origins.forEach(origin => {
56+
if (typeof origin.module.resource === 'string') {
5557
resourceOrigin[origin.module.resource] = true;
5658
}
5759
});
5860

5961
// Each entry point will have its own dependencies, based on the files inner deps or the array deps in entry
60-
chunk.modules.forEach((module) => {
61-
if(!Array.isArray(module.fileDependencies)){
62-
return;
62+
chunk.modules.forEach(module => {
63+
if (!Array.isArray(module.fileDependencies)) {
64+
return;
65+
}
66+
module.fileDependencies.forEach(filepath => {
67+
if (!resourceOrigin[filepath] && !/(\bnode_modules\b)/.test(filepath)) {
68+
/\.(css)$/i.test(filepath) ? assetTypeCount.css++ : assetTypeCount.internal++;
6369
}
64-
module.fileDependencies.forEach((filepath) => {
65-
if(!resourceOrigin[filepath] && !(/(\bnode_modules\b)/).test(filepath)) {
66-
(/\.(css)$/i).test(filepath) ? assetTypeCount.css++ : assetTypeCount.internal++;
67-
}
68-
});
70+
});
6971
});
7072

7173
// Get the filenames that will be emitted, generated by the chunk, and omit JS if applicable
72-
chunk.files.forEach((filename) => {
74+
chunk.files.forEach(filename => {
7375
// If all dependencies of this entry were CSS, then a JS version of this file will be created
7476
// This js file will be empty due to extract-text-webpack-plugin
75-
if((assetTypeCount.css > 0 && assetTypeCount.internal === 0) && ((/\.(js)$/i).test(filename) || (/\.(js).map$/i).test(filename))){
77+
if (assetTypeCount.css > 0 && assetTypeCount.internal === 0 && (/\.(js)$/i.test(filename) || /\.(js).map$/i.test(filename))) {
7678
let omitted = {
77-
'filename' : filename,
78-
'chunkName' : chunk.name
79+
filename: filename,
80+
chunkName: chunk.name
7981
};
8082

8183
this.cacheOmittedFilename.push(omitted);
8284
this.omitFiles(omitted, compilation);
8385
}
8486
});
85-
8687
});
8788
};
8889

@@ -92,15 +93,15 @@ OmitJSforCSSPlugin.prototype.findOmissibleFiles = function(compilation){
9293
*/
9394
OmitJSforCSSPlugin.prototype.apply = function(compiler) {
9495
compiler.plugin('emit', (compilation, callback) => {
95-
96-
if(this.options.cacheOnWatch && this.cacheOmittedFilename.length){
97-
this.cacheOmittedFilename.forEach((omitted) => { this.omitFiles(omitted, compilation) });
98-
}
99-
else {
96+
if (this.options.cacheOnWatch && this.cacheOmittedFilename.length) {
97+
this.cacheOmittedFilename.forEach(omitted => {
98+
this.omitFiles(omitted, compilation);
99+
});
100+
} else {
100101
this.findOmissibleFiles(compilation);
101102
}
102103
callback();
103104
});
104105
};
105106

106-
module.exports = OmitJSforCSSPlugin;
107+
module.exports = OmitJSforCSSPlugin;

test/css-entry-array.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ const dirPath = path.join(__dirname, './fixtures/css-array-entry/dir');
66
const fileShouldNotExist = require('./utils/file-should-not-exist.js');
77

88
describe('Array of CSS Dependencies as Entry', () => {
9-
before((done) => {
9+
before(done => {
1010
rimraf(dirPath, () => {
1111
done();
1212
});
1313
});
1414

15-
it('JS entry should not exist', (done) => {
15+
it('JS entry should not exist', done => {
1616
webpack(options, () => {
1717
fileShouldNotExist(dirPath, '/a.js');
1818
done();
1919
});
2020
});
2121

22-
it('JS entry source map should not exist', (done) => {
22+
it('JS entry source map should not exist', done => {
2323
var optionSourceMap = Object.assign({}, options);
24-
optionSourceMap.devtool = 'source-map';
25-
24+
optionSourceMap.devtool = 'source-map';
25+
2626
webpack(optionSourceMap, () => {
2727
fileShouldNotExist(dirPath, '/a.js.map');
2828
done();
2929
});
3030
});
31-
});
31+
});

test/fixtures/css-array-entry/webpack.config.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,25 @@ const OmitJSforCSSPlugin = require('../../../src/index.js');
33
const path = require('path');
44

55
module.exports = {
6-
entry : {
7-
'a' : [
8-
path.join(__dirname, 'one.css'),
9-
path.join(__dirname, 'two.css')
10-
]
6+
entry: {
7+
a: [path.join(__dirname, 'one.css'), path.join(__dirname, 'two.css')]
118
},
12-
output : {
13-
filename : '[name].js',
14-
path : path.join(__dirname, '/dir')
9+
output: {
10+
filename: '[name].js',
11+
path: path.join(__dirname, '/dir')
1512
},
16-
module : {
17-
rules : [
18-
{
19-
test: /\.css$/,
20-
use: ExtractTextPlugin.extract({
21-
fallback: "style-loader",
22-
use: "css-loader"
23-
})
24-
}],
13+
module: {
14+
rules: [
15+
{
16+
test: /\.css$/,
17+
use: ExtractTextPlugin.extract({
18+
fallback: 'style-loader',
19+
use: 'css-loader'
20+
})
21+
}
22+
]
2523
},
26-
plugins : [
27-
new ExtractTextPlugin({ filename : '[name].css' }),
28-
new OmitJSforCSSPlugin()
29-
],
30-
devtool : 'source-map',
31-
stats : 'none'
32-
};
24+
plugins: [new ExtractTextPlugin({ filename: '[name].css' }), new OmitJSforCSSPlugin()],
25+
devtool: 'source-map',
26+
stats: 'none'
27+
};

test/fixtures/js-file-entry/webpack.config.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@ const OmitJSforCSSPlugin = require('../../../src/index.js');
33
const path = require('path');
44

55
module.exports = {
6-
entry : {
7-
'b' : path.join(__dirname, 'styles.js')
6+
entry: {
7+
b: path.join(__dirname, 'styles.js')
88
},
9-
output : {
10-
filename : '[name].js',
11-
path : path.join(__dirname, '/dir')
9+
output: {
10+
filename: '[name].js',
11+
path: path.join(__dirname, '/dir')
1212
},
13-
module : {
14-
rules : [{
15-
test: /\.css$/,
16-
use: ExtractTextPlugin.extract({
17-
fallback: "style-loader",
18-
use: "css-loader"
19-
})
20-
}],
13+
module: {
14+
rules: [
15+
{
16+
test: /\.css$/,
17+
use: ExtractTextPlugin.extract({
18+
fallback: 'style-loader',
19+
use: 'css-loader'
20+
})
21+
}
22+
]
2123
},
22-
plugins : [
23-
new ExtractTextPlugin({ filename : '[name].css' }),
24-
new OmitJSforCSSPlugin()
25-
],
26-
stats : 'none'
27-
};
24+
plugins: [new ExtractTextPlugin({ filename: '[name].css' }), new OmitJSforCSSPlugin()],
25+
stats: 'none'
26+
};

test/fixtures/options/cache-on-watch/webpack.config.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@ const OmitJSforCSSPlugin = require('../../../../src/index.js');
33
const path = require('path');
44

55
module.exports = {
6-
entry : {
7-
'b' : path.join(__dirname, '../shared/styles.js')
6+
entry: {
7+
b: path.join(__dirname, '../shared/styles.js')
88
},
9-
output : {
10-
filename : '[name].js',
11-
path : path.join(__dirname, '/dir')
9+
output: {
10+
filename: '[name].js',
11+
path: path.join(__dirname, '/dir')
1212
},
13-
module : {
14-
rules : [{
15-
test: /\.css$/,
16-
use: ExtractTextPlugin.extract({
17-
fallback: "style-loader",
18-
use: "css-loader"
19-
})
20-
}],
13+
module: {
14+
rules: [
15+
{
16+
test: /\.css$/,
17+
use: ExtractTextPlugin.extract({
18+
fallback: 'style-loader',
19+
use: 'css-loader'
20+
})
21+
}
22+
]
2123
},
22-
plugins : [
23-
new ExtractTextPlugin({ filename : '[name].css' }),
24-
new OmitJSforCSSPlugin({ cacheOnWatch : true })
25-
],
26-
stats : 'none',
27-
devtool : 'source-map'
28-
};
24+
plugins: [new ExtractTextPlugin({ filename: '[name].css' }), new OmitJSforCSSPlugin({ cacheOnWatch: true })],
25+
stats: 'none',
26+
devtool: 'source-map'
27+
};

test/fixtures/options/preview/webpack.config.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@ const OmitJSforCSSPlugin = require('../../../../src/index.js');
33
const path = require('path');
44

55
module.exports = {
6-
entry : {
7-
'b' : path.join(__dirname, '../shared/styles.js')
6+
entry: {
7+
b: path.join(__dirname, '../shared/styles.js')
88
},
9-
output : {
10-
filename : '[name].js',
11-
path : path.join(__dirname, '/dir')
9+
output: {
10+
filename: '[name].js',
11+
path: path.join(__dirname, '/dir')
1212
},
13-
module : {
14-
rules : [{
15-
test: /\.css$/,
16-
use: ExtractTextPlugin.extract({
17-
fallback: "style-loader",
18-
use: "css-loader"
19-
})
20-
}],
13+
module: {
14+
rules: [
15+
{
16+
test: /\.css$/,
17+
use: ExtractTextPlugin.extract({
18+
fallback: 'style-loader',
19+
use: 'css-loader'
20+
})
21+
}
22+
]
2123
},
22-
plugins : [
23-
new ExtractTextPlugin({ filename : '[name].css' }),
24-
new OmitJSforCSSPlugin({ preview : true })
25-
],
26-
devtool : 'source-map',
27-
stats : 'none'
28-
};
24+
plugins: [new ExtractTextPlugin({ filename: '[name].css' }), new OmitJSforCSSPlugin({ preview: true })],
25+
devtool: 'source-map',
26+
stats: 'none'
27+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
require('./one.css');
2-
require('./two.css');
2+
require('./two.css');

0 commit comments

Comments
 (0)