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

feat(sanitizer): remove jsdom from browser builds & add new commonjs bundle for node env #62

Merged
merged 2 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(sanitizer): remove jsdom from browser builds & add new commonjs …
…bundle for node env
  • Loading branch information
lyngai committed Dec 14, 2021
commit 4c0963370448188caef84ae160187d1755a91800
27 changes: 19 additions & 8 deletions build/rollup.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ import envReplacePlugin from './env';

const IS_PRODUCTION = process.env.NODE_ENV === 'production';
const PROJECT_ROOT_PATH = path.resolve(__dirname, '..');
/** 构建目标是否 node */
const IS_COMMONJS_BUILD = process.env.BUILD_TARGET === 'commonjs';

const aliasPluginOptions = {
entries: [
{
find: '@',
replacement: path.resolve(PROJECT_ROOT_PATH, 'src'),
},
],
};

if (IS_COMMONJS_BUILD) {
aliasPluginOptions.entries.unshift({
find: '@/Sanitizer',
replacement: path.resolve(PROJECT_ROOT_PATH, 'src', 'Sanitizer.node.js'),
});
}

export default {
input: 'src/index.js',
Expand All @@ -40,14 +58,7 @@ export default {
}),
json(),
envReplacePlugin(),
alias({
entries: [
{
find: '@',
replacement: path.resolve(PROJECT_ROOT_PATH, 'src'),
},
],
}),
alias(aliasPluginOptions),
resolve({
ignoreGlobal: false,
browser: true,
Expand Down
11 changes: 11 additions & 0 deletions build/rollup.core.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ if (!Array.isArray(options.external)) {
}
options.external.push('mermaid');

/** 构建目标是否 node */
const IS_COMMONJS_BUILD = process.env.BUILD_TARGET === 'commonjs';

if (IS_COMMONJS_BUILD) {
options.output = {
...options.output,
file: options.output.file.replace(/\.js$/, '.common.js'),
format: 'cjs',
};
}

export default options;
11 changes: 11 additions & 0 deletions build/rollup.engine.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ if (!Array.isArray(options.external)) {
}
options.external.push('mermaid');

/** 构建目标是否 node */
const IS_COMMONJS_BUILD = process.env.BUILD_TARGET === 'commonjs';

if (IS_COMMONJS_BUILD) {
options.output = {
...options.output,
file: options.output.file.replace(/\.js$/, '.common.js'),
format: 'cjs',
};
}

export default options;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
"scripts": {
"iconfont": "gulp",
"dev": "cross-env NODE_ENV=development rollup -w --config build/dev.js",
"build": "run-s clean build:all",
"build": "run-s clean build:all build:cjs",
"build:all": "run-p iconfont build:types build:addons build:full build:debug build:core build:engine",
"build:cjs": "run-p build:core-cjs build:engine-cjs",
"build:full": "cross-env NODE_ENV=production rollup --config build/build.js",
"build:core": "cross-env CORE_BUILD=true NODE_ENV=production rollup --config build/rollup.core.config.js",
"build:core-cjs": "cross-env BUILD_TARGET=commonjs CORE_BUILD=true NODE_ENV=production rollup --config build/rollup.core.config.js",
"build:engine": "cross-env CORE_BUILD=true NODE_ENV=production rollup --config build/rollup.engine.config.js",
"build:engine-cjs": "cross-env BUILD_TARGET=commonjs CORE_BUILD=true NODE_ENV=production rollup --config build/rollup.engine.config.js",
"build:debug": "cross-env NODE_ENV=development HOT_RELOAD=false rollup --config build/dev.js",
"build:addons": "tsc --project tsconfig.addons.json",
"build:types": "tsc --project tsconfig.json",
Expand Down
4 changes: 4 additions & 0 deletions src/Sanitizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import createDOMPurify from 'dompurify';

// for browser
export const sanitizer = createDOMPurify(window);
7 changes: 7 additions & 0 deletions src/Sanitizer.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import createDOMPurify from 'dompurify';
import { JSDOM } from 'jsdom';

const { window } = new JSDOM('');

// @ts-expect-error
export const sanitizer = createDOMPurify(window);
2 changes: 1 addition & 1 deletion src/core/HookCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class HookCenter {
}
} else {
hookName = /** @type {typeof SyntaxBase}*/ (HookClass).HOOK_NAME;
const config = syntax[hookName] ? syntax[hookName] : customSyntax[hookName] ? customSyntax[hookName] : {};
const config = syntax[hookName] || customSyntax[hookName] || {};
instance = new /** @type {typeof SyntaxBase}*/ (HookClass)({ externals, config, globalConfig: engine.global });
}
// Skip Internal Hook
Expand Down
12 changes: 1 addition & 11 deletions src/core/hooks/HtmlBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,15 @@
* limitations under the License.
*/
import ParagraphBase from '@/core/ParagraphBase';
import createDOMPurify from 'dompurify';
import {
whiteList,
convertHTMLNumberToName,
// isValidScheme, encodeURIOnce,
escapeHTMLEntitiesWithoutSemicolon,
} from '@/utils/sanitize';
import { JSDOM } from 'jsdom';
import { sanitizer } from '@/Sanitizer';
import { isBrowser } from '@/utils/env';

let sanitizer;

if (isBrowser()) {
sanitizer = createDOMPurify(window);
} else {
const { window } = new JSDOM('');
sanitizer = createDOMPurify(/** @type {any} */ (window));
}

export default class HtmlBlock extends ParagraphBase {
static HOOK_NAME = 'htmlBlock';
constructor() {
Expand Down