Can I create my own npm package script that targets a file of my NEXT.js project? - babeljs

For quicker development, I am interested in exposing one of the files of my NEXT.js project as an npm package script. This enables consuming that code in a simple way from the terminal, orchestrate maintenance tasks, etc.
Let's assume the file:
import { Something } from 'some_library';
console.log("Some task to be performed");
And in my package.json:
"scripts": {
"dev": "next dev",
...
"mine": "execute my file" < HERE
},
My first intuition would be calling node directly over my file in the mine task, but this would fail with the following error:
SyntaxError: Cannot use import statement outside a module
I understand the babel transpilation needs to be performed before calling my file, activity that I assume is performed automatically by the dev command.
Is there any way in which I could have my own task that targets code that could be used directly in NEXT.js?. Is there any way to trigger the transpilation and after execute my file?.

Your package is not part of Next.js project and won't be transpilled by Next.js.
You can create a command (npm scripts) that calls Babel to transpile the package and then executes it but it's just confusing. Next.js projects don't include Babel as a root dependency by default, so your package would rely on a module that doesn't explicitly included.
Instead, make the package ready to use in production. Install Babel or another compiler as a dev tool in the package project. Before publishing the package, transpile it to a file that Node.js can execute.
In another words distribute executable files (the package itself) and not just a source of code of the package.

Related

Fix for "Package manager key paket was not registered" in build.fsx

When I open a Fake build script in Visual Studio Code, like this:
> dotnet new -i fake-template
> dotnet new fake
> dotnet tool update fake-cli
> code build.fsx
I see this error message in the editor and none of the Fake namespaces, modules, or types are defined:
Package manager key 'paket' was not registered in
c:\Users\wallace.kelly\.vscode\extensions\ionide.ionide-fsharp-5.4.0\bin\
Currently registered: nuget
How do I correct this error?
Running dotnet fake build works fine. The error just appears in the editor.
I have "FSharp.dotNetRoot": "C:\\Program Files\\dotnet\\sdk", in my Settings file. That folder includes folders 2.1.701 2.2.401 3.1.407 5.0.103 and 5.0.104.
maintainer of Ionide for VS Code here. That's error's just going to happen from here on out. FAKE is behind in its versions of the FSharp.Compiler.Services, and without updates to that component the integration we have with FAKE has atrophied to the point where the editor shows errors in that script.
There are a few solutions:
continue to use FAKE and just deal with editor issues
stop using FAKE as the script runner, but continue to use FAKE libraries from a build project or 'standard' fsx script
use another build system of some kind
contribute updates to FAKE (and by extension the deprecated code in Ionide)
etc etc.
I just got rid of the warning by using a ifdef.
#if FAKE
#r "paket:
nuget Fake.DotNet.Cli
nuget Fake.IO.FileSystem
nuget Fake.Core.Target //"
#endif
#load ".fake/build.fsx/intellisense.fsx"
open Fake.Core
open Fake.DotNet
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.Core.TargetOperators
Target.initEnvironment ()
Target.create "Clean" (fun _ ->
!! "src/**/bin"
++ "src/**/obj"
|> Shell.cleanDirs
)
Target.create "Build" (fun _ ->
!! "src/**/*.*proj"
|> Seq.iter (DotNet.build id)
)
Target.create "All" ignore
"Clean"
==> "Build"
==> "All"
Target.runOrDefault "All"
Following up on Chester's answer, I've opted to use FAKE libraries in a build console project. This is sometimes referred to as the build.fsproj pattern. It's actually straightforward to convert your script to this pattern, and I've found the debugging and tooling support to be much better. Unfortunately at the time of writing there's no official documentation of this pattern.
In essence you need to:
Create a new fsharp console project (build.fsproj)
Put your existing build script in the project (build.fs)
Include your dependencies in the project using paket or nuget
Add an [<EntryPoint>] to your script
Call your project using dotnet run. Probably from a build.cmd or build.sh file
The best examples I've found to work from are:
MiniScaffold
In particular the /build/ directory
SAFE-Dojo
They even have some documentation: Add build automation
I ended up including the helper.fs file from SAFE-Dojo as it made the conversion super easy. Basically the script ends up being:
//
// Existing FAKE script open library calls go here
//
open Helpers
initializeContext()
//
// Existing FAKE script body goes here
//
[<EntryPoint>]
let main args = runOrDefault args
MiniScaffold has nice build.cmd and build.sh scripts. Here's the build.cmd:
echo Restoring dotnet tools...
dotnet tool restore
dotnet run --project ./build/build.fsproj -- -t %*
If you've got an existing script that uses environment variables then the -- -t %* portion is superflous.
I first saw the approach mentioned in this tweet.

How to install and Implement the HtmlRenderer.PdfSharp in Azure Webapp (api)

