Skip to content

Commit

Permalink
fix import meta, included webpack to generate cjs version
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzyma committed Apr 14, 2020
1 parent 7455702 commit a4b219a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 17 deletions.
28 changes: 28 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -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())
File renamed without changes.
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -16,6 +20,9 @@
"keywords": [
"svgjs",
"dom",
"xml",
"xmldom",
"svgdom",
"nodejs"
],
"author": "Ulrich-Matthias Schäfer",
Expand All @@ -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",
Expand All @@ -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"
}
}
9 changes: 5 additions & 4 deletions src/utils/defaults.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
1 change: 1 addition & 0 deletions src/utils/dirname.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = __dirname
25 changes: 22 additions & 3 deletions webpack.config.cjs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,5 +36,6 @@ module.exports = {
// set the current working directory for displaying module paths
cwd: process.cwd()
})
]
],
target: 'node'
}

0 comments on commit a4b219a

Please sign in to comment.