最近、ビルドプロセスをGrunt
からwebpack-2
に移動することを考えています。私たちのコードは、requirejsを使って完全にAMD形式で書かれています。デモの目的で、私が使用しているindex.js
ファイルの最小限のバージョンを投稿します。
ファイルをコミットする際にjQuery-1.6.4が使用され、npm経由でロードされるリンクがあります。当分の間、これを許してください。 require-config.js
ファイル対応jQuery-1.6.4
index.js
define(['jQuery', 'arrayUtils'], function ($, arrayUtils) {
'use strict';
console.log($);
console.log(arrayUtils);
});
:
(function() {
'use strict';
require.config({
baseUrl: '../../src',
waitSeconds: 0,
paths: {
jQuery: '/path to/jquery-1.6.4',
urlUtils: '/path to/urlUtils',
// ...
},
shim: {
jQuery: {
exports: '$'
},
urlUtils: {
exports: 'urlUtils'
},
// ...
}
});
})();
を、私は私達のAMDのアプローチを使用しようとしたが、WebPACKの-2を使用して、すべてをバンドルします。私はドキュメントやさまざまなブログを見て、古いgold jQueryを除いてすべてが魅力的に機能するようにするconfigを見つけました。ここ
webpack-2
によって生成されたファイルwebpack.config.js
var webpack = require('webpack');
var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
// Import the plugin:
var path = require('path');
var env = require('yargs').argv.mode;
var outputFile;
var options = {
target: 'this'
};
var libOptions = {
name: 'heatmap-inject',
version: JSON.stringify(require("./package.json").version)
};
var plugins = [];
if (env === 'build') {
plugins.push(new UglifyJsPlugin({
sourceMap: true,
minimize: true
}));
}
// At the end of the file:
module.exports = {
entry: __dirname + '/src/library.js',
devtool: 'source-map',
output: {
path: __dirname + '/dist/webpack',
library: 'library.js',
filename: libOptions.name + '.js', // libOptions.name + (options.target ? '.' + options.target : '') + (env === 'build' ? '.min.js' : '.js')
libraryTarget: options.target || 'umd',
umdNamedDefine: true
},
module: {
rules: [{
test: /(\.js)$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['es2015'],
plugins: []
}
}
}, {
enforce: 'pre',
test: /(\.js)$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'eslint-loader',
options: {}
}
}, { test: /jQuery/, loader: 'exports-loader?$'
}, { test: /urlUtils/, loader: 'exports-loader?urlUtils'
}]
},
resolve: {
alias: {
'jQuery': 'heatmaps/vendor/jquery-1.6.4',
'urlUtils': 'lib/heatmap/UrlUtils'
},
modules: [
'src/**/*.js',
'src/bower_components',
path.resolve('./src')
],
extensions: ['.js']
},
plugins: plugins
};
ある$.fn
は、それが普通空のオブジェクトのjQueryの関数である代わり$
のすなわち定義されていないことを示すエラーをスロー。 ウィンドウ$が定義されていて正しいjQuery関数が返されますが、なぜ私は$
と同じ結果になっていませんか? AMDの場合、$
が終了して1.6.4 jQueryを返すので、私は正しい結果を得ています。 webpackを使用すると、奇妙な動作をします。
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [ __webpack_require__(1), __webpack_require__(13) ], __WEBPACK_AMD_DEFINE_RESULT__ = function ($, urlUtils) { ... }
私が試してみました他にはどんなもの:
{ test: /jQuery/, loader: 'expose-loader?$'
{ test: /jQuery/, loader: 'exports-loader?jQuery=jQuery,$=jQuery,this=>window'
-
{ test: /jQuery/, loader: 'expose-loader?$=jQuery'
externals
プロパティを使用できません。これは、window.jQuery
を汚染し、このライブラリがユーザーのWebサイトに挿入/ロードされるため、ターゲットソース上のものが破損します。
私を助けてください!私はこの不思議なjQueryのことで今や不満です。
どうすればhttps://webpack.js.org/plugins/provide-plugin/? – Chay22