diff --git a/index.cjs b/index.cjs new file mode 100644 index 0000000..efb2c31 --- /dev/null +++ b/index.cjs @@ -0,0 +1,28 @@ +const { createSVGWindow, setFontDir, setFontFamilyMappings } = require('./main-require.cjs') +const svgjs = require('../svg.js/dist/svg.node.js') + +const { SVG, registerWindow } = svgjs + +const window = createSVGWindow() +const document = window.document + +setFontDir('./fonts') +setFontFamilyMappings({ + Calibri2: 'calibri.ttf', + Arial2: 'arial.ttf', + Comic2: 'comic.ttf', + Coop2: 'COOPBL.TTF', + Finale2: 'FinaleCopyistText.ttf', + Free2: 'FREESCPT.TTF', + Georgia2: 'georgia.ttf' +}) + +registerWindow(window, document) + +const canvas = SVG(document.documentElement) + .size(2000, 1000) + .viewbox(-300, -300, 2000, 1000) + +canvas.rect(100, 100).move(200, 100).size(200) + +console.log(canvas.svg()) diff --git a/main.js b/main-module.js similarity index 100% rename from main.js rename to main-module.js diff --git a/package-lock.json b/package-lock.json index d9e5008..8f86dba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6425,12 +6425,6 @@ "async-limiter": "~1.0.0" } }, - "xmldom": { - "version": "0.1.27", - "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.27.tgz", - "integrity": "sha1-1QH5ezvbQDr4757MIFcxh6rawOk=", - "dev": true - }, "xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/package.json b/package.json index 8205b05..1a3a605 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,12 @@ "name": "svgdom", "version": "0.0.21", "description": "Straightforward DOM implementation to make SVG.js run headless on Node.js", + "main": "./main-require.cjs", + "exports": { + "import": "./main-module.js", + "require": "./main-require.cjs" + }, "type": "module", - "main": "index.js", "scripts": { "test": "mocha", "fix": "npx eslint ./ --fix", @@ -16,6 +20,9 @@ "keywords": [ "svgjs", "dom", + "xml", + "xmldom", + "svgdom", "nodejs" ], "author": "Ulrich-Matthias Schäfer", @@ -31,7 +38,7 @@ "sax": "^1.2.2" }, "devDependencies": { - "@svgdotjs/svg.js": "^3.0.6", + "@svgdotjs/svg.js": "^3.0.16", "acorn": "^7.1.1", "circular-dependency-plugin": "^5.2.0", "eslint": "^6.6.0", @@ -44,7 +51,6 @@ "puppeteer": "^1.9.0", "reify": "^0.12.3", "webpack": "^4.42.1", - "webpack-cli": "^3.3.11", - "xmldom": "^0.1.27" + "webpack-cli": "^3.3.11" } } diff --git a/src/utils/defaults.js b/src/utils/defaults.js index 042d042..615a54d 100644 --- a/src/utils/defaults.js +++ b/src/utils/defaults.js @@ -1,11 +1,12 @@ -import { dirname, join } from 'path' -import { fileURLToPath } from 'url' +import { join } from 'path' +import { default as __dirname } from './dirname.cjs' // eslint-disable-line -const __dirname = dirname(fileURLToPath(import.meta.url)); +// use this as soon as import.meta is standardized +// const __dirname = dirname(fileURLToPath(import.meta.url)); export const fontSize = 16 export const fontFamily = 'sans-serif' -export const fontDir = join(__dirname, '..', 'fonts/') +export const fontDir = join(__dirname, '../../', 'fonts/') export const fontFamilyMappings = { 'sans-serif': 'OpenSans-Regular.ttf', OpenSans: 'OpenSans-Regular.ttf' diff --git a/src/utils/dirname.cjs b/src/utils/dirname.cjs new file mode 100644 index 0000000..eb1b8e8 --- /dev/null +++ b/src/utils/dirname.cjs @@ -0,0 +1 @@ +module.exports = __dirname diff --git a/webpack.config.cjs b/webpack.config.cjs index cfe33ed..8e8eb07 100644 --- a/webpack.config.cjs +++ b/webpack.config.cjs @@ -1,9 +1,27 @@ // webpack.config.js const CircularDependencyPlugin = require('circular-dependency-plugin') -const path = require('path') +const fs = require('fs') + +const nodeModules = {} +fs.readdirSync('node_modules') + .filter(function (x) { + return [ '.bin' ].indexOf(x) === -1 + }) + .forEach(function (mod) { + nodeModules[mod] = 'commonjs ' + mod + }) module.exports = { - entry: './src/dom/Window.js', + mode: 'development', + entry: './main-module.js', + output: { + // library: 'svgdom', + libraryTarget: 'commonjs', + filename: './main-require.cjs', + path: __dirname + }, + externals: nodeModules, + devtool: 'inline-source-map', plugins: [ new CircularDependencyPlugin({ // exclude detection of files based on a RegExp @@ -18,5 +36,6 @@ module.exports = { // set the current working directory for displaying module paths cwd: process.cwd() }) - ] + ], + target: 'node' }