Browse Source

Add CJS build fallback

pull/979/head
Vitaly Puzrin 1 year ago
parent
commit
7f0f211a20
  1. 10
      package.json
  2. 41
      support/rollup.config.mjs
  3. 10
      test/cjs.js

10
package.json

@ -11,12 +11,12 @@
], ],
"repository": "markdown-it/markdown-it", "repository": "markdown-it/markdown-it",
"license": "MIT", "license": "MIT",
"main": "dist/markdown-it.js", "main": "dist/markdown-it.cjs.js",
"module": "index.mjs", "module": "index.mjs",
"exports": { "exports": {
".": { ".": {
"import": "./index.mjs", "import": "./index.mjs",
"require": "./dist/markdown-it.js" "require": "./dist/markdown-it.cjs.js"
}, },
"./*": { "./*": {
"require": "./*", "require": "./*",
@ -28,16 +28,16 @@
}, },
"scripts": { "scripts": {
"lint": "eslint --ext js --ext mjs .", "lint": "eslint --ext js --ext mjs .",
"test": "npm run lint && c8 -r text -r html -r lcov mocha && node support/specsplit.mjs", "test": "npm run lint && CJS_ONLY=1 npm run build && c8 --exclude dist --exclude test -r text -r html -r lcov mocha && node support/specsplit.mjs",
"doc": "node support/build_doc.mjs", "doc": "node support/build_doc.mjs",
"gh-doc": "npm run doc && gh-pages -d apidoc -f", "gh-doc": "npm run doc && gh-pages -d apidoc -f",
"demo": "npm run lint && node support/build_demo.mjs", "demo": "npm run lint && node support/build_demo.mjs",
"gh-demo": "npm run demo && gh-pages -d demo -f -b master -r git@github.com:markdown-it/markdown-it.github.io.git", "gh-demo": "npm run demo && gh-pages -d demo -f -b master -r git@github.com:markdown-it/markdown-it.github.io.git",
"build": "npm run lint && rollup -c support/rollup.config.mjs", "build": "rollup -c support/rollup.config.mjs",
"benchmark-deps": "npm install --prefix benchmark/extra/ -g marked@0.3.6 commonmark@0.26.0 markdown-it/markdown-it.git#2.2.1", "benchmark-deps": "npm install --prefix benchmark/extra/ -g marked@0.3.6 commonmark@0.26.0 markdown-it/markdown-it.git#2.2.1",
"specsplit": "support/specsplit.mjs good -o test/fixtures/commonmark/good.txt && support/specsplit.mjs bad -o test/fixtures/commonmark/bad.txt && support/specsplit.mjs", "specsplit": "support/specsplit.mjs good -o test/fixtures/commonmark/good.txt && support/specsplit.mjs bad -o test/fixtures/commonmark/bad.txt && support/specsplit.mjs",
"todo": "grep 'TODO' -n -r ./lib 2>/dev/null", "todo": "grep 'TODO' -n -r ./lib 2>/dev/null",
"prepublishOnly": "npm run build && npm run gh-demo && npm run gh-doc" "prepublishOnly": "npm test && npm run build && npm run gh-demo && npm run gh-doc"
}, },
"files": [ "files": [
"index.mjs", "index.mjs",

41
support/rollup.config.mjs

@ -1,12 +1,11 @@
import nodeResolve from '@rollup/plugin-node-resolve' import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import terser from '@rollup/plugin-terser' import terser from '@rollup/plugin-terser'
import { babel } from '@rollup/plugin-babel' import { babel } from '@rollup/plugin-babel'
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url))) const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url)))
export default { const config_umd_full = {
input: 'index.mjs', input: 'index.mjs',
output: [ output: [
{ {
@ -18,12 +17,7 @@ export default {
terser({ terser({
mangle: false, mangle: false,
compress: false, compress: false,
format: { format: { comments: 'all', beautify: true, ascii_only: true, indent_level: 2 }
comments: 'all',
beautify: true,
ascii_only: true,
indent_level: 2
}
}) })
] ]
}, },
@ -33,16 +27,13 @@ export default {
name: 'markdownit', name: 'markdownit',
plugins: [ plugins: [
terser({ terser({
format: { format: { ascii_only: true }
ascii_only: true
}
}) })
] ]
} }
], ],
plugins: [ plugins: [
nodeResolve({ preferBuiltins: true }), resolve(),
commonjs(),
babel({ babelHelpers: 'bundled' }), babel({ babelHelpers: 'bundled' }),
{ {
banner () { banner () {
@ -51,3 +42,25 @@ export default {
} }
] ]
} }
const config_cjs_no_deps = {
input: 'index.mjs',
output: {
file: 'dist/markdown-it.cjs.js',
format: 'cjs'
},
external: Object.keys(pkg.dependencies),
plugins: [
resolve(),
babel({ babelHelpers: 'bundled' })
]
}
let config = [
config_umd_full,
config_cjs_no_deps
]
if (process.env.CJS_ONLY) config = [config_cjs_no_deps]
export default config

10
test/cjs.js

@ -0,0 +1,10 @@
'use strict'
const assert = require('assert')
const md = require('../')()
describe('CJS', () => {
it('require', () => {
assert.strictEqual(md.render('abc'), '<p>abc</p>\n')
})
})
Loading…
Cancel
Save