@freshpack/webpack 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ import Webpack from "webpack";
2
+ export declare class PrefreshRuntimeModule extends Webpack.RuntimeModule {
3
+ constructor();
4
+ generate(): string;
5
+ }
@@ -0,0 +1,52 @@
1
+ import Webpack from "webpack";
2
+ const NAMESPACE = "__PREFRESH__";
3
+ const beforeModule = `
4
+ self.$RefreshSig$ = function() {
5
+ var status = 'begin';
6
+ var savedType;
7
+
8
+ return function(type, key, forceReset, getCustomHooks) {
9
+ if (!savedType) savedType = type;
10
+ status = self.${NAMESPACE}.sign(type || savedType, key, forceReset, getCustomHooks, status);
11
+ return type;
12
+ }
13
+ }
14
+ `;
15
+ export class PrefreshRuntimeModule extends Webpack.RuntimeModule {
16
+ constructor() {
17
+ super("prefresh", 5);
18
+ }
19
+ generate() {
20
+ const { runtimeTemplate } = this.compilation;
21
+ const declare = runtimeTemplate.supportsConst() ? "const" : "var";
22
+ return Webpack.Template.asString([
23
+ `${Webpack.RuntimeGlobals.interceptModuleExecution}.push(${runtimeTemplate.basicFunction("options", [
24
+ `${declare} originalFactory = options.factory;`,
25
+ `options.factory = ${runtimeTemplate.basicFunction("moduleObject, moduleExports, webpackRequire", [
26
+ `if (typeof self !== "undefined" && typeof self.${NAMESPACE} !== "undefined") {`,
27
+ Webpack.Template.indent([
28
+ `${declare} prevRefreshReg = self.$RefreshReg$;`,
29
+ `${declare} prevRefreshSig = self.$RefreshSig$;`,
30
+ beforeModule,
31
+ `${declare} reg = ${runtimeTemplate.basicFunction("currentModuleId", [
32
+ "self.$RefreshReg$ = function(type, id) {",
33
+ Webpack.Template.indent(`self.${NAMESPACE}.register(type, currentModuleId + ' ' + id);`),
34
+ "};",
35
+ ])}`,
36
+ "reg()",
37
+ "try {",
38
+ Webpack.Template.indent("originalFactory.call(this, moduleObject, moduleExports, webpackRequire);"),
39
+ "} finally {",
40
+ Webpack.Template.indent("self.$RefreshReg$ = prevRefreshReg;"),
41
+ Webpack.Template.indent("self.$RefreshSig$ = prevRefreshSig;"),
42
+ "}",
43
+ ]),
44
+ "} else {",
45
+ Webpack.Template.indent("originalFactory.call(this, moduleObject, moduleExports, webpackRequire);"),
46
+ "}",
47
+ ])}`,
48
+ ])})`,
49
+ "",
50
+ ]);
51
+ }
52
+ }
@@ -0,0 +1,8 @@
1
+ import Webpack from "webpack";
2
+ export declare const prefreshUtils = "__prefresh_utils__";
3
+ export declare const NAME = "PrefreshWebpackPlugin";
4
+ export declare const matcherOptions: {
5
+ include: RegExp;
6
+ exclude: RegExp;
7
+ };
8
+ export declare function injectRefreshFunctions(compilation: Webpack.Compilation): void;
@@ -0,0 +1,23 @@
1
+ import Webpack from "webpack";
2
+ export const prefreshUtils = "__prefresh_utils__";
3
+ export const NAME = "PrefreshWebpackPlugin";
4
+ export const matcherOptions = {
5
+ include: /\.([jt]sx?)$/,
6
+ exclude: /node_modules|\/deno\/npm\//,
7
+ };
8
+ export function injectRefreshFunctions(compilation) {
9
+ const hookVars = compilation.mainTemplate.hooks.localVars;
10
+ hookVars.tap("ReactFreshWebpackPlugin", (source) => Webpack.Template.asString([
11
+ source,
12
+ "",
13
+ "// noop fns to prevent runtime errors during initialization",
14
+ 'if (typeof self !== "undefined") {',
15
+ Webpack.Template.indent("self.$RefreshReg$ = function () {};"),
16
+ Webpack.Template.indent("self.$RefreshSig$ = function () {"),
17
+ Webpack.Template.indent(Webpack.Template.indent("return function (type) {")),
18
+ Webpack.Template.indent(Webpack.Template.indent(Webpack.Template.indent("return type;"))),
19
+ Webpack.Template.indent(Webpack.Template.indent("};")),
20
+ Webpack.Template.indent("};"),
21
+ "}",
22
+ ]));
23
+ }
@@ -0,0 +1,5 @@
1
+ import { flush } from "@prefresh/utils";
2
+ export declare const getExports: (m: any) => any;
3
+ export declare function registerExports(moduleExports: any, moduleId: string): void;
4
+ export declare const shouldBind: (m: any) => boolean;
5
+ export { flush };
@@ -0,0 +1,47 @@
1
+ import { flush, isComponent } from "@prefresh/utils";
2
+ // eslint-disable-next-line
3
+ export const getExports = (m) => m.exports || m.__proto__.exports;
4
+ function isSafeExport(key) {
5
+ return (key === "__esModule" ||
6
+ key === "__N_SSG" ||
7
+ key === "__N_SSP" ||
8
+ key === "config");
9
+ }
10
+ export function registerExports(moduleExports, moduleId) {
11
+ if (typeof self !== "undefined" && typeof self["__PREFRESH__"] !== "undefined") {
12
+ self["__PREFRESH__"].register(moduleExports, moduleId + " %exports%");
13
+ if (moduleExports == null || typeof moduleExports !== "object")
14
+ return;
15
+ for (const key in moduleExports) {
16
+ if (isSafeExport(key))
17
+ continue;
18
+ const exportValue = moduleExports[key];
19
+ const typeID = moduleId + " %exports% " + key;
20
+ self["__PREFRESH__"].register(exportValue, typeID);
21
+ }
22
+ }
23
+ }
24
+ export const shouldBind = (m) => {
25
+ let isCitizen = false;
26
+ const moduleExports = getExports(m);
27
+ if (isComponent(moduleExports)) {
28
+ isCitizen = true;
29
+ }
30
+ if (moduleExports === undefined ||
31
+ moduleExports === null ||
32
+ typeof moduleExports !== "object") {
33
+ isCitizen = isCitizen || false;
34
+ }
35
+ else {
36
+ for (const key in moduleExports) {
37
+ if (key === "__esModule")
38
+ continue;
39
+ const exportValue = moduleExports[key];
40
+ if (isComponent(exportValue)) {
41
+ isCitizen = isCitizen || true;
42
+ }
43
+ }
44
+ }
45
+ return isCitizen;
46
+ };
47
+ export { flush };
@@ -0,0 +1,9 @@
1
+ export type JsRaw = {
2
+ _jsRaw: string;
3
+ };
4
+ type ToJsRaw = string | JsRaw | (ToJsRaw[]) | Record<string, string | JsRaw> | Map<string, JsRaw> | undefined;
5
+ export declare function toJsRaw(v: ToJsRaw): JsRaw;
6
+ export declare const js: (template: TemplateStringsArray, ...subs: ToJsRaw[]) => {
7
+ _jsRaw: string;
8
+ };
9
+ export {};
package/quasiquote.js ADDED
@@ -0,0 +1,30 @@
1
+ export function toJsRaw(v) {
2
+ if (typeof v === "string") {
3
+ return { _jsRaw: JSON.stringify(v) };
4
+ }
5
+ if (v instanceof Array) {
6
+ return {
7
+ _jsRaw: "[" + v.map((v) => toJsRaw(v)._jsRaw).join(v.length > 5 ? ",\n" : ",") +
8
+ "]",
9
+ };
10
+ }
11
+ if (v instanceof Map) {
12
+ return js `new Map(${v.entries().map(([name, v]) => js `[${name},${v}]`).toArray()})`;
13
+ }
14
+ if (typeof v === "object" && !("_jsRaw" in v)) {
15
+ const entries = Object.entries(v);
16
+ return {
17
+ _jsRaw: "{" + entries.map(([name, v]) => `${name}:${toJsRaw(v)._jsRaw}`).join(entries.length > 10 ? ",\n" : ",") + "}",
18
+ };
19
+ }
20
+ if (typeof v === "undefined") {
21
+ return js `undefined`;
22
+ }
23
+ if (v === null) {
24
+ return js `null`;
25
+ }
26
+ return v;
27
+ }
28
+ export const js = (template, ...subs) => ({
29
+ _jsRaw: String.raw(template, ...subs.map((v) => toJsRaw(v)._jsRaw)),
30
+ });
@@ -0,0 +1,5 @@
1
+ import type { LoaderContext } from "webpack";
2
+ type Context = LoaderContext<Record<never, never>>;
3
+ export declare function pitch(this: Context): string;
4
+ export default function rawBytesLoader(this: Context, source: string): string;
5
+ export {};
@@ -0,0 +1,12 @@
1
+ export function pitch() {
2
+ const resource = this.resourcePath;
3
+ this.addDependency(resource);
4
+ const request = `${resource}.webpack[asset/resource]!=!${resource}`;
5
+ return [
6
+ `import url from ${JSON.stringify(request)};`,
7
+ `export default new Uint8Array(await (await fetch(url)).arrayBuffer());`,
8
+ ].join("\n");
9
+ }
10
+ export default function rawBytesLoader(source) {
11
+ return source;
12
+ }
@@ -0,0 +1,13 @@
1
+ import type { Compiler } from "webpack";
2
+ export interface RawImportsOptions {
3
+ /**
4
+ * "resource" (default) emits a bytes import as a standalone asset fetched at runtime,
5
+ * "inline" embeds it into the importing chunk as base64.
6
+ */
7
+ bytes?: "resource" | "inline";
8
+ }
9
+ export declare class RawImportsPlugin {
10
+ options: RawImportsOptions;
11
+ constructor(options?: RawImportsOptions);
12
+ apply(compiler: Compiler): void;
13
+ }
package/rawImports.js ADDED
@@ -0,0 +1,44 @@
1
+ import { defineRequire } from "./requireHook.js";
2
+ const NAME = "RawImportsPlugin";
3
+ const internalRawBytesLoader = defineRequire("@freshpack/webpack/internal-raw-bytes-loader", await import("./rawBytesLoader.js"));
4
+ function setModuleType(factory, data, type) {
5
+ data.type = type;
6
+ data.settings.type = type;
7
+ data.parser = factory.getParser(type);
8
+ data.parserOptions = undefined;
9
+ data.generator = factory.getGenerator(type);
10
+ data.generatorOptions = undefined;
11
+ }
12
+ export class RawImportsPlugin {
13
+ options;
14
+ constructor(options = {}) {
15
+ this.options = options;
16
+ }
17
+ apply(compiler) {
18
+ compiler.hooks.compilation.tap(NAME, (_compilation, { normalModuleFactory }) => {
19
+ normalModuleFactory.hooks.afterResolve.tap(NAME, (resolveData) => {
20
+ const attributes = resolveData.dependencies[0]?.attributes;
21
+ const data = resolveData.createData;
22
+ if (attributes?.type === "text") {
23
+ setModuleType(normalModuleFactory, data, "asset/source");
24
+ data.loaders = [];
25
+ }
26
+ else if (attributes?.type === "bytes") {
27
+ if (this.options.bytes === "inline") {
28
+ setModuleType(normalModuleFactory, data, "asset/bytes");
29
+ data.loaders = [];
30
+ }
31
+ else {
32
+ setModuleType(normalModuleFactory, data, "javascript/esm");
33
+ data.loaders = [{
34
+ loader: internalRawBytesLoader,
35
+ options: {},
36
+ ident: null,
37
+ type: null,
38
+ }];
39
+ }
40
+ }
41
+ });
42
+ });
43
+ }
44
+ }
@@ -0,0 +1 @@
1
+ export declare function defineRequire(name: string, module: Record<string, unknown>): string;
package/requireHook.js ADDED
@@ -0,0 +1,16 @@
1
+ import Module from "node:module";
2
+ const overrides = new Map();
3
+ let overrideId = 0;
4
+ const originalLoad = Module.prototype.require;
5
+ Module.prototype.require = function hookedRequire(id) {
6
+ const override = overrides.get(id);
7
+ if (override) {
8
+ return override;
9
+ }
10
+ return originalLoad.apply(this, arguments);
11
+ };
12
+ export function defineRequire(name, module) {
13
+ const generatedName = `override:${name}/${overrideId++}`;
14
+ overrides.set(generatedName, module);
15
+ return generatedName;
16
+ }
package/resolve.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { type Loader, Workspace, type WorkspaceOptions } from "@deno/loader";
2
+ import { type Compiler } from "webpack";
3
+ export declare class DenoLoaderPlugin {
4
+ #private;
5
+ opts: WorkspaceOptions;
6
+ patchImport: (f: string, v: string) => string;
7
+ workspace: Workspace;
8
+ get loader(): Promise<Loader>;
9
+ constructor(opts: WorkspaceOptions, patchImport?: (f: string, v: string) => string);
10
+ apply(compiler: Compiler): void;
11
+ }