npm modules in Firefox-Addon content scripts - firefox

With jpm, one now (from Firefox 38 on) has the possibility to use npm modules in Firefox Add-ons, cf. the documentation on MDN.
This appears to be restricted to the main entry file (index.js by default), especially since require() isn't defined in content scripts.
Is there any way of using npm modules in content scripts for Firefox-Addons?

You can get the url of a file via require.resolve, so use something like:
contentScriptFile: [ require.resolve("something/lib/file.js"), require("sdk/self").data.url("content-script.js") ]
This will use a file from npm in a content script.
If the npm file is a CommonJS file then this becomes more tricky, you might have to read the content of the file, and pass that to the content script (using message passing or the contentScriptOptions) to have it evaluated with requireJS or something.

Related

How do I use my tsconfig file with ng-packagr?

I'm packaging an Angular 5 library with ng-packagr and I need to use my existing tsconfig.json. I've read the docs here https://github.com/ng-packagr/ng-packagr/blob/master/docs/override-tsconfig.md and it says I need to write a script (I was expecting an option in the config) and pass through the filepath to my tsconfig file.
I don't understand where I write this code? I guess this needs to be an NPM script? The documentation and example seems to be very minimal.
Help with this would be appreciated.
I did this by creating a build-package.ts file with the contents (but require not import) then did:
node build-package.ts.

PostCSS modules are loading from the wrong directory

I'm trying to get PostCSS CLI to work on Windows, to use with PHPStorm. I've got it working via command line options, but I want to use it with the javascript config file.
Here are the commands I used to install PostCSS through NPM:
# NPM is installed at PostCSS\node\npm. Commands are run in the PostCSS dir.
"node\npm" install --global postcss-cli # PostCSS\node\node_modules\postcss-cli\
"node\npm" install --global lost # PostCSS\node\node_modules\lost\
# and a few other modules...
So I've got about 6 modules are in the node/node_modules directory now.
I have this batch file (split into multiple lines for your viewing pleasure). This is in the PostCSS folder, just for testing:
"node\postcss" -u postcss-normalize
-u postcss-cssnext
-u postcss-import
-u lost
-o output.css
source.css
This batch file works, and all of the plugins work too.
My problem:
I want to use postcss.config.js, instead of command line parameters, so that I can customize the plugins easily and make it even more portable. I have dozens of separate projects that I will use the same settings for in different directories and computers. So to do that, I've got a new batch file called "run-from-config.bat" with the following content:
"node\postcss" -c "postcss.config.js"
-o output-configured.css
source.css
This new batch file is in the same folder as the command line version, along with the new file postcss.config.js:
module.exports = {
plugins: [
require('postcss-normalize')({
"browserslist": "last 2 versions" // I intend to add more options...
}),
require('postcss-cssnext')({}),
require('postcss-import')({}),
require('lost')({})
]
}
This follows the instructions on the PostCSS-CLI page under "Content": https://github.com/postcss/postcss-cli
But it gives me an error, and appears to be loading modules from the wrong location:
Processing source.css
{ Error: Cannot find module 'postcss-normalize'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (G:\Dropbox\Web Development\PostCSS\postcss.config.js:3:5)
at Module._compile (module.js:570:32)
at requireFromString (G:\Dropbox\Web Development\PostCSS\node\node_modules\postcss-cli\node_modules\require-from-string\index.js:27:4)
at G:\Dropbox\Web Development\PostCSS\node\node_modules\postcss-cli\node_modules\cosmiconfig\lib\loadJs.js:11:15 code: 'MODULE_NOT_FOUND' }
So it seems it can't find the module when using this javascript config file. But it can when loading plugins through the command line option -u. I simply don't know how to change the module directory.
I tried using some other options to change the directory, including -b and -d, but nothing seems to work. And I just don't understand PostCSS (or the underlying javascript software) enough to debug the problem on my own.

How transpile via IDE ES6 to ES5 (and React-JSX) with WebStorm on Win10 with Babel6?

