UNPKG

2.56 kBJavaScriptView Raw
1const webpack = require("webpack");
2const process = require("process");
3const path = require("path");
4
5const ExtractTextPlugin = require("extract-text-webpack-plugin");
6const ManifestPlugin = require("webpack-manifest-plugin");
7const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
8
9exports.webpackConfig = function webpackConfig(publisherName, currentDirectory, opts = {}) {
10 const PUBLIC_PATH = `/${publisherName}/assets/`;
11 const OUTPUT_DIRECTORY = currentDirectory + `/public/${PUBLIC_PATH}`;
12
13 const BABEL_PRESET = {
14 loader: "babel-loader",
15 options: {
16 presets: ["es2015-tree-shaking", "react"]
17 }
18 };
19
20 const config =
21 process.env.NODE_ENV == "production"
22 ? {
23 outputFileName: suffix => `[name]-[hash:20].${suffix}`,
24 sassLoader: ExtractTextPlugin.extract(
25 "css-loader?minimize=true!sass-loader"
26 ),
27 cssFile: `[name]-[contenthash:20].css`,
28 compressJSPlugins: [new UglifyJSPlugin()],
29 outputPublicPath: PUBLIC_PATH
30 }
31 : {
32 outputFileName: suffix => `[name].${suffix}`,
33 sassLoader: "style-loader!css-loader!sass-loader",
34 cssFile: `[name].css`,
35 compressJSPlugins: [new webpack.NamedModulesPlugin()],
36 outputPublicPath: "http://localhost:8080" + PUBLIC_PATH
37 };
38
39 return {
40 entry: {
41 app: "./app/client/app.js",
42 serviceWorkerHelper: "./app/client/serviceWorkerHelper.sjs"
43 },
44 output: {
45 path: OUTPUT_DIRECTORY,
46 filename: config.outputFileName("js"),
47 publicPath: config.outputPublicPath
48 },
49 module: {
50 rules: [
51 { test: /\.jsx?$/, exclude: /node_modules/, use: BABEL_PRESET },
52 { test: /\.jsx?$/, include: /node_modules\/@quintype\/framework/, use: BABEL_PRESET },
53 { test: /\.(sass|scss)$/, loader: config.sassLoader },
54 {
55 test: /\.(jpe?g|gif|png|svg|woff|woff2|eot|ttf|wav|mp3|ico|mp4)$/,
56 loader: "file-loader",
57 query: {
58 context: "./app/assets",
59 name: config.outputFileName("[ext]")
60 }
61 }
62 ]
63 },
64 plugins: [
65 new webpack.EnvironmentPlugin({ NODE_ENV: "development" }),
66 new ExtractTextPlugin({ filename: config.cssFile, allChunks: true }),
67 new ManifestPlugin({
68 fileName: "../../../asset-manifest.json",
69 publicPath: PUBLIC_PATH,
70 writeToFileEmit: true
71 })
72 ].concat(config.compressJSPlugins),
73
74 devServer: {
75 headers: { "Access-Control-Allow-Origin": "*" }
76 }
77 };
78};