Skip to content

Commit

Permalink
chore(webpack): construct package in dist
Browse files Browse the repository at this point in the history
  • Loading branch information
floryst committed Oct 1, 2021
1 parent ce588ec commit 9aa5155
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Utilities/build/absolutify-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = (code, replaceFunc) => {
const importRegex = /(?:import|from) ['"]([^'"]*)['"]/g;
while ((m = importRegex.exec(code)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === importRegex.lastIndex) {
importRegex.lastIndex++;
}

if (m[1].startsWith('../') || m[1].startsWith('./')) {
const importPath = replaceFunc(m[1]);
code = code.replace(m[0], `from '${importPath}'`);
}
}
return code;
};
File renamed without changes.
File renamed without changes.
61 changes: 61 additions & 0 deletions 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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"babel-plugin-istanbul": "6.0.0",
"buffer": "6.0.3",
"commitizen": "4.2.4",
"copy-webpack-plugin": "9.0.1",
"cross-env": "7.0.3",
"css-loader": "6.2.0",
"dotenv": "10.0.0",
Expand Down
53 changes: 53 additions & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ const path = require('path');
const { merge } = require('webpack-merge');

// webpack plugins
const CopyPlugin = require('copy-webpack-plugin');
const WebpackNotifierPlugin = require('webpack-notifier');
const ESLintWebpackPlugin = require('eslint-webpack-plugin');

// config files
const pkg = require('./package.json');
const settings = require('./webpack.settings.js');

const absolutifyImports = require('./Utilities/build/absolutify-imports.js');

// basic regex for matching imports
const importRegex = /(?:import|from) ['"]([^'"]*)['"]/g;

// Configure Entries
const configureEntries = () => {
const entries = {};
Expand Down Expand Up @@ -96,6 +102,53 @@ const baseConfig = {
},
plugins: [
!process.env.NOLINT && new ESLintWebpackPlugin(),
new CopyPlugin({
patterns: [
{
from: 'Sources/**/*',
globOptions: {
dot: true,
gitignore: true,
ignore: [
'**/test/**',
'**/example/**',
'**/example_/**',
'**/*.md',
],
},
transform(content, absoluteFrom) {
// transforms typescript defs to use absolute imports
if (absoluteFrom.endsWith('.d.ts')) {
return absolutifyImports(content.toString(), (relImport) => {
const importPath = path.join(path.dirname(absoluteFrom), relImport);
return path.join('vtk.js', path.relative(__dirname, importPath));
});
}
return content;
}
},
{ from: 'Utilities/prepare.js', to: 'Utilities/prepare.js' },
{ from: 'Utilities/XMLConverter', to: 'Utilities/XMLConverter' },
{ from: 'Utilities/DataGenerator', to: 'Utilities/DataGenerator' },
{ from: 'Utilities/config', to: 'Utilities/config' },
{ from: 'Utilities/build/macro-shim.d.ts', to: 'Sources/macro.d.ts' },
{ from: 'Utilities/build/macro-shim.js', to: 'Sources/macro.js' },
{ from: '*.txt' },
{ from: '*.md' },
{ from: 'LICENSE' },
{ from: '.npmignore' },
{
from: 'package.json',
transform(content) {
const pkg = JSON.parse(content);
pkg.name = 'vtk.js';
pkg.main = './vtk.js';
delete pkg.module;
return JSON.stringify(pkg, null, 2);
}
},
],
}),
new WebpackNotifierPlugin({
title: 'Webpack - vtk.js',
excludeWarnings: true,
Expand Down
10 changes: 10 additions & 0 deletions webpack.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const { merge } = require('webpack-merge');
const moment = require('moment');
const webpack = require('webpack');
const TerserPlugin = require("terser-webpack-plugin");

// webpack plugins
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
Expand Down Expand Up @@ -43,6 +44,15 @@ function configureBundleAnalyzer(name) {
function configureOptimization() {
return {
minimize: true,
minimizer: [
new TerserPlugin({
exclude: [
// do not minify Sources/ and Utilities/ dirs
/Sources\//,
/Utilities\//,
],
}),
],
};
}

Expand Down

0 comments on commit 9aa5155

Please sign in to comment.