Skip to content

Commit

Permalink
Move platform specific eol to svgo-node entry point
Browse files Browse the repository at this point in the history
`os` package in js2svg module bothered me for a long time.
We had to hack rollup to mock it for browser.

Thanks to #1546 we now can pass eol from
svgo-node entry point and simplify build.
  • Loading branch information
TrySound committed Sep 11, 2021
1 parent e8321f0 commit 484d67b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
23 changes: 19 additions & 4 deletions lib/svgo-node.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
'use strict';

const os = require('os');
const fs = require('fs');
const path = require('path');
const {
extendDefaultPlugins,
optimize,
optimize: optimizeAgnostic,
createContentItem,
} = require('./svgo.js');

exports.extendDefaultPlugins = extendDefaultPlugins;
exports.createContentItem = createContentItem;

const importConfig = async (configFile) => {
const config = require(configFile);
if (config == null || typeof config !== 'object' || Array.isArray(config)) {
Expand Down Expand Up @@ -47,8 +51,19 @@ const loadConfig = async (configFile, cwd = process.cwd()) => {
dir = parent;
}
};

exports.loadConfig = loadConfig;
exports.extendDefaultPlugins = extendDefaultPlugins;

const optimize = (input, config) => {
if (typeof config !== 'object') {
throw Error('Config should be an object');
}
return optimizeAgnostic(input, {
...config,
js2svg: {
// specify platform specific default for end of line
eol: os.EOL === '\r\n' ? 'crlf' : 'lf',
...(config == null ? null : config.js2svg),
},
});
};
exports.optimize = optimize;
exports.createContentItem = createContentItem;
5 changes: 2 additions & 3 deletions lib/svgo/js2svg.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var platformEOL = require('os').EOL,
textElems = require('../../plugins/_collections.js').textElems;
const { textElems } = require('../../plugins/_collections.js');

var defaults = {
doctypeStart: '<!DOCTYPE',
Expand All @@ -28,7 +27,7 @@ var defaults = {
encodeEntity: encodeEntity,
pretty: false,
useShortTags: true,
eol: platformEOL === '\r\n' ? 'crlf' : 'lf',
eol: 'lf',
finalNewline: false,
};

Expand Down
8 changes: 0 additions & 8 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@ export default {
plugins: [
{
resolveId(importee, importer) {
if (importee === 'os') {
return importee;
}
// see https://github.com/csstree/csstree/pull/152
if (importee === 'css-tree') {
return this.resolve('css-tree/dist/csstree.min.js', importer);
}
},
load(id) {
if (id === 'os') {
return `export var EOL = '\\n'`;
}
},
},
nodeResolve({ browser: true, preferBuiltins: false }),
commonjs(),
Expand Down

0 comments on commit 484d67b

Please sign in to comment.