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: pass all Node.js globals to templates #1796

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 50 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,56 @@ class HtmlWebpackPlugin {
HTML_WEBPACK_PLUGIN: true,
require: require,
htmlWebpackPluginPublicPath: publicPath,
URL: require('url').URL,
__filename: templateWithoutLoaders
__filename: templateWithoutLoaders,
__dirname: path.dirname(templateWithoutLoaders),
AbortController: global.AbortController,
AbortSignal: global.AbortSignal,
Blob: global.Blob,
Buffer: global.Buffer,
ByteLengthQueuingStrategy: global.ByteLengthQueuingStrategy,
BroadcastChannel: global.BroadcastChannel,
CompressionStream: global.CompressionStream,
CountQueuingStrategy: global.CountQueuingStrategy,
Crypto: global.Crypto,
CryptoKey: global.CryptoKey,
CustomEvent: global.CustomEvent,
DecompressionStream: global.DecompressionStream,
Event: global.Event,
EventTarget: global.EventTarget,
File: global.File,
FormData: global.FormData,
Headers: global.Headers,
MessageChannel: global.MessageChannel,
MessageEvent: global.MessageEvent,
MessagePort: global.MessagePort,
PerformanceEntry: global.PerformanceEntry,
PerformanceMark: global.PerformanceMark,
PerformanceMeasure: global.PerformanceMeasure,
PerformanceObserver: global.PerformanceObserver,
PerformanceObserverEntryList: global.PerformanceObserverEntryList,
PerformanceResourceTiming: global.PerformanceResourceTiming,
ReadableByteStreamController: global.ReadableByteStreamController,
ReadableStream: global.ReadableStream,
ReadableStreamBYOBReader: global.ReadableStreamBYOBReader,
ReadableStreamBYOBRequest: global.ReadableStreamBYOBRequest,
ReadableStreamDefaultController: global.ReadableStreamDefaultController,
ReadableStreamDefaultReader: global.ReadableStreamDefaultReader,
Response: global.Response,
Request: global.Request,
SubtleCrypto: global.SubtleCrypto,
DOMException: global.DOMException,
TextDecoder: global.TextDecoder,
TextDecoderStream: global.TextDecoderStream,
TextEncoder: global.TextEncoder,
TextEncoderStream: global.TextEncoderStream,
TransformStream: global.TransformStream,
TransformStreamDefaultController: global.TransformStreamDefaultController,
URL: global.URL,
URLSearchParams: global.URLSearchParams,
WebAssembly: global.WebAssembly,
WritableStream: global.WritableStream,
WritableStreamDefaultController: global.WritableStreamDefaultController,
WritableStreamDefaultWriter: global.WritableStreamDefaultWriter
});
const vmScript = new vm.Script(source, { filename: templateWithoutLoaders });
// Evaluate code and cast to string
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
]
},
"devDependencies": {
"@types/node": "11.13.9",
"@types/node": "^20.2.5",
"commitizen": "^4.2.4",
"css-loader": "5.0.1",
"cz-conventional-changelog": "2.1.0",
Expand Down
18 changes: 18 additions & 0 deletions spec/fixtures/templateParam.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
module.exports = function (templateParams) {
const version = parseInt(process.version.match(/^v(\d+)/)[1]);

if (typeof URL !== 'function') {
throw new Error('Error');
}

if (typeof URLSearchParams !== 'function') {
throw new Error('Error');
}

if (version >= 11 && typeof TextEncoder !== 'function') {
throw new Error('Error');
}

if (version >= 11 && typeof TextDecoder !== 'function') {
throw new Error('Error');
}

return 'templateParams keys: "' + Object.keys(templateParams).join(',') + '"';
};