Skip to content
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

feat: injection of custom root element for react/vue/angular #1637

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class HtmlWebpackPlugin {
meta: {},
base: false,
title: 'Webpack App',
xhtml: false
xhtml: false,
rootElement: false
};

/** @type {ProcessedHtmlWebpackOptions} */
Expand All @@ -82,6 +83,11 @@ class HtmlWebpackPlugin {
options.meta = Object.assign({}, options.meta, defaultMeta, userOptions.meta);
}

// Only adds the root element if it was set
if (options.rootElement) {
options.rootElement = userOptions.rootElement;
}

// entryName to fileName conversion function
const userOptionFilename = userOptions.filename || defaultOptions.filename;
const filenameFunction = typeof userOptionFilename === 'function'
Expand Down Expand Up @@ -298,6 +304,18 @@ function hookIntoCompiler (compiler, options, plugin) {
(options.inject !== 'body' && options.scriptLoading !== 'blocking') ? 'head' : 'body';
// Group assets to `head` and `body` tag arrays
const assetGroups = generateAssetGroups(assetTags, scriptTarget);

if (options.rootElement) {
assetGroups.bodyTags.push({
tagName: options.rootElement.tag || 'div',
voidTag: false,
attributes: {
id: options.rootElement.id || 'root'
},
meta: {}
});
}

// Allow third-party-plugin authors to reorder and change the assetTags once they are grouped
return getHtmlWebpackPluginHooks(compilation).alterAssetTagGroups.promise({
headTags: assetGroups.headTags,
Expand Down
52 changes: 52 additions & 0 deletions spec/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2799,4 +2799,56 @@ describe('HtmlWebpackPlugin', () => {
done();
});
});
it('allows you to inject a custom root element', done => {
testHtmlPlugin({
mode: 'none',
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
rootElement: {
tag: 'main',
id: 'app'
}
})]
}, ['<main id="app"></main>'], null, done);
});
it('provides a default tag', done => {
testHtmlPlugin({
mode: 'none',
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
rootElement: {
id: 'app'
}
})]
}, ['<div id="app"></div>'], null, done);
});
it('provides a default id', done => {
testHtmlPlugin({
mode: 'none',
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
rootElement: {
tag: 'div'
}
})]
}, ['<div id="root"></div>'], null, done);
});
});