Skip to content

Commit b87d737

Browse files
authored
Merge pull request #32 from NetCZ/feature/fs-import
feat: import node fs library instead of relying on webpack passed one
2 parents f6465ee + d42a3c3 commit b87d737

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var loaderUtils = require('loader-utils'),
22
_ = require('lodash'),
33
path = require('path'),
4+
fs = require('fs'),
45
helper = require('./src/helper');
56

67
var defaultOptions = {
@@ -19,7 +20,7 @@ module.exports = function (content, map) {
1920
var inputFile = map.file || path.basename(this.resourcePath),
2021
options = _.assign({}, _.cloneDeep(defaultOptions), loaderOptions),
2122
dirPath = this.context,
22-
fileNames = this.fs.readdirSync(dirPath).filter(function (file) {
23+
fileNames = fs.readdirSync(dirPath).filter(function (file) {
2324
return !file.match(/^\./);
2425
});
2526

plugin.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ function VueSeparateFilesWebpackLoaderPlugin(options) {
1414
this.testString = this.options.test.source.replace(/\\\./g, '.');
1515
}
1616

17+
function matcher(item) {
18+
return _.findIndex(item.use, function (use) {
19+
return use.loader === 'vue-separate-files-webpack-loader';
20+
}) !== -1;
21+
}
22+
1723
VueSeparateFilesWebpackLoaderPlugin.prototype.apply = function (compiler) {
1824
var plugins = _.filter(compiler.options.plugins, function (plugin) {
1925
var name = plugin.constructor.name;
@@ -36,7 +42,7 @@ VueSeparateFilesWebpackLoaderPlugin.prototype.apply = function (compiler) {
3642
throw new Error('[VueSeparateFilesWebpackLoaderPlugin] No matching rule for ' + this.testString + ' files found.\n' +
3743
'Make sure there is at least one root-level rule that matches ' + this.testString + ' files.\n' +
3844
'Also make sure you pass options.test property when not using default one.'
39-
)
45+
);
4046
}
4147

4248
var ruleUse = _.concat([], rule.use);
@@ -57,10 +63,4 @@ VueSeparateFilesWebpackLoaderPlugin.prototype.apply = function (compiler) {
5763
compiler.options.module.rules.push(rule);
5864
};
5965

60-
function matcher(item) {
61-
return _.findIndex(item.use, function (use) {
62-
return use.loader === 'vue-separate-files-webpack-loader';
63-
}) !== -1;
64-
}
65-
6666
module.exports = VueSeparateFilesWebpackLoaderPlugin;

src/helper.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ var _ = require('lodash'),
44

55
var exports = {};
66

7+
function checkTagExistence(file, parts, settings) {
8+
if (_.has(parts, settings.tagName) || _.has(parts, settings.fileType)) {
9+
var type = settings.tagName || settings.fileType;
10+
throw new TypeError(`File "${ file }" can't be used as "${ type }", because it was already defined in "${ _.get(parts[type], 'file', null) }".`);
11+
}
12+
}
13+
14+
function prepareTag() {
15+
16+
}
17+
718
exports.addTag = function addTag(tags, tagName, attributes) {
819
tags.push(tag({
920
name: tagName,
@@ -62,16 +73,18 @@ exports.resolveOptions = function resolveOptions(options, defaultOptions) {
6273
};
6374

6475
exports.createPart = function createPart(settings, options) {
76+
var attributes = _.assign({}, {
77+
separated: true,
78+
src: path.join(settings.dirPath, settings.file),
79+
scoped: settings.tagName === 'style' && settings.scoped,
80+
lang: settings.tagName ? settings.fileType : false
81+
}, options.global, options[settings.fileType] || {}, options[settings.tagName] || {});
82+
6583
return {
6684
name: settings.tagName || settings.fileType,
6785
file: settings.file,
6886
fileName: settings.fileName,
69-
attributes: _.assign({}, {
70-
separated: true,
71-
src: path.join(settings.dirPath, settings.file),
72-
scoped: settings.tagName === 'style' && settings.scoped,
73-
lang: settings.tagName ? settings.fileType : false
74-
}, options.global, options[settings.fileType] || {}, options[settings.tagName] || {})
87+
attributes: attributes
7588
};
7689
};
7790

@@ -87,10 +100,7 @@ exports.createParts = function createParts(options, dirPath, inputFile, fileName
87100

88101
var settings = that.parseFile(options, dirPath, file);
89102

90-
if (_.has(parts, settings.tagName) || _.has(parts, settings.fileType)) {
91-
var type = settings.tagName || settings.fileType;
92-
throw new TypeError(`File "${ file }" can't be used as "${ type }", because it was already defined in "${ _.get(parts[type], 'file', null) }".`);
93-
}
103+
checkTagExistence(file, parts, settings);
94104

95105
parts[settings.tagName || settings.fileType] = that.createPart(settings, options);
96106
});

test/plugin.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ describe('plugin: success', function () {
243243
var expected = {
244244
resource: {
245245
test: /\.vue\./
246-
},
247-
resourceQuery: undefined
246+
}
248247
};
249248

250249
plugin.apply(compiler);
@@ -255,7 +254,7 @@ describe('plugin: success', function () {
255254
assert.deepEqual(rule.resource, expected.resource);
256255
assert.equal(rule.resourceQuery, expected.resourceQuery);
257256
assert.isFunction(rule.use);
258-
assert.deepEqual(rule.use({ resourceQuery: undefined }), defaultLoaderRuleDefinition.use)
257+
assert.deepEqual(rule.use({}), defaultLoaderRuleDefinition.use)
259258
});
260259

261260
it('should apply loader rule with custom plugin options', function () {
@@ -279,8 +278,7 @@ describe('plugin: success', function () {
279278
var expected = {
280279
resource: {
281280
test: /\.condition/
282-
},
283-
resourceQuery: undefined
281+
}
284282
};
285283

286284
plugin.apply(compiler);
@@ -291,7 +289,7 @@ describe('plugin: success', function () {
291289
assert.deepEqual(rule.resource, expected.resource);
292290
assert.equal(rule.resourceQuery, expected.resourceQuery);
293291
assert.isFunction(rule.use);
294-
assert.deepEqual(rule.use({ resourceQuery: undefined }), defaultLoaderRuleDefinition.use)
292+
assert.deepEqual(rule.use({}), defaultLoaderRuleDefinition.use)
295293
});
296294

297295
it('should contain exactly one rule for loader', function () {
@@ -310,8 +308,7 @@ describe('plugin: success', function () {
310308
var expected = {
311309
resource: {
312310
test: /\.vue\./
313-
},
314-
resourceQuery: undefined
311+
}
315312
};
316313

317314
plugin.apply(compiler);
@@ -323,6 +320,6 @@ describe('plugin: success', function () {
323320
assert.deepEqual(rule.resource, expected.resource);
324321
assert.equal(rule.resourceQuery, expected.resourceQuery);
325322
assert.isFunction(rule.use);
326-
assert.deepEqual(rule.use({ resourceQuery: undefined }), defaultLoaderRuleDefinition.use)
323+
assert.deepEqual(rule.use({}), defaultLoaderRuleDefinition.use)
327324
});
328325
});

0 commit comments

Comments
 (0)