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

+1-1
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

+9-10
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

+4-1
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

+11-10
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

+11-9
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

+5-4
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

+3-3
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

+5-5
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

+12-12
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

+5-5
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);

src/core/Docsify.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { VirtualRoutes } from './virtual-routes/index.js';
66
import initGlobalAPI from './global-api.js';
77

88
import config from './config.js';
9-
import { isFn } from './util/core';
10-
import { Lifecycle } from './init/lifecycle';
9+
import { isFn } from './util/core.js';
10+
import { Lifecycle } from './init/lifecycle.js';
1111

1212
/** @typedef {new (...args: any[]) => any} Constructor */
1313

src/core/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { merge, hyphenate, isPrimitive, hasOwn } from './util/core';
1+
import { merge, hyphenate, isPrimitive, hasOwn } from './util/core.js';
22

33
const currentScript = document.currentScript;
44

src/core/event/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { isMobile } from '../util/env';
2-
import { body, on } from '../util/dom';
3-
import * as sidebar from './sidebar';
4-
import { scrollIntoView, scroll2Top } from './scroll';
1+
import { isMobile } from '../util/env.js';
2+
import { body, on } from '../util/dom.js';
3+
import * as sidebar from './sidebar.js';
4+
import { scrollIntoView, scroll2Top } from './scroll.js';
55

66
/** @typedef {import('../Docsify').Constructor} Constructor */
77

src/core/event/scroll.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import Tweezer from 'tweezer.js';
2-
import { isMobile } from '../util/env';
3-
import * as dom from '../util/dom';
4-
import { removeParams } from '../router/util';
5-
import config from '../config';
2+
import { isMobile } from '../util/env.js';
3+
import * as dom from '../util/dom.js';
4+
import { removeParams } from '../router/util.js';
5+
import config from '../config.js';
66

77
const nav = {};
88
let hoverOver = false;

src/core/event/sidebar.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-unused-vars */
2-
import { isMobile } from '../util/env';
3-
import * as dom from '../util/dom';
2+
import { isMobile } from '../util/env.js';
3+
import * as dom from '../util/dom.js';
44

55
const title = dom.$.title;
66
/**

src/core/fetch/ajax.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-unused-vars */
2-
import progressbar from '../render/progressbar';
3-
import { noop, hasOwn } from '../util/core';
2+
import progressbar from '../render/progressbar.js';
3+
import { noop, hasOwn } from '../util/core.js';
44

55
const cache = {};
66

src/core/fetch/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable no-unused-vars */
2-
import { getParentPath, stringifyQuery } from '../router/util';
3-
import { noop, isExternal } from '../util/core';
4-
import { getAndActive } from '../event/sidebar';
5-
import { get } from './ajax';
2+
import { getParentPath, stringifyQuery } from '../router/util.js';
3+
import { noop, isExternal } from '../util/core.js';
4+
import { getAndActive } from '../event/sidebar.js';
5+
import { get } from './ajax.js';
66

77
function loadNested(path, qs, file, next, vm, first) {
88
path = first ? path : path.replace(/\/$/, '');

src/core/global-api.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import prism from 'prismjs';
22
import { marked } from 'marked';
3-
import * as util from './util';
4-
import * as dom from './util/dom';
5-
import { Compiler } from './render/compiler';
6-
import { slugify } from './render/slugify';
7-
import { get } from './fetch/ajax';
3+
import * as util from './util/index.js';
4+
import * as dom from './util/dom.js';
5+
import { Compiler } from './render/compiler.js';
6+
import { slugify } from './render/slugify.js';
7+
import { get } from './fetch/ajax.js';
88

99
// TODO This is deprecated, kept for backwards compatibility. Remove in next
1010
// major release. We'll tell people to get everything from the DOCSIFY global

src/core/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { documentReady } from './util/dom';
2-
import { Docsify } from './Docsify';
1+
import { documentReady } from './util/dom.js';
2+
import { Docsify } from './Docsify.js';
33

44
/**
55
* Run Docsify

src/core/init/lifecycle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noop } from '../util/core';
1+
import { noop } from '../util/core.js';
22

33
/** @typedef {import('../Docsify').Constructor} Constructor */
44

src/core/render/compiler.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { marked } from 'marked';
2-
import { isAbsolutePath, getPath, getParentPath } from '../router/util';
3-
import { isFn, merge, cached, isPrimitive } from '../util/core';
4-
import { tree as treeTpl } from './tpl';
5-
import { genTree } from './gen-tree';
6-
import { slugify } from './slugify';
7-
import { emojify } from './emojify';
2+
import { isAbsolutePath, getPath, getParentPath } from '../router/util.js';
3+
import { isFn, merge, cached, isPrimitive } from '../util/core.js';
4+
import { tree as treeTpl } from './tpl.js';
5+
import { genTree } from './gen-tree.js';
6+
import { slugify } from './slugify.js';
7+
import { emojify } from './emojify.js';
88
import {
99
getAndRemoveConfig,
1010
removeAtag,
1111
getAndRemoveDocisfyIgnorConfig,
12-
} from './utils';
13-
import { imageCompiler } from './compiler/image';
14-
import { highlightCodeCompiler } from './compiler/code';
15-
import { paragraphCompiler } from './compiler/paragraph';
16-
import { taskListCompiler } from './compiler/taskList';
17-
import { taskListItemCompiler } from './compiler/taskListItem';
18-
import { linkCompiler } from './compiler/link';
12+
} from './utils.js';
13+
import { imageCompiler } from './compiler/image.js';
14+
import { highlightCodeCompiler } from './compiler/code.js';
15+
import { paragraphCompiler } from './compiler/paragraph.js';
16+
import { taskListCompiler } from './compiler/taskList.js';
17+
import { taskListItemCompiler } from './compiler/taskListItem.js';
18+
import { linkCompiler } from './compiler/link.js';
1919

2020
const cachedLinks = {};
2121

@@ -115,6 +115,7 @@ export class Compiler {
115115
})(text);
116116

117117
const curFileName = this.router.parse().file;
118+
console.log('filename:', curFileName);
118119

119120
if (isCached) {
120121
this.toc = this.cacheTOC[curFileName];

src/core/render/compiler/code.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Prism from 'prismjs';
22
// See https://github.com/PrismJS/prism/pull/1367
3-
import 'prismjs/components/prism-markup-templating';
3+
import 'prismjs/components/prism-markup-templating.js';
44

55
export const highlightCodeCompiler = ({ renderer }) =>
66
(renderer.code = function (code, lang = 'markup') {

src/core/render/compiler/headline.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {
22
getAndRemoveConfig,
33
removeAtag,
44
getAndRemoveDocisfyIgnorConfig,
5-
} from '../utils';
6-
import { slugify } from './slugify';
5+
} from '../utils.js';
6+
import { slugify } from './slugify.js';
77

88
export const headingCompiler = ({ renderer, router, _self }) =>
99
(renderer.code = (text, level) => {

src/core/render/compiler/image.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getAndRemoveConfig } from '../utils';
2-
import { isAbsolutePath, getPath, getParentPath } from '../../router/util';
1+
import { getAndRemoveConfig } from '../utils.js';
2+
import { isAbsolutePath, getPath, getParentPath } from '../../router/util.js';
33

44
export const imageCompiler = ({ renderer, contentBase, router }) =>
55
(renderer.image = (href, title, text) => {

0 commit comments

Comments
 (0)