Skip to content

Commit

Permalink
Integrate css-loader and babel-loader
Browse files Browse the repository at this point in the history
and remove unnecessary code
  • Loading branch information
nicholaslee119 committed Oct 1, 2017
1 parent f78bea7 commit d9ca1c5
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 298 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module.exports = function (source) {
const builtCSSPath = options.builtCSSPath;
const builtTemplatePath = options.builtTemplatePath;
const selfPath = this.resourcePath;
core(source, extractor, injector, ext, srcPath, builtJSPath, builtCSSPath, builtTemplatePath, selfPath);
return '';
const out = core(source, extractor, injector, ext, srcPath, builtJSPath, builtCSSPath, builtTemplatePath, selfPath);
return out;
}
7 changes: 0 additions & 7 deletions lib/buildComponents.js

This file was deleted.

13 changes: 0 additions & 13 deletions lib/buildJSandCSS.js

This file was deleted.

14 changes: 0 additions & 14 deletions lib/buildPageTemplate.js

This file was deleted.

14 changes: 9 additions & 5 deletions lib/buildTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ module.exports = function buildTemplate(components, buildOption) {
if (!Array.isArray(components))
reportError('components is not an array');
components.forEach((component) => {
if (component.pageFlag) return;
const templatePath = path.join(buildOption.srcPath, component.dir, component.name, `${component.name}${buildOption.ext}`);
let templatePath;
if (component.pageFlag) {
templatePath = path.resolve(buildOption.srcPath, component.dir, `${component.name}${buildOption.ext}`);
} else {
templatePath = path.resolve(buildOption.srcPath, component.dir, component.name, `${component.name}${buildOption.ext}`);
}
if (fs.existsSync(templatePath)) {
try {
const template = fs.readFileSync(templatePath, 'utf8');
fsx.ensureDirSync(path.join(buildOption.builtTemplatePath, component.dir));
fs.writeFileSync(path.join(buildOption.builtTemplatePath, component.dir, `${component.name}${buildOption.ext}`), template);
fsx.ensureDirSync(path.resolve(buildOption.builtTemplatePath, component.dir));
fs.writeFileSync(path.resolve(buildOption.builtTemplatePath, component.dir, `${component.name}${buildOption.ext}`), template);
} catch (e) {
reportError(e);
}
} else {
reportError(`${component.name} is non existence`);
reportError(`${templatePath} is non existence`);
}
});
};
Expand Down
6 changes: 4 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const fs = require('fs');
const isValidPath = require('is-valid-path');

const parsePage = require('./parsePage');
const buildComponents = require('./buildComponents');
const createOutput = require('./createOutput');
const buildTemplate = require('./buildTemplate');

function checkPath (...url) {
return url.every((url)=>{
Expand Down Expand Up @@ -37,7 +38,8 @@ function main(source, extractor, injector, ext, srcPath, builtJSPath, builtCSSPa
currentPagePath,
};
const components = parsePage(source, extractor, buildOption);
buildComponents(components, injector, buildOption);
buildTemplate(components, buildOption);
return createOutput(components, buildOption);
}

module.exports = main;
23 changes: 0 additions & 23 deletions lib/createJSandCSS.js

This file was deleted.

23 changes: 23 additions & 0 deletions lib/createOutput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs');
const path = require('path');

module.exports = function createOutput(components, buildOption) {
let output = '';
if (!Array.isArray(components))
throw '[webpack-component-loader]: the component is not an array';

components.forEach((component)=>{
let compoPath;

if(component.pageFlag) {
compoPath = path.resolve(buildOption.srcPath, component.dir, component.name);
} else {
compoPath = path.resolve(buildOption.srcPath, component.dir, component.name, component.name);
}

output += `require('babel-loader!${compoPath}.js');\n`;
output += `require('css-loader!${compoPath}.css');\n`
});

return output;
}
26 changes: 0 additions & 26 deletions lib/insertJSandCSS.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/parsePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function recursiveParse(source, extractor, srcPath) {
pushedComponents.set(componentPath, true);
const parsed = path.parse(componentPath);
components.push(parsed);
recursiveParse(fs.readFileSync(path.join(srcPath, parsed.dir, parsed.name, parsed.base), 'utf8'), extractor, srcPath, components, pushedComponents);
recursiveParse(fs.readFileSync(path.resolve(srcPath, parsed.dir, parsed.name, parsed.base), 'utf8'), extractor, srcPath, components, pushedComponents);
});
}

Expand Down
23 changes: 0 additions & 23 deletions lib/readJSandCSS.js

This file was deleted.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"homepage": "https://github.com/nicholaslee119/webpack-component-loader#readme",
"devDependencies": {
"babel-jest": "^21.2.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"css-loader": "^0.28.7",
"eslint": "^4.7.1",
"eslint-config-airbnb-base": "^12.0.0",
"eslint-plugin-import": "^2.7.0",
Expand Down
79 changes: 0 additions & 79 deletions test/buildJSandCSS.test.js

This file was deleted.

32 changes: 0 additions & 32 deletions test/buildPageTemplate.test.js

This file was deleted.

4 changes: 3 additions & 1 deletion test/buildTemplate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ describe('test buildTemplate', function() {
}
];

const errorPath = path.resolve(buildOptionNormal.srcPath, inexistentComponents[0].dir, inexistentComponents[0].name, `${inexistentComponents[0].name}${buildOptionNormal.ext}`);

expect(()=>{
buildTemplate(inexistentComponents, buildOptionNormal);
}).toThrowError(`[webpack-component-loader]: something wrong with building Template: ${inexistentComponents[0].name} is non existence`);
}).toThrowError(`[webpack-component-loader]: something wrong with building Template: ${errorPath} is non existence`);
})

it('should throw error when on-array component was passed in', function() {
Expand Down
33 changes: 0 additions & 33 deletions test/createJSandCSS.test.js

This file was deleted.

1 change: 1 addition & 0 deletions test/fixture/pageC/pageC.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
function C () {
/* I came from C*/
let a = 1;
}
Loading

0 comments on commit d9ca1c5

Please sign in to comment.