A lot of sources explain that for this you need to
create a "File Watcher"-Job in the WebStorm-Settings (Tools)
define a "Scope" in WebStorm for the files you want to process
define a .babelrc file for configuration. Babel will use this automatically so you save some params in the call
{
"presets": ["es2015", "react"],
"plugins": ["transform-es2015-arrow-functions"]
}
npm install --save-dev the corresponding packages together with the babel-cli package.
BUT... how can I run the babel-Command on Windows when babel-cli module just delivers a "babel.js" file in its bin-folder? However Windows can only execute .exe, .bat or .cmd-files.
I tried to wrap the call in a cmd-script containing babel %* as I found a solution in one web article, but this did not work for me.
The solution for me was to fill the File Watcher form in a way to have the call of the node executable as Program and add the call for babel.js as first of the Arguments
Program: C:\Program Files\nodejs\node.exe
Arguments: $ProjectFileDir$/node_modules/babel-cli/bin/babel.js $FilePathRelativeToProjectRoot$ --source-maps --out-dir src/test/js
Working Directory: $ProjectFileDir$
Output paths to refresh: $ProjectFileDir$\src\test\js
This makes the babel.js callable for the File Watcher.

How do you serve simple documentation for go programs using godoc as a webpage?

I was trying to serve a specific local go file as a documentation web page, but was not able to do it.
The official godoc documentation says:
With the -http flag (i.e. the godoc command), it runs as a web server and presents the documentation as a web page.
user_me$ godoc -http=:6060
This does create something similar as the go page but it does not render the specific file that I want to render. So I tried to provide the name of the file I wanted:
user_me$ godoc -http=:6000 hello.go
However, it just replies with:
usage: godoc package [name ...]
godoc -http=:6060
-ex=false: show examples in command line mode
-goroot="/usr/local/go": Go root directory
-html=false: print HTML in command-line mode
-http="": HTTP service address (e.g., ':6060')
-httptest.serve="": if non-empty, httptest.NewServer serves on this address and blocks
-index=false: enable search index
-index_files="": glob pattern specifying index files;if not empty, the index is read from these files in sorted order
-index_throttle=0.75: index throttle value; 0.0 = no time allocated, 1.0 = full throttle
-links=true: link identifiers to their declarations
-maxresults=10000: maximum number of full text search results shown
-notes="BUG": regular expression matching note markers to show
-play=false: enable playground in web interface
-q=false: arguments are considered search queries
-server="": webserver address for command line searches
-src=false: print (exported) source in command-line mode
-tabwidth=4: tab width
-templates="": directory containing alternate template files
-timestamps=false: show timestamps with directory listings
-url="": print HTML for named URL
-v=false: verbose mode
-write_index=false: write index to a file; the file name must be specified with -index_files
-zip="": zip file providing the file system to serve; disabled if empty
I also tried:
user_me$ godoc -url="localhost:8080" hello.go
but it didn't work.
I also tried:
godoc -server=localhost:8080 hello.go
but it replied with:
2014/07/01 10:45:56 open /usr/local/go/src/pkg/hello.go: no such file or directory
I even tried just generating the html thing itself:
godoc -html hello.go > hello.html
same error as above.
I also tried (since it was complaining that there was no file in the pkg dir):
godoc -html -goroo=$GOPATH hello.go > hello.html
At the end, I gave up. I don't know how this godoc thing works. I installed the hello.go program so that I there was something in the pkg file in the workspace. How do you generate a webpage with your documentation for your code?
godoc operates on package and type names, not filenames.
For example, to learn about io/ioutil package:
text output: godoc io/ioutil
just the ReadAll function: godoc io/ioutil ReadAll
in HTML: godoc -html io/ioutil ReadAll
in the browser:
godoc -http=:6060
click Packages and navigate from there
or go directly to http://localhost:6060/pkg/io/ioutil#ReadAll
To view documentation for your own code, it has to be included in your GOPATH.
Suppose your GOPATH includes $HOME/go/src, and the file you are interested in is $HOME/go/src/hey/world/doc.go, you would run:
godoc hey/world
...or start godoc in HTTP mode and browse to http://localhost:6060/pkg/hey/world
By default, godoc looks at the packages it finds via $GOROOT and $GOPATH. So given that your package is in Go workspace i.e in GOPATH, you can run
godoc fmt
which prints out documentation for fmt package.
If you want to generate docs for your package foo which is in $GOPATH/src/github.com/abcd/foo location, you should run
godoc github.com/abcd/foo
With the -http flag, godoc runs as a web server and presents the documentation as a web page.
godoc -http=:6060
Now navigate to http://localhost:6060/pkg/github.com/abcd/foo in browser to find docs as web page.
The -play flag can be used to enable playground in web interface.
To show HTML doc generated for your own code
Step 1) At command line start up the document web server, that is:
C:\>godoc -http=:6060
Step 2) Open a browser and use an explicit url the folder your code is.
The URL structure comes from the folder names under your GOPATH.
For example:
If my GOPATH is c:\go and I have code in c:\go\src\myfolder\mysubfolder
The URL I would uses is http://localhost:6060/pkg/myfolder/mysubfolder and this would show an HTML page for the .go files in there
Also you can use URL http://localhost:6060/pkg/myfolder, which will have a link to mysubfolder
Notes:
I'm not sure how to see your local code at the the http://localhost:6060/pkg level, maybe you can't
It is possible to "specify additional paths" so I don't think it has to be the src folder, see https://blog.golang.org/godoc-documenting-go-code
Running godoc on its own worked for me, but was really slow because it
generates docs for every single package in the standard library, while I only
care about the local package that I am working on. To that end, if your package is in a folder called something, you can move
the folder so that it looks like this:
godoc/src/something
Then, go to the godoc folder, and run
godoc -goroot .
Then, browse to localhost:6060.
On linux, and assuming you have cd'd into the package of which you want to read the documentation.
if you are using go modules, you can run below command
godoc -http=:6060 & xdg-open http://localhost:6060/pkg/$(go list -m)
It uses the -m flag to get the package path even though the root module directory does not contain any .go file.
If you are not yet using modules, you can run,
godoc -http=:6060 & xdg-open http://localhost:6060/pkg/$(go list -f "{{.ImportPath}}")
Note that unlike -m this command will not work appropriately if there is no .go files into the directory.
Check the go list subcommand help at https://golang.org/pkg/cmd/go/internal/list/

