├── .eslint.js ├── .gitignore ├── LICENSE ├── README.md ├── bin └── nuxt-module ├── eslint └── index.js ├── index.js ├── lib ├── index.js └── rollup.js ├── package.json └── yarn.lock /.eslint.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./eslint-config-module-kit') -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.iml 3 | .idea 4 | *.log* 5 | .nuxt 6 | .vscode 7 | .DS_STORE 8 | coverage 9 | dist -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Nuxt.js 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nuxt Module Builder 2 | [![npm (scoped with tag)](https://img.shields.io/npm/v/nuxt-module-builder/latest.svg?style=flat-square)](https://npmjs.com/package/nuxt-module-builder) 3 | [![npm](https://img.shields.io/npm/dt/nuxt-module-builder.svg?style=flat-square)](https://npmjs.com/package/nuxt-module-builder) 4 | [![Dependencies](https://david-dm.org/nuxt/module-builder/status.svg?style=flat-square)](https://david-dm.org/nuxt/module-builder) 5 | 6 | 7 | > 🏗️ Nuxt Standard Module Builder 8 | 9 | ### ⚠️ This package is no longer maintained as Nuxt 2+ modules natively support ESM! Please use [module-template](https://github.com/nuxt-community/module-template) 10 | 11 | ## Features 12 | - Build modules using same tool-chain of nuxt 13 | - ES6 Support out of the box 14 | - Nuxt inspired build system using optional single `nuxt.module.config.js` file 15 | - Standard eslint rules 16 | 17 | ### Using module starter template 18 | Just head to [nuxt-community/module-template](https://github.com/nuxt-community/module-template) 19 | to make your fresh module in few minutes. 20 | 21 | ## Getting started 22 | 23 | Edit **package.json** like this: 24 | 25 | ```json 26 | { 27 | "scripts": { 28 | "build": "nuxt-module" 29 | }, 30 | "devDependencies": { 31 | "nuxt-module-builder": "latest" 32 | } 33 | } 34 | ``` 35 | 36 | Create **src/index.js** file: 37 | 38 | ```js 39 | export default async function (moduleOptions) { 40 | // ... 41 | } 42 | ``` 43 | 44 | Optionally add a `moduleBuilder` section in **package.json** 45 | or create **nuxt.module.config.js** in the root of module project to customize module builder: 46 | 47 | ```js 48 | module.exports = { 49 | meta: { 50 | name: 'myModule' 51 | }, 52 | plugins: [ 53 | // rollup plugins 54 | ] 55 | } 56 | ``` 57 | 58 | You can now use `npm run nuxt-module` to generate `dist/index.js` 59 | 60 | 61 | For watch mode, you can use `nuxt-module --watch` 62 | 63 | ## ESLint 64 | TODO 65 | 66 | ## License 67 | 68 | [MIT License](./LICENSE) 69 | 70 | Copyright (c) 2017 Nuxt.js 71 | -------------------------------------------------------------------------------- /bin/nuxt-module: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | process.env.DEBUG = process.env.DEBUG || 'nuxt-module:*' 4 | 5 | const { join, resolve } = require('path') 6 | const fs = require('fs') 7 | const parseArgs = require('minimist') 8 | const ModuleBuilder = require('../lib') 9 | const debug = require('debug')('nuxt-module:') 10 | 11 | const argv = parseArgs(process.argv.slice(2), { 12 | alias: { 13 | h: 'help', 14 | c: 'config-file', 15 | w: 'watch' 16 | }, 17 | boolean: ['h', 'w'], 18 | string: ['c'], 19 | default: { 20 | c: 'nuxt.module.config.js' 21 | } 22 | }) 23 | 24 | if (argv.help) { 25 | console.log(` 26 | Description 27 | Nuxt Standard Module Builder 28 | Usage 29 | $ nuxt-module 30 | Options 31 | --config-file, -c Path to Nuxt.js config file (default: nuxt.module.config.js) 32 | --help, -h Displays this message 33 | `) 34 | process.exit(0) 35 | } 36 | 37 | const rootDir = resolve(argv._[0] || '.') 38 | const nuxtModuleConfigFile = resolve(rootDir, argv['config-file']) 39 | 40 | let options = {} 41 | 42 | if (fs.existsSync(nuxtModuleConfigFile)) { 43 | options = require(nuxtModuleConfigFile) 44 | } else if (argv['config-file'] !== 'nuxt.module.config.js') { 45 | console.error(`> Could not load config file ${argv['config-file']}`) 46 | process.exit(1) 47 | } 48 | 49 | if (typeof options.rootDir !== 'string') { 50 | options.rootDir = rootDir 51 | } 52 | 53 | // Build module 54 | debug('Starting build') 55 | const builder = new ModuleBuilder(options) 56 | 57 | if (argv.watch) { 58 | builder.watch().catch(console.error) 59 | } else { 60 | builder.build().catch(console.error) 61 | } 62 | 63 | -------------------------------------------------------------------------------- /eslint/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | // parser: 'babel-eslint', 4 | parserOptions: { 5 | sourceType: 'module' 6 | }, 7 | env: { 8 | browser: true, 9 | node: true, 10 | jest: true 11 | }, 12 | extends: 'standard', 13 | plugins: [ 14 | 'jest', 15 | 'vue' 16 | ], 17 | rules: { 18 | // Allow paren-less arrow functions 19 | 'arrow-parens': 0, 20 | // Allow async-await 21 | 'generator-star-spacing': 0, 22 | // Allow debugger during development 23 | 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0, 24 | // Do not allow console.logs etc... 25 | 'no-console': 2, 26 | // Allow trailing comma 27 | 'comma-dangle': 0 28 | }, 29 | globals: { 30 | 'jest/globals': true, 31 | jasmine: true 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const ModuleBuilder = require('./lib/index.js') 2 | 3 | module.exports = { 4 | ModuleBuilder 5 | } -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | const { resolve, dirname } = require('path') 2 | const fs = require('fs-extra') 3 | const rollupConfig = require('./rollup') 4 | const rollup = require('rollup') 5 | const debug = require('debug')('nuxt-module:build') 6 | 7 | class ModuleBuilder { 8 | constructor(options = {}) { 9 | this.options = options 10 | 11 | // Meta 12 | const defaultMeta = { 13 | name: '', 14 | version: '0.0.0', 15 | dependencies: [], 16 | author: { 17 | name: '' 18 | } 19 | } 20 | if (!this.options.meta) { 21 | const packageJsonPath = resolve(this.options.rootDir, 'package.json') 22 | if (fs.existsSync(packageJsonPath)) { 23 | const packageJson = fs.readJSONSync(packageJsonPath) 24 | Object.assign(defaultMeta, packageJson) 25 | } 26 | } 27 | this.options.meta = Object.assign({}, defaultMeta, this.options.meta) 28 | 29 | // Using moduleBuilder section of package.json 30 | if (this.options.meta.moduleBuilder) { 31 | Object.assign(this.options, this.options.meta.moduleBuilder) 32 | } 33 | 34 | // Banner 35 | const banner = this.options.banner || `/*! 36 | * ${this.options.meta.name} v${this.options.meta.version} 37 | * Copyright (c) ${new Date().getFullYear()} ${this.options.meta.author.name} 38 | * 39 | * Released under the MIT License. 40 | */` 41 | 42 | // Rollup 43 | this.options.entry = this.options.entry || resolve(this.options.rootDir, 'src/index.js') 44 | this.options.dest = this.options.dest || resolve(this.options.rootDir, 'dist/index.js') 45 | } 46 | 47 | build() { 48 | debug('Building') 49 | const config = rollupConfig(this.options) 50 | return rollup.rollup(config).then(bundle => { 51 | fs.ensureDirSync(dirname(this.options.dest)) 52 | return bundle.write(config.output).then((err) => { 53 | debug('Done') 54 | }) 55 | }) 56 | } 57 | 58 | watch() { 59 | debug('Starting watch') 60 | const config = rollupConfig(this.options) 61 | const watcher = rollup.watch(config) 62 | watcher.on('event', event => { 63 | switch (event.code) { 64 | case 'BUNDLE_START': 65 | debug('Building ' + event.input) 66 | break; 67 | case 'BUNDLE_END': 68 | debug('Built ' + event.output) 69 | break; 70 | case 'ERROR': 71 | case 'FATAL': 72 | console.log(event.error) 73 | break; 74 | case 'FATAL': 75 | console.log(event.error) 76 | process.exit(1) 77 | break; 78 | } 79 | }) 80 | return Promise.resolve() 81 | } 82 | } 83 | 84 | module.exports = ModuleBuilder -------------------------------------------------------------------------------- /lib/rollup.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | const rollupBabel = require('rollup-plugin-babel') 3 | const rollupAlias = require('rollup-plugin-alias') 4 | const rollupCommonJS = require('rollup-plugin-commonjs') 5 | const rollupNodeResolve = require('rollup-plugin-node-resolve') 6 | const rollupBuiltins = require('rollup-plugin-node-builtins') 7 | 8 | function rollupConfig(options = {}, meta = {}) { 9 | 10 | const config = { 11 | input: options.entry, 12 | external: ['fs', 'path', 'http', 'module'].concat(options.meta.dependencies, options.external), 13 | format: options.format || 'cjs', 14 | banner: options.banner, 15 | name: options.name || options.meta.name || '', 16 | output: { 17 | format: 'cjs', 18 | file: options.dest, 19 | sourcemap: true, 20 | }, 21 | plugins: [ 22 | rollupAlias(Object.assign({ resolve: ['.js', '.json', '.jsx', '.ts'] }, options.alias)), 23 | rollupNodeResolve(Object.assign({ preferBuiltins: true }, options.nodeResolve)), 24 | rollupCommonJS(Object.assign({}, options.commonjs)), 25 | rollupBabel(Object.assign({ 26 | exclude: 'node_modules/**', 27 | plugins: [ 28 | 'transform-async-to-generator', 29 | 'array-includes' 30 | ], 31 | presets: [ 32 | ['es2015', { modules: false }] 33 | ], 34 | 'env': { 35 | 'test': { 36 | 'plugins': ['istanbul'] 37 | } 38 | } 39 | }, options.babel)) 40 | ].concat(options.plugins || []) 41 | } 42 | 43 | return config 44 | } 45 | 46 | module.exports = rollupConfig 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nuxt-module-builder", 3 | "version": "0.0.2", 4 | "description": "Nuxt Standard Module Builder", 5 | "license": "MIT", 6 | "main": "./index.js", 7 | "homepage": "https://github.com/nuxt/module-builder", 8 | "bin": { 9 | "nuxt-module": "./bin/nuxt-module" 10 | }, 11 | "dependencies": { 12 | "babel-plugin-array-includes": "^2.0.3", 13 | "babel-plugin-istanbul": "^4.1.4", 14 | "babel-preset-es2015": "^6.24.1", 15 | "codecov": "^2.3.0", 16 | "debug": "^3.0.1", 17 | "eslint": "^4.5.0", 18 | "eslint-config-standard": "^10.2.1", 19 | "eslint-plugin-import": "^2.7.0", 20 | "eslint-plugin-jest": "^20.0.3", 21 | "eslint-plugin-node": "^5.1.1", 22 | "eslint-plugin-promise": "^3.5.0", 23 | "eslint-plugin-standard": "^3.0.1", 24 | "eslint-plugin-vue": "^2.1.0", 25 | "fs-extra": "^4.0.1", 26 | "jest": "^20.0.4", 27 | "jsdom": "^11.2.0", 28 | "minimist": "^1.2.0", 29 | "nuxt": "^1.0.0-rc6", 30 | "request-promise-native": "^1.0.4", 31 | "rollup": "^0.49.1", 32 | "rollup-plugin-alias": "^1.3.1", 33 | "rollup-plugin-babel": "^3.0.2", 34 | "rollup-plugin-commonjs": "^8.2.0", 35 | "rollup-plugin-copy": "^0.2.3", 36 | "rollup-plugin-filesize": "^1.4.2", 37 | "rollup-plugin-node-builtins": "^2.1.2", 38 | "rollup-plugin-node-resolve": "^3.0.0", 39 | "standard-version": "^4.2.0" 40 | } 41 | } 42 | --------------------------------------------------------------------------------