Skip to content

added script to upload vanilla .svelte components. #31

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"module": "dist/index.mjs",
"main": "dist/index.js",
"scripts": {
"postbuild": "node script.js",
"build": "rollup -c",
"prepublishOnly": "npm run build"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^6.0.0",
"glob": "^7.1.6",
"rollup": "^1.20.0",
"rollup-plugin-svelte": "^5.0.0",
"svelte": "^3.0.0"
"svelte": "^3.0.0",
"svelte-preprocess": "^4.1.1"
},
"keywords": [
"svelte"
Expand Down
54 changes: 54 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const svelte = require("svelte/compiler");
const autoprocessor = require("svelte-preprocess");
const fs = require("fs");
const path = require("path");
const glob = require("glob");

const runCommand = async () => {
// source file paths
const srcPath = path.join(__dirname, "src");

// read glob of files in directory
glob(path.join(srcPath, "**/*.svelte"), {}, (error, files) => {
// handling error
if (error) {
return console.log("Unable to scan directory: " + error);
}
// listing all files using forEach
files.forEach(async (file) => {
// load file
const sourceFile = fs.readFileSync(file, "utf-8");
const distFile = file.replace("/src/", "/dist/");

// process .svelte file
if (file.includes(".svelte")) {
// run autopreprocessor
await parseSvelte(sourceFile, distFile);
}
// copy other files
else {
// copy static files
fs.copyFileSync(file, distFile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just copying other files will not work if they need preprocessing, too, like TS files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The svelte pre-processor can handle the .svelte files, i was thinking of using the typescript plugin for rollup to compile the .ts files after the preprocessing. I can just get rid of the copy file logic to prevent the .ts files from going into the output folder.

as an alternative, i could change the postbuild to a prebuild script so that the preprocessor can spit our compiled .svelte files and then have rollup bundle/minify the remaining .svelte and .ts files.

Copy link

@firefish5000 firefish5000 Sep 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note typescript (and maybe other) compiler can simply be called directly on the source with the output directory specified.
tsc --declaration --outDir dist
It will then compile the files over to the out directory. Done this way, I would say copying the files over is unnecessary We still need to copy though so non-preprocessed files can be imported, like plain js. Perhaps we should only preprocess svelte files in this script, and let any non-svelte files be handled separately afterwards (possibly by their native compilers).

}
});
});
};

const parseSvelte = async (source, destination) => {
await svelte
.preprocess(source, autoprocessor(), {
filename: path.basename(destination),
})
.then((item) => {
// create directory and file
fs.mkdirSync(path.dirname(destination), {
recursive: true,
});
// write compiled code to dist file
fs.writeFileSync(destination, item.code);
})
.catch((error) => {
console.error(error.message);
});
};
runCommand();