Skip to content

Commit

Permalink
feat(build): expose true ESM bundle as primary artifact (#7937)
Browse files Browse the repository at this point in the history
Refs #7907
Refs #7831
  • Loading branch information
char0n committed Mar 24, 2022
1 parent ec51dc3 commit 7409f41
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
1 change: 1 addition & 0 deletions config/jest/jest.artifact.config.js
Expand Up @@ -4,4 +4,5 @@ module.exports = {
rootDir: path.join(__dirname, '..', '..'),
testEnvironment: 'jsdom',
testMatch: ['**/test/build-artifacts/**/*.js'],
transformIgnorePatterns: ['/node_modules/(?!(swagger-client|react-syntax-highlighter)/)'],
};
18 changes: 17 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -180,6 +180,7 @@
"webpack-bundle-size-analyzer": "^3.1.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.7.4",
"webpack-node-externals": "=3.0.0",
"webpack-stats-plugin": "=1.0.3"
},
"config": {
Expand Down
29 changes: 7 additions & 22 deletions webpack/_config-builder.js
Expand Up @@ -3,16 +3,13 @@
*/

import path from "path"
import fs from "fs"
import deepExtend from "deep-extend"
import webpack from "webpack"
import TerserPlugin from "terser-webpack-plugin"
import nodeExternals from "webpack-node-externals"

import { getRepoInfo } from "./_helpers"
import pkg from "../package.json"
const nodeModules = fs.readdirSync("node_modules").filter(function(x) {
return x !== ".bin"
})

const projectBasePath = path.join(__dirname, "../")

Expand All @@ -30,7 +27,7 @@ const baseRules = [
},
},
{ test: /\.(txt|yaml)$/,
type: "asset/source",
type: "asset/source",
},
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
Expand Down Expand Up @@ -93,24 +90,12 @@ export default function buildConfig(

externals: includeDependencies
? {
// json-react-schema/deeper depends on buffertools, which fails.
buffertools: true,
esprima: true,
esprima: "esprima",
}
: (context, request, cb) => {
// webpack injects some stuff into the resulting file,
// these libs need to be pulled in to keep that working.
var exceptionsForWebpack = ["ieee754", "base64-js"]
if (
nodeModules.indexOf(request) !== -1 ||
exceptionsForWebpack.indexOf(request) !== -1
) {
cb(null, "commonjs " + request)
return
}
cb()
},

: [nodeExternals({
importType: (moduleName) => {
return `commonjs ${moduleName}`
}})],
resolve: {
modules: [path.join(projectBasePath, "./src"), "node_modules"],
extensions: [".web.js", ".js", ".jsx", ".json", ".less"],
Expand Down
30 changes: 23 additions & 7 deletions webpack/es-bundle-core.babel.js
Expand Up @@ -2,17 +2,17 @@
* @prettier
*/

/** Dev Note:
/** Dev Note:
* StatsWriterPlugin is disabled by default; uncomment to enable
* when enabled, rebuilding the bundle will cause error for assetSizeLimit,
* which we want to keep out of CI/CD
* post build, cli command: npx webpack-bundle-analyzer <path>
*/
*/

import configBuilder from "./_config-builder"
import { DuplicatesPlugin } from "inspectpack/plugin"
import { WebpackBundleSizeAnalyzerPlugin } from "webpack-bundle-size-analyzer"
// import path from "path"
import nodeExternals from "webpack-node-externals"
// import { StatsWriterPlugin } from "webpack-stats-plugin"

const result = configBuilder(
Expand All @@ -28,17 +28,32 @@ const result = configBuilder(
"./src/index.js",
],
},
experiments: {
outputModule: true,
},
output: {
module: true,
libraryTarget: "module",
library: {
type: "commonjs2",
export: "default",
type: "module",
},
environment: {
module: true,
},
},
externalsType: "module",
externals: [
{
esprima: "esprima",
},
nodeExternals({
importType: (moduleName) => {
return `module ${moduleName}`
}})
],
plugins: [
new DuplicatesPlugin({
// emit compilation warning or error? (Default: `false`)
emitErrors: false,
// display full duplicates information? (Default: `false`)
verbose: false,
}),
new WebpackBundleSizeAnalyzerPlugin("log.es-bundle-core-sizes.swagger-ui.txt"),
Expand All @@ -50,4 +65,5 @@ const result = configBuilder(
}
)


export default result
4 changes: 2 additions & 2 deletions webpack/es-bundle.babel.js
Expand Up @@ -2,12 +2,12 @@
* @prettier
*/

/** Dev Note:
/** Dev Note:
* StatsWriterPlugin is disabled by default; uncomment to enable
* when enabled, rebuilding the bundle will cause error for assetSizeLimit,
* which we want to keep out of CI/CD
* post build, cli command: npx webpack-bundle-analyzer <path>
*/
*/

import configBuilder from "./_config-builder"
import { DuplicatesPlugin } from "inspectpack/plugin"
Expand Down

0 comments on commit 7409f41

Please sign in to comment.