1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | var path_1 = require("path");
|
11 | var webpack = require("webpack");
|
12 | var AssetsPlugin = require("assets-webpack-plugin");
|
13 | var WebpackMd5Hash = require("webpack-md5-hash");
|
14 | var utilities_1 = require("./utilities");
|
15 | function deepMergeObject(objectOne, objectTwo) {
|
16 | if (objectOne === void 0) { objectOne = {}; }
|
17 | if (objectTwo === void 0) { objectTwo = {}; }
|
18 | var newObject = objectOne;
|
19 | Object.keys(objectTwo).forEach(function (key) {
|
20 | if (typeof objectTwo[key] === 'function') {
|
21 | var mergeFunction = objectTwo[key];
|
22 | newObject[key] = mergeFunction(objectOne[key]);
|
23 | }
|
24 | else if (typeof objectTwo[key] !== 'object') {
|
25 | newObject[key] = objectOne[key] || objectTwo[key];
|
26 | }
|
27 | else if (Array.isArray(objectTwo[key])) {
|
28 | newObject[key] = (objectOne[key] || []).concat(objectTwo[key]);
|
29 | }
|
30 | else {
|
31 | newObject[key] = deepMergeObject(objectOne[key], objectTwo[key]);
|
32 | }
|
33 | });
|
34 | return newObject;
|
35 | }
|
36 | function smartMergeUserConfig(object, merger) {
|
37 | var finalConfig = __assign({}, object);
|
38 | return deepMergeObject(object, merger);
|
39 | }
|
40 | function removeEmptyPlugins(plugins) {
|
41 | var filteredPlugins = [];
|
42 | for (var _i = 0, plugins_1 = plugins; _i < plugins_1.length; _i++) {
|
43 | var plugin = plugins_1[_i];
|
44 | if (plugin) {
|
45 | filteredPlugins.push(plugin);
|
46 | }
|
47 | }
|
48 | return filteredPlugins;
|
49 | }
|
50 | module.exports = function createShopifyWebpackConfigFactory(options) {
|
51 | if (options === void 0) { options = {}; }
|
52 | var env = new utilities_1.Env(options);
|
53 | var paths = new utilities_1.Paths();
|
54 | var project = new utilities_1.Project(paths);
|
55 | var cdn = options.cdn, appRoot = options.appRoot, assetDevServerPort = options.assetDevServerPort, assetDevServerHost = options.assetDevServerHost, assetDevServerPath = options.assetDevServerPath, outputDirectory = options.outputDirectory;
|
56 | var uiRoot = appRoot || path_1.resolve(paths.app.root, project.ifRails('./app/ui', './app'));
|
57 | return function createShopifyWebpackConfig(additionalConfig) {
|
58 | if (additionalConfig === void 0) { additionalConfig = {}; }
|
59 | var config = additionalConfig;
|
60 | return smartMergeUserConfig(config, {
|
61 | target: env.ifServer('node', 'web'),
|
62 |
|
63 |
|
64 |
|
65 | node: {
|
66 | __dirname: true,
|
67 | __filename: true,
|
68 | },
|
69 | devtool: env.ifServer('source-map', env.ifDevelopment('source-map', 'hidden-source-map')),
|
70 | entry: function (currentEntry) {
|
71 | return currentEntry || uiRoot;
|
72 | },
|
73 | output: {
|
74 | filename: env.ifProduction('[name]-[chunkhash].js', '[name].js'),
|
75 | path: outputDirectory || project.ifRails(path_1.join(paths.app.root, 'public/bundles'), path_1.join(paths.app.root, 'build')),
|
76 | publicPath: env.ifProduction(cdn, project.ifRails('/webpack/assets/', "http://" + (assetDevServerHost || 'localhost') + ":" + assetDevServerPort + "/" + (assetDevServerPath || ''))) || '/assets/',
|
77 | chunkFilename: '[name]-[chunkhash].js',
|
78 | libraryTarget: env.ifServer('commonjs2', 'var'),
|
79 | },
|
80 | resolve: {
|
81 | modules: function (currentModules) {
|
82 | var modules = new Set([uiRoot].concat((currentModules || ['node_modules'])));
|
83 | return Array.from(modules);
|
84 | },
|
85 | extensions: function (currentExtensions) {
|
86 | if (currentExtensions === void 0) { currentExtensions = []; }
|
87 | var extensions = new Set(currentExtensions);
|
88 | extensions.add('.js');
|
89 | extensions.add('.json');
|
90 | if (project.isTypeScript) {
|
91 | extensions.add('.ts');
|
92 | extensions.add('.tsx');
|
93 | }
|
94 | return Array.from(extensions);
|
95 | },
|
96 | },
|
97 | resolveLoader: {
|
98 | modules: function (currentModules) {
|
99 | var modules = new Set([paths.own.nodeModules].concat((currentModules || ['node_modules'])));
|
100 | return Array.from(modules);
|
101 | },
|
102 | },
|
103 | module: {
|
104 | rules: function (currentRules) {
|
105 | var rules = currentRules || [];
|
106 | var jsRuleMatcher = utilities_1.createPartialObjectMatcher({ test: /\.js$/ });
|
107 | if (!rules.some(jsRuleMatcher)) {
|
108 | rules.push({
|
109 | test: /\.js$/,
|
110 | exclude: /node_modules/,
|
111 | loader: 'babel-loader',
|
112 | options: {
|
113 | presets: [
|
114 | [require.resolve('babel-preset-shopify/web'), { modules: false }],
|
115 | require.resolve('babel-preset-shopify/react'),
|
116 | ],
|
117 | },
|
118 | });
|
119 | }
|
120 | if (project.isTypeScript) {
|
121 | var tsRuleMatcher = utilities_1.createPartialObjectMatcher({ test: /\.tsx?$/ });
|
122 | if (!rules.some(tsRuleMatcher)) {
|
123 | rules.push({
|
124 | test: /\.tsx?$/,
|
125 | loader: 'ts-loader',
|
126 | exclude: /node_modules/,
|
127 | options: {
|
128 | silent: true,
|
129 | compiler: path_1.resolve(paths.app.nodeModules, 'typescript'),
|
130 | },
|
131 | });
|
132 | }
|
133 | }
|
134 | if (project.hasDependency('@shopify/quilt')) {
|
135 | rules.push({
|
136 | test: /\.scss$/,
|
137 | loader: 'scss-loader',
|
138 | include: [path_1.resolve(paths.app.nodeModules, '@shopify/quilt')],
|
139 | });
|
140 | }
|
141 | return rules;
|
142 | },
|
143 | },
|
144 | plugins: removeEmptyPlugins([
|
145 | project.ifRails(new AssetsPlugin({
|
146 | filename: 'webpack-manifest.json',
|
147 | path: path_1.resolve(paths.app.root, 'tmp'),
|
148 | })),
|
149 | env.ifDevelopmentClient(new webpack.NamedModulesPlugin()),
|
150 | env.ifProductionClient(new WebpackMd5Hash()),
|
151 |
|
152 |
|
153 |
|
154 | env.ifProductionClient(new webpack.optimize.UglifyJsPlugin({
|
155 | sourceMap: true,
|
156 | comments: false,
|
157 | compress: {
|
158 | screw_ie8: true,
|
159 | warnings: false,
|
160 | },
|
161 | mangle: {
|
162 | screw_ie8: true,
|
163 | },
|
164 | })),
|
165 | ]),
|
166 | });
|
167 | };
|
168 | };
|