diff --git a/packages/docusaurus-cssnano-preset/.npmignore b/packages/docusaurus-cssnano-preset/.npmignore index c912533dd7d2..16beccca2e07 100644 --- a/packages/docusaurus-cssnano-preset/.npmignore +++ b/packages/docusaurus-cssnano-preset/.npmignore @@ -1 +1,4 @@ +copyUntypedFiles.mjs +.tsbuildinfo* +tsconfig* __tests__ diff --git a/packages/docusaurus-cssnano-preset/index.js b/packages/docusaurus-cssnano-preset/index.js deleted file mode 100644 index cac7eda8999d..000000000000 --- a/packages/docusaurus-cssnano-preset/index.js +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -const advancedBasePreset = require('cssnano-preset-advanced'); -const postCssSortMediaQueries = require('postcss-sort-media-queries'); -const postCssRemoveOverriddenCustomProperties = require('./src/remove-overridden-custom-properties'); - -module.exports = function docusaurusCssnanoPreset(opts) { - const advancedPreset = advancedBasePreset({ - autoprefixer: {add: false}, - discardComments: {removeAll: true}, - ...opts, - }); - - advancedPreset.plugins.unshift( - [postCssSortMediaQueries], - [postCssRemoveOverriddenCustomProperties], - ); - - return advancedPreset; -}; diff --git a/packages/docusaurus-cssnano-preset/package.json b/packages/docusaurus-cssnano-preset/package.json index 35b39ae66833..0fa9229f07a9 100644 --- a/packages/docusaurus-cssnano-preset/package.json +++ b/packages/docusaurus-cssnano-preset/package.json @@ -2,11 +2,15 @@ "name": "@docusaurus/cssnano-preset", "version": "2.0.0-beta.20", "description": "Advanced cssnano preset for maximum optimization.", - "main": "index.js", + "main": "lib/index.js", "license": "MIT", "publishConfig": { "access": "public" }, + "scripts": { + "build": "tsc", + "watch": "tsc --watch" + }, "repository": { "type": "git", "url": "https://github.com/facebook/docusaurus.git", @@ -15,7 +19,8 @@ "dependencies": { "cssnano-preset-advanced": "^5.3.4", "postcss": "^8.4.13", - "postcss-sort-media-queries": "^4.2.1" + "postcss-sort-media-queries": "^4.2.1", + "tslib": "^2.4.0" }, "devDependencies": { "to-vfile": "^6.1.0" diff --git a/packages/docusaurus-cssnano-preset/src/deps.d.ts b/packages/docusaurus-cssnano-preset/src/deps.d.ts new file mode 100644 index 000000000000..f508fb9197d5 --- /dev/null +++ b/packages/docusaurus-cssnano-preset/src/deps.d.ts @@ -0,0 +1,11 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +declare module 'postcss-sort-media-queries' { + const plugin: import('postcss').PluginCreator; + export default plugin; +} diff --git a/packages/docusaurus-cssnano-preset/src/index.ts b/packages/docusaurus-cssnano-preset/src/index.ts new file mode 100644 index 000000000000..6bae42c6f8c7 --- /dev/null +++ b/packages/docusaurus-cssnano-preset/src/index.ts @@ -0,0 +1,27 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import advancedBasePreset from 'cssnano-preset-advanced'; +import postCssSortMediaQueries from 'postcss-sort-media-queries'; +import postCssRemoveOverriddenCustomProperties from './remove-overridden-custom-properties'; + +const preset: typeof advancedBasePreset = function preset(opts) { + const advancedPreset = advancedBasePreset({ + autoprefixer: {add: false}, + discardComments: {removeAll: true}, + ...opts, + }); + + advancedPreset.plugins.unshift( + [postCssSortMediaQueries, undefined], + [postCssRemoveOverriddenCustomProperties, undefined], + ); + + return advancedPreset; +}; + +export = preset; diff --git a/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/__snapshots__/index.test.js.snap b/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/__snapshots__/index.test.ts.snap similarity index 100% rename from packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/__snapshots__/index.test.js.snap rename to packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/__snapshots__/index.test.ts.snap diff --git a/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/index.test.js b/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/index.test.ts similarity index 78% rename from packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/index.test.js rename to packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/index.test.ts index b54446179796..8e50db5a74bd 100644 --- a/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/index.test.js +++ b/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/__tests__/index.test.ts @@ -5,12 +5,12 @@ * LICENSE file in the root directory of this source tree. */ -const path = require('path'); -const vfile = require('to-vfile'); -const postcss = require('postcss'); -const postCssRemoveOverriddenCustomProperties = require('../index'); +import path from 'path'; +import vfile from 'to-vfile'; +import postcss from 'postcss'; +import postCssRemoveOverriddenCustomProperties from '../index'; -const processFixture = (name) => { +const processFixture = (name: string) => { const input = vfile.readSync( path.join(__dirname, '__fixtures__', `${name}.css`), 'utf8', diff --git a/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/index.js b/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/index.ts similarity index 69% rename from packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/index.js rename to packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/index.ts index 51eb53b35327..a7dbba88f3ce 100644 --- a/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/index.js +++ b/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/index.ts @@ -5,6 +5,10 @@ * LICENSE file in the root directory of this source tree. */ +import type {Plugin, Rule, Node} from 'postcss'; + +const isRule = (node: Node | undefined): node is Rule => node?.type === 'rule'; + /** * This PostCSS plugin will remove duplicate/same custom properties (which are * actually overridden ones) **only** from `:root` selector. @@ -17,32 +21,31 @@ * applied). * - If the same custom properties have at least one `!important` rule, then * only those properties that do not have this rule will be removed. - * @returns {import('postcss').Plugin} */ -module.exports = function creator() { +function creator(): Plugin { return { postcssPlugin: 'postcss-remove-overridden-custom-properties', Declaration(decl) { - if (decl.parent.selector !== ':root') { + if (!isRule(decl.parent) || decl.parent.selector !== ':root') { return; } const sameProperties = decl.parent.nodes.filter( - (n) => n.prop === decl.prop, + (n) => 'prop' in n && n.prop === decl.prop, ); - const hasImportantProperties = sameProperties.some((p) => - Object.prototype.hasOwnProperty.call(p, 'important'), + const hasImportantProperties = sameProperties.some( + (p) => 'important' in p, ); const overriddenProperties = hasImportantProperties - ? sameProperties.filter( - (p) => !Object.prototype.hasOwnProperty.call(p, 'important'), - ) + ? sameProperties.filter((p) => !('important' in p)) : sameProperties.slice(0, -1); overriddenProperties.map((p) => p.remove()); }, }; -}; +} + +creator.postcss = true as const; -module.exports.postcss = true; +export default creator; diff --git a/packages/docusaurus-cssnano-preset/tsconfig.json b/packages/docusaurus-cssnano-preset/tsconfig.json new file mode 100644 index 000000000000..f5902ba1089b --- /dev/null +++ b/packages/docusaurus-cssnano-preset/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo", + "rootDir": "src", + "outDir": "lib" + } +}