Skip to content

Commit

Permalink
test: refactor test cases for preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Sep 29, 2024
1 parent 1831536 commit c2af4cc
Show file tree
Hide file tree
Showing 394 changed files with 315 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand All @@ -28,9 +28,7 @@ module.exports = {
},
},
},
loaderOptions: {
preprocessor: (content, { rootContext: root, data }) => ejs.render(content, data, { root }),
},
preprocessor: (content, { rootContext: root, data }) => ejs.render(content, data, { root }),
}),
],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand All @@ -27,9 +27,7 @@ module.exports = {
},
},
},
loaderOptions: {
preprocessor: 'ejs',
},
preprocessor: 'ejs',
}),
],

Expand Down
53 changes: 53 additions & 0 deletions test/cases/_preprocessor/ejs-option-async/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const path = require('path');
const HtmlBundlerPlugin = require('@test/html-bundler-webpack-plugin');
const ejs = require('ejs');

module.exports = {
mode: 'production',

output: {
path: path.join(__dirname, 'dist/'),
},

resolve: {
alias: {
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

plugins: [
new HtmlBundlerPlugin({
test: /\.(html|ejs)$/,
entry: {
index: {
import: './src/home.ejs',
data: {
title: 'Home',
headline: 'Breaking Bad',
people: ['Walter White', 'Jesse Pinkman'],
},
},
},

// use custom function which return promise
//preprocessor: (content, { rootContext: root, data }) => ejs.render(content, data, { root, async: true }),

preprocessor: 'ejs',
preprocessorOptions: {
async: true,
},
}),
],

module: {
rules: [
{
test: /\.(png|svg|jpe?g|webp)$/i,
type: 'asset/resource',
generator: {
filename: 'assets/img/[name].[hash:8][ext]',
},
},
],
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand All @@ -26,8 +26,8 @@ module.exports = {
},
},
},
// simplify config for preprocessor: instead of place it under loaderOptions add it in here
preprocessor: 'ejs',
// using preprocessor option
preprocessorOptions: {
views: [path.join(__dirname, 'src/views/partials/')],
},
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const path = require('path');
const HtmlBundlerPlugin = require('@test/html-bundler-webpack-plugin');
const ejs = require('ejs');

module.exports = {
mode: 'production',
Expand All @@ -23,10 +22,8 @@ module.exports = {
filename: 'assets/js/[name].[contenthash:8].js',
},

loaderOptions: {
// test: when template contain CRLF and used EJS compiler then occur the error: Unterminated string constant
preprocessor: (template, { data }) => ejs.render(template, data),
},
// test fix: when template contain CRLF and used EJS compiler then occur the error: Unterminated string constant
preprocessor: 'ejs',
}),
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand All @@ -27,11 +27,10 @@ module.exports = {
},
},
},
loaderOptions: {
preprocessorOptions: {
async: true, // defaults false, wenn async is true then must be used `await includeAsync()`
views: path.join(process.cwd(), 'src/views'), // a relative or absolute path
},
preprocessor: 'eta',
preprocessorOptions: {
async: true, // defaults false, wenn async is true then must be used `await includeAsync()`
views: path.join(process.cwd(), 'src/views'), // a relative or absolute path
},
}),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand All @@ -27,10 +27,9 @@ module.exports = {
},
},
},
loaderOptions: {
preprocessorOptions: {
views: 'src/views/', // include a partial relative to this directory, a relative or absolute path,
},
preprocessor: 'eta',
preprocessorOptions: {
views: 'src/views/', // include a partial relative to this directory, a relative or absolute path,
},
}),
],
Expand Down

Large diffs are not rendered by default.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand All @@ -32,11 +32,9 @@ module.exports = {
},
},
},
loaderOptions: {
preprocessor: 'handlebars',
preprocessorOptions: {
views: [PATHS.partials],
},
preprocessor: 'handlebars',
preprocessorOptions: {
views: [PATHS.partials],
},
}),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand All @@ -21,13 +21,11 @@ module.exports = {
index: 'src/views/pages/home.hbs',
},

loaderOptions: {
preprocessor: 'handlebars',
preprocessorOptions: {
// define helpers manually
helpers: {
bold: (options) => new Handlebars.SafeString(`<strong>${options.fn(this)}</strong>`),
},
preprocessor: 'handlebars',
preprocessorOptions: {
// define helpers manually
helpers: {
bold: (options) => new Handlebars.SafeString(`<strong>${options.fn(this)}</strong>`),
},
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand All @@ -20,15 +20,13 @@ module.exports = {
index: 'src/views/pages/home.hbs',
},

loaderOptions: {
preprocessor: 'handlebars',
preprocessorOptions: {
helpers: [
// helper paths
'src/views/helpers/',
'src/views/helpers2/',
],
},
preprocessor: 'handlebars',
preprocessorOptions: {
helpers: [
// helper paths
'src/views/helpers/',
'src/views/helpers2/',
],
},
}),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {

resolve: {
alias: {
'@images': path.join(__dirname, '../../fixtures/images'),
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

Expand All @@ -27,16 +27,14 @@ module.exports = {
},
},

loaderOptions: {
preprocessor: 'handlebars',
preprocessorOptions: {
partials: [
// absolute path to partials
path.join(__dirname, 'src/views/includes'),
// relative path to partials
'src/views/partials/',
],
},
preprocessor: 'handlebars',
preprocessorOptions: {
partials: [
// absolute path to partials
path.join(__dirname, 'src/views/includes'),
// relative path to partials
'src/views/partials/',
],
},
}),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require('path');
const HtmlBundlerPlugin = require('@test/html-bundler-webpack-plugin');

module.exports = {
mode: 'production',

output: {
path: path.join(__dirname, 'dist/'),
},

resolve: {
alias: {
'@images': path.join(__dirname, '../../../fixtures/images'),
},
},

plugins: [
new HtmlBundlerPlugin({
entry: {
index: {
import: './src/views/pages/home.hbs',
data: {
title: 'Home',
headline: 'Breaking Bad',
people: ['Walter White', 'Jesse Pinkman'],
},
},
},

preprocessor: 'handlebars',
preprocessorOptions: {
// define partials manually
partials: {
gallery: path.join(__dirname, 'src/views/includes/gallery.html'),
teaser: path.join(__dirname, 'src/views/includes/teaser.hbs'),
header: path.join(__dirname, 'src/views/partials/header.html'),
footer: path.join(__dirname, 'src/views/partials/footer.html'),
'menu/nav': path.join(__dirname, 'src/views/partials/menu/nav.html'),
'menu/aside/left': path.join(__dirname, 'src/views/partials/menu/aside/left.html'),
'menu/aside/right': path.join(__dirname, 'src/views/partials/menu/aside/right.hbs'),
'menu/top/desktop': path.join(__dirname, 'src/views/partials/menu/top/desktop.html'),
'menu/top/mobile': path.join(__dirname, 'src/views/partials/menu/top/mobile.hbs'),
},
},
}),
],

module: {
rules: [
{
test: /\.(png|svg|jpe?g|webp)$/i,
type: 'asset/resource',
generator: {
filename: 'assets/img/[name].[hash:8][ext]',
},
},
],
},
};
Loading

0 comments on commit c2af4cc

Please sign in to comment.