Skip to content

Commit

Permalink
bundle bin to avoid bs-platform build on postinstall + artifacts issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MoOx committed Apr 14, 2020
1 parent c62989f commit ed6262c
Show file tree
Hide file tree
Showing 17 changed files with 1,889 additions and 141 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ package-lock.json
**/lib/ocaml
**/.merlin
*.bs.js

# webpack build
dist
5 changes: 0 additions & 5 deletions __tests__/Test.proxy.js

This file was deleted.

12 changes: 6 additions & 6 deletions __tests__/Test.re
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ let testAll = svg => {
};

describe("simple svg", () =>
readFileSync("./test/simple.svg", `utf8)->testAll
readFileSync("./__tests__/fixtures/simple.svg", `utf8)->testAll
);

describe("edge case width svg", () =>
readFileSync("./test/edge-case-width.svg", `utf8)->testAll
readFileSync("./__tests__/fixtures/edge-case-width.svg", `utf8)->testAll
);

describe("clean & minimal svg", () =>
readFileSync("./test/clean.svg", `utf8)->testAll
readFileSync("./__tests__/fixtures/clean.svg", `utf8)->testAll
);

describe("sketch export", () =>
readFileSync("./test/sketch-export.svg", `utf8)->testAll
readFileSync("./__tests__/fixtures/sketch-export.svg", `utf8)->testAll
);

describe("with fill", () =>
readFileSync("./test/with-fill.svg", `utf8)->testAll
readFileSync("./__tests__/fixtures/with-fill.svg", `utf8)->testAll
);

describe("with stroke", () =>
readFileSync("./test/with-stroke.svg", `utf8)->testAll
readFileSync("./__tests__/fixtures/with-stroke.svg", `utf8)->testAll
);
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
78 changes: 1 addition & 77 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,3 @@
#!/usr/bin/env node
"use strict";
const meow = require("meow");

// UPDATE README IF YOU UPDATE THIS PLEASE
const cli = meow(
`
Usage
$ react-from-svg <sourcePath> <outputPath> [--with-native|--with-web]
Options
--with-native, -native Output code for react-native-svg
--with-web, -web Output code for DOM. If --with-native is also used, will be output as .web.js files
--with-reason, -bs Output ReasonML bindings code
--remove-fill, -rf Remove all 'fill' properties from SVGs, convenient for icons
--remove-stroke, -rs Remove all 'stroke' properties from SVGs, convenient for icons
--commonjs, -cjs Export as commonjs instead of es6 import/export
--bs-module-path, -bsp Allow to customise ReasonML output path
Example
$ react-from-svg assets/svgs src/Svgs --remove-fill
`,
{
flags: {
"with-native": { type: "boolean", alias: "native" },
"with-web": { type: "boolean", alias: "web" },
"with-reason": { type: "boolean", alias: "bs" },
"remove-fill": { type: "boolean", alias: "rf" },
"remove-stroke": { type: "boolean", alias: "rs" },
commonjs: { type: "boolean", alias: "cjs" },
"bs-module-path": { type: "string", alias: "bsp" },
},
},
);

if (cli.flags.withNative === undefined && cli.flags.withWeb === undefined) {
console.error(
"You should at least choose an option between --with-native or --with web!",
);
cli.showHelp();
process.exit(1);
}

///

let compiledArtifact = "./lib/js/src/Transformer.bs.js";
let transformer;
try {
transformer = require(compiledArtifact);
} catch (e) {
console.log(
"react-from-svg: Missing compiled " + compiledArtifact + ", rebuilding",
);
require("child_process").execSync("bsb -clean-world -make-world", {
cwd: __dirname,
stdio: "inherit",
});
}