Our development team implemented Pdfsharp in order to generate the pdf at website
they installed the pdfsharp package in a local environment with using this command at VisualStudio "Install-Package HtmlRenderer.PdfSharp -Version 1.5.0.5" it working fine
comes to production environment code is deployed through Azure DevOps CI/CD
when the build process creation we are implemented Nuget, NPM, and other steps.
now we added same line command in with .Net-CLI " add package HtmlRenderer.PdfSharp --version 1.5.0.5"-----> when I doing like it through the error message
Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1
Dotnet command failed with non-zero exit code on the following projects :
I tried to install a pdfsharp package directly in webapp (API) using PowerShell
but I don't have an idea about installing the pdfshap in backend server level is it the right way or not is still ??
Please see the overview on how to use packages.
In short, you only install/add the package to the project once. This modifies the project, and you save the modified csproj into source control. Then on the CI machine, you run dotnet restore on your solution (if your solution contains only SDK style projects, nuget restore otherwise).
And for future reference, telling us that dotnet.exe returned exit code 1 is insufficient to understand the problem. dotnet.exe's output will contain additional error information about why it failed.

How to customize babel 6 plugin/preset root

I have one cli project and one boilerplate project. Now I want to install all babel plugins and presets inside the cli, and use the cli to transpile the boilerplate.
The problem is, I'd like to run cli commands in boilerplate directory, and babel always looks for plugins/presets from boilerplate/node_modules instead of cli/node_modules.
How can I config babel to search only cli/node_modules?
I've tried to set sourceRoot and moduleRoot, but neither work.
You can explicitly pass the resolved plugins, e.g.
transform(code, {
preset: [require('babel-preset-es2015')],
});

Using coffeescript with basic Yeoman project.

I've used Yeoman to make a quick project skeleton using the yo webapp generator command. In the resulting Gruntfile I see that it's setup to compile CoffeeScript but it seems like its just sticking compiled files in a tmp folder.
coffee: {
dist: {
files: {
'.tmp/scripts/coffee.js': '<%= yeoman.app %>/scripts/*.coffee'
}
},
},
How do these get included in the project during development. I'm not using RequireJS.
The yeoman docs are unclear on how to use coffeescript. They only mention that it gets automatically compiled.
Using yeomen 1.0.0-rc1.4. I use:
$ yo angular --coffee
The resulting project has controller and app scripts in CoffeeScript.
grunt configuration file remains in js (what is not really a problem).
Running
$ grunt test
runs tests and all seems fine.
$ grunt server
is also doing what one expects (build the app, test it, starts server, opens the app in web browser and starts watching for changes, so if I change a coffee script file, it is quickly reflected in the web broser.
Documentation also states, one can use yo to add particular pieces like
angular:controller
angular:directive
angular:filter
angular:route
angular:service
angular:decorator
angular:view
each can be called with a --coffee switch and get the script in CoffeeScript, e.g.:
yo angular:controller user --coffee
I just found an issue in the github repo referencing this problem. https://github.com/yeoman/generator-webapp/issues/12
It offers a temporary solution: https://github.com/yeoman/generator-webapp/issues/12#issuecomment-13731929

What is the deployment workflow if using CoffeeScript and an IDE for developing a web application?

I've just picked up CoffeeScript and I'm struggling to understand the deployment workflow. It seems you constantly have to compile the .coffee files before using them. (Yes, I'm aware that you can have it embedded in the browser, but that's not recommended for production applications).
Does one have to constantly (manually) compile the files before deploying? (For example, if using Eclipse, a simple Ctrl+S saves and deploys the .war/.ear on the local machine's server.) Do we have to change the build scripts (for a central, possible CI server) for deploying .coffee files? Is there anyway to have integrated compiling via the IDEs (Eclipse/Netbeans)
Any ideas/pointers/examples on this? How/what have you used in the past?
I call browserify in my Cakefile to pre-compile and package my CoffeeScript for the browser. For an example of how I call browserify as well as coffeedoc and coffeedoctest take a look at the Cakefile for my Lumenize project.
If you are using express or some other node based server, you can have your CoffeeScript compiled at request time, using tools like NibJS or as described in The Little Book on CoffeeScript (Applications chapter), you can use Stitch. BTW, I highly recommend, The Little Book. The "Compiling" chapter has information about Cake and compiling that might help you.
Yes, you should have a build script. Most CoffeeScript projects use a Cakefile for this; see, for example, 37signals' pow. With a Cakefile, you can just run
cake build
from the command line to run the build task in the Cakefile.
You can run the Cakefile on a CI server, assuming that you have Node and CoffeeScript installed on that server.
Don't deploy the coffee files, use something like "coffee -cwj" to constantly watch and compile the .coffee files into javascript (.js) files and deploy those.
The options are c=compile, w=watch and j=join the files.
See the coffee-script web site for details of the options you can pass in.