@ms-cloudpack/bundler-webpack 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/lib/getPathsOfModules.d.ts +8 -0
- package/lib/getPathsOfModules.d.ts.map +1 -0
- package/lib/getPathsOfModules.js +27 -0
- package/lib/getPathsOfModules.js.map +1 -0
- package/lib/getWebpackConfiguration.d.ts +8 -0
- package/lib/getWebpackConfiguration.d.ts.map +1 -0
- package/lib/getWebpackConfiguration.js +169 -0
- package/lib/getWebpackConfiguration.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/lib/normalizeWebpackOutput.d.ts +13 -0
- package/lib/normalizeWebpackOutput.d.ts.map +1 -0
- package/lib/normalizeWebpackOutput.js +66 -0
- package/lib/normalizeWebpackOutput.js.map +1 -0
- package/lib/tsdoc-metadata.json +11 -0
- package/lib/webpack.d.ts +3 -0
- package/lib/webpack.d.ts.map +1 -0
- package/lib/webpack.js +53 -0
- package/lib/webpack.js.map +1 -0
- package/lib/webpackCapabilities.d.ts +4 -0
- package/lib/webpackCapabilities.d.ts.map +1 -0
- package/lib/webpackCapabilities.js +27 -0
- package/lib/webpackCapabilities.js.map +1 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @ms-cloudpack/bundler-webpack
|
|
2
|
+
|
|
3
|
+
Provides a Cloudpack bundler abstraction around the Webpack bundler.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
### `async function bundlePackage(options)`
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { bundlePackage } from '@ms-cloudpack/bundler';
|
|
11
|
+
|
|
12
|
+
async function start() {
|
|
13
|
+
const result = await bundlePackage({
|
|
14
|
+
packagePath: process.cwd(),
|
|
15
|
+
outputPath: path.join(process.cwd(), 'dist'),
|
|
16
|
+
outputType: 'library',
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
console.log(result);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
start();
|
|
23
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the paths of the provided modules relative to the bundler-webpack package,
|
|
3
|
+
* and returns a list of unique containing folders which webpack can use as module search paths.
|
|
4
|
+
* @param modules - The modules to resolve. Must be dependencies of this package.
|
|
5
|
+
* @returns - Containing folders of the modules. (ie: /path/to/node_modules/)
|
|
6
|
+
*/
|
|
7
|
+
export declare function getPathsOfModules(modules: string[]): string[];
|
|
8
|
+
//# sourceMappingURL=getPathsOfModules.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPathsOfModules.d.ts","sourceRoot":"","sources":["../src/getPathsOfModules.ts"],"names":[],"mappings":"AASA;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAc7D"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { makeUrl } from '@ms-cloudpack/path-string-parsing';
|
|
2
|
+
import { findPackageRoot } from '@ms-cloudpack/path-utilities';
|
|
3
|
+
import { moduleResolve } from 'import-meta-resolve';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
const conditions = new Set(['node', 'require', 'import', 'default']);
|
|
7
|
+
const resolveFrom = makeUrl(import.meta.url);
|
|
8
|
+
/**
|
|
9
|
+
* Resolves the paths of the provided modules relative to the bundler-webpack package,
|
|
10
|
+
* and returns a list of unique containing folders which webpack can use as module search paths.
|
|
11
|
+
* @param modules - The modules to resolve. Must be dependencies of this package.
|
|
12
|
+
* @returns - Containing folders of the modules. (ie: /path/to/node_modules/)
|
|
13
|
+
*/
|
|
14
|
+
export function getPathsOfModules(modules) {
|
|
15
|
+
const paths = new Set();
|
|
16
|
+
for (const moduleName of modules) {
|
|
17
|
+
// Resolve the module path and find the package root from there.
|
|
18
|
+
// preserveSymlinks ensures we get the path where it's installed as a dependency, not from the store.
|
|
19
|
+
const modulePath = moduleResolve(moduleName, resolveFrom, conditions, true /*preserveSymlinks*/);
|
|
20
|
+
const packageRoot = findPackageRoot(fileURLToPath(modulePath));
|
|
21
|
+
if (packageRoot) {
|
|
22
|
+
paths.add(path.dirname(packageRoot));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return Array.from(paths);
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=getPathsOfModules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPathsOfModules.js","sourceRoot":"","sources":["../src/getPathsOfModules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAEpC,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC;AACrE,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAiB;IACjD,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IAEhC,KAAK,MAAM,UAAU,IAAI,OAAO,EAAE,CAAC;QACjC,gEAAgE;QAChE,qGAAqG;QACrG,MAAM,UAAU,GAAG,aAAa,CAAC,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAAC;QACjG,MAAM,WAAW,GAAG,eAAe,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC,CAAC;QAC/D,IAAI,WAAW,EAAE,CAAC;YAChB,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;QACvC,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import { makeUrl } from '@ms-cloudpack/path-string-parsing';\nimport { findPackageRoot } from '@ms-cloudpack/path-utilities';\nimport { moduleResolve } from 'import-meta-resolve';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\n\nconst conditions = new Set(['node', 'require', 'import', 'default']);\nconst resolveFrom = makeUrl(import.meta.url);\n\n/**\n * Resolves the paths of the provided modules relative to the bundler-webpack package,\n * and returns a list of unique containing folders which webpack can use as module search paths.\n * @param modules - The modules to resolve. Must be dependencies of this package.\n * @returns - Containing folders of the modules. (ie: /path/to/node_modules/)\n */\nexport function getPathsOfModules(modules: string[]): string[] {\n const paths = new Set<string>();\n\n for (const moduleName of modules) {\n // Resolve the module path and find the package root from there.\n // preserveSymlinks ensures we get the path where it's installed as a dependency, not from the store.\n const modulePath = moduleResolve(moduleName, resolveFrom, conditions, true /*preserveSymlinks*/);\n const packageRoot = findPackageRoot(fileURLToPath(modulePath));\n if (packageRoot) {\n paths.add(path.dirname(packageRoot));\n }\n }\n\n return Array.from(paths);\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { BundleContext, BundleOptions } from '@ms-cloudpack/common-types';
|
|
2
|
+
import type { WriteESMStubsResult } from '@ms-cloudpack/esm-stub-utilities';
|
|
3
|
+
import type { Configuration } from 'webpack';
|
|
4
|
+
export declare function getWebpackConfiguration(params: {
|
|
5
|
+
options: Omit<BundleOptions, 'entries' | 'outputPath'>;
|
|
6
|
+
outputPath: string;
|
|
7
|
+
} & Pick<WriteESMStubsResult, 'newEntries'>, context: BundleContext): Configuration;
|
|
8
|
+
//# sourceMappingURL=getWebpackConfiguration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getWebpackConfiguration.d.ts","sourceRoot":"","sources":["../src/getWebpackConfiguration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC/E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAE5E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAI7C,wBAAgB,uBAAuB,CACrC,MAAM,EAAE;IACN,OAAO,EAAE,IAAI,CAAC,aAAa,EAAE,SAAS,GAAG,YAAY,CAAC,CAAC;IACvD,UAAU,EAAE,MAAM,CAAC;CACpB,GAAG,IAAI,CAAC,mBAAmB,EAAE,YAAY,CAAC,EAC3C,OAAO,EAAE,aAAa,iBAmLvB"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { getSwcConfig, shouldExternalizePackage } from '@ms-cloudpack/bundler-utilities';
|
|
2
|
+
import ResolveTypescriptPlugin from 'resolve-typescript-plugin';
|
|
3
|
+
import { merge } from 'webpack-merge';
|
|
4
|
+
import { getPathsOfModules } from './getPathsOfModules.js';
|
|
5
|
+
export function getWebpackConfiguration(params, context) {
|
|
6
|
+
const { options, outputPath, newEntries } = params;
|
|
7
|
+
const { unsafeDisableInlineSvg: _, ...bundlerOptions } = options.bundlerOptions || {};
|
|
8
|
+
const isLibraryMode = context.config.mode === 'library';
|
|
9
|
+
const swcConfig = getSwcConfig({ packagePath: options.inputPath, sourcemap: options.sourcemap });
|
|
10
|
+
const config = {
|
|
11
|
+
mode: options.minify ? 'production' : 'development',
|
|
12
|
+
// Webpack (at least older versions) expects entry keys without a leading `./`.
|
|
13
|
+
// (The leading `./` seems to be okay with the latest version, but in older versions, it appears
|
|
14
|
+
// to cause the runtime chunk runtime.js to be generated with an incorrect relative path?)
|
|
15
|
+
entry: Object.fromEntries(Object.entries(newEntries).map(([key, value]) => [key.replace(/^\.\//, ''), value])),
|
|
16
|
+
context: options.inputPath,
|
|
17
|
+
resolve: {
|
|
18
|
+
plugins: [new ResolveTypescriptPlugin()],
|
|
19
|
+
extensions: ['.tsx', '.ts', '.js', '.jsx', '.json'],
|
|
20
|
+
},
|
|
21
|
+
resolveLoader: {
|
|
22
|
+
modules: [
|
|
23
|
+
// Resolve loaders from the package's node_modules
|
|
24
|
+
'node_modules',
|
|
25
|
+
// Cloudpack provided loaders
|
|
26
|
+
...getPathsOfModules(['swc-loader', 'css-loader', 'style-loader', 'sass-loader', 'worker-loader']),
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
externals: isLibraryMode
|
|
30
|
+
? (data, callback) => {
|
|
31
|
+
const { request } = data;
|
|
32
|
+
if (request &&
|
|
33
|
+
!request.includes('!') && // webpack loader
|
|
34
|
+
shouldExternalizePackage({
|
|
35
|
+
id: request,
|
|
36
|
+
inlined: options.inlined,
|
|
37
|
+
external: options.external,
|
|
38
|
+
shouldInlineNodeBuiltins: false,
|
|
39
|
+
})) {
|
|
40
|
+
return callback(undefined, request);
|
|
41
|
+
}
|
|
42
|
+
callback();
|
|
43
|
+
}
|
|
44
|
+
: undefined,
|
|
45
|
+
target: ['web', 'es2020'],
|
|
46
|
+
module: {
|
|
47
|
+
rules: [
|
|
48
|
+
{
|
|
49
|
+
oneOf: [
|
|
50
|
+
// Rule for global CSS files
|
|
51
|
+
{
|
|
52
|
+
test: /\.global\.css$/,
|
|
53
|
+
use: [
|
|
54
|
+
'style-loader',
|
|
55
|
+
{
|
|
56
|
+
loader: 'css-loader',
|
|
57
|
+
options: {
|
|
58
|
+
modules: {
|
|
59
|
+
localIdentName: '[local]', // Use the local name for global CSS
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
// Rule for CSS Modules files
|
|
66
|
+
{
|
|
67
|
+
test: /\.module\.css$/,
|
|
68
|
+
use: [
|
|
69
|
+
'style-loader',
|
|
70
|
+
{
|
|
71
|
+
loader: 'css-loader',
|
|
72
|
+
options: {
|
|
73
|
+
modules: true, // Enable CSS Modules
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
// Rule for regular CSS files
|
|
79
|
+
{
|
|
80
|
+
test: /\.css$/,
|
|
81
|
+
exclude: /\.module\.css$/, // Exclude CSS Modules files
|
|
82
|
+
use: [
|
|
83
|
+
'style-loader',
|
|
84
|
+
'css-loader', // Use css-loader without CSS Modules for regular CSS
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
test: /\.json$/,
|
|
91
|
+
type: 'json',
|
|
92
|
+
sideEffects: false,
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
test: /\.ejs$/,
|
|
96
|
+
loader: 'ejs-loader',
|
|
97
|
+
options: {
|
|
98
|
+
variable: 'data',
|
|
99
|
+
esModule: true,
|
|
100
|
+
},
|
|
101
|
+
resolve: {
|
|
102
|
+
fullySpecified: false,
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
// transform internal jsx files
|
|
107
|
+
test: /\.[cm]?jsx$/,
|
|
108
|
+
loader: 'swc-loader',
|
|
109
|
+
exclude: /(node_modules)/,
|
|
110
|
+
options: swcConfig.js,
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
// transform internal ts files
|
|
114
|
+
test: /\.[cm]?tsx?$/,
|
|
115
|
+
loader: 'swc-loader',
|
|
116
|
+
exclude: /(node_modules)/,
|
|
117
|
+
options: swcConfig.ts,
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
test: /\.s[ac]ss$/i,
|
|
121
|
+
use: ['style-loader', 'css-loader', 'sass-loader'],
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
test: /\.worker\.js$/,
|
|
125
|
+
use: { loader: 'worker-loader' },
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
},
|
|
129
|
+
output: {
|
|
130
|
+
library: {
|
|
131
|
+
type: 'module',
|
|
132
|
+
},
|
|
133
|
+
chunkFormat: 'module',
|
|
134
|
+
chunkLoading: 'import',
|
|
135
|
+
path: outputPath,
|
|
136
|
+
module: true,
|
|
137
|
+
filename: '[name].js',
|
|
138
|
+
chunkFilename: '[id].chunk.js',
|
|
139
|
+
},
|
|
140
|
+
experiments: {
|
|
141
|
+
outputModule: true,
|
|
142
|
+
},
|
|
143
|
+
devtool: options.sourcemap ? (isLibraryMode ? 'cheap-module-source-map' : 'source-map') : false,
|
|
144
|
+
optimization: {
|
|
145
|
+
runtimeChunk: 'single',
|
|
146
|
+
minimize: options.minify,
|
|
147
|
+
mangleExports: false,
|
|
148
|
+
splitChunks: {
|
|
149
|
+
// Prevents exposing path info when creating names for parts splitted by maxSize.
|
|
150
|
+
hidePathInfo: true,
|
|
151
|
+
// Figure out which exports are used by modules to mangle export names, omit unused
|
|
152
|
+
// exports and generate more efficient code.
|
|
153
|
+
usedExports: !isLibraryMode,
|
|
154
|
+
chunks: 'all',
|
|
155
|
+
filename: '[id].chunk.js',
|
|
156
|
+
// Production chunks should be at least 1000 bytes to prevent large numbers of file requests
|
|
157
|
+
minSize: isLibraryMode ? 0 : 1000,
|
|
158
|
+
cacheGroups: {
|
|
159
|
+
defaultVendors: false,
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
if (Object.keys(bundlerOptions).length) {
|
|
165
|
+
return merge(config, bundlerOptions);
|
|
166
|
+
}
|
|
167
|
+
return config;
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=getWebpackConfiguration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getWebpackConfiguration.js","sourceRoot":"","sources":["../src/getWebpackConfiguration.ts"],"names":[],"mappings":"AAAA,OAA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aAAa,CAAC,CAAC,CAAC,yBAAyB,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,KAAK;QAC/F,YAAY,EAAE;YACZ,YAAY,EAAE,QAAQ;YACtB,QAAQ,EAAE,OAAO,CAAC,MAAM;YACxB,aAAa,EAAE,KAAK;YACpB,WAAW,EAAE;gBACX,iFAAiF;gBACjF,YAAY,EAAE,IAAI;gBAElB,mFAAmF;gBACnF,4CAA4C;gBAC5C,WAAW,EAAE,CAAC,aAAa;gBAE3B,MAAM,EAAE,KAAK;gBAEb,QAAQ,EAAE,eAAe;gBAEzB,4FAA4F;gBAC5F,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI;gBAEjC,WAAW,EAAE;oBACX,cAAc,EAAE,KAAK;iBACtB;aACF;SACF;KACF,CAAC;IAEF,IAAI,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,MAAM,EAAE,CAAC;QACvC,OAAO,KAAK,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACvC,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { getSwcConfig, shouldExternalizePackage } from '@ms-cloudpack/bundler-utilities';\nimport type { BundleContext, BundleOptions } from '@ms-cloudpack/common-types';\nimport type { WriteESMStubsResult } from '@ms-cloudpack/esm-stub-utilities';\nimport ResolveTypescriptPlugin from 'resolve-typescript-plugin';\nimport type { Configuration } from 'webpack';\nimport { merge } from 'webpack-merge';\nimport { getPathsOfModules } from './getPathsOfModules.js';\n\nexport function getWebpackConfiguration(\n params: {\n options: Omit<BundleOptions, 'entries' | 'outputPath'>;\n outputPath: string;\n } & Pick<WriteESMStubsResult, 'newEntries'>,\n context: BundleContext,\n) {\n const { options, outputPath, newEntries } = params;\n const { unsafeDisableInlineSvg: _, ...bundlerOptions } = options.bundlerOptions || {};\n\n const isLibraryMode = context.config.mode === 'library';\n\n const swcConfig = getSwcConfig({ packagePath: options.inputPath, sourcemap: options.sourcemap });\n\n const config: Configuration = {\n mode: options.minify ? 'production' : 'development',\n // Webpack (at least older versions) expects entry keys without a leading `./`.\n // (The leading `./` seems to be okay with the latest version, but in older versions, it appears\n // to cause the runtime chunk runtime.js to be generated with an incorrect relative path?)\n entry: Object.fromEntries(Object.entries(newEntries).map(([key, value]) => [key.replace(/^\\.\\//, ''), value])),\n context: options.inputPath,\n resolve: {\n plugins: [new ResolveTypescriptPlugin()],\n extensions: ['.tsx', '.ts', '.js', '.jsx', '.json'],\n },\n resolveLoader: {\n modules: [\n // Resolve loaders from the package's node_modules\n 'node_modules',\n\n // Cloudpack provided loaders\n ...getPathsOfModules(['swc-loader', 'css-loader', 'style-loader', 'sass-loader', 'worker-loader']),\n ],\n },\n externals: isLibraryMode\n ? (data, callback) => {\n const { request } = data;\n\n if (\n request &&\n !request.includes('!') && // webpack loader\n shouldExternalizePackage({\n id: request,\n inlined: options.inlined,\n external: options.external,\n shouldInlineNodeBuiltins: false,\n })\n ) {\n return callback(undefined, request);\n }\n\n callback();\n }\n : undefined,\n target: ['web', 'es2020'],\n module: {\n rules: [\n {\n oneOf: [\n // Rule for global CSS files\n {\n test: /\\.global\\.css$/,\n use: [\n 'style-loader',\n {\n loader: 'css-loader',\n options: {\n modules: {\n localIdentName: '[local]', // Use the local name for global CSS\n },\n },\n },\n ],\n },\n // Rule for CSS Modules files\n {\n test: /\\.module\\.css$/,\n use: [\n 'style-loader',\n {\n loader: 'css-loader',\n options: {\n modules: true, // Enable CSS Modules\n },\n },\n ],\n },\n // Rule for regular CSS files\n {\n test: /\\.css$/,\n exclude: /\\.module\\.css$/, // Exclude CSS Modules files\n use: [\n 'style-loader',\n 'css-loader', // Use css-loader without CSS Modules for regular CSS\n ],\n },\n ],\n },\n {\n test: /\\.json$/,\n type: 'json',\n sideEffects: false,\n },\n {\n test: /\\.ejs$/,\n loader: 'ejs-loader',\n options: {\n variable: 'data',\n esModule: true,\n },\n resolve: {\n fullySpecified: false,\n },\n },\n {\n // transform internal jsx files\n test: /\\.[cm]?jsx$/,\n loader: 'swc-loader',\n exclude: /(node_modules)/,\n options: swcConfig.js,\n },\n {\n // transform internal ts files\n test: /\\.[cm]?tsx?$/,\n loader: 'swc-loader',\n exclude: /(node_modules)/,\n options: swcConfig.ts,\n },\n {\n test: /\\.s[ac]ss$/i,\n use: ['style-loader', 'css-loader', 'sass-loader'],\n },\n {\n test: /\\.worker\\.js$/,\n use: { loader: 'worker-loader' },\n },\n ],\n },\n output: {\n library: {\n type: 'module',\n },\n chunkFormat: 'module',\n chunkLoading: 'import',\n path: outputPath,\n module: true,\n filename: '[name].js',\n chunkFilename: '[id].chunk.js',\n },\n experiments: {\n outputModule: true,\n },\n devtool: options.sourcemap ? (isLibraryMode ? 'cheap-module-source-map' : 'source-map') : false,\n optimization: {\n runtimeChunk: 'single',\n minimize: options.minify,\n mangleExports: false,\n splitChunks: {\n // Prevents exposing path info when creating names for parts splitted by maxSize.\n hidePathInfo: true,\n\n // Figure out which exports are used by modules to mangle export names, omit unused\n // exports and generate more efficient code.\n usedExports: !isLibraryMode,\n\n chunks: 'all',\n\n filename: '[id].chunk.js',\n\n // Production chunks should be at least 1000 bytes to prevent large numbers of file requests\n minSize: isLibraryMode ? 0 : 1000,\n\n cacheGroups: {\n defaultVendors: false,\n },\n },\n },\n };\n\n if (Object.keys(bundlerOptions).length) {\n return merge(config, bundlerOptions);\n }\n\n return config;\n}\n"]}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC"}
|
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC","sourcesContent":["export { webpack as default } from './webpack.js';\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { BundleOptions, BundlerResult } from '@ms-cloudpack/common-types';
|
|
2
|
+
import type { Configuration, Stats } from 'webpack';
|
|
3
|
+
export declare function normalizeWebpackOutput(params: {
|
|
4
|
+
/** Webpack config */
|
|
5
|
+
config: Configuration;
|
|
6
|
+
/** Original options */
|
|
7
|
+
options: Pick<BundleOptions, 'entries' | 'inputPath'>;
|
|
8
|
+
/** Absolute output path */
|
|
9
|
+
outputPath: string;
|
|
10
|
+
/** Stats from webpack callback */
|
|
11
|
+
stats: Stats | undefined;
|
|
12
|
+
}): BundlerResult;
|
|
13
|
+
//# sourceMappingURL=normalizeWebpackOutput.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalizeWebpackOutput.d.ts","sourceRoot":"","sources":["../src/normalizeWebpackOutput.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,aAAa,EAAoB,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAIhH,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAc,MAAM,SAAS,CAAC;AAEhE,wBAAgB,sBAAsB,CAAC,MAAM,EAAE;IAC7C,qBAAqB;IACrB,MAAM,EAAE,aAAa,CAAC;IACtB,uBAAuB;IACvB,OAAO,EAAE,IAAI,CAAC,aAAa,EAAE,SAAS,GAAG,WAAW,CAAC,CAAC;IACtD,2BAA2B;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,kCAAkC;IAClC,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;CAC1B,GAAG,aAAa,CA8ChB"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { normalizeRelativePath } from '@ms-cloudpack/path-string-parsing';
|
|
2
|
+
import { normalizedPathRelativeTo } from '@ms-cloudpack/path-utilities';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
export function normalizeWebpackOutput(params) {
|
|
5
|
+
const { config, options: { entries, inputPath }, outputPath, stats, } = params;
|
|
6
|
+
// Process the errors and warnings from the stats json.
|
|
7
|
+
// This provides more straightforward access to file paths.
|
|
8
|
+
const statsJson = stats?.toJson();
|
|
9
|
+
return {
|
|
10
|
+
outputPath,
|
|
11
|
+
outputFiles: Object.keys(stats?.compilation?.assets || {}).map((asset) => {
|
|
12
|
+
const outFile = {
|
|
13
|
+
outputPath: normalizeRelativePath(asset),
|
|
14
|
+
};
|
|
15
|
+
// Webpack doesn't include entry point info in the output, so add manually if applicable
|
|
16
|
+
if (outFile.outputPath.endsWith('.js')) {
|
|
17
|
+
const entryPoint = entries[outFile.outputPath.replace(/\.js$/, '')];
|
|
18
|
+
if (entryPoint) {
|
|
19
|
+
outFile.entryPoint = entryPoint;
|
|
20
|
+
}
|
|
21
|
+
for (const chunk of statsJson?.chunks || []) {
|
|
22
|
+
// We don't have enough info to determine if a file is a worker
|
|
23
|
+
if (!chunk.files || !chunk.modules || !chunk.files.includes(asset)) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
// Check if the chunk has a worker
|
|
27
|
+
if (chunk.modules.some(({ reasons = [] }) => reasons.some((reason) => reason.type === 'new Worker()'))) {
|
|
28
|
+
// It is a worker!
|
|
29
|
+
outFile.isWorker = true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return outFile;
|
|
34
|
+
}),
|
|
35
|
+
errors: createBundleMessages(statsJson?.errors, inputPath),
|
|
36
|
+
warnings: createBundleMessages(statsJson?.warnings, inputPath),
|
|
37
|
+
rawInput: config,
|
|
38
|
+
rawOutput: statsJson,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Create BundleMessages from WebpackErrors */
|
|
42
|
+
function createBundleMessages(webpackErrors, inputPath) {
|
|
43
|
+
return (webpackErrors || []).map((error) => {
|
|
44
|
+
const bundleMessage = {
|
|
45
|
+
text: error.message,
|
|
46
|
+
source: 'webpack',
|
|
47
|
+
};
|
|
48
|
+
const filePath = error.file || error.moduleName;
|
|
49
|
+
if (filePath) {
|
|
50
|
+
let [, line, col] = error?.loc?.match(/^(\d+):(\d+)/) || [];
|
|
51
|
+
if (!line && error.stack?.includes('ModuleBuildError')) {
|
|
52
|
+
// try to get the line/col from the message; it should be the first pair like that
|
|
53
|
+
[, line, col] = error.message.match(/(\d+):(\d+)/) || [];
|
|
54
|
+
}
|
|
55
|
+
bundleMessage.location = {
|
|
56
|
+
file: path.isAbsolute(filePath)
|
|
57
|
+
? normalizedPathRelativeTo(inputPath, filePath)
|
|
58
|
+
: normalizeRelativePath(filePath),
|
|
59
|
+
line: typeof line === 'string' ? Number(line) : undefined,
|
|
60
|
+
column: typeof col === 'string' ? Number(col) : undefined,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return bundleMessage;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=normalizeWebpackOutput.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalizeWebpackOutput.js","sourceRoot":"","sources":["../src/normalizeWebpackOutput.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AACxE,OAAO,IAAI,MAAM,MAAM,CAAC;AAGxB,MAAM,UAAU,sBAAsB,CAAC,MAStC;IACC,MAAM,EACJ,MAAM,EACN,OAAO,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE,EAC/B,UAAU,EACV,KAAK,GACN,GAAG,MAAM,CAAC;IAEX,uDAAuD;IACvD,2DAA2D;IAC3D,MAAM,SAAS,GAAG,KAAK,EAAE,MAAM,EAAE,CAAC;IAElC,OAAO;QACL,UAAU;QACV,WAAW,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,WAAW,EAAE,MAAM,IAAI,EAAE,CAAC,CAA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undleMessage, BundleOptions, BundleOutputFile, BundlerResult } from '@ms-cloudpack/common-types';\nimport { normalizeRelativePath } from '@ms-cloudpack/path-string-parsing';\nimport { normalizedPathRelativeTo } from '@ms-cloudpack/path-utilities';\nimport path from 'path';\nimport type { Configuration, Stats, StatsError } from 'webpack';\n\nexport function normalizeWebpackOutput(params: {\n /** Webpack config */\n config: Configuration;\n /** Original options */\n options: Pick<BundleOptions, 'entries' | 'inputPath'>;\n /** Absolute output path */\n outputPath: string;\n /** Stats from webpack callback */\n stats: Stats | undefined;\n}): BundlerResult {\n const {\n config,\n options: { entries, inputPath },\n outputPath,\n stats,\n } = params;\n\n // Process the errors and warnings from the stats json.\n // This provides more straightforward access to file paths.\n const statsJson = stats?.toJson();\n\n return {\n outputPath,\n outputFiles: Object.keys(stats?.compilation?.assets || {}).map((asset): BundleOutputFile => {\n const outFile: BundleOutputFile = {\n outputPath: normalizeRelativePath(asset),\n };\n\n // Webpack doesn't include entry point info in the output, so add manually if applicable\n if (outFile.outputPath.endsWith('.js')) {\n const entryPoint = entries[outFile.outputPath.replace(/\\.js$/, '')];\n if (entryPoint) {\n outFile.entryPoint = entryPoint;\n }\n for (const chunk of statsJson?.chunks || []) {\n // We don't have enough info to determine if a file is a worker\n if (!chunk.files || !chunk.modules || !chunk.files.includes(asset)) {\n continue;\n }\n\n // Check if the chunk has a worker\n if (chunk.modules.some(({ reasons = [] }) => reasons.some((reason) => reason.type === 'new Worker()'))) {\n // It is a worker!\n outFile.isWorker = true;\n }\n }\n }\n\n return outFile;\n }),\n errors: createBundleMessages(statsJson?.errors, inputPath),\n warnings: createBundleMessages(statsJson?.warnings, inputPath),\n rawInput: config as unknown as Record<string, unknown>,\n rawOutput: statsJson,\n };\n}\n\n/** Create BundleMessages from WebpackErrors */\nfunction createBundleMessages(webpackErrors: StatsError[] | undefined, inputPath: string): BundleMessage[] {\n return (webpackErrors || []).map((error) => {\n const bundleMessage: BundleMessage = {\n text: error.message,\n source: 'webpack',\n };\n\n const filePath = error.file || error.moduleName;\n\n if (filePath) {\n let [, line, col] = error?.loc?.match(/^(\\d+):(\\d+)/) || [];\n if (!line && error.stack?.includes('ModuleBuildError')) {\n // try to get the line/col from the message; it should be the first pair like that\n [, line, col] = error.message.match(/(\\d+):(\\d+)/) || [];\n }\n\n bundleMessage.location = {\n file: path.isAbsolute(filePath)\n ? normalizedPathRelativeTo(inputPath, filePath)\n : normalizeRelativePath(filePath),\n line: typeof line === 'string' ? Number(line) : undefined,\n column: typeof col === 'string' ? Number(col) : undefined,\n };\n }\n\n return bundleMessage;\n });\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
+
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
+
{
|
|
4
|
+
"tsdocVersion": "0.12",
|
|
5
|
+
"toolPackages": [
|
|
6
|
+
{
|
|
7
|
+
"packageName": "@microsoft/api-extractor",
|
|
8
|
+
"packageVersion": "7.47.9"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
package/lib/webpack.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpack.d.ts","sourceRoot":"","sources":["../src/webpack.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAA6C,OAAO,EAAE,MAAM,4BAA4B,CAAC;AASrG,eAAO,MAAM,OAAO,EAAE,OAiDrB,CAAC"}
|
package/lib/webpack.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { processCapabilities } from '@ms-cloudpack/bundler-capabilities';
|
|
2
|
+
import { writeESMStubsInWorker } from '@ms-cloudpack/esm-stub-utilities';
|
|
3
|
+
import runWebpack from 'webpack';
|
|
4
|
+
import { getWebpackConfiguration } from './getWebpackConfiguration.js';
|
|
5
|
+
import { normalizeWebpackOutput } from './normalizeWebpackOutput.js';
|
|
6
|
+
import { webpackCapabilities } from './webpackCapabilities.js';
|
|
7
|
+
const bundlerName = 'webpack';
|
|
8
|
+
export const webpack = {
|
|
9
|
+
name: bundlerName,
|
|
10
|
+
bundle: async function (options, context) {
|
|
11
|
+
const { errors, newEntries } = await writeESMStubsInWorker(options);
|
|
12
|
+
if (errors?.length) {
|
|
13
|
+
return { errors };
|
|
14
|
+
}
|
|
15
|
+
const outputPath = options.outputPath ?? options.inputPath;
|
|
16
|
+
const defaultExtensionsToInline = ['png', 'jpg', 'jpeg', 'gif'];
|
|
17
|
+
const { unsafeDisableInlineSvg = false } = options.bundlerOptions || {};
|
|
18
|
+
// TODO: This will be deleted in the upcoming PRs
|
|
19
|
+
if (!unsafeDisableInlineSvg) {
|
|
20
|
+
defaultExtensionsToInline.push('svg');
|
|
21
|
+
}
|
|
22
|
+
const bundlerCapabilitiesOptions = {
|
|
23
|
+
// Enable asset-inline by default for common image types
|
|
24
|
+
'asset-inline': { extensions: defaultExtensionsToInline },
|
|
25
|
+
...(options.bundlerCapabilities || {}),
|
|
26
|
+
};
|
|
27
|
+
const config = await processCapabilities({
|
|
28
|
+
bundlerName,
|
|
29
|
+
baseConfig: getWebpackConfiguration({ options, outputPath, newEntries }, context),
|
|
30
|
+
bundlerCapabilitiesOptions,
|
|
31
|
+
internalCapabilities: webpackCapabilities,
|
|
32
|
+
});
|
|
33
|
+
try {
|
|
34
|
+
const stats = await new Promise((resolve, reject) => {
|
|
35
|
+
runWebpack(config, (err, statsResult) => (err ? reject(err) : resolve(statsResult)));
|
|
36
|
+
});
|
|
37
|
+
// Normalize the output outside the callback, so if there's an exception it's not swallowed
|
|
38
|
+
return normalizeWebpackOutput({ stats, config, outputPath, options });
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
const errorMessage = { text: err.message || String(err), source: bundlerName };
|
|
42
|
+
if (err instanceof Error && err.stack) {
|
|
43
|
+
// remove the message from the stack
|
|
44
|
+
errorMessage.notes = [{ text: err.stack.includes(err.message) ? err.stack.split(err.message)[1] : err.stack }];
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
errors: [errorMessage],
|
|
48
|
+
rawInput: config,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=webpack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpack.js","sourceRoot":"","sources":["../src/webpack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AAEzE,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,UAA0B,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAE/D,MAAM,WAAW,GAAG,SAAS,CAAC;AAE9B,MAAM,CAAC,MAAM,OAAO,GAAY;IAC9B,IAAI,EAAE,WAAW;IACjB,MAAM,EAAE,KAAK,WAAW,OAAO,EAAE,OAAO;QACtC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,qBAAqB,CAAC,OAAO,CAAC,CAAC;QACpE,IAAI,MAAM,EAAE,MAAM,EAAE,CAAC;YACnB,OAAO,EAAE,MAAM,EAAE,CAAC;QACpB,CAAC;QAED,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,OAAO,CAAC,SAAS,CAAC;QAE3D,MAAM,yBAAyB,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;QAChE,MAAM,EAAE,sBAAsB,GAAG,KAAK,EAAE,GAAG,OAAO,CAAC,cAAc,IAAI,EAAE,CAAC;QACxE,iDAAiD;QACjD,IAAI,CAAC,sBAAsB,EAAE,CAAC;YAC5B,yBAAyB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACxC,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C,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC;YACjH,CAAC;YACD,OAAO;gBACL,MAAM,EAAE,CAAC,YAAY,CAAC;gBACtB,QAAQ,EAAE,MAAiC;aAC5C,CAAC;QACJ,CAAC;IACH,CAAC;CACF,CAAC","sourcesContent":["import { processCapabilities } from '@ms-cloudpack/bundler-capabilities';\nimport type { BundleMessage, BundlerCapabilitiesOptions, Bundler } from '@ms-cloudpack/common-types';\nimport { writeESMStubsInWorker } from '@ms-cloudpack/esm-stub-utilities';\nimport runWebpack, { type Stats } from 'webpack';\nimport { getWebpackConfiguration } from './getWebpackConfiguration.js';\nimport { normalizeWebpackOutput } from './normalizeWebpackOutput.js';\nimport { webpackCapabilities } from './webpackCapabilities.js';\n\nconst bundlerName = 'webpack';\n\nexport const webpack: Bundler = {\n name: bundlerName,\n bundle: async function (options, context) {\n const { errors, newEntries } = await writeESMStubsInWorker(options);\n if (errors?.length) {\n return { errors };\n }\n\n const outputPath = options.outputPath ?? options.inputPath;\n\n const defaultExtensionsToInline = ['png', 'jpg', 'jpeg', 'gif'];\n const { unsafeDisableInlineSvg = false } = options.bundlerOptions || {};\n // TODO: This will be deleted in the upcoming PRs\n if (!unsafeDisableInlineSvg) {\n defaultExtensionsToInline.push('svg');\n }\n\n const bundlerCapabilitiesOptions: BundlerCapabilitiesOptions = {\n // Enable asset-inline by default for common image types\n 'asset-inline': { extensions: defaultExtensionsToInline },\n ...(options.bundlerCapabilities || {}),\n };\n\n const config = await processCapabilities({\n bundlerName,\n baseConfig: getWebpackConfiguration({ options, outputPath, newEntries }, context),\n bundlerCapabilitiesOptions,\n internalCapabilities: webpackCapabilities,\n });\n\n try {\n const stats = await new Promise<Stats | undefined>((resolve, reject) => {\n runWebpack(config, (err, statsResult) => (err ? reject(err) : resolve(statsResult)));\n });\n\n // Normalize the output outside the callback, so if there's an exception it's not swallowed\n return normalizeWebpackOutput({ stats, config, outputPath, options });\n } catch (err) {\n const errorMessage: BundleMessage = { text: (err as Error).message || String(err), source: bundlerName };\n if (err instanceof Error && err.stack) {\n // remove the message from the stack\n errorMessage.notes = [{ text: err.stack.includes(err.message) ? err.stack.split(err.message)[1] : err.stack }];\n }\n return {\n errors: [errorMessage],\n rawInput: config as Record<string, unknown>,\n };\n }\n },\n};\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { InternalBundlerCapabilityImplementations } from '@ms-cloudpack/common-types';
|
|
2
|
+
import type { Configuration } from 'webpack';
|
|
3
|
+
export declare const webpackCapabilities: InternalBundlerCapabilityImplementations<Configuration>;
|
|
4
|
+
//# sourceMappingURL=webpackCapabilities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpackCapabilities.d.ts","sourceRoot":"","sources":["../src/webpackCapabilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wCAAwC,EAAE,MAAM,4BAA4B,CAAC;AAC3F,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,mBAAmB,EAAE,wCAAwC,CAAC,aAAa,CAwBvF,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const webpackCapabilities = {
|
|
2
|
+
'asset-inline': (config, options) => {
|
|
3
|
+
// Remove the leading dot from the extensions
|
|
4
|
+
const extensions = options.extensions.map((ext) => (ext.startsWith('.') ? ext.slice(1) : ext));
|
|
5
|
+
config.module ??= {};
|
|
6
|
+
config.module.rules ??= [];
|
|
7
|
+
config.module.rules.push({
|
|
8
|
+
test: new RegExp(`\\.(${extensions.join('|')})$`, 'i'),
|
|
9
|
+
type: 'asset/inline',
|
|
10
|
+
});
|
|
11
|
+
return config;
|
|
12
|
+
},
|
|
13
|
+
alias: (config, options) => {
|
|
14
|
+
// Add aliases for externalized packages to the webpack configuration
|
|
15
|
+
if (!config.externals) {
|
|
16
|
+
config.externals = [];
|
|
17
|
+
}
|
|
18
|
+
else if (!Array.isArray(config.externals)) {
|
|
19
|
+
config.externals = [config.externals];
|
|
20
|
+
}
|
|
21
|
+
// The aliased packaged must be the first element in the array,
|
|
22
|
+
// so that it is resolved before all other packages are externalized.
|
|
23
|
+
config.externals.unshift(options);
|
|
24
|
+
return config;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=webpackCapabilities.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpackCapabilities.js","sourceRoot":"","sources":["../src/webpackCapabilities.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,mBAAmB,GAA4D;IAC1F,cAAc,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE;QAClC,6CAA6C;QAC7C,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAC/F,MAAM,CAAC,MAAM,KAAK,EAAE,CAAC;QACrB,MAAM,CAAC,MAAM,CAAC,KAAK,KAAK,EAAE,CAAC;QAC3B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;YACvB,IAAI,EAAE,IAAI,MAAM,CAAC,OAAO,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC;YACtD,IAAI,EAAE,cAAc;SACrB,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,KAAK,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE;QACzB,qEAAqE;QACrE,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;YACtB,MAAM,CAAC,SAAS,GAAG,EAAE,CAAC;QACxB,CAAC;aAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;YAC5C,MAAM,CAAC,SAAS,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACxC,CAAC;QACD,+DAA+D;QAC/D,qEAAqE;QACrE,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAClC,OAAO,MAAM,CAAC;IAChB,CAAC;CACF,CAAC","sourcesContent":["import type { InternalBundlerCapabilityImplementations } from '@ms-cloudpack/common-types';\nimport type { Configuration } from 'webpack';\n\nexport const webpackCapabilities: InternalBundlerCapabilityImplementations<Configuration> = {\n 'asset-inline': (config, options) => {\n // Remove the leading dot from the extensions\n const extensions = options.extensions.map((ext) => (ext.startsWith('.') ? ext.slice(1) : ext));\n config.module ??= {};\n config.module.rules ??= [];\n config.module.rules.push({\n test: new RegExp(`\\\\.(${extensions.join('|')})$`, 'i'),\n type: 'asset/inline',\n });\n return config;\n },\n alias: (config, options) => {\n // Add aliases for externalized packages to the webpack configuration\n if (!config.externals) {\n config.externals = [];\n } else if (!Array.isArray(config.externals)) {\n config.externals = [config.externals];\n }\n // The aliased packaged must be the first element in the array,\n // so that it is resolved before all other packages are externalized.\n config.externals.unshift(options);\n return config;\n },\n};\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ms-cloudpack/bundler-webpack",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A cloudpack plugin for abstracting webpack.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"source": "./src/index.ts",
|
|
12
|
+
"types": "./lib/index.d.ts",
|
|
13
|
+
"import": "./lib/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@ms-cloudpack/bundler-capabilities": "^0.1.25",
|
|
18
|
+
"@ms-cloudpack/bundler-utilities": "^0.1.3",
|
|
19
|
+
"@ms-cloudpack/common-types": "^0.22.0",
|
|
20
|
+
"@ms-cloudpack/esm-stub-utilities": "^0.13.6",
|
|
21
|
+
"@ms-cloudpack/path-string-parsing": "^1.2.4",
|
|
22
|
+
"@ms-cloudpack/path-utilities": "^2.7.48",
|
|
23
|
+
"@swc/core": "^1.3.0",
|
|
24
|
+
"css-loader": "^6.0.0",
|
|
25
|
+
"ejs-loader": "^0.5.0",
|
|
26
|
+
"import-meta-resolve": "^4.0.0",
|
|
27
|
+
"resolve-typescript-plugin": "^2.0.0",
|
|
28
|
+
"sass": "^1.0.0",
|
|
29
|
+
"sass-loader": "^14.0.0",
|
|
30
|
+
"style-loader": "^3.0.0",
|
|
31
|
+
"swc-loader": "^0.2.0",
|
|
32
|
+
"webpack": "^5.0.0",
|
|
33
|
+
"webpack-merge": "^6.0.0",
|
|
34
|
+
"worker-loader": "^3.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@ms-cloudpack/bundler-tests": "^0.1.0",
|
|
38
|
+
"@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
|
|
39
|
+
"@ms-cloudpack/scripts": "^0.0.1",
|
|
40
|
+
"@ms-cloudpack/test-utilities": "^0.5.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"api": "cloudpack-scripts api",
|
|
44
|
+
"build:watch": "cloudpack-scripts build-watch",
|
|
45
|
+
"build": "cloudpack-scripts build",
|
|
46
|
+
"lint:update": "cloudpack-scripts lint-update",
|
|
47
|
+
"lint": "cloudpack-scripts lint",
|
|
48
|
+
"test:update": "cloudpack-scripts test-update",
|
|
49
|
+
"test:watch": "cloudpack-scripts test-watch",
|
|
50
|
+
"test": "cloudpack-scripts test"
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"lib/**/!(*.test.*)"
|
|
54
|
+
]
|
|
55
|
+
}
|