Skip to content

Commit

Permalink
feat: dynamic build plugin based on system node version
Browse files Browse the repository at this point in the history
  • Loading branch information
shirotech committed Sep 24, 2017
1 parent 0727571 commit 7988e64
Show file tree
Hide file tree
Showing 12 changed files with 1,067 additions and 239 deletions.
9 changes: 9 additions & 0 deletions .babelrc
@@ -0,0 +1,9 @@
{
"presets": [
["env", {
"targets": {
"node": "current"
}
}]
]
}
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,7 +1,7 @@
.DS_Store
/.idea/
/.nyc_output/
/coverage/
/node_modules/
dist/
/dist.js
*.log
main.js
8 changes: 0 additions & 8 deletions .istanbul.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .nvmrc
@@ -1 +1 @@
lts/*
8.5.0
24 changes: 24 additions & 0 deletions .nycrc
@@ -0,0 +1,24 @@
{
"check-coverage": true,
"lines": 100,
"statements": 100,
"functions": 100,
"branches": 100,
"exclude": [
"spec/**"
],
"reporter": [
"lcov",
"text"
],
"require": [
"babel-register"
],
"cache": true,
"watermarks": {
"lines": [ 90, 100 ],
"functions": [ 90, 100 ],
"branches": [ 90, 100 ],
"statements": [ 90, 100 ]
}
}
2 changes: 1 addition & 1 deletion codecov.yml
@@ -1,7 +1,7 @@
coverage:
precision: 2
round: down
range: 99...100
range: 90...100

status:
project:
Expand Down
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

24 changes: 15 additions & 9 deletions src.js → module.js
@@ -1,5 +1,5 @@
const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';

const empty = '';
const slash = '/';
Expand All @@ -9,18 +9,18 @@ const paramsRegex = /:([a-z]+)/gi;

class WebpackCdnPlugin {
constructor({
modules, prod = true,
modules, prod,
prodUrl = '//unpkg.com/:name@:version/:path',
devUrl = ':name/:path', publicPath,
}) {
this.modules = modules;
this.prod = prod;
this.prod = prod !== false;
this.prefix = publicPath;
this.url = prod ? prodUrl : devUrl;
this.url = this.prod ? prodUrl : devUrl;
}

apply(compiler) {
const output = compiler.options.output;
const { output } = compiler.options;
output.publicPath = output.publicPath || '/';

if (!this.prod && output.publicPath.slice(-1) !== slash) {
Expand Down Expand Up @@ -55,7 +55,10 @@ class WebpackCdnPlugin {
return require(path.join(nodeModules, name, packageJson)).version;
}

static _getCss(modules, url, prefix = empty, prod = false) {
static _getCss(modules, url, prefix, prod) {
prefix = prefix || empty;
prod = prod !== false;

return modules.filter(p => p.style).map((p) => {
p.version = WebpackCdnPlugin.getVersion(p.name);

Expand All @@ -69,7 +72,10 @@ class WebpackCdnPlugin {
});
}

static _getJs(modules, url, prefix = empty, prod = false) {
static _getJs(modules, url, prefix, prod) {
prefix = prefix || empty;
prod = prod !== false;

return modules.filter(p => !p.cssOnly).map((p) => {
p.version = WebpackCdnPlugin.getVersion(p.name);
p.path = p.path || require.resolve(p.name).match(/[\\/]node_modules[\\/].+?[\\/](.*)/)[1].replace(/\\/g, '/');
Expand All @@ -87,4 +93,4 @@ class WebpackCdnPlugin {

WebpackCdnPlugin.node_modules = nodeModules;

module.exports = WebpackCdnPlugin;
export default WebpackCdnPlugin;
27 changes: 14 additions & 13 deletions package.json
Expand Up @@ -2,23 +2,20 @@
"name": "webpack-cdn-plugin",
"version": "1.5.0",
"description": "A webpack plugin that use externals of CDN urls for production and local node_modules for development",
"main": "index.js",
"main": "main.js",
"module": "module.js",
"scripts": {
"start": "webpack --config example/webpack.config.js",
"test": "eslint {src.js,spec/*.js} --fix && istanbul cover jasmine",
"build": "buble -i src.js -o dist.js",
"test": "nyc jasmine",
"build": "babel module.js --out-file main.js",
"commitmsg": "validate-commit-msg",
"precommit": "npm test",
"prepublish": "npm run build"
"precommit": "npm test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/van-nguyen/webpack-cdn-plugin.git"
},
"files": [
"index.js",
"src.js",
"dist.js",
"module.js",
"README.md"
],
"keywords": [
Expand Down Expand Up @@ -54,16 +51,20 @@
},
"homepage": "https://github.com/van-nguyen/webpack-cdn-plugin",
"devDependencies": {
"buble": "^0.15.2",
"eslint": "^4.5.0",
"eslint-config-airbnb-base": "^11.3.1",
"babel-preset-env": "^1.6.0",
"babel-register": "^6.26.0",
"eslint": "^4.7.2",
"eslint-config-airbnb-base": "12.0.0",
"eslint-plugin-import": "^2.7.0",
"html-webpack-plugin": "^2.30.1",
"husky": "^0.14.3",
"istanbul": "^0.4.5",
"jasmine": "^2.7.0",
"jasmine-spec-reporter": "^4.2.1",
"nyc": "^11.2.1",
"validate-commit-msg": "^2.14.0",
"webpack": "^3.5.5"
},
"dependencies": {
"babel-cli": "^6.26.0"
}
}
2 changes: 1 addition & 1 deletion spec/helpers/reporter.js
@@ -1,4 +1,4 @@
const SpecReporter = require('jasmine-spec-reporter').SpecReporter;
import { SpecReporter } from 'jasmine-spec-reporter';

const env = jasmine.getEnv();
env.clearReporters();
Expand Down
32 changes: 16 additions & 16 deletions spec/webpack.spec.js
@@ -1,7 +1,7 @@
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackCdnPlugin = require('../');
import path from 'path';
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import WebpackCdnPlugin from '../module';

const cssMatcher = /<link href="(.+?)" rel="stylesheet">/g;
const jsMatcher = /<script type="text\/javascript" src="(.+?)">/g;
Expand All @@ -12,7 +12,7 @@ let jsAssets;
const versions = {
jasmine: WebpackCdnPlugin.getVersion('jasmine'),
jasmineSpecReporter: WebpackCdnPlugin.getVersion('jasmine-spec-reporter'),
istanbul: WebpackCdnPlugin.getVersion('istanbul'),
nyc: WebpackCdnPlugin.getVersion('nyc'),
};

const fs = new webpack.MemoryOutputFileSystem();
Expand Down Expand Up @@ -53,7 +53,7 @@ function getConfig({ prod, publicPath = '/node_modules', publicPath2 = '/assets'
const options = {
modules: [
{ name: 'jasmine-spec-reporter', path: 'index.js' },
{ name: 'istanbul', style: 'style.css' },
{ name: 'nyc', style: 'style.css' },
{ name: 'jasmine', cdn: 'jasmine2', style: 'style.css' },
],
prod,
Expand Down Expand Up @@ -84,15 +84,15 @@ describe('Webpack Integration', () => {

it('should output the right assets (css)', () => {
expect(cssAssets).toEqual([
`//unpkg.com/istanbul@${versions.istanbul}/style.css`,
`//unpkg.com/nyc@${versions.nyc}/style.css`,
`//unpkg.com/jasmine2@${versions.jasmine}/style.css`,
]);
});

it('should output the right assets (js)', () => {
expect(jsAssets).toEqual([
`//unpkg.com/jasmine-spec-reporter@${versions.jasmineSpecReporter}/index.js`,
`//unpkg.com/istanbul@${versions.istanbul}/index.js`,
`//unpkg.com/nyc@${versions.nyc}/index.js`,
`//unpkg.com/jasmine2@${versions.jasmine}/lib/jasmine.js`,
'/assets/app.js',
]);
Expand All @@ -106,15 +106,15 @@ describe('Webpack Integration', () => {

it('should output the right assets (css)', () => {
expect(cssAssets).toEqual([
`//cdnjs.cloudflare.com/ajax/libs/istanbul/${versions.istanbul}/style.css`,
`//cdnjs.cloudflare.com/ajax/libs/nyc/${versions.nyc}/style.css`,
`//cdnjs.cloudflare.com/ajax/libs/jasmine2/${versions.jasmine}/style.css`,
]);
});

it('should output the right assets (js)', () => {
expect(jsAssets).toEqual([
`//cdnjs.cloudflare.com/ajax/libs/jasmine-spec-reporter/${versions.jasmineSpecReporter}/index.js`,
`//cdnjs.cloudflare.com/ajax/libs/istanbul/${versions.istanbul}/index.js`,
`//cdnjs.cloudflare.com/ajax/libs/nyc/${versions.nyc}/index.js`,
`//cdnjs.cloudflare.com/ajax/libs/jasmine2/${versions.jasmine}/lib/jasmine.js`,
'/assets/app.js',
]);
Expand All @@ -130,15 +130,15 @@ describe('Webpack Integration', () => {

it('should output the right assets (css)', () => {
expect(cssAssets).toEqual([
'/istanbul/style.css',
'/nyc/style.css',
'/jasmine/style.css',
]);
});

it('should output the right assets (js)', () => {
expect(jsAssets).toEqual([
'/jasmine-spec-reporter/index.js',
'/istanbul/index.js',
'/nyc/index.js',
'/jasmine/lib/jasmine.js',
'/app.js',
]);
Expand All @@ -152,15 +152,15 @@ describe('Webpack Integration', () => {

it('should output the right assets (css)', () => {
expect(cssAssets).toEqual([
'/istanbul/style.css',
'/nyc/style.css',
'/jasmine/style.css',
]);
});

it('should output the right assets (js)', () => {
expect(jsAssets).toEqual([
'/jasmine-spec-reporter/index.js',
'/istanbul/index.js',
'/nyc/index.js',
'/jasmine/lib/jasmine.js',
'/app.js',
]);
Expand All @@ -174,15 +174,15 @@ describe('Webpack Integration', () => {

it('should output the right assets (css)', () => {
expect(cssAssets).toEqual([
'/node_modules/istanbul/style.css',
'/node_modules/nyc/style.css',
'/node_modules/jasmine/style.css',
]);
});

it('should output the right assets (js)', () => {
expect(jsAssets).toEqual([
'/node_modules/jasmine-spec-reporter/index.js',
'/node_modules/istanbul/index.js',
'/node_modules/nyc/index.js',
'/node_modules/jasmine/lib/jasmine.js',
'/assets/app.js',
]);
Expand Down

0 comments on commit 7988e64

Please sign in to comment.