Skip to content

Commit 4be7323

Browse files
author
Martynas Žilinskas
committed
v0.1.0-alpha.14
1 parent bd59281 commit 4be7323

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@simplrjs/webpack",
3-
"version": "0.1.0-alpha.13",
3+
"version": "0.1.0-alpha.14",
44
"description": "Tailored webpack for SPA.",
55
"publishConfig": {
66
"access": "public"

src/contracts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export interface SimplrWebpackOptions {
1616
emitHtml?: boolean;
1717
htmlOptions?: Options;
1818
projectDirectory: string;
19-
entryFile?: string;
20-
outputDirectory?: string;
19+
entryFile: string;
20+
outputDirectory: string;
2121
staticContentDirectory?: string;
2222
/**
2323
* Full path is: {outputDirectory} + {staticContentDirectoryOutput}

src/webpack-config.ts

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,39 @@ import * as CopyWebpackPlugin from "copy-webpack-plugin";
1212
import { SimplrWebpackOptions } from "./contracts";
1313
import { Helpers } from "./helpers";
1414

15-
export function generateWebpackConfig(opts: SimplrWebpackOptions): Configuration {
16-
const options: Required<SimplrWebpackOptions> = {
17-
...opts,
18-
htmlOptions: opts.htmlOptions || ({} as Options),
19-
devServerPort: opts.devServerPort || 3000,
20-
entryFile: opts.entryFile || "./src/index.ts",
21-
outputDirectory: opts.outputDirectory || "./wwwroot",
22-
staticContentDirectory: opts.staticContentDirectory || "./src/static",
23-
staticContentDirectoryOutput: opts.staticContentDirectoryOutput || "./static",
24-
fontsDirectoryOutput: opts.fontsDirectoryOutput || "./assets/fonts",
25-
imagesDirectoryOutput: opts.imagesDirectoryOutput || "./assets/images",
26-
emitHtml: opts.emitHtml != null ? opts.emitHtml : true,
27-
target: opts.target || "web",
28-
publicPath: opts.publicPath || "/"
15+
export function getDefaultWebOptions(): Partial<SimplrWebpackOptions> {
16+
return {
17+
htmlOptions: {} as Options,
18+
devServerPort: 3000,
19+
entryFile: "./src/index.ts",
20+
outputDirectory: "./wwwroot",
21+
staticContentDirectory: "./src/static",
22+
staticContentDirectoryOutput: "./static",
23+
fontsDirectoryOutput: "./assets/fonts",
24+
imagesDirectoryOutput: "./assets/images",
25+
emitHtml: true,
26+
target: "web",
27+
publicPath: "/"
28+
};
29+
}
30+
31+
export function getDefaultNodeOptions(): Partial<SimplrWebpackOptions> {
32+
return {
33+
devServerPort: 3000,
34+
entryFile: "./src/index.ts",
35+
outputDirectory: "./dist",
36+
target: "node"
2937
};
38+
}
39+
40+
export function generateWebpackConfig(options: SimplrWebpackOptions): Configuration {
41+
if (options.entryFile == null) {
42+
throw new Error("[Simplr Webpack] Entry file is undefined.");
43+
}
44+
if (options.outputDirectory == null) {
45+
throw new Error("[Simplr Webpack] Output directory is undefined.");
46+
}
47+
3048
const fullOutputDirectoryLocation = path.resolve(options.projectDirectory, options.outputDirectory);
3149
const fullTsconfigLocation = path.resolve(options.projectDirectory, Helpers.TS_CONFIG_NAME);
3250

@@ -54,7 +72,7 @@ export function generateWebpackConfig(opts: SimplrWebpackOptions): Configuration
5472
filename: "[name].bundle.js",
5573
chunkFilename: "[name].bundle.js",
5674
path: fullOutputDirectoryLocation,
57-
publicPath: opts.publicPath
75+
publicPath: options.publicPath
5876
},
5977
resolve: {
6078
extensions: [".ts", ".tsx", ".js", ".json", ".scss", ".css"],
@@ -108,7 +126,7 @@ export function generateWebpackConfig(opts: SimplrWebpackOptions): Configuration
108126
test: /\.(woff|woff2|eot|ttf|otf)$/,
109127
options: {
110128
name: `./${options.fontsDirectoryOutput}/[name].[ext]`,
111-
publicPath: opts.publicPath,
129+
publicPath: options.publicPath,
112130
limit: 10000
113131
},
114132
loader: "url-loader"
@@ -117,7 +135,7 @@ export function generateWebpackConfig(opts: SimplrWebpackOptions): Configuration
117135
test: /\.(png|jpg|gif|svg)$/,
118136
options: {
119137
name: `./${options.imagesDirectoryOutput}/[name].[ext]`,
120-
publicPath: opts.publicPath,
138+
publicPath: options.publicPath,
121139
limit: 10000
122140
},
123141
loader: "url-loader"
@@ -127,7 +145,7 @@ export function generateWebpackConfig(opts: SimplrWebpackOptions): Configuration
127145
plugins: [
128146
new CleanWebpackPlugin([fullOutputDirectoryLocation], { root: options.projectDirectory }),
129147
new WriteFilePlugin(),
130-
...(!opts.emitHtml
148+
...(!options.emitHtml
131149
? []
132150
: [
133151
new HtmlWebpackPlugin({
@@ -148,12 +166,16 @@ export function generateWebpackConfig(opts: SimplrWebpackOptions): Configuration
148166
checkSyntacticErrors: true,
149167
tslint: true
150168
}),
151-
new CopyWebpackPlugin([
152-
{
153-
from: options.staticContentDirectory,
154-
to: options.staticContentDirectoryOutput
155-
}
156-
])
169+
...(options.staticContentDirectory != null && options.staticContentDirectoryOutput != null
170+
? [
171+
new CopyWebpackPlugin([
172+
{
173+
from: options.staticContentDirectory,
174+
to: options.staticContentDirectoryOutput
175+
}
176+
])
177+
]
178+
: [])
157179
],
158180
optimization: {
159181
splitChunks: {
@@ -163,7 +185,7 @@ export function generateWebpackConfig(opts: SimplrWebpackOptions): Configuration
163185
// addition - add source-map support
164186
devtool: "inline-source-map",
165187
devServer:
166-
opts.target === "node"
188+
options.target === "node"
167189
? {}
168190
: {
169191
contentBase: fullOutputDirectoryLocation,
@@ -173,10 +195,10 @@ export function generateWebpackConfig(opts: SimplrWebpackOptions): Configuration
173195
port: options.devServerPort,
174196
historyApiFallback: true
175197
},
176-
target: opts.target,
198+
target: options.target,
177199
mode: "development",
178200
node:
179-
opts.target === "node"
201+
options.target === "node"
180202
? {}
181203
: {
182204
fs: "empty",

0 commit comments

Comments
 (0)