style: run prettier on webpack5 files

This commit is contained in:
Igor Randjelovic
2021-03-29 01:24:23 +02:00
parent c55782bfaf
commit c922e77129
13 changed files with 138 additions and 110 deletions

View File

@@ -6,92 +6,109 @@
if (module.hot) {
let hash = __webpack_require__.h();
const logVerbose = (title: string, ...info?: any) => {
const logVerbose = (title: string, ...info: any) => {
if (__NS_ENV_VERBOSE__) {
console.log(`[HMR][Verbose] ${title}`)
console.log(`[HMR][Verbose] ${title}`);
if (info?.length) {
console.log(...info)
console.log('---')
console.log(...info);
console.log('---');
}
}
}
};
const setStatus = (hash: string, status: 'success' | 'failure', message?: string, ...info?: any): boolean => {
const setStatus = (
hash: string,
status: 'success' | 'failure',
message?: string,
...info: any
): boolean => {
// format is important - CLI expects this exact format
console.log(`[HMR][${hash}] ${status} | ${message}`)
console.log(`[HMR][${hash}] ${status} | ${message}`);
if (info?.length) {
logVerbose('Additional Info', info)
logVerbose('Additional Info', info);
}
// return true if operation was successful
return status === 'success'
}
return status === 'success';
};
const applyOptions = {
ignoreUnaccepted: false,
ignoreDeclined: false,
ignoreErrored: false,
onDeclined(info) {
setStatus(hash, 'failure', 'A module has been declined.', info)
setStatus(hash, 'failure', 'A module has been declined.', info);
},
onUnaccepted(info) {
setStatus(hash, 'failure', 'A module has not been accepted.', info)
setStatus(hash, 'failure', 'A module has not been accepted.', info);
},
onAccepted(info) {
// console.log('accepted', info)
logVerbose('Module Accepted', info)
logVerbose('Module Accepted', info);
},
onDisposed(info) {
// console.log('disposed', info)
logVerbose('Module Disposed', info)
logVerbose('Module Disposed', info);
},
onErrored(info) {
setStatus(hash, 'failure', 'A module has errored.', info)
}
}
setStatus(hash, 'failure', 'A module has errored.', info);
},
};
const checkAndApply = async () => {
hash = __webpack_require__.h();
const modules = await module.hot.check().catch(error => {
return setStatus(hash, 'failure', 'Failed to check.', error.message || error.stack)
})
const modules = await module.hot.check().catch((error) => {
return setStatus(
hash,
'failure',
'Failed to check.',
error.message || error.stack
);
});
if (!modules) {
logVerbose('No modules to apply.')
logVerbose('No modules to apply.');
return false;
}
const appliedModules = await module.hot.apply(applyOptions).catch(error => {
return setStatus(hash, 'failure', 'Failed to apply.', error.message || error.stack)
})
const appliedModules = await module.hot
.apply(applyOptions)
.catch((error) => {
return setStatus(
hash,
'failure',
'Failed to apply.',
error.message || error.stack
);
});
if (!appliedModules) {
logVerbose('No modules applied.')
logVerbose('No modules applied.');
return false;
}
return setStatus(hash, 'success', 'Successfully applied update.')
}
return setStatus(hash, 'success', 'Successfully applied update.');
};
const hasUpdate = () => {
try {
__non_webpack_require__(`~/bundle.${__webpack_hash__}.hot-update.json`)
__non_webpack_require__(`~/bundle.${__webpack_hash__}.hot-update.json`);
return true;
} catch (err) {
return false;
}
}
};
const originalOnLiveSync = global.__onLiveSync
const originalOnLiveSync = global.__onLiveSync;
global.__onLiveSync = async function () {
logVerbose('LiveSync')
logVerbose('LiveSync');
if (!hasUpdate()) {
return;
}
await checkAndApply()
await checkAndApply();
originalOnLiveSync();
};
}

View File

