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 utilities_1 = require("./utilities");
|
12 | function deepMergeObject(objectOne, objectTwo) {
|
13 | if (objectOne === void 0) { objectOne = {}; }
|
14 | if (objectTwo === void 0) { objectTwo = {}; }
|
15 | var newObject = objectOne;
|
16 | Object.keys(objectTwo).forEach(function (key) {
|
17 | if (typeof objectTwo[key] === 'function') {
|
18 | var mergeFunction = objectTwo[key];
|
19 | newObject[key] = mergeFunction(objectOne[key]);
|
20 | }
|
21 | else if (typeof objectTwo[key] !== 'object') {
|
22 | newObject[key] = objectOne[key] || objectTwo[key];
|
23 | }
|
24 | else {
|
25 | newObject[key] = deepMergeObject(objectOne[key], objectTwo[key]);
|
26 | }
|
27 | });
|
28 | return newObject;
|
29 | }
|
30 | function smartMergeUserConfig(object, merger) {
|
31 | var finalConfig = __assign({}, object);
|
32 | return deepMergeObject(object, merger);
|
33 | }
|
34 | function addReactHotLoader(entry) {
|
35 | if (entry == null) {
|
36 | return [];
|
37 | }
|
38 | else if (typeof entry === 'string') {
|
39 | return ['react-hot-loader/patch', entry];
|
40 | }
|
41 | else if (Array.isArray(entry)) {
|
42 | return entry.indexOf('react-hot-loader/patch') < 0
|
43 | ? ['react-hot-loader/patch'].concat(entry) : entry;
|
44 | }
|
45 | else {
|
46 | return Object.keys(entry).reduce(function (newEntry, key) {
|
47 | newEntry[key] = addReactHotLoader(entry[key]);
|
48 | return newEntry;
|
49 | }, {});
|
50 | }
|
51 | }
|
52 | module.exports = function createShopifyWebpackConfigFactory(options) {
|
53 | if (options === void 0) { options = {}; }
|
54 | var env = new utilities_1.Env(options);
|
55 | var paths = new utilities_1.Paths();
|
56 | var project = new utilities_1.Project(paths);
|
57 | var cdn = options.cdn, appRoot = options.appRoot, assetDevServerPort = options.assetDevServerPort, assetDevServerHost = options.assetDevServerHost, assetDevServerPath = options.assetDevServerPath, outputDirectory = options.outputDirectory;
|
58 | var uiRoot = appRoot || path_1.resolve(paths.app.root, project.ifRails('./app/ui', './app'));
|
59 | return function createShopifyWebpackConfig(additionalConfig) {
|
60 | if (additionalConfig === void 0) { additionalConfig = {}; }
|
61 | var config = additionalConfig;
|
62 | return smartMergeUserConfig(config, {
|
63 | target: env.ifServer('node', 'web'),
|
64 | devtool: env.ifServer('source-map', env.ifDevelopment('source-map', 'hidden-source-map')),
|
65 | entry: function (currentEntry) {
|
66 | var entry = currentEntry || uiRoot;
|
67 | return project.hasDependency('react-hot-loader') ? addReactHotLoader(entry) : entry;
|
68 | },
|
69 | output: {
|
70 | filename: env.ifProduction('[name]-[chunkhash].js', '[name].js'),
|
71 | path: outputDirectory || project.ifRails(path_1.join(paths.app.root, 'public/bundles'), path_1.join(paths.app.root, 'build')),
|
72 | publicPath: env.ifProduction(cdn, "http://" + (assetDevServerHost || 'localhost') + ":" + assetDevServerPort + "/" + (assetDevServerPath || '')) || '/assets/',
|
73 | chunkFilename: '[name]-[chunkhash].js',
|
74 | library: 'foo',
|
75 | libraryTarget: env.ifServer('commonjs2', 'var'),
|
76 | },
|
77 |
|
78 |
|
79 |
|
80 | node: {
|
81 | __dirname: true,
|
82 | __filename: true,
|
83 | },
|
84 | resolve: {
|
85 | modules: function (currentModules) {
|
86 | var modules = new Set([uiRoot].concat((currentModules || ['node_modules'])));
|
87 | return Array.from(modules);
|
88 | },
|
89 | extensions: function (currentExtensions) {
|
90 | if (currentExtensions === void 0) { currentExtensions = []; }
|
91 | var extensions = new Set(currentExtensions);
|
92 | extensions.add('.js');
|
93 | extensions.add('.json');
|
94 | if (project.isTypeScript) {
|
95 | extensions.add('.ts');
|
96 | extensions.add('.tsx');
|
97 | }
|
98 | return Array.from(extensions);
|
99 | },
|
100 | },
|
101 | resolveLoader: {
|
102 | modules: function (currentModules) {
|
103 | var modules = new Set([paths.own.nodeModules].concat((currentModules || ['node_modules'])));
|
104 | return Array.from(modules);
|
105 | },
|
106 | },
|
107 | module: {
|
108 | rules: function (currentRules) {
|
109 | var rules = currentRules || [];
|
110 | var jsRuleMatcher = utilities_1.createPartialObjectMatcher({ test: /\.js$/ });
|
111 | if (!rules.some(jsRuleMatcher)) {
|
112 | rules.push({
|
113 | test: /\.js$/,
|
114 | exclude: /node_modules/,
|
115 | loader: 'babel-loader',
|
116 | options: {
|
117 | presets: [
|
118 | [require.resolve('babel-preset-shopify/web'), { modules: false }],
|
119 | require.resolve('babel-preset-shopify/react'),
|
120 | ],
|
121 | },
|
122 | });
|
123 | }
|
124 | if (project.isTypeScript) {
|
125 | var tsRuleMatcher = utilities_1.createPartialObjectMatcher({ test: /\.tsx?$/ });
|
126 | if (!rules.some(tsRuleMatcher)) {
|
127 | rules.push({
|
128 | test: /\.tsx?$/,
|
129 | loader: 'ts-loader',
|
130 | exclude: /node_modules/,
|
131 | options: {
|
132 | silent: true,
|
133 | compiler: path_1.resolve(paths.app.nodeModules, 'typescript'),
|
134 | },
|
135 | });
|
136 | }
|
137 | }
|
138 | if (project.hasDependency('@shopify/quilt')) {
|
139 | rules.push({
|
140 | test: /\.scss$/,
|
141 | loader: 'scss-loader',
|
142 | include: [path_1.resolve(paths.app.nodeModules, '@shopify/quilt')],
|
143 | });
|
144 | }
|
145 | return rules;
|
146 | },
|
147 | },
|
148 | });
|
149 | };
|
150 | };
|