У меня есть модуль javascript es6, который экспортирует объект следующим образом:
import HealthCarePlan from './health_care_plan/health-care-plan';
import CourseRequest from './course_request/course-request';
export default {
CourseRequest: CourseRequest,
HealthCarePlan: HealthCarePlan
};
Как я могу сказать webpack экспортировать этот объект в глобальную переменную с именем appScripts
?Я хочу иметь возможность ссылаться на appScripts.CourseRequest
и appScripts.HealthCarePlan
из глобального контекста.
Вот мой текущий веб-пакет (разделенный на модули common, dev.babel и dev.include):
index.js
export default {
one: 'one',
two: 'two'
}
webpack.common.babel.js
import path from 'path';
import webpack from 'webpack';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import webpackRxjsExternals from 'webpack-rxjs-externals';
const appsPath = 'powerschool_apps';
const staticPath = `${appsPath}/static`;
const config = {
target: 'web',
node: {
fs: 'empty'
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'static/bundles'),
publicPath: 'http://localhost:8081/',
library: 'app',
libraryTarget: 'var'
},
entry: {
app: [
'./powerschool_apps/static/js/index.js'
],
vendor: [
'jquery',
`./${staticPath}/js/vendor-include.js`,
`./${staticPath}/lib/materialize/js/bin/materialize.js`,
'iframe-resizer',
'underscore',
`./${staticPath}/lib/materialize/sass/materialize.scss`,
]
},
performance: {
hints: false
},
optimization: {
splitChunks: {
cacheGroups: {
default: false,
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor_app',
chunks: 'all',
minChunks: 2
}
}
}
},
externals: [
webpackRxjsExternals()
],
module: {
rules: [
{
test: /\.(ttf|eot|woff|woff2)/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]',
}
},
{
test: /\.(png|svg|gif)$/,
loader:
'file-loader',
options: {
name: 'images/[name].[ext]',
},
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
]
},
plugins: [
// new HardSourceWebpackPlugin(),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
}),
],
resolve: {
modules: [
'/node_modules'
]
}
};
export default config;
webpack.dev.babel.js
require('babel-register');
import path from 'path';
import webpack from 'webpack';
import merge from 'webpack-merge';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import common from './webpack.common.babel';
export default merge(common, {
devtool: 'inline-source-map',
mode: 'development',
module: {
rules: [
{
test: /\.scss$/,
use: [
'css-hot-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].bundle.css',
chunkFilename: '[id].css'
}),
new webpack.NamedModulesPlugin()
],
serve: {
dev: {
headers: {
'Access-Control-Allow-Origin': '*'
}
},
host: '0.0.0.0',
port: '8081',
clipboard: false,
hot: {
port: '8500',
host: {
client: 'localhost',
server: '0.0.0.0'
}
}
}
});
webpack.dev.include.js
require('babel-register');
const config = require('./webpack.dev.babel');
module.exports = config.default;
webpack.prod.babel.js
import path from 'path';
import merge from 'webpack-merge';
import CleanWebpackPlugin from 'clean-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import BundleTracker from 'webpack-bundle-tracker';
import common from './webpack.common.babel';
export default merge(common, {
mode: 'production',
output: {
filename: '[name].[hash].js',
path: path.resolve(__dirname, 'static/bundles'),
publicPath: 'http://localhost:8081/static/bundles/'
},
module: {
rules: [{
test: /\.(css|scss|sass)$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
}],
},
plugins: [
new CleanWebpackPlugin(['./powerschool_apps/static/bundles/*']),
new BundleTracker({
filename: './webpack-stats.json'
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: '[name].[hash].css',
chunkFilename: '[id].css'
}),
]
})
package.json зависимости
"dependencies": {
"@reactivex/rxjs": "^6.0.0",
"bootstrap": "^4.0.0",
"bootstrap-markdown": "^2.10.0",
"font-awesome": "^4.7.0",
"font-awesome-animation": "^0.2.0",
"gulp": "gulpjs/gulp.git#4.0",
"hammerjs": "^2.0.8",
"iframe-resizer": "^3.5.16",
"jquery": "^3.3.1",
"jquery-datetimepicker": "^2.5.16",
"jquery.formset": "^1.3.0",
"ladda": "^1.0.5",
"materialize-autocomplete": "^1.0.7",
"rx-dom": "^7.0.3",
"select2": "^4.0.6-rc.1",
"underscore": "^1.8.3"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"browser-sync": "^2.23.7",
"clean-webpack-plugin": "^0.1.19",
"css-hot-loader": "^1.3.9",
"css-loader": "^0.28.11",
"file-loader": "^1.1.11",
"merge-stream": "^1.0.1",
"mini-css-extract-plugin": "^0.4.0",
"normalize-package-data": "^2.4.0",
"run-sequence": "^2.2.1",
"sass-loader": "^7.0.1",
"source-map-loader": "^0.2.3",
"style-loader": "^0.21.0",
"webpack": "^4.6.0",
"webpack-bundle-tracker": "^0.3.0",
"webpack-cli": "^2.0.15",
"webpack-hot-client": "^2.2.2",
"webpack-merge": "^4.1.2",
"webpack-rxjs-externals": "^2.0.0",
"webpack-stream": "^4.0.0"
},
Я начинаю webpack-serve
с: webpack-serve --config webpack.dev.include.js
.В Chrome DevTools, когда я открываю консоль и набираю app
и нажимаю enter
, я получаю undefined
.
Однако, когда я запускаю webpack --config webpack.prod.babel.js
и набираю app
в DevToolsконсоль, я возвращаю объект {one: 'one', two: 'two'}
.
Моя цель здесь - использовать этот пакет в проекте Django.Мне нужно передать данные из контекста Django в функцию JS, поэтому мне нужно представить пакет в виде библиотеки.