@@ -1,31 +1,33 @@
import { relative, resolve } from "path";
import dedent from "ts-dedent";
import { relative, resolve } from 'path';
import dedent from 'ts-dedent';
import fs from 'fs';
// note: this will bail even if module.hot appears in a comment
const MODULE_HOT_RE = /module\.hot/
const MODULE_HOT_RE = /module\.hot/;
export default function loader(content: string, map: any) {
if (MODULE_HOT_RE.test(content)) {
// Code already handles HMR - we don't need to do anything
return this.callback(null, content, map)
return this.callback(null, content, map);
}
const opts = this.getOptions();
// used to inject the HMR runtime into the entry file
if(opts.injectHMRRuntime) {
const hmrRuntimePath = resolve(__dirname, './hmr.runtime.js')
const hmrRuntime = fs.readFileSync(hmrRuntimePath).toString()
if (opts.injectHMRRuntime) {
const hmrRuntimePath = resolve(__dirname, './hmr.runtime.js');
const hmrRuntime = fs
.readFileSync(hmrRuntimePath)
.toString()
.split('// ---')[1]
.replace('//# sourceMappingURL=hmr.runtime.js.map', '')
.replace('//# sourceMappingURL=hmr.runtime.js.map', '');
return this.callback(null, `${content}\n${hmrRuntime}`, map)
return this.callback(null, `${content}\n${hmrRuntime}`, map);
}
const relativePath = relative(
opts.appPath ?? this.rootContext,
this.resourcePath
).replace(/\\/g, '/')
).replace(/\\/g, '/');
const hmrCode = this.hot
? dedent`
@@ -36,8 +38,7 @@ export default function loader(content: string, map: any) {
`
: ``;
const source = `${content}\n${hmrCode}`
const source = `${content}\n${hmrCode}`;
this.callback(null, source, map)
this.callback(null, source, map);
}

View File

@@ -1,5 +1,5 @@
const WorkerDependency = require('webpack/lib/dependencies/WorkerDependency');
const RuntimeGlobals = require("webpack/lib/RuntimeGlobals");
const RuntimeGlobals = require('webpack/lib/RuntimeGlobals');
/**
* Patch WorkerDependency to change:
@@ -14,15 +14,19 @@ const RuntimeGlobals = require("webpack/lib/RuntimeGlobals");
* break when the dependency range changes, for example if this PR is merged:
* - https://github.com/webpack/webpack/pull/12750
*/
WorkerDependency.Template.prototype.apply = function apply(dependency, source, templateContext) {
WorkerDependency.Template.prototype.apply = function apply(
dependency,
source,
templateContext
) {
const { chunkGraph, moduleGraph, runtimeRequirements } = templateContext;
const dep = /** @type {WorkerDependency} */ (dependency);
const block = /** @type {AsyncDependenciesBlock} */ (moduleGraph.getParentBlock(
const dep = /** @type {WorkerDependency} */ dependency;
const block = /** @type {AsyncDependenciesBlock} */ moduleGraph.getParentBlock(
dependency
));
const entrypoint = /** @type {Entrypoint} */ (chunkGraph.getBlockChunkGroup(
);
const entrypoint = /** @type {Entrypoint} */ chunkGraph.getBlockChunkGroup(
block
));
);
const chunk = entrypoint.getEntrypointChunk();
// runtimeRequirements.add(RuntimeGlobals.publicPath);
@@ -40,9 +44,9 @@ WorkerDependency.Template.prototype.apply = function apply(dependency, source, t
RuntimeGlobals.getChunkScriptFilename
}(${JSON.stringify(chunk.id)})`
);
}
};
const NEW_WORKER_WITH_STRING_RE = /new\s+Worker\((['"`].+['"`])\)/
const NEW_WORKER_WITH_STRING_RE = /new\s+Worker\((['"`].+['"`])\)/;
/**
* Replaces
@@ -51,7 +55,9 @@ const NEW_WORKER_WITH_STRING_RE = /new\s+Worker\((['"`].+['"`])\)/
* new Worker(new URL('./somePath', import.meta.url))
*/
export default function loader(content: string, map: any) {
const source = content.replace(NEW_WORKER_WITH_STRING_RE, 'new Worker(new URL($1, import.meta.url))')
this.callback(null, source, map)
const source = content.replace(
NEW_WORKER_WITH_STRING_RE,
'new Worker(new URL($1, import.meta.url))'
);
this.callback(null, source, map);
}