How to deal with AMD plugins in webpack? - plugins

AMD plugins (http://requirejs.org/docs/plugins.html) allow you to load content conditionally.
An example is the text plugin (http://requirejs.org/docs/api.html#text):
require(["requirejs/text!file.txt"], function(myFile){
console.log("contents of file.txt: " + myFile);
});
How can I configure webpack (webpack.config.js) so I can actually continue to use this pattern in my code? I basically want a bundle where both the contents of file.txt and my application are all bundled in a single file.

Related

Using bower with plugins that need AMD

I have a trying to use Bower.io with my RequireJS AMD project but I'm pretty new to it. I have a plugin in my bower.json file thats like
"jquery-ui-touch-punch": "https://github.com/furf/jquery-ui-touch-punch.git",
but the problem is that this file is not wrapped in AMD - so it errors. I can manually edit the file to wrap it - but that makes no sense for the purposes of using bower. Is there some resource I can use or a way I can make this wrap inside requireJS ?
shim: Configure the dependencies, exports, and custom initialization
for older, traditional "browser globals" scripts that do not use
define() to declare the dependencies and set a module value.
http://requirejs.org/docs/api.html#config-shim

how to remove coffeescript from all.js (using r.js)?

I am using requirejs with coffeescript and cs plugin. When I go to production I bundle all using r.js optimizer which uses coffeescript.js to complie my cs files into javascript and put it all in all.js
In runtime, there is no need to the 159K coffeescript files, how can I prevent r.js from pushing it into the bundle and save this huge file space
Use StubModules in requirejs configuration:
    stubModules: ['cs','coffee-script']
If you're using this plugin it seems this should just work as part of the build process.
If thats not the case, its easy to set up manually:
In your bundle script, run the coffee executable first to build the coffeescript files to js, and then run r.js on those javascript files to optimize them.
Then you can even delete the temp js files if you like and only keep your single bundled files.

How to add a DLL Plugin without NSH file into my NSIS script?

I'm using NSIS 2.46. Plugin I'm trying to use is HwInfo plug-in (Official Link). The ZIP file comes with some source codes and a DLL file. I put the HwInfo.dll inside \NSIS\Plugins directory. When adding a plugin, I'm supposed to !include the .nsh file as well, which HwInfo does not supply.
I'm trying to analyze the client's harware before installing-
Function .onInit
HwInfo::GetCpuSpeed
StrCpy $R0 $0
MessageBox MB_OK "You have a $0GHz CPU"
HwInfo::GetSystemMemory
StrCpy $0
MessageBox MB_OK "You have $0MB of RAM"
FunctionEnd
But the line HwInfo::GetCpuSpeed is 'invalid command'.
How do I use a plugin without a NSH file? And are there any alternatives?
Solved:
I added !addplugindir "${NSISDIR}\Plugins" at the very top of this script. This helped detect HwInfo.dll inside \NSIS\Plugins directory at compile-time.
Not all plugins have a .nsh file but the wiki page usually tells you how to use a specific plugin.
If you run makensis /V4 yourscript.nsi it will list all plugins and the functions they export, if your plugin is not on the list it is probably not in the correct directory. Make sure you put it in the correct directory or use !addplugindir...
I know this question has an accepted answer, but for info for people using NSIS v3.x:
The plugin folder now has two sub-folders, one for ANSI and one for UNICODE, so you'd need to copy your plugins into ${NSISDIR}\Plugins\x86-ansi\ and ${NSISDIR}\Plugins\x86-unicode\ for the ANSI and UNICODE versions of the plugin dlls respectively.
I'm guessing if you use the 64-bit NSIS port, you'd have \x64-ansi\ and \x64-unicode subfolders too, but I've not checked that specifically.

Require CoffeeScript files in the browser

I'm trying to get some options out of a separate config.coffee into my main app.coffee. The way I try doesn't work somehow, I'm only getting:
ReferenceError: Can't find variable: require
Here are my two files, I really hope you can help me out here.
# app.coffee
config = require './config.coffee'
console.log config.api_key
# config.coffee
exports.config =
api_key: 'MY_SECRET_API_KEY'
oh, I thought if I require another file and compile my app.coffee it gets included.. is there a way to do this or do I have to put everything in one file?
You might want to edit your question to specify this.
There are some projects that let you "require" files and introduce a build step to concatenate and minify them into a single one. Not all of them follow the Common/JS module spec. You can also use AMD based tools to load files asynchronously.
Sprockets
Which uses comments like #= require jquery to require other files and then compiles them into a single file. While it's aimed at Rack based apps, it has a command line tool which you can use to automate the process.
url: https://github.com/sstephenson/sprockets
Snockets
This is based on Sprockets, but it runs on Node.js
url: https://github.com/TrevorBurnham/snockets
Browserify
This is another Node.js based tool.
Make node-style require() work in the browser with a server-side build step, as if by magic!
https://github.com/substack/node-browserify
Require.JS
This is AMD based, so it can load files asynchronously, but it also has an optimizer wich can concatenate files.
I'm sure there are many more. Each have their own way of doing things and you can make them work with CoffeeScript.

Temporarily disable some plugins using pathogen in vim.

I think I have a bug in one plugin. I would like to load only this plugin, without having to delete all the other bundles in my pathogen's bundle folder, to debug.
Is it possible?
The easiest method to disable a plugin when you use Pathogen is by adding it's bundle name to the g:pathogen_disabled variable, before starting pathogen.
So an example from my own vimrc
" To disable a plugin, add it's bundle name to the following list
let g:pathogen_disabled = []
" for some reason the csscolor plugin is very slow when run on the terminal
" but not in GVim, so disable it if no GUI is running
if !has('gui_running')
call add(g:pathogen_disabled, 'csscolor')
endif
" Gundo requires at least vim 7.3
if v:version < '703' || !has('python')
call add(g:pathogen_disabled, 'gundo')
endif
if v:version < '702'
call add(g:pathogen_disabled, 'autocomplpop')
call add(g:pathogen_disabled, 'fuzzyfinder')
call add(g:pathogen_disabled, 'l9')
endif
call pathogen#infect()
Update: Another method, supported by Pathogen, is to simply rename the directory for the bundle you want to disable so that it ends in a tilde (~). So to disable the autocomplpop bundle, simply rename it to autocomplpop~.
vim -u NONE -N will load vim with no plugins, with no settings from your .vimrc. You could then :source /path/to/plugin/you-want.vim inside vim to load the one plugin you want loaded.
vim --noplugin
In this case vim will not load any plugins but your vimrc will be used.
After you can load your plugin in vim:
:source 'your plugin path'
Why not just:
rename the current bundle directory
create a new empty bundle directory
put your test plugin files into the new bundle dir?
When done put everything back the way it was. (The suggested method of loading Vim without plugins and sourcing the plugin file would work if it's a simple one-file plugin, but if you're doing an ftplugin then moving dirs around is probably best way and not that hard.)
You could rename the specific plugin by putting a tilde sign ~ after its original name.
Every plugin folders within the bundle with the tilde sign ~ at the end will not be loaded by the pathogen.