Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/drop-webpack-4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@vanilla-extract/webpack-plugin': major
---

Drop support for webpack 4. The `peerDependencies` range for `webpack` is now `^5.73.0`, as the plugin now uses the native `this.getOptions()` and `this.utils.contextify()` loader context APIs instead of the `loader-utils` dependency, which has been removed.

**BREAKING CHANGE**: Projects using `@vanilla-extract/webpack-plugin` must upgrade to webpack `5.73.0` or later.
3 changes: 1 addition & 2 deletions packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@
"author": "SEEK",
"license": "MIT",
"peerDependencies": {
"webpack": "^4.30.0 || ^5.20.2"
"webpack": "^5.73.0"
},
"dependencies": {
"@vanilla-extract/integration": "workspace:^",
"debug": "^4.3.1",
"loader-utils": "^2.0.0",
"picocolors": "^1.0.0"
},
"devDependencies": {
Expand Down
51 changes: 0 additions & 51 deletions packages/webpack-plugin/src/compat.ts

This file was deleted.

73 changes: 19 additions & 54 deletions packages/webpack-plugin/src/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { LoaderContext } from './types';
import createCompat from './compat';

// Webpack exposes the `Externals` type which is a union of several types.
// We likely intended to only accept a subset of these, but chose to use `any`.
Expand Down Expand Up @@ -75,11 +74,6 @@ function compileVanillaSource(
externals: Externals | undefined,
): Promise<CompilationResult> {
return new Promise((resolve, reject) => {
const isWebpack5 = Boolean(
loader._compiler.webpack && loader._compiler.webpack.version,
);
const compat = createCompat(isWebpack5);

// Escape webpack template strings and Next.js dynamic routes in output files so they don't get replaced
// Non-standard escape syntax, see https://webpack.js.org/configuration/output/#template-strings
// and https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
Expand All @@ -95,41 +89,27 @@ function compileVanillaSource(
[],
);

const NodeTemplatePlugin = compat.getNodeTemplatePlugin(loader._compiler);
const NodeTargetPlugin = compat.getNodeTargetPlugin(loader._compiler);
const LimitChunkCountPlugin = compat.getLimitChunkCountPlugin(
loader._compiler,
);
const ExternalsPlugin = compat.getExternalsPlugin(loader._compiler);
const {
node: { NodeTemplatePlugin, NodeTargetPlugin },
optimize: { LimitChunkCountPlugin },
ExternalsPlugin,
EntryOptionPlugin,
library: { EnableLibraryPlugin },
} = loader._compiler.webpack;

new NodeTemplatePlugin().apply(childCompiler);
new NodeTargetPlugin().apply(childCompiler);

if (compat.isWebpack5) {
const {
EntryOptionPlugin,
library: { EnableLibraryPlugin },
} = loader._compiler.webpack;
new EnableLibraryPlugin('commonjs2').apply(childCompiler);

new EnableLibraryPlugin('commonjs2').apply(childCompiler);

EntryOptionPlugin.applyEntryOption(childCompiler, loader.context, {
child: {
library: {
type: 'commonjs2',
},
import: [loader.resourcePath],
EntryOptionPlugin.applyEntryOption(childCompiler, loader.context, {
child: {
library: {
type: 'commonjs2',
},
});
} else {
// Webpack 4 code. Remove once support is removed
const { LibraryTemplatePlugin, SingleEntryPlugin } = require('webpack');

new LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
new SingleEntryPlugin(loader.context, loader.resourcePath).apply(
childCompiler,
);
}
import: [loader.resourcePath],
},
});

new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
new ExternalsPlugin('commonjs', [
Expand All @@ -140,35 +120,20 @@ function compileVanillaSource(

let source: string;

if (compat.isWebpack5) {
childCompiler.hooks.compilation.tap(compilerName, (compilation) => {
compilation.hooks.processAssets.tap(compilerName, () => {
source =
compilation.assets[loader.resourcePath] &&
(compilation.assets[loader.resourcePath].source() as string);

// Remove all chunk assets
compilation.chunks.forEach((chunk) => {
chunk.files.forEach((file) => {
compilation.deleteAsset(file);
});
});
});
});
} else {
childCompiler.hooks.afterCompile.tap(compilerName, (compilation) => {
childCompiler.hooks.compilation.tap(compilerName, (compilation) => {
compilation.hooks.processAssets.tap(compilerName, () => {
source =
compilation.assets[loader.resourcePath] &&
(compilation.assets[loader.resourcePath].source() as string);

// Remove all chunk assets
compilation.chunks.forEach((chunk) => {
chunk.files.forEach((file) => {
delete compilation.assets[file];
compilation.deleteAsset(file);
});
});
});
}
});

try {
childCompiler.runAsChild((err, _entries, compilation) => {
Expand Down
25 changes: 17 additions & 8 deletions packages/webpack-plugin/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import path from 'path';
// @ts-expect-error
import loaderUtils from 'loader-utils';
import {
getPackageInfo,
type IdentifierOption,
Expand Down Expand Up @@ -43,14 +41,25 @@ interface InternalLoaderOptions extends LoaderOptions {
virtualLoader: 'virtualFileLoader' | 'virtualNextFileLoader';
}

const stringifyRequest = (loaderContext: LoaderContext, request: string) =>
JSON.stringify(
loaderContext.utils.contextify(
loaderContext.context || loaderContext.rootContext,
request,
),
);

const defaultIdentifierOption = (
mode: LoaderContext['mode'],
identifiers?: IdentifierOption,
): IdentifierOption =>
identifiers ?? (mode === 'production' ? 'short' : 'debug');

export default function (this: LoaderContext, source: string): void {
const { identifiers } = loaderUtils.getOptions(this) as InternalLoaderOptions;
export default function (
this: LoaderContext<InternalLoaderOptions>,
source: string,
): void {
const { identifiers } = this.getOptions();

const { name } = getPackageInfo(this.rootContext);

Expand All @@ -71,9 +80,9 @@ export default function (this: LoaderContext, source: string): void {
});
}

export function pitch(this: LoaderContext): void {
export function pitch(this: LoaderContext<InternalLoaderOptions>): void {
const { childCompiler, outputCss, identifiers, virtualLoader } =
loaderUtils.getOptions(this) as InternalLoaderOptions;
this.getOptions();

const log = debug(
`vanilla-extract:loader:${formatResourcePath(this.resourcePath)}`,
Expand Down Expand Up @@ -113,14 +122,14 @@ export function pitch(this: LoaderContext): void {
source: serializedCss,
},
)}`;
const request = loaderUtils.stringifyRequest(
const request = stringifyRequest(
this,
`${fileName}!=!${virtualResourceLoader}!${virtualFileLoaderExtractionFile}`,
);
return `import ${request}`;
} else {
// https://github.com/SukkaW/style9-webpack/blob/f51c46bbcd95ea3b988d3559c3b35cc056874366/src/next-appdir/style9-next-loader.ts#L64-L72
const request = loaderUtils.stringifyRequest(
const request = stringifyRequest(
this,
// Next.js RSC CSS extraction will discard any loaders in the request.
// So we need to pass virtual css information through resourceQuery.
Expand Down
60 changes: 18 additions & 42 deletions packages/webpack-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,31 @@ import {
import type { Compiler, RuleSetRule } from 'webpack';

import { ChildCompiler } from './compiler';
import createCompat, { type WebpackCompat } from './compat';

import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);

const pluginName = 'VanillaExtractPlugin';

function markCSSFilesAsSideEffects(compiler: Compiler, compat: WebpackCompat) {
function markCSSFilesAsSideEffects(compiler: Compiler) {
compiler.hooks.normalModuleFactory.tap(pluginName, (nmf) => {
if (compat.isWebpack5) {
nmf.hooks.createModule.tap(
pluginName,
// @ts-expect-error CreateData is typed as 'object'...
(createData: {
matchResource?: string;
settings: { sideEffects?: boolean };
}) => {
if (
createData.matchResource &&
(createData.matchResource.endsWith('.vanilla.css') ||
createData.matchResource.endsWith('vanilla.virtual.css'))
) {
createData.settings.sideEffects = true;
}
},
);
} else {
nmf.hooks.afterResolve.tap(
pluginName,
// @ts-expect-error Can't be typesafe for webpack 4
(result: {
matchResource?: string;
settings: { sideEffects?: boolean };
}) => {
if (
result.matchResource &&
(result.matchResource.endsWith('.vanilla.css') ||
result.matchResource.endsWith('vanilla.virtual.css'))
) {
result.settings.sideEffects = true;
}
},
);
}
nmf.hooks.createModule.tap(
pluginName,
// @ts-expect-error CreateData is typed as 'object'...
(createData: {
matchResource?: string;
settings: { sideEffects?: boolean };
}) => {
if (
createData.matchResource &&
(createData.matchResource.endsWith('.vanilla.css') ||
createData.matchResource.endsWith('vanilla.virtual.css'))
) {
createData.settings.sideEffects = true;
}
},
);
});
}

Expand Down Expand Up @@ -97,11 +77,7 @@ export abstract class AbstractVanillaExtractPlugin {
compiler: Compiler,
virtualLoader: 'virtualFileLoader' | 'virtualNextFileLoader',
): void {
const compat = createCompat(
Boolean(compiler.webpack && compiler.webpack.version),
);

markCSSFilesAsSideEffects(compiler, compat);
markCSSFilesAsSideEffects(compiler);

compiler.options.module?.rules.splice(0, 0, {
test: this.test,
Expand Down
10 changes: 8 additions & 2 deletions packages/webpack-plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { Compilation, Compiler } from 'webpack';
import type {
Compilation,
Compiler,
LoaderContext as WebpackLoaderContext,
} from 'webpack';

export interface LoaderContext {
export interface LoaderContext<OptionsType = unknown> {
getOptions: WebpackLoaderContext<OptionsType>['getOptions'];
utils: WebpackLoaderContext<OptionsType>['utils'];
addDependency: (filePath: string) => void;
addContextDependency: (filePath: string) => void;
cacheable: (value: boolean) => void;
Expand Down
7 changes: 3 additions & 4 deletions packages/webpack-plugin/src/virtualFileLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { deserializeCss } from '@vanilla-extract/integration';
// @ts-expect-error
import { getOptions } from 'loader-utils';
import type { LoaderContext } from './types';

export default function (this: any): void {
const { source } = getOptions(this);
export default function (this: LoaderContext<{ source: string }>): void {
const { source } = this.getOptions();
const callback = this.async();

deserializeCss(source).then((deserializedCss) => {
Expand Down
Loading
Loading