Why is npm not honoring user/global npmignore?

My editor produces workspace files and backup folders which are of no interest to users of the software I write. In order to avoid having to list my editor-specific ignores in each project, I'm trying to tell npm to ignore them at the user or global level.
Unfortunately, I'm having no luck doing so. Running npm pack inside my project folder, even if I clear the npm cache first, includes both the workspace file and and two megabytes of backup files. (For a project with only ten kilobytes of code!) I've tried the ignore config setting, a per-user .npmignore, and a global npmignore, all to no effect.
Here's my output from npm config ls -l, snipped to relevant sections:
; userconfig C:\Users\benblank\.npmrc
ignore = "__history *.epp"
; builtin config undefined
prefix = "C:\\Users\\benblank\\AppData\\Roaming\\npm"
; default values
globalignorefile = "C:\\Users\\benblank\\AppData\\Roaming\\npm\\etc\\npmignore"
userignorefile = "C:\\Users\\benblank\\.npmignore"
And the (identical) contents of C:\Users\benblank\.npmignore and C:\Users\benblank\AppData\Roaming\npm\etc\npmignore:
__history
*.epp
What am I doing wrong? I'm running Windows 7, node#0.8.9, and npm#1.1.61.
npm has some major outstanding issues related to npmignore files.
Thankfully there is an even better alternative! It is the package.json files property.
Here is an example from my delivr project.
"files": [
"lib",
"index.js"
]
Why is it better?
The internal machinery makes this mechanism more reliable.
It is a whitelist instead of a blacklist. Typically, assuming you use a lib folder (or similar), there are less files/directories you want included vs those you want excluded, so it is more succinct.
It is DRY. If a .npmignore file exists, npm will not consult .gitignore for ignore patterns, which often needs overlapping and repetitive info. This problem does not exist with a whitelist.
One less file in the repo. package.json is already a manifest that defines your package and it makes a ton of sense for this configuration to live there.

Resources