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

Fix compatibility with webpack 5 #698

Merged
merged 20 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: compatibility with webpack v5
  • Loading branch information
alexander-akait committed Feb 9, 2021
commit 29b8752fccfe97c27b2b5a754cd8f34c6bf8a723
9 changes: 8 additions & 1 deletion src/CssLoadingRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ module.exports = class CssLoadingRuntimeModule extends RuntimeModule {
'}',
])
: '',
this.runtimeOptions.insert,
typeof this.runtimeOptions.insert !== 'undefined'
? typeof this.runtimeOptions.insert === 'function'
? `(${this.runtimeOptions.insert.toString()})(linkTag)`
: Template.asString([
`var target = document.querySelector("${this.runtimeOptions.insert}");`,
`target.parentNode.insertBefore(linkTag, target.nextSibling);`,
])
: Template.asString(['document.head.appendChild(linkTag);']),
'return linkTag;',
]
)};`,
Expand Down
25 changes: 11 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import CssDependency from './CssDependency';
import schema from './plugin-options.json';
import { MODULE_TYPE, compareModulesByIdentifier } from './utils';

const { Template } = webpack;

const pluginName = 'mini-css-extract-plugin';

const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
Expand Down Expand Up @@ -39,23 +37,13 @@ class MiniCssExtractPlugin {
baseDataPath: 'options',
});

const insert =
typeof options.insert !== 'undefined'
? typeof options.insert === 'function'
? `(${options.insert.toString()})(linkTag)`
: Template.asString([
`var target = document.querySelector("${options.insert}");`,
`target.parentNode.insertBefore(linkTag, target.nextSibling);`,
])
: Template.asString(['document.head.appendChild(linkTag);']);

this.options = Object.assign(
{ filename: DEFAULT_FILENAME, ignoreOrder: false },
options
);

this.runtimeOptions = {
insert,
insert: options.insert,
linkType:
// Todo in next major release set default to "false"
options.linkType === true || typeof options.linkType === 'undefined'
Expand Down Expand Up @@ -462,7 +450,16 @@ class MiniCssExtractPlugin {
'}',
])
: '',
this.runtimeOptions.insert,
typeof this.runtimeOptions.insert !== 'undefined'
? typeof this.runtimeOptions.insert === 'function'
? `(${this.runtimeOptions.insert.toString()})(linkTag)`
: Template.asString([
`var target = document.querySelector("${this.runtimeOptions.insert}");`,
`target.parentNode.insertBefore(linkTag, target.nextSibling);`,
])
: Template.asString([
'document.head.appendChild(linkTag);',
]),
]),
'}).then(function() {',
Template.indent(['installedCssChunks[chunkId] = 0;']),
Expand Down