Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prebundle PostCSS plugins for minifyCSS option #315

Merged
merged 4 commits into from
Sep 8, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Prebundle PostCSS plugins for `minifyCSS` option, to fix incompatibility with ESM ([#314](https://github.com/marp-team/marp-core/issues/314), [#315](https://github.com/marp-team/marp-core/pull/315))

## v3.3.2 - 2022-08-12

### Fixed
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-replace": "^4.0.0",
"@rollup/plugin-typescript": "^8.3.4",
"@types/jest": "^28.1.6",
"@typescript-eslint/eslint-plugin": "^5.33.0",
Expand All @@ -90,6 +91,9 @@
"mkdirp": "^1.0.4",
"nodemon": "^2.0.19",
"npm-run-all": "^4.1.5",
"postcss-minify-params": "^5.1.3",
"postcss-minify-selectors": "^5.2.1",
"postcss-normalize-whitespace": "^5.1.1",
"postcss-url": "^10.1.3",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
Expand All @@ -116,9 +120,6 @@
"markdown-it-emoji": "^2.0.2",
"mathjax-full": "^3.2.2",
"postcss": "^8.4.16",
"postcss-minify-params": "^5.1.3",
"postcss-minify-selectors": "^5.2.1",
"postcss-normalize-whitespace": "^5.1.1",
"postcss-selector-parser": "^6.0.10",
"twemoji": "^14.0.2",
"xss": "^1.0.13"
Expand Down
37 changes: 36 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import alias from '@rollup/plugin-alias'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import typescript from '@rollup/plugin-typescript'
import autoprefixer from 'autoprefixer'
import cssnano from 'cssnano'
Expand All @@ -19,7 +20,11 @@ const plugins = ({ browser = false } = {}) => [
entries: [
{
find: /^.+browser-script$/,
replacement: path.resolve(__dirname, './lib/browser.js'),
replacement: path.resolve(__dirname, 'lib/browser.js'),
},
{
find: /^.*prebundles[\\/]postcss-minify-plugins$/,
replacement: path.resolve(__dirname, 'tmp/postcss-minify-plugins.mjs'),
},
],
}),
Expand Down Expand Up @@ -53,10 +58,27 @@ const plugins = ({ browser = false } = {}) => [
!process.env.ROLLUP_WATCH && terser(),
]

const prebundlePlugins = () => [
alias({
entries: [
{
find: 'browserslist',
replacement: path.resolve(
__dirname,
'src/prebundles/mocks/browserslist.ts'
),
},
],
}),
...plugins(),
replace({ preventAssignment: true, __dirname: '""' }),
]

const external = (deps) => (id) =>
deps.some((dep) => dep === id || id.startsWith(`${dep}/`))

export default [
// Browser helpers
{
input: 'scripts/browser.js',
output: { file: 'lib/browser.js', format: 'iife' },
Expand All @@ -67,6 +89,19 @@ export default [
output: { exports: 'named', file: 'lib/browser.cjs.js', format: 'cjs' },
plugins: plugins({ browser: true }),
},

// Prebundles
{
input: `src/prebundles/postcss-minify-plugins.ts`,
output: {
exports: 'named',
file: 'tmp/postcss-minify-plugins.mjs',
format: 'es',
},
plugins: prebundlePlugins(),
},

// Main bundle
{
external: external(Object.keys(pkg.dependencies)),
input: `src/${path.basename(pkg.main, '.js')}.ts`,
Expand Down
12 changes: 2 additions & 10 deletions src/marp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Marpit, Options, ThemeSetPackOptions } from '@marp-team/marpit'
import highlightjs from 'highlight.js'
import postcss, { AcceptedPlugin } from 'postcss'
import postcssMinifyParams from 'postcss-minify-params'
import postcssMinifySelectors from 'postcss-minify-selectors'
import postcssNormalizeWhitespace from 'postcss-normalize-whitespace'
import defaultTheme from '../themes/default.scss'
import gaiaTheme from '../themes/gaia.scss'
import uncoverTheme from '../themes/uncover.scss'
Expand All @@ -12,6 +9,7 @@ import * as customElements from './custom-elements'
import * as emojiPlugin from './emoji/emoji'
import * as htmlPlugin from './html/html'
import * as mathPlugin from './math/math'
import minifyPlugins from './prebundles/postcss-minify-plugins'
import * as scriptPlugin from './script/script'
import * as sizePlugin from './size/size'

Expand Down Expand Up @@ -103,13 +101,7 @@ export class Marp extends Marpit {
const original = super.renderStyle(theme)
const postprocessorPlugins: AcceptedPlugin[] = [
customElements.css,
...(this.options.minifyCSS
? [
postcssNormalizeWhitespace,
postcssMinifyParams,
postcssMinifySelectors,
]
: []),
...(this.options.minifyCSS ? minifyPlugins : []),
]

const postprocessor = postcss(postprocessorPlugins)
Expand Down
6 changes: 6 additions & 0 deletions src/prebundles/mocks/browserslist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// postcss-minify-params is depending on browserslist to detect whether using
// IE. Marp does never use this detection so we will mock the module and return
// empty array. You can see the setting for pre-bundling in rollup.config.js.

/* istanbul ignore next */
export default () => []
9 changes: 9 additions & 0 deletions src/prebundles/postcss-minify-plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import postcssMinifyParams from 'postcss-minify-params'
import postcssMinifySelectors from 'postcss-minify-selectors'
import postcssNormalizeWhitespace from 'postcss-normalize-whitespace'

export default [
postcssNormalizeWhitespace,
postcssMinifyParams,
postcssMinifySelectors,
]
16 changes: 10 additions & 6 deletions test/marp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,17 +887,21 @@ function matchwo(a,b)
)

// Custom theme
const customTheme = '/* @theme a */ div { color: #f00; }'
const customTheme =
'/* @theme a */ @media screen \t and ( min-width : 768px ) { div { color: #f00; } }'

enabled.themeSet.add(customTheme)
disabled.themeSet.add(customTheme)

expect(disabled.render('<!-- theme: a -->').css).toContain(
'div { color: #f00; }'
)
expect(enabled.render('<!-- theme: a -->').css).toContain(
'div{color:#f00}'
const enabledCss = enabled.render('<!-- theme: a -->').css
const disabledCss = disabled.render('<!-- theme: a -->').css

expect(disabledCss).toContain(
'@media screen \t and ( min-width : 768px )'
)
expect(disabledCss).toContain('div { color: #f00; }')
expect(enabledCss).toContain('@media screen and (min-width:768px)')
expect(enabledCss).toContain('div{color:#f00}')
})

it('applies minifier by default', () => {
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,14 @@
is-module "^1.0.0"
resolve "^1.19.0"

"@rollup/plugin-replace@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-4.0.0.tgz#e34c457d6a285f0213359740b43f39d969b38a67"
integrity sha512-+rumQFiaNac9y64OHtkHGmdjm7us9bo1PlbgQfdihQtuNxzjpaB064HbRnewUOggLQxVCCyINfStkgmBeQpv1g==
dependencies:
"@rollup/pluginutils" "^3.1.0"
magic-string "^0.25.7"

"@rollup/plugin-typescript@^8.3.4":
version "8.3.4"
resolved "https://registry.yarnpkg.com/@rollup/plugin-typescript/-/plugin-typescript-8.3.4.tgz#45cdc0787b658b37d0362c705d8de86bc8bc040e"
Expand Down