Skip to content

Commit

Permalink
feat(webpack): build your lib using webpack
Browse files Browse the repository at this point in the history
dropped building with @babel/cli in favor of using webpack

BREAKING CHANGE: @babel/cli dropped and the build script uses webpack and babel-loader
  • Loading branch information
sharvit committed Dec 21, 2019
1 parent 0a37fe2 commit 04402df
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/app/generators/project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export default class extends BaseGenerator {
templatePath: 'config/jest.config.js',
destinationPath: 'config/jest.config.js',
},
{
templatePath: 'config/webpack.config.js',
destinationPath: 'config/webpack.config.js',
},
];

if (this.options.esdoc) {
Expand Down
3 changes: 1 addition & 2 deletions src/app/generators/project/templates/_babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
"targets": { "node": 8 }
}
]
],
"plugins": ["add-module-exports"]
]
}
10 changes: 6 additions & 4 deletions src/app/generators/project/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
},
"scripts": {
"prebuild": "rimraf dist",
"build": "<%= runScriptCommand %> build:babel<% if (esdoc) { %> && <%= runScriptCommand %> build:docs<% } %>",
"build:babel": "babel src --out-dir dist --ignore **/*.test.js",<% if (esdoc) { %>
"build": "<%= runScriptCommand %> build:webpack<% if (esdoc) { %> && <%= runScriptCommand %> build:docs<% } %>",
"build:webpack": "webpack --config ./config/webpack.config.js",<% if (esdoc) { %>
"build:docs": "esdoc -c ./config/esdoc.config.js",
"develop:docs": "watch \"<%= runScriptCommand %> build:docs\" . --ignoreDirectoryPattern='/node_modules|docs|dist|coverage|.git|.nyc*./'",<% } %>
"test:watch": "jest --config ./config/jest.config.js --watch",
Expand All @@ -34,14 +34,14 @@
],
"dependencies": {},
"devDependencies": {
"@babel/cli": "^7.5.0",
"@babel/core": "^7.5.0",
"@babel/preset-env": "^7.5.0",<% if (semanticRelease) { %>
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"@commitlint/travis-cli": "^8.1.0",<% } %>
"babel-eslint": "^10.0.1",
"babel-jest": "^24.8.0",
"babel-loader": "^8.0.6",
"babel-plugin-add-module-exports": "^1.0.0",<% if (semanticRelease) { %>
"commitlint-config-cz": "^0.12.0",<% } %><% if (coveralls) { %>
"coveralls": "^3.0.2",<% } %><% if (esdoc) { %>
Expand All @@ -58,7 +58,9 @@
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.8.0",
"prettier": "^1.15.3",
"rimraf": "^3.0.0"
"rimraf": "^3.0.0",
"webpack": "^4.41.4",
"webpack-cli": "^3.3.10"
},<% if (semanticRelease) { %>
"optionalDependencies": {<% if (semanticRelease) { %>
"cz-conventional-changelog": "^3.0.2",<% } %>
Expand Down
27 changes: 27 additions & 0 deletions src/app/generators/project/templates/config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const path = require('path');

const config = {
mode: 'production',
target: 'node',

entry: path.resolve(__dirname, '../src/index.js'),

output: {
path: path.resolve(__dirname, '../dist'),
filename: 'index.js',
library: '<%= projectName %>',
libraryTarget: 'umd',
},

module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
};

module.exports = config;
7 changes: 7 additions & 0 deletions src/app/generators/project/templates/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
* @return {String} return the recived import.
*/
export default (input = 'No args passed!') => input;

/**
* Return the passed argument as is.
* @param {String} [input='No args passed!'] input argument to return.
* @return {String} return the recived import.
*/
export const namedExport = (input = 'No args passed!') => input;
5 changes: 4 additions & 1 deletion src/app/generators/project/templates/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import <%= camelProject %> from '.';
import <%= camelProject %>, { namedExport } from './index';

test('output', () => {
expect(<%= camelProject %>('🐰')).toBe('🐰');
expect(<%= camelProject %>()).toBe('No args passed!');

expect(namedExport('🐰')).toBe('🐰');
expect(namedExport()).toBe('No args passed!');
});
2 changes: 2 additions & 0 deletions src/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ test('default files', () => {
'package.json',
'readme.md',
'config/jest.config.js',
'config/webpack.config.js',
'src/index.js',
'src/index.test.js',
// esdoc
Expand Down Expand Up @@ -95,6 +96,7 @@ test('default files with --noDefaults', () => {
'package.json',
'readme.md',
'config/jest.config.js',
'config/webpack.config.js',
'src/index.js',
'src/index.test.js',
]);
Expand Down

0 comments on commit 04402df

Please sign in to comment.