diff --git a/.changeset/drop-webpack-4.md b/.changeset/drop-webpack-4.md new file mode 100644 index 000000000..caad11b6a --- /dev/null +++ b/.changeset/drop-webpack-4.md @@ -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. diff --git a/packages/webpack-plugin/package.json b/packages/webpack-plugin/package.json index 1f679f91c..9b4bec72d 100644 --- a/packages/webpack-plugin/package.json +++ b/packages/webpack-plugin/package.json @@ -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": { diff --git a/packages/webpack-plugin/src/compat.ts b/packages/webpack-plugin/src/compat.ts deleted file mode 100644 index 7499554c9..000000000 --- a/packages/webpack-plugin/src/compat.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { - Compiler, - ExternalsPlugin, - Compilation, - Module, - node, - optimize, -} from 'webpack'; - -export interface WebpackCompat { - isWebpack5: boolean; - getNodeTemplatePlugin: (compiler: Compiler) => typeof node.NodeTemplatePlugin; - getNodeTargetPlugin: (compiler: Compiler) => typeof node.NodeTargetPlugin; - getLimitChunkCountPlugin: ( - compiler: Compiler, - ) => typeof optimize.LimitChunkCountPlugin; - getExternalsPlugin: (compiler: Compiler) => typeof ExternalsPlugin; - isModuleUsed: (compilation: Compilation, module: Module) => boolean; -} - -const webpack4: WebpackCompat = { - isWebpack5: false, - getNodeTemplatePlugin: () => require('webpack/lib/node/NodeTemplatePlugin'), - getNodeTargetPlugin: () => require('webpack/lib/node/NodeTargetPlugin'), - getLimitChunkCountPlugin: () => - require('webpack/lib/optimize/LimitChunkCountPlugin'), - getExternalsPlugin: () => require('webpack/lib/ExternalsPlugin'), - isModuleUsed: (_compilation, module) => - typeof module.used === 'boolean' ? module.used : true, -}; - -const webpack5: WebpackCompat = { - isWebpack5: true, - getNodeTemplatePlugin: (compiler) => compiler.webpack.node.NodeTemplatePlugin, - getNodeTargetPlugin: (compiler) => compiler.webpack.node.NodeTargetPlugin, - getLimitChunkCountPlugin: (compiler) => - compiler.webpack.optimize.LimitChunkCountPlugin, - getExternalsPlugin: (compiler) => compiler.webpack.ExternalsPlugin, - isModuleUsed: (compilation, module) => { - const exportsInfo = compilation.moduleGraph.getExportsInfo(module); - - return exportsInfo.isModuleUsed('main'); - }, -}; - -export default (isWebpack5: boolean): WebpackCompat => { - if (isWebpack5) { - return webpack5; - } - return webpack4; -}; diff --git a/packages/webpack-plugin/src/compiler.ts b/packages/webpack-plugin/src/compiler.ts index aca7f84cb..18edf04b0 100644 --- a/packages/webpack-plugin/src/compiler.ts +++ b/packages/webpack-plugin/src/compiler.ts @@ -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`. @@ -75,11 +74,6 @@ function compileVanillaSource( externals: Externals | undefined, ): Promise { 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 @@ -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', [ @@ -140,23 +120,8 @@ 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); @@ -164,11 +129,11 @@ function compileVanillaSource( // 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) => { diff --git a/packages/webpack-plugin/src/loader.ts b/packages/webpack-plugin/src/loader.ts index 71302b779..c13ef1489 100644 --- a/packages/webpack-plugin/src/loader.ts +++ b/packages/webpack-plugin/src/loader.ts @@ -1,6 +1,4 @@ import path from 'path'; -// @ts-expect-error -import loaderUtils from 'loader-utils'; import { getPackageInfo, type IdentifierOption, @@ -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, + source: string, +): void { + const { identifiers } = this.getOptions(); const { name } = getPackageInfo(this.rootContext); @@ -71,9 +80,9 @@ export default function (this: LoaderContext, source: string): void { }); } -export function pitch(this: LoaderContext): void { +export function pitch(this: LoaderContext): void { const { childCompiler, outputCss, identifiers, virtualLoader } = - loaderUtils.getOptions(this) as InternalLoaderOptions; + this.getOptions(); const log = debug( `vanilla-extract:loader:${formatResourcePath(this.resourcePath)}`, @@ -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. diff --git a/packages/webpack-plugin/src/plugin.ts b/packages/webpack-plugin/src/plugin.ts index 1429650b0..9c6242d7b 100644 --- a/packages/webpack-plugin/src/plugin.ts +++ b/packages/webpack-plugin/src/plugin.ts @@ -5,7 +5,6 @@ import { import type { Compiler, RuleSetRule } from 'webpack'; import { ChildCompiler } from './compiler'; -import createCompat, { type WebpackCompat } from './compat'; import { createRequire } from 'node:module'; @@ -13,43 +12,24 @@ 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; + } + }, + ); }); } @@ -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, diff --git a/packages/webpack-plugin/src/types.ts b/packages/webpack-plugin/src/types.ts index 49a7e079c..a518e4ede 100644 --- a/packages/webpack-plugin/src/types.ts +++ b/packages/webpack-plugin/src/types.ts @@ -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 { + getOptions: WebpackLoaderContext['getOptions']; + utils: WebpackLoaderContext['utils']; addDependency: (filePath: string) => void; addContextDependency: (filePath: string) => void; cacheable: (value: boolean) => void; diff --git a/packages/webpack-plugin/src/virtualFileLoader.ts b/packages/webpack-plugin/src/virtualFileLoader.ts index a251fb291..127c2ac83 100644 --- a/packages/webpack-plugin/src/virtualFileLoader.ts +++ b/packages/webpack-plugin/src/virtualFileLoader.ts @@ -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) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 19acb483b..6bb1112e7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -552,9 +552,6 @@ importers: debug: specifier: ^4.3.1 version: 4.4.3(supports-color@9.2.3) - loader-utils: - specifier: ^2.0.0 - version: 2.0.2 picocolors: specifier: ^1.0.0 version: 1.1.1 @@ -725,9 +722,6 @@ importers: '@parcel/core': specifier: ^2.15.0 version: 2.16.4 - '@types/mini-css-extract-plugin': - specifier: ^1.2.2 - version: 1.4.3(@swc/core@1.15.8)(esbuild@0.28.0)(webpack-cli@6.0.1) '@types/webpack-dev-server': specifier: ^3.11.1 version: 3.11.6 @@ -4333,9 +4327,6 @@ packages: '@types/mime@1.3.2': resolution: {integrity: sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==} - '@types/mini-css-extract-plugin@1.4.3': - resolution: {integrity: sha512-jyOSVaF4ie2jUGr1uohqeyDrp7ktRthdFxDKzTgbPZtl0QI5geEopW7UKD/DEfn0XgV1KEq/RnZlUmnrEAWbmg==} - '@types/minimatch@3.0.5': resolution: {integrity: sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==} @@ -14663,17 +14654,6 @@ snapshots: '@types/mime@1.3.2': {} - '@types/mini-css-extract-plugin@1.4.3(@swc/core@1.15.8)(esbuild@0.28.0)(webpack-cli@6.0.1)': - dependencies: - '@types/node': 22.15.3 - tapable: 2.3.0 - webpack: 5.104.0(@swc/core@1.15.8)(esbuild@0.28.0)(webpack-cli@6.0.1) - transitivePeerDependencies: - - '@swc/core' - - esbuild - - uglify-js - - webpack-cli - '@types/minimatch@3.0.5': {} '@types/minimist@1.2.2': {} diff --git a/test-helpers/package.json b/test-helpers/package.json index 03b33068d..4ac65acb1 100644 --- a/test-helpers/package.json +++ b/test-helpers/package.json @@ -19,7 +19,6 @@ "@fixtures/unused-modules": "workspace:*", "@parcel/config-default": "^2.15.0", "@parcel/core": "^2.15.0", - "@types/mini-css-extract-plugin": "^1.2.2", "@types/webpack-dev-server": "^3.11.1", "@vanilla-extract/esbuild-plugin": "workspace:*", "@vanilla-extract/esbuild-plugin-next": "workspace:*",