-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add webpack tests #106
add webpack tests #106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,4 @@ coverage/ | |
|
||
# Bundle Analyser | ||
frontend/stats.json | ||
__snapshots__/ |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
"@babel/plugin-transform-runtime": "^7.23.2", | ||
"@babel/preset-env": "^7.23.2", | ||
"autoprefixer": "^10.4.16", | ||
"babel-loader": "^9.1.3", | ||
"babel-loader": "9.1.2", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Latest version has a bug: babel/babel-loader#1015 |
||
"core-js": "^3.6.5", | ||
"eslint": "^8.51.0", | ||
"eslint-webpack-plugin": "^4.0.1", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,36 +5,42 @@ const renderStyles = require('../utils/renderStyles'); | |
|
||
// extend log to proper say what file is running | ||
module.exports = (config) => { | ||
if (config && config.general && config.general.watch) { | ||
try { | ||
log(__filename, 'Watcher Sass / autoprefixer running...', '', 'info'); | ||
return new Promise((resolve) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only change here is wrapping the task to Promise. |
||
if (config && config.general && config.general.watch) { | ||
try { | ||
log(__filename, 'Watcher Sass / autoprefixer running...', '', 'info'); | ||
|
||
const gaze = require('gaze'); | ||
const sassPattern = path.join(config.general.sourcesPath, `**/*.${config.general.sourceKey}.scss`); | ||
const gaze = require('gaze'); | ||
const sassPattern = path.join(config.general.sourcesPath, `**/*.${config.general.sourceKey}.scss`); | ||
|
||
gaze(sassPattern, function () { | ||
// simple debounce with timeout for only save the last if several events are triggered | ||
this.on('all', (event, file) => { | ||
// trigger a save | ||
const relativePath = path.relative(config.general.sourcesPath, path.dirname(file)); | ||
const fileName = path.basename(file) | ||
.replace(config.general.sourceKey, config.general.bundleKey); | ||
const destFile = path.join(relativePath, fileName); | ||
gaze(sassPattern, function () { | ||
// simple debounce with timeout for only save the last if several events are triggered | ||
this.on('all', (event, file) => { | ||
// trigger a save | ||
const relativePath = path.relative(config.general.sourcesPath, path.dirname(file)); | ||
const fileName = path.basename(file) | ||
.replace(config.general.sourceKey, config.general.bundleKey); | ||
const destFile = path.join(relativePath, fileName); | ||
|
||
// override to keep alive | ||
config.stylelint.failOnError = false; | ||
// override to keep alive | ||
config.stylelint.failOnError = false; | ||
|
||
renderStyles(file, destFile, config); | ||
renderStyles(file, destFile, config); | ||
}); | ||
}); | ||
} catch (e) { | ||
log(__filename, 'Something is missing, you need install dev dependencies for this.', e.message, 'error'); | ||
} | ||
} else { | ||
log(__filename, 'Sass / autoprefixer running...', '', 'info'); | ||
|
||
// checking all entries at this configuration | ||
const entries = generateEntries(config, 'scss'); | ||
const promises = Object.keys(entries).map(file => renderStyles(entries[file], file, config)); | ||
Promise.allSettled(promises).then((results) => { | ||
log(__filename, 'Styles done', '', 'info'); | ||
resolve(); | ||
}); | ||
} catch (e) { | ||
log(__filename, 'Something is missing, you need install dev dependencies for this.', e.message, 'error'); | ||
} | ||
} else { | ||
log(__filename, 'Sass / autoprefixer running...', '', 'info'); | ||
|
||
// checking all entries at this configuration | ||
const entries = generateEntries(config, 'scss'); | ||
Object.keys(entries).forEach(file => renderStyles(entries[file], file, config)); | ||
} | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,68 +3,71 @@ const ESLintPlugin = require('eslint-webpack-plugin'); | |
|
||
const { log } = require('../utils/log'); | ||
const generateEntries = require('../utils/generateEntries'); | ||
|
||
// extend log to proper say what file is running | ||
module.exports = (config) => { | ||
// checking all entries at this configuration | ||
const entry = generateEntries(config); | ||
return new Promise((r) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only change here is wrapping the task to Promise. |
||
// checking all entries at this configuration | ||
const entry = generateEntries(config); | ||
|
||
// make sure destination path is the same config as output | ||
if (config && config.general && config.general.destinationPath) { | ||
config.output.path = config.general.destinationPath; | ||
} | ||
// make sure destination path is the same config as output | ||
if (config && config.general && config.general.destinationPath) { | ||
config.output.path = config.general.destinationPath; | ||
} | ||
|
||
// setting rules for modules | ||
const module = { | ||
rules: config.general.modules.map(rule => config[rule]) | ||
}; | ||
// setting rules for modules | ||
const module = { | ||
rules: config.general.modules.map(rule => config[rule]) | ||
}; | ||
|
||
// log at the beginning | ||
log(__filename, 'Webpack transpile running...', '', 'info'); | ||
// log at the beginning | ||
log(__filename, 'Webpack transpile running...', '', 'info'); | ||
|
||
// extract from flatten configs to webpack | ||
const { output, plugins, optimization, resolve, externals, stats, performance, cache, devServer, eslint} = config; | ||
const { mode, watch, devtool } = config.general; | ||
// check if there is any entry | ||
plugins.push(new ESLintPlugin(eslint)); | ||
// extract from flatten configs to webpack | ||
const { output, plugins, optimization, resolve, externals, stats, performance, cache, devServer, eslint } = config; | ||
const { mode, watch, devtool } = config.general; | ||
// check if there is any entry | ||
plugins.push(new ESLintPlugin(eslint)); | ||
|
||
if (entry && Object.keys(entry).length > 0) { | ||
// run webpack | ||
webpack({ | ||
mode, | ||
watch, | ||
entry, | ||
output, | ||
module, | ||
plugins, | ||
optimization, | ||
devtool, | ||
resolve, | ||
performance, | ||
stats, | ||
cache, | ||
devServer, | ||
...externals && { externals } | ||
}, (err, stats) => { | ||
// output the resulting stats. | ||
if (stats && stats.toString) { | ||
log(__filename, stats.toString({ colors: true })); | ||
} | ||
if (entry && Object.keys(entry).length > 0) { | ||
// run webpack | ||
webpack({ | ||
mode, | ||
watch, | ||
entry, | ||
output, | ||
module, | ||
plugins, | ||
optimization, | ||
devtool, | ||
resolve, | ||
performance, | ||
stats, | ||
cache, | ||
devServer, | ||
...externals && { externals } | ||
}, (err, stats) => { | ||
// output the resulting stats. | ||
if (stats && stats.toString) { | ||
log(__filename, stats.toString({ colors: true })); | ||
} | ||
|
||
if (!watch && (stats && stats.hasErrors())) { | ||
log(__filename, stats.toString(), '', 'error'); | ||
process.exit(1); | ||
} | ||
if (!watch && (stats && stats.hasErrors())) { | ||
log(__filename, stats.toString(), '', 'error'); | ||
process.exit(1); | ||
} | ||
|
||
if (err) { | ||
log(__filename, err.toString(), '', 'error'); | ||
process.exit(1); | ||
} | ||
if (err) { | ||
log(__filename, err.toString(), '', 'error'); | ||
process.exit(1); | ||
} | ||
|
||
// log completion | ||
log(__filename, 'Webpack transpile ended', '', 'success'); | ||
}); | ||
} else { | ||
log(__filename, 'No entries for webpack, nothing found', '', 'info'); | ||
} | ||
// log completion | ||
log(__filename, 'Webpack transpile ended', '', 'success'); | ||
r(); | ||
}); | ||
} else { | ||
log(__filename, 'No entries for webpack, nothing found', '', 'info'); | ||
r(); | ||
} | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const webpackTask = require('./webpack'); | ||
const defaults = require('../config'); | ||
const extendConfig = require('../utils/extendConfig'); | ||
const generateEntries = require('../utils/generateEntries'); | ||
|
||
let config = extendConfig('./test/.febuild', defaults); | ||
let entries = { | ||
...generateEntries(config, 'js') | ||
}; | ||
const { destinationPath, projectKey } = config.general; | ||
|
||
beforeAll(async () => | ||
await new Promise(async (r) => { | ||
await webpackTask(config); | ||
r(); | ||
}) | ||
); | ||
|
||
describe('Test task/webpack.js', () => { | ||
Object.keys(entries).forEach((entry) => { | ||
const file = path.join(destinationPath, entry); | ||
const source = entries[entry]; | ||
const ext = path.extname(file) === '.js' ? 'js' : 'css'; | ||
const fileName = `${file.split('.').slice(0, -1).join('.')}.${ext}`; | ||
it(`Compile ${source} file and save ${entry} at destination folder`, async () => { | ||
const bundleContent = fs.readFileSync(fileName, { encoding:'utf8', flag:'r' }); | ||
const sourceContent = fs.readFileSync(source, { encoding:'utf8', flag:'r' }); | ||
expect(bundleContent).not.toBe(sourceContent); | ||
}); | ||
}) | ||
}); | ||
|
||
|
||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,20 @@ const renderPostcss = require('../utils/renderPostcss'); | |
const runStylelint = require('../utils/runStylelint'); | ||
const writeFile = require('../utils/writeFile'); | ||
|
||
module.exports = function renderStyles(file, dest, config) { | ||
return runStylelint( | ||
[file], config, () => | ||
renderSass( | ||
dest, file, config, (result, outFile) => | ||
renderPostcss( | ||
result, outFile, config, postCssResult => | ||
// only write file at the end | ||
writeFile(outFile, postCssResult.css, true) | ||
) | ||
) | ||
); | ||
module.exports = async function renderStyles(file, dest, config) { | ||
return new Promise((resolve) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only change here is wrapping the task to Promise. |
||
runStylelint( | ||
[file], config, () => | ||
renderSass( | ||
dest, file, config, (result, outFile) => | ||
renderPostcss( | ||
result, outFile, config, postCssResult => { | ||
// only write file at the end | ||
writeFile(outFile, postCssResult.css, true); | ||
resolve(); | ||
} | ||
) | ||
) | ||
); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plugin is deprecated