Skip to content

Commit

Permalink
feat: removed cjs wrapper (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Dec 16, 2021
1 parent 57d7c11 commit f0fa0a7
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 202 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"type": "opencollective",
"url": "https://opencollective.com/webpack"
},
"main": "dist/cjs.js",
"types": "types/cjs.d.ts",
"main": "dist/index.js",
"types": "types/index.d.ts",
"engines": {
"node": ">= 12.13.0"
},
"scripts": {
"start": "npm run build -- -w",
"clean": "del-cli dist",
"clean": "del-cli dist types",
"prebuild": "npm run clean",
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
Expand Down
1 change: 0 additions & 1 deletion src/cjs.js

This file was deleted.

18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as path from "path";
import * as os from "os";
const path = require("path");
const os = require("os");

import { validate } from "schema-utils";
import serialize from "serialize-javascript";
const { validate } = require("schema-utils");
const serialize = require("serialize-javascript");

import worker from "./worker";
import schema from "./plugin-options.json";
import {
const worker = require("./worker");
const schema = require("./plugin-options.json");
const {
throttleAll,
imageminNormalizeConfig,
imageminMinify,
imageminGenerate,
squooshMinify,
squooshGenerate,
} from "./utils.js";
} = require("./utils.js");

/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
Expand Down Expand Up @@ -515,4 +515,4 @@ ImageMinimizerPlugin.imageminGenerate = imageminGenerate;
ImageMinimizerPlugin.squooshMinify = squooshMinify;
ImageMinimizerPlugin.squooshGenerate = squooshGenerate;

export default ImageMinimizerPlugin;
module.exports = ImageMinimizerPlugin;
17 changes: 10 additions & 7 deletions src/loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from "path";
const path = require("path");

import worker from "./worker";
import schema from "./loader-options.json";
import { isAbsoluteURL } from "./utils.js";
const worker = require("./worker");
const schema = require("./loader-options.json");
const { isAbsoluteURL } = require("./utils.js");

/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compilation} Compilation */
Expand All @@ -19,8 +19,9 @@ import { isAbsoluteURL } from "./utils.js";
* @template T
* @this {import("webpack").LoaderContext<LoaderOptions<T>>}
* @param {Buffer} content
* @returns {Promise<Buffer | undefined>}
*/
module.exports = async function loader(content) {
async function loader(content) {
// Avoid optimize twice
if (
this._module &&
Expand Down Expand Up @@ -170,6 +171,8 @@ module.exports = async function loader(content) {
}

callback(null, output.data);
};
}

module.exports.raw = true;
loader.raw = true;

module.exports = loader;
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from "path";
const path = require("path");

/** @typedef {import("./index").WorkerResult} WorkerResult */
/** @typedef {import("./index").SquooshOptions} SquooshOptions */
Expand Down Expand Up @@ -927,7 +927,7 @@ squooshMinify.setup = squooshImagePoolSetup;

squooshMinify.teardown = squooshImagePoolTeardown;

export {
module.exports = {
throttleAll,
isAbsoluteURL,
imageminNormalizeConfig,
Expand Down
4 changes: 3 additions & 1 deletion src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @param {import("./index").InternalWorkerOptions<T>} options
* @returns {Promise<WorkerResult>}
*/
export default async function worker(options) {
async function worker(options) {
/** @type {WorkerResult} */
let result = {
data: options.input,
Expand Down Expand Up @@ -117,3 +117,5 @@ export default async function worker(options) {

return result;
}

module.exports = worker;
8 changes: 0 additions & 8 deletions test/cjs.test.js

This file was deleted.

Loading

0 comments on commit f0fa0a7

Please sign in to comment.