Skip to content

Commit 0dbc8b6

Browse files
committed
refactor: convert to ES Modulesand remove traces of CommonJS
BREAKING: The new project layout might break in some tooling setups. We've added an exports field to `package.json` to specify where statements like `import ... from 'docsify'` will import from, and left the `main` and `unpkg` fields as-is for backwards compatibility with the global <script> import method. Most people who use a non-module `<script>` tag to import Docsify will not notice a difference. Anyone else who is importing Docsify into a specilized build setup using `import` statements has a chance of being broken, so we've marked this as BREAKING.
1 parent 8d5d204 commit 0dbc8b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+166
-156
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
sourceType: 'module',
1313
ecmaVersion: 2019,
1414
},
15-
plugins: ['prettier', 'import'],
15+
plugins: ['prettier', 'import', 'importAssertions'],
1616
env: {
1717
browser: true,
1818
es6: true,

.vscode/launch.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
{
88
"type": "node",
99
"request": "launch",
10-
"name": "Run tests",
10+
"name": "Run unit tests",
1111
"runtimeExecutable": "npm",
12-
"runtimeArgs": ["run-script", "test"],
12+
"runtimeArgs": ["test"],
1313
"console": "integratedTerminal"
1414
},
1515
{
1616
"type": "node",
1717
"request": "launch",
1818
"name": "Run current test file",
19-
"runtimeExecutable": "npm",
20-
"runtimeArgs": ["run-script", "test"],
21-
"args": ["--", "-i", "${relativeFile}", "--testPathIgnorePatterns"],
19+
"runtimeExecutable": "npx",
20+
"runtimeArgs": ["jest"],
21+
"args": ["-i", "${relativeFile}", "--testPathIgnorePatterns"],
2222
"console": "integratedTerminal"
2323
},
2424
{
@@ -41,10 +41,9 @@
4141
"type": "node",
4242
"request": "launch",
4343
"name": "Update current test file snapshot(s)",
44-
"runtimeExecutable": "npm",
45-
"runtimeArgs": ["run-script", "test"],
44+
"runtimeExecutable": "npx",
45+
"runtimeArgs": ["jest"],
4646
"args": [
47-
"--",
4847
"-i",
4948
"${relativeFile}",
5049
"--updateSnapshot",
@@ -56,8 +55,8 @@
5655
"type": "node",
5756
"request": "launch",
5857
"name": "Update selected test name snapshot(s)",
59-
"runtimeExecutable": "npm",
60-
"runtimeArgs": ["run-script", "test"],
58+
"runtimeExecutable": "npx",
59+
"runtimeArgs": ["jest"],
6160
"args": [
6261
"--",
6362
"-i",

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
- [`develop` branch preview](https://docsify-preview.vercel.app/)
3333
- [Documentation](https://docsify.js.org)
3434
- [CLI](https://github.com/docsifyjs/docsify-cli)
35-
- CDN: [UNPKG](https://unpkg.com/docsify/) | [jsDelivr](https://cdn.jsdelivr.net/npm/docsify/) | [cdnjs](https://cdnjs.com/libraries/docsify)
35+
- CDN:
36+
- [UNPKG](https://unpkg.com/docsify/)
37+
- [jsDelivr](https://cdn.jsdelivr.net/npm/docsify/)
38+
- [cdnjs](https://cdnjs.com/libraries/docsify)
3639
- [Awesome docsify](https://github.com/docsifyjs/awesome-docsify)
3740
- [Community chat](https://discord.gg/3NwKFyR)
3841

build/build.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
const rollup = require('rollup')
2-
const buble = require('rollup-plugin-buble')
3-
const commonjs = require('rollup-plugin-commonjs')
4-
const nodeResolve = require('rollup-plugin-node-resolve')
5-
const { uglify } = require('rollup-plugin-uglify')
6-
const replace = require('rollup-plugin-replace')
7-
const isProd = process.env.NODE_ENV === 'production'
8-
const version = process.env.VERSION || require('../package.json').version
9-
const chokidar = require('chokidar')
10-
const path = require('path')
1+
import rollup from 'rollup';
2+
import buble from 'rollup-plugin-buble';
3+
import commonjs from 'rollup-plugin-commonjs';
4+
import nodeResolve from 'rollup-plugin-node-resolve';
5+
import { uglify } from 'rollup-plugin-uglify';
6+
import replace from 'rollup-plugin-replace';
7+
import chokidar from 'chokidar';
8+
import path from 'path';
9+
import pkg from '../package.json' assert { type: 'json' };
10+
const isProd = process.env.NODE_ENV === 'production';
11+
const version = process.env.VERSION || pkg.version;
1112

1213
/**
1314
* @param {{

build/cover.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
var fs = require('fs')
2-
var read = fs.readFileSync
3-
var write = fs.writeFileSync
4-
var version = process.env.VERSION || require('../package.json').version
1+
import fs from 'fs';
2+
import pkg from '../package.json' assert { type: 'json' };
3+
var read = fs.readFileSync;
4+
var write = fs.writeFileSync;
5+
var version = process.env.VERSION || pkg.version;
56

6-
var file = __dirname + '/../docs/_coverpage.md'
7-
var cover = read(file, 'utf8').toString()
7+
const relative = path => new URL(path, import.meta.url);
8+
var file = relative('../docs/_coverpage.md');
9+
var cover = read(file, 'utf8').toString();
810

9-
console.log('Replace version number in cover page...')
11+
console.log('Replace version number in cover page...');
1012
cover = cover.replace(
1113
/<small>(\S+)?<\/small>/g,
1214
'<small>' + version + '</small>'
13-
)
14-
write(file, cover)
15+
);
16+
write(file, cover);

build/css.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const {spawn} = require('child_process')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import {spawn} from 'child_process'
44

5+
const relative = path => new URL(path, import.meta.url);
56
const args = process.argv.slice(2)
6-
fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => {
7+
fs.readdir(relative('../src/themes'), (err, files) => {
78
if (err) {
89
console.error('err', err)
910
process.exit(1)

build/emoji.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const axios = require('axios');
2-
const fs = require('fs');
3-
const path = require('path');
1+
import axios from 'axios';
2+
import fs from 'fs';
3+
import path from 'path';
44

55
const filePaths = {
66
emojiMarkdown: path.resolve(process.cwd(), 'docs', 'emoji.md'),

build/mincss.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const cssnano = require('cssnano').process
2-
const path = require('path')
3-
const fs = require('fs')
1+
import cssnano from 'cssnano';
2+
import path from 'path'
3+
import fs from 'fs'
44

5-
files = fs.readdirSync(path.resolve('lib/themes'))
5+
const files = fs.readdirSync(path.resolve('lib/themes'))
66

77
files.forEach(file => {
88
file = path.resolve('lib/themes', file)
9-
cssnano(fs.readFileSync(file)).then(result => {
9+
cssnano.process(fs.readFileSync(file)).then(result => {
1010
fs.writeFileSync(file, result.css)
1111
}).catch(e => {
1212
console.error(e)

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
"name": "docsify",
33
"version": "4.13.0",
44
"description": "A magical documentation generator.",
5-
"author": {
6-
"name": "qingwei-li",
7-
"email": "[email protected]",
8-
"url": "https://github.com/QingWei-Li"
9-
},
105
"homepage": "https://docsify.js.org",
6+
"repository": "github:docsifyjs/docsify",
7+
"authors": "https://github.com/docsifyjs/docsify/graphs/contributors",
118
"license": "MIT",
12-
"repository": {
13-
"type": "git",
14-
"url": "git+https://github.com/docsifyjs/docsify.git"
9+
"collective": {
10+
"url": "https://opencollective.com/docsify"
1511
},
12+
"type": "module",
13+
"// The 'main' and 'unpkg' fields will remain as legacy for backwards compatbility for now. We will add deprectaion warnings to these outputs to give people time to see the warnings in their apps in a non-breaking way, and eventually we can remove the legacy stuff.": "",
1614
"main": "lib/docsify.js",
1715
"unpkg": "lib/docsify.min.js",
16+
"// We're using the 'exports' field as an override of the 'main' field to provide the new ESM setup. Once we remove legacy 'main', we will remove the 'exports' field and have a simple ESM setup with only a 'main' field.": "",
17+
"exports": {
18+
".": "./src/Docsify.js",
19+
"./*": "./*"
20+
},
1821
"files": [
1922
"lib",
2023
"themes"
@@ -117,8 +120,5 @@
117120
"documentation",
118121
"creator",
119122
"generator"
120-
],
121-
"collective": {
122-
"url": "https://opencollective.com/docsify"
123-
}
123+
]
124124
}

server.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const liveServer = require('live-server')
2-
const middleware = []
1+
import liveServer from 'live-server';
2+
const middleware = [];
33

44
const params = {
55
port: 3000,
66
watch: ['lib', 'docs', 'themes'],
7-
middleware
8-
}
7+
middleware,
8+
};
99

10-
liveServer.start(params)
10+
liveServer.start(params);

0 commit comments

Comments
 (0)