setTimeout(function() {
try {
delete require.cache[compiledArtifact];
transformer = require(compiledArtifact);
} catch (e) {
throw new Error(
"react-from-svg: missing compiled script. You might have used a `bsb -clean-world` which is currently causing trouble to the way this bin is generated." +
"\n" +
"***************************************************************************************************************" +
"\n" +
"** Try running this command again, we tried to compile it asynchronously (should be ready as you read this). **" +
"\n" +
"***************************************************************************************************************" +
"\n" +
"If this issue persist, open an issue with as much as information as you can (bsconfig, etc) https://github.com/MoOx/react-from-svg/issues/new",
);
}

transformer.make(cli.input, cli.flags);
}, 0);
require("./dist/bin.js");
6 changes: 3 additions & 3 deletions bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"react-jsx": 3
},
"package-specs": {
"module": "commonjs"
// "in-source": true
"module": "commonjs",
"in-source": true
},
"suffix": ".bs.js",
"sources": [
Expand All @@ -20,6 +20,6 @@
"type": "dev"
}
],
"bs-dependencies": ["reason-future", "reason-react"],
"bs-dependencies": ["reason-future"],
"bs-dev-dependencies": ["@glennsl/bs-jest"]
}
27 changes: 15 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,40 @@
"react-from-svg": "./bin.js"
},
"files": [
"src",
"!test",
"bin.js",
"bsconfig.json"
"*",
"!.github",
"!.DS_Store",
"!**/*.bs.js",
"!.merlin",
"!lib/bs",
"!lib/ocaml"
],
"scripts": {
"postinstall": "bsb -clean-world -make-world",
"prepare": "bsb -clean-world -make-world && webpack",
"re:clean": "bsb -clean-world",
"re:build": "bsb -make-world",
"svg:format": "find test/components -name \"*.re\" | grep -v \"node_modules\" | xargs bsrefmt --in-place",
"tests": "jest \"__tests__/(.*).proxy.js\"",
"test": "yarn re:clean && yarn re:build && yarn tests",
"tests": "jest",
"test": "yarn re:clean && yarn re:build && yarn tests && webpack",
"release": "npmpub"
},
"dependencies": {
"bs-platform": "^7.2.2",
"camelcase": "^6.0.0",
"glob": "^7.1.3",
"meow": "^6.1.0",
"mkdirp": "^0.5.1",
"reason-future": "^2.4.0",
"reason-react": "^0.7.0"
"mkdirp": "^0.5.1"
},
"devDependencies": {
"@glennsl/bs-jest": "^0.5.1",
"bs-platform": "^7.2.2",
"husky": "^4.2.0",
"jest": "^25.3.0",
"lint-staged": "^10.0.2",
"npmpub": "^5.0.0",
"prettier": "^1.19.1",
"trash-cli": "^1.4.0"
"reason-future": "^2.4.0",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11"
},
"prettier": {
"trailingComma": "all",
Expand Down
42 changes: 42 additions & 0 deletions src/bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const meow = require("meow");

// UPDATE README IF YOU UPDATE THIS PLEASE
const cli = meow(
`
Usage
$ react-from-svg <sourcePath> <outputPath> [--with-native|--with-web]
Options
--with-native, -native Output code for react-native-svg
--with-web, -web Output code for DOM. If --with-native is also used, will be output as .web.js files
--with-reason, -bs Output ReasonML bindings code
--remove-fill, -rf Remove all 'fill' properties from SVGs, convenient for icons
--remove-stroke, -rs Remove all 'stroke' properties from SVGs, convenient for icons
--commonjs, -cjs Export as commonjs instead of es6 import/export
--bs-module-path, -bsp Allow to customise ReasonML output path
Example
$ react-from-svg assets/svgs src/Svgs --remove-fill
`,
{
flags: {
"with-native": { type: "boolean", alias: "native" },
"with-web": { type: "boolean", alias: "web" },
"with-reason": { type: "boolean", alias: "bs" },
"remove-fill": { type: "boolean", alias: "rf" },
"remove-stroke": { type: "boolean", alias: "rs" },
commonjs: { type: "boolean", alias: "cjs" },
"bs-module-path": { type: "string", alias: "bsp" },
},
},
);

if (cli.flags.withNative === undefined && cli.flags.withWeb === undefined) {
console.error(
"You should at least choose an option between --with-native or --with web!",
);
cli.showHelp();
process.exit(1);
}

require("./Transformer.bs.js").make(cli.input, cli.flags);
17 changes: 17 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require("path");

module.exports = {
mode: "production",
entry: "./src/bin.js",
output: {
filename: "bin.js",
libraryTarget: "commonjs",
},
target: "node",
externals: {
meow: "meow",
camelcase: "camelcase",
glob: "glob",
mkdirp: "mkdirp",
},
};
Loading

0 comments on commit ed6262c

Please sign in